wxapi.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //引入微信jssdk
  2. const wx = require('@/js_sdk/jweixin-1.4.0.js')
  3. //配置注册url
  4. const url = '/app/getJsapiTicket';
  5. //http方法
  6. import Request from '@/js_sdk/luch-request/luch-request/index.js';
  7. const $http = new Request();
  8. //系统域名url
  9. // encodeURIComponent
  10. const href = location.href.split('#')[0];
  11. // const href = location.href.split("?")[0];
  12. console.log('href',href);
  13. let token = uni.getStorageSync("token");
  14. import { config } from './config/config';
  15. //系统域名url
  16. export default {
  17. ...wx,
  18. isOk: false,
  19. async config() {
  20. wx.config({
  21. debug: false,
  22. jsApiList: [
  23. "updateAppMessageShareData",
  24. "updateTimelineShareData",
  25. "onMenuShareTimeline",
  26. "onMenuShareAppMessage",
  27. "onMenuShareQQ",
  28. "onMenuShareWeibo",
  29. "onMenuShareQZone",
  30. "startRecord",
  31. "stopRecord",
  32. "onVoiceRecordEnd",
  33. "playVoice",
  34. "pauseVoice",
  35. "stopVoice",
  36. "onVoicePlayEnd",
  37. "uploadVoice",
  38. "downloadVoice",
  39. "chooseImage",
  40. "previewImage",
  41. "uploadImage",
  42. "downloadImage",
  43. "translateVoice",
  44. "getNetworkType",
  45. "openLocation",
  46. "getLocation",
  47. "hideOptionMenu",
  48. "showOptionMenu",
  49. "hideMenuItems",
  50. "showMenuItems",
  51. "hideAllNonBaseMenuItem",
  52. "showAllNonBaseMenuItem",
  53. "closeWindow",
  54. "scanQRCode",
  55. "chooseWXPay",
  56. "openProductSpecificView",
  57. "addCard",
  58. "chooseCard",
  59. "openCard",
  60. ],
  61. ...(await $http.get(config.apiBaseurl + url, {
  62. params: {url: href},
  63. header:{Accept:'application/json',
  64. Authorization: 'Bearer '+ token},
  65. dataType: 'json'})
  66. ).data.data,
  67. });
  68. wx.ready(e => {
  69. this.isOk = true;
  70. this.hideMenu();
  71. });
  72. wx.error(e => {
  73. console.log("失败",e.errMsg);
  74. this.isOk = false;
  75. });
  76. },
  77. hideMenu() {
  78. // wx.hideAllNonBaseMenuItem();
  79. },
  80. showMenu() {
  81. wx.showMenuItems({
  82. menuList: [
  83. "menuItem:share:appMessage",
  84. "menuItem:profile",
  85. "menuItem:addContact",
  86. "menuItem:dayMode",
  87. "menuItem:nightMode",
  88. "menuItem:share:timeline",
  89. "menuItem:favorite"
  90. ] // 要显示的菜单项,所有menu项见附录3
  91. });
  92. },
  93. /**
  94. * 分享配置
  95. * @param {Object} info
  96. */
  97. share(data = {}, info = {}) {
  98. //添加链接时间戳
  99. // data.v = new Date().getTime();
  100. // console.log("shareData",data)
  101. // let search = [];
  102. // for (const [key, value] of Object.entries(data)) {
  103. // // search.push(`${key}=${encodeURIComponent(value)}`);
  104. // search.push(`${key}=${value}`);
  105. // }
  106. // search = "?" + search.join("&");
  107. let afterEndUrl = config.afterEndUrl;
  108. // let afterEndUrl = 'http://tanhui.hongweisoft.com/api/wechat/h5/authorize?returnUrl='
  109. // let afterEndUrl = 'http://gzhjt.gzsdzth.com/api/wechat/h5/authorize?returnUrl='
  110. let fullUrl = afterEndUrl + location.href.split('?')[0] + '?togetherId=' + data.togetherId + '/#/' + data.routeUrl
  111. const shareInfo = {
  112. title: data.nickname + '邀请你参与贵州省单株碳汇精准扶贫',
  113. link: fullUrl,
  114. desc: "购碳扶贫,你我同行",
  115. imgUrl: href + "/static/logo.png",
  116. trigger(res) {},
  117. success(res) {},
  118. cancel(res) {},
  119. fail(res) {},
  120. ...info
  121. };
  122. console.log('shareInfo',shareInfo,'fullUrl',fullUrl)
  123. //自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
  124. wx.updateAppMessageShareData(shareInfo);
  125. //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
  126. wx.updateTimelineShareData(shareInfo);
  127. },
  128. //拉起微信浏览器端支付
  129. JSAPI(res) {
  130. return new Promise(r => {
  131. wx.chooseWXPay({
  132. "appId": res.appId,
  133. "timestamp": res.timeStamp, //时间戳,自1970年以来的秒数
  134. "nonceStr": res.nonceStr, //随机串
  135. "package": res.packageValue,
  136. "signType": res.signType, //微信签名方式:
  137. "paySign": res.paySign, //微信签名
  138. success() {
  139. r({
  140. code: 0,
  141. msg: "成功"
  142. });
  143. },
  144. cancel() {
  145. r({
  146. code: 1,
  147. msg: "取消"
  148. });
  149. },
  150. fail() {
  151. r({
  152. code: 2,
  153. msg: "失败"
  154. });
  155. }
  156. });
  157. });
  158. },
  159. }