wxapi.js 4.5 KB

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