js_sdk.uts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. function checkPlatform() {
  2. // #ifdef H5
  3. type SystemInfo = {
  4. win: boolean,
  5. mac: boolean,
  6. xll: boolean
  7. };
  8. let system = {
  9. win: false,
  10. mac: false,
  11. xll: false
  12. } as SystemInfo;
  13. let p = navigator.platform;
  14. system.win = p.indexOf("Win") == 0;
  15. system.mac = p.indexOf("Mac") == 0;
  16. system.x11 = p == "X11" || p.indexOf("Linux") == 0;
  17. if (system.win || system.mac || system.xll) {
  18. let ua = navigator.userAgent.toLowerCase();
  19. if (ua.indexOf("micromessenger") > -1) {
  20. // 微信开发者工具下访问(注意微信开发者工具下无法唤起微信公众号支付)
  21. return "pc-weixin";
  22. } else {
  23. return "pc";
  24. }
  25. } else {
  26. if (p.indexOf("iPhone") > -1 || p.indexOf("iPad") > -1) {
  27. return "ios";
  28. } else {
  29. return "android";
  30. }
  31. }
  32. // #endif
  33. }
  34. /**
  35. * 获取当前H5所在的环境
  36. */
  37. function getH5Env() {
  38. // #ifdef H5
  39. const ua = window.navigator.userAgent.toLowerCase();
  40. const isWeixin = /micromessenger/i.test(ua);
  41. const isAlipay = /alipay/i.test(ua);
  42. const isMiniProgram = /miniprogram/i.test(ua);
  43. if (isWeixin) {
  44. if (isMiniProgram) {
  45. return "mp-weixin";
  46. } else {
  47. return "h5-weixin";
  48. }
  49. } else if (isAlipay) {
  50. if (isMiniProgram) {
  51. return "mp-alipay";
  52. } else {
  53. return "h5-alipay";
  54. }
  55. }
  56. return "h5";
  57. // #endif
  58. }
  59. // json2的属性全部赋值给json1
  60. function objectAssign(json1:UTSJSONObject,json2:UTSJSONObject) :UTSJSONObject{
  61. for(let key in json2) {
  62. json1[key] = json2[key];
  63. }
  64. return json1;
  65. }
  66. function getWeixinCode () : Promise<string>{
  67. return new Promise((resolve, reject) => {
  68. // #ifdef MP-WEIXIN
  69. uni.login({
  70. provider: 'weixin',
  71. success(res) {
  72. resolve(res.code)
  73. },
  74. fail(err) {
  75. reject(new Error('获取微信code失败'))
  76. }
  77. })
  78. // #endif
  79. // #ifndef MP-WEIXIN
  80. resolve('')
  81. // #endif
  82. })
  83. };
  84. function getAlipayCode () : Promise<string>{
  85. return new Promise((resolve, reject) => {
  86. // #ifdef MP-ALIPAY
  87. uni.login({
  88. provider: 'alipay',
  89. success(res) {
  90. resolve(res.code);
  91. },
  92. fail(err) {
  93. reject(new Error('获取支付宝code失败,可能是没有关联appid或你的支付宝开发者工具还没有登录'));
  94. }
  95. });
  96. // #endif
  97. // #ifndef MP-ALIPAY
  98. resolve('');
  99. // #endif
  100. });
  101. };
  102. export {
  103. checkPlatform,
  104. getH5Env,
  105. objectAssign,
  106. getWeixinCode,
  107. getAlipayCode
  108. };