wxapi.js 4.5 KB

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