wxapi.js 4.4 KB

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