感谢 v-shop

感谢 v-shop 作者的无私奉献,同时也希望作者可以在主项目中增加微信登录的支持、微信自动注册的支持、微信支付的支持

v-shop 的开源地址:

https://github.com/JoeshuTT/v-shop
[码云镜像:] https://gitee.com/joeshu/v-shop

大家可以关注作者项目主页,关注作者最新更新动态

v-shop代码调整

拦截器调整

src/common/request.js

toLogin 方法调整:

  1. let _domian = 'http://' + document.domain + '/login?redirect=' + router.currentRoute.fullPath
  2. _domian = encodeURIComponent(_domian)
  3. if (!window.goLogin) {
  4. window.goLogin = true
  5. Axios.get(window.subDomain + '/site/statistics').then(res => {
  6. // console.log(res.data)
  7. parent.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + res.data.data.wxMpAppid + '&redirect_uri=' + _domian + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
  8. })
  9. }

可通过 /site/statistics 接口获取到后台配置的公众号的 APPID;
结合微信公众号的网页授权相关说明,进行授权跳转,获取 code ;

根据code进行用户注册

src/pages/login/reg.vue

  1. async register(code) {
  2. const res = await this.$request.post('/user/wxmp/register/simple', { code })
  3. if (res.code !== 0) {
  4. this.$toast(res.msg)
  5. return
  6. }
  7. let _domian = 'http://' + document.domain + '/login'
  8. _domian = encodeURIComponent(_domian)
  9. this.$request.get('/site/statistics').then(res => {
  10. parent.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + res.data.wxMpAppid + '&redirect_uri=' + _domian + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
  11. })
  12. }

根据code进行用户自动登录

src/pages/login/login.vue

  1. async login(code) {
  2. const res = await this.$request.post('/user/wxmp/login', { code })
  3. if (res.code === 10000) {
  4. let _domian = 'http://' + document.domain + '/reg'
  5. _domian = encodeURIComponent(_domian)
  6. this.$request.get('/site/statistics').then(res => {
  7. parent.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + res.data.wxMpAppid + '&redirect_uri=' + _domian + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
  8. })
  9. return
  10. }
  11. if (res.code !== 0) {
  12. this.$toast(res.msg)
  13. return
  14. }
  15. util.storage.set('token', res.data.token)
  16. util.storage.set('uid', res.data.uid)
  17. this.$route.query.redirect ? this.$router.replace({ path: this.$route.query.redirect }) : this.$router.replace({ path: '/home' })
  18. // this.$toast.clear()
  19. this.$toast.success({
  20. message: '登录成功',
  21. duration: 1500
  22. })
  23. }

微信支付支持

src/common/pay.js

  1. export function wxPay_jsapi(money, nextAction, payName, remark = '', token) {
  2. return request.post('/pay/wx/jsapi', { money, nextAction, payName, remark, token })
  3. }

页面上发起微信支付

public/index.html

增加全局的 WeixinJSBridge 组件

  1. if (typeof WeixinJSBridge == "undefined"){
  2. if( document.addEventListener ){
  3. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  4. }else if (document.attachEvent){
  5. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  6. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  7. }
  8. }else{
  9. onBridgeReady();
  10. }

在具体支付页面上(目前就订单列表、订单详情),调用支付:

  1. wxPay_jsapi(amountReal, '{type:0, id:' + orderId + '}', '在线支付', '', storage.get('token')).then(res => {
  2. if (res.code === 0) {
  3. this.$toast.clear()
  4. window.WeixinJSBridge.invoke(
  5. 'getBrandWCPayRequest', {
  6. 'appId': res.data.appid,
  7. 'timeStamp': res.data.timeStamp,
  8. 'nonceStr': res.data.nonceStr,
  9. 'package': 'prepay_id=' + res.data.prepayId,
  10. 'signType': 'MD5',
  11. 'paySign': res.data.sign
  12. },
  13. function(res) {
  14. if (res.err_msg === 'get_brand_wcpay_request:ok') {
  15. // this.$router.replace({ path: '/order-detail', query: { id: orderId }})
  16. parent.location.reload()
  17. }
  18. })
  19. } else {
  20. this.$toast(res.msg)
  21. }
  22. })

微信支付配置

MA 授权,把你的微信支付授权给你的服务号使用

微信支付MA授权

登录微信支付后台,开通 JSAPI 支付:

image.png

公众号支付,需要配置支付域名的,在开发设置中配置如下:

通过点击上图,左侧菜单的 “开发配置” ,然后进行配置如下:

image.png

请如实配置你自己的h5商城的域名

api工厂后台配置公众号信息

左侧菜单 “微信设置” —> “公众号设置”

image.png

如果没出现上面的菜单,请通过左侧菜单 “工厂设置” —> “模块管理” ,启用 “微信公众号” 模块,并 F5 刷新后台后即可出现菜单