diff --git a/api/product/spu.js b/api/product/spu.js index 394b60d2..a685a7e7 100644 --- a/api/product/spu.js +++ b/api/product/spu.js @@ -9,7 +9,9 @@ export function getSpuList(recommendType) { // 获得商品 SPU 分页 export function getSpuPage(data) { - return request.get('app-api/product/spu/page', data); + return request.get('app-api/product/spu/page', data, { + noAuth: true // TODO 芋艿:后续要做调整 + }); } // 查询商品 diff --git a/api/system/weixin.js b/api/system/weixin.js new file mode 100644 index 00000000..64bffbb2 --- /dev/null +++ b/api/system/weixin.js @@ -0,0 +1,8 @@ +import request from "@/utils/request.js"; + +// 创建微信 JS SDK 初始化所需的签名 +export function createJsapiSignature(url) { + return request.post("app-api/system/wx-mp/create-jsapi-signature?url=" + url, {}, { + noAuth: true // TODO 芋艿:后续要做调整 + }); +} diff --git a/libs/wechat.js b/libs/wechat.js index e43a00c3..99bc9603 100644 --- a/libs/wechat.js +++ b/libs/wechat.js @@ -3,9 +3,9 @@ import WechatJSSDK from "@/plugin/jweixin-module/index.js"; import { - getWechatConfig, wechatAuth } from "@/api/public"; +import * as WeiXinApi from '@/api/system/weixin.js'; import { WX_AUTH, STATE_KEY, @@ -21,47 +21,33 @@ import Cache from '@/utils/cache'; class AuthWechat { constructor() { - //微信实例化对象 - this.instance = WechatJSSDK; - //是否实例化 - this.status = false; - + this.instance = WechatJSSDK; // 微信实例化对象 + this.status = false; // 是否实例化 this.initConfig = {}; - } - - isAndroid(){ - let u = navigator.userAgent; - return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; - } - - signLink() { - if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') { - window.entryUrl = location.href.split('#')[0] - } - return /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window.entryUrl; - } - /** - * 初始化wechat(分享配置) + * 初始化 wechat(分享配置) */ wechat() { return new Promise((resolve, reject) => { - // if (this.status && !this.isAndroid()) return resolve(this.instance); - getWechatConfig() - .then(res => { - this.instance.config(res.data); - this.initConfig = res.data; - this.status = true; - this.instance.ready(() => { - resolve(this.instance); - }) - }).catch(err => { - console.log('微信分享配置失败',err); - this.status = false; - reject(err); - }); + // if (this.status) return resolve(this.instance); + WeiXinApi.createJsapiSignature(location.href).then(res => { + // debugger + const jsapiTicket = res.data; + jsapiTicket.jsApiList = ['chooseWXPay']; // TODO 芋艿:这里要设置下 + jsapiTicket.debug = false; + this.instance.config(jsapiTicket); + this.initConfig = jsapiTicket; + this.status = true; + this.instance.ready(() => { + resolve(this.instance); + }) + }).catch(err => { + console.log('WechatJSSDK 初始化失败 ',err); + this.status = false; + reject(err); + }); }); } @@ -110,8 +96,8 @@ class AuthWechat { reject(err); }) }); - } - + } + // 使用微信内置地图查看位置接口; seeLocation(config){ return new Promise((resolve, reject) => { @@ -126,7 +112,7 @@ class AuthWechat { }) }); } - + /** * 微信支付 * @param {Object} config @@ -144,7 +130,7 @@ class AuthWechat { }); }); } - + toPromise(fn, config = {}) { return new Promise((resolve, reject) => { fn({ @@ -169,6 +155,10 @@ class AuthWechat { * 自动去授权 */ oAuth(snsapiBase,url) { + // TODO 芋艿:先链式去掉这个逻辑; + if (true) { + return; + } if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return; const { code @@ -201,10 +191,6 @@ class AuthWechat { // } } - clearAuthStatus() { - - } - /** * 授权登录获取token * @param {Object} code @@ -220,7 +206,7 @@ class AuthWechat { Cache.clear(STATE_KEY); // Cache.clear('spread'); loginType && Cache.clear(LOGINTYPE); - + }) .catch(reject); }); @@ -259,7 +245,7 @@ class AuthWechat { // return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`; // } } - + /** * 跳转自动登录 */ diff --git a/manifest.json b/manifest.json index c44a88bb..ca43c336 100644 --- a/manifest.json +++ b/manifest.json @@ -91,7 +91,8 @@ }, "h5" : { "devServer" : { - "https" : false + "https" : false, + "disableHostCheck": true }, "router" : { "mode" : "history", diff --git a/pages/goods/cashier/index.vue b/pages/goods/cashier/index.vue index e53d7a5c..214f9ff6 100644 --- a/pages/goods/cashier/index.vue +++ b/pages/goods/cashier/index.vue @@ -107,7 +107,6 @@ title: '加载订单中' }); PayOrderApi.getOrder(this.orderId).then(res => { - // TODO 芋艿:如果已支付,则跳转回 if (res.data.status === 10) { uni.showToast({ title: '支付成功' @@ -166,6 +165,9 @@ channelCode: channelCode, displayMode: 'url', // TODO 芋艿:后续可以优化 returnUrl: this.getPayReturnUrl(), + channelExtras: { + openid: "ockUAwIZ-0OeMZl9ogcZ4ILrGba0" + } }).then(res => { this.handleSubmitOrderResult(res.data); @@ -245,29 +247,7 @@ }) // #endif // #ifdef H5 - this.$wechat.pay(res.data.result.jsConfig).then(res => { - return that.$util.Tips({ - title: '支付成功', - icon: 'success' - }, { - tab: 5, - url: goPages - }); - }).catch(res => { - if (!this.$wechat.isWeixin()) { - uni.redirectTo({ - url: goPages + '&msg=' + that.$t(`支付失败`) + - '&status=2' - // '&msg=支付失败&status=2' - }) - } - if (res.errMsg == 'chooseWXPay:cancel') return that.$util.Tips({ - title: '取消支付' - }, { - tab: 5, - url: goPages + '&status=2' - }); - }) + // 已删除,已经实现; // #endif // #ifdef APP-PLUS uni.requestPayment({ @@ -409,7 +389,52 @@ window.location = displayContent; return; } - // 2.2 + // 2.2 如果返回的是 CUSTOM,则自定义处理 + if (displayMode === 'custom') { + if (this.channelCode === 'wx_pub') { + this.handleSubmitOrderResultForWxPub(displayContent) + return; + } + } + }, + /** + * 发起微信公众号支付 + */ + handleSubmitOrderResultForWxPub(displayContent) { + const payConfig = JSON.parse(displayContent); + this.$wechat.pay({ + timestamp: payConfig.timeStamp, + nonceStr: payConfig.nonceStr, + package: payConfig.packageValue, + signType: payConfig.signType, + paySign: payConfig.paySign, + }).then(res => { + // 失败的情况 + if (res.errMsg === 'chooseWXPay:cancel') { + return this.$util.Tips({ + title: '取消微信支付' + }); + } + if (res.errMsg) { + return this.$util.Tips({ + title: res.errMsg, + icon: 'error' + }) + } + + // 成功的情况 + return this.$util.Tips({ + title: '支付成功', + icon: 'success' + }, () => { + this.goReturnUrl('success'); + }); + }).catch(res => { + return this.$util.Tips({ + title: '初始化微信支付失败,请重试或者选择其它支付方式', + icon: 'error' + }) + }) }, /** diff --git a/plugin/jweixin-module/index.js b/plugin/jweixin-module/index.js index 1b5014bf..1ee4428a 100644 --- a/plugin/jweixin-module/index.js +++ b/plugin/jweixin-module/index.js @@ -1 +1 @@ -!function(e,n){"function"==typeof define&&(define.amd||define.cmd)?define(function(){return n(e)}):n(e,!0)}(window,function(o,e){if(!o.jWeixin){var n,c={config:"preVerifyJSAPI",onMenuShareTimeline:"menu:share:timeline",onMenuShareAppMessage:"menu:share:appmessage",onMenuShareQQ:"menu:share:qq",onMenuShareWeibo:"menu:share:weiboApp",onMenuShareQZone:"menu:share:QZone",previewImage:"imagePreview",getLocation:"geoLocation",openProductSpecificView:"openProductViewWithPid",addCard:"batchAddCard",openCard:"batchViewCard",chooseWXPay:"getBrandWCPayRequest",openEnterpriseRedPacket:"getRecevieBizHongBaoRequest",startSearchBeacons:"startMonitoringBeacons",stopSearchBeacons:"stopMonitoringBeacons",onSearchBeacons:"onBeaconsInRange",consumeAndShareCard:"consumedShareCard",openAddress:"editAddress"},a=function(){var e={};for(var n in c)e[c[n]]=n;return e}(),i=o.document,t=i.title,r=navigator.userAgent.toLowerCase(),s=navigator.platform.toLowerCase(),d=!(!s.match("mac")&&!s.match("win")),u=-1!=r.indexOf("wxdebugger"),l=-1!=r.indexOf("micromessenger"),p=-1!=r.indexOf("android"),f=-1!=r.indexOf("iphone")||-1!=r.indexOf("ipad"),m=(n=r.match(/micromessenger\/(\d+\.\d+\.\d+)/)||r.match(/micromessenger\/(\d+\.\d+)/))?n[1]:"",g={initStartTime:L(),initEndTime:0,preVerifyStartTime:0,preVerifyEndTime:0},h={version:1,appId:"",initTime:0,preVerifyTime:0,networkType:"",isPreVerifyOk:1,systemType:f?1:p?2:-1,clientVersion:m,url:encodeURIComponent(location.href)},v={},S={_completes:[]},y={state:0,data:{}};O(function(){g.initEndTime=L()});var I=!1,_=[],w={config:function(e){B("config",v=e);var t=!1!==v.check;O(function(){if(t)M(c.config,{verifyJsApiList:C(v.jsApiList),verifyOpenTagList:C(v.openTagList)},function(){S._complete=function(e){g.preVerifyEndTime=L(),y.state=1,y.data=e},S.success=function(e){h.isPreVerifyOk=0},S.fail=function(e){S._fail?S._fail(e):y.state=-1};var t=S._completes;return t.push(function(){!function(){if(!(d||u||v.debug||m<"6.0.2"||h.systemType<0)){var i=new Image;h.appId=v.appId,h.initTime=g.initEndTime-g.initStartTime,h.preVerifyTime=g.preVerifyEndTime-g.preVerifyStartTime,w.getNetworkType({isInnerInvoke:!0,success:function(e){h.networkType=e.networkType;var n="https://open.weixin.qq.com/sdk/report?v="+h.version+"&o="+h.isPreVerifyOk+"&s="+h.systemType+"&c="+h.clientVersion+"&a="+h.appId+"&n="+h.networkType+"&i="+h.initTime+"&p="+h.preVerifyTime+"&u="+h.url;i.src=n}})}}()}),S.complete=function(e){for(var n=0,i=t.length;n