Skip to content

Commit 219f86d

Browse files
committed
整合微信jssdk、实现微信网页授权、
1 parent 54d412c commit 219f86d

24 files changed

+624
-28
lines changed

.env.development

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NODE_ENV="dev"
2+
BASE_URL="http://wmhello.mynatapp.cc/"
3+
VUE_APP_REDIRECT="http://wmhello.mynatapp.cc/api/start_oauth"
4+
VUE_APP_TIMEOUT=2*60*60*1000

.env.production

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NODE_ENV="production"
2+
BASE_URL="http://wmhello.mynatapp.cc/"
3+
VUE_APP_REDIRECT="http://wmhello.mynatapp.cc/api/start_oauth"
4+
VUE_APP_TIMEOUT=2*60*60*1000

package-lock.json

+85-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
"build": "vue-cli-service build"
88
},
99
"dependencies": {
10+
"axios": "^0.19.2",
1011
"core-js": "^3.4.4",
12+
"good-storage": "^1.1.0",
13+
"js-cookie": "^2.2.1",
14+
"qs": "^6.9.1",
15+
"vant": "^2.4.6",
1116
"vue": "^2.6.10",
17+
"vue-ls": "^3.2.1",
1218
"vue-router": "^3.1.3",
13-
"vuex": "^3.1.2"
19+
"vuex": "^3.1.2",
20+
"weixin-js-sdk": "^1.4.0-test"
1421
},
1522
"devDependencies": {
1623
"@vue/cli-plugin-babel": "^4.1.0",

public/.htaccess

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine On
3+
RewriteBase /
4+
RewriteRule ^index\.html$ - [L]
5+
RewriteCond %{REQUEST_FILENAME} !-f
6+
RewriteCond %{REQUEST_FILENAME} !-d
7+
RewriteRule . /index.html [L]
8+
</IfModule>

src/App.vue

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<template>
22
<div id="app">
3-
<div id="nav">
4-
<router-link to="/">Home</router-link> |
5-
<router-link to="/about">About</router-link>
6-
</div>
73
<router-view/>
84
</div>
95
</template>

src/api/jssdk.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import request from '@/utils/request'
2+
3+
export function getConfig(url, params) {
4+
return request({
5+
url: '/api/jssdk/config',
6+
method: 'post',
7+
data: {
8+
api: params,
9+
url
10+
}
11+
})
12+
}
13+
14+
15+
export function pay(price) {
16+
return request({
17+
url: '/api/spa-pay',
18+
method: 'get',
19+
params: {
20+
price
21+
}
22+
})
23+
}
24+
25+

src/assets/jssdk.png

125 KB
Loading

src/main.js

+29
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,37 @@ import Vue from 'vue'
22
import App from './App.vue'
33
import router from './router'
44
import store from './store'
5+
import '@/plugins/storage.js'
6+
import '@/permission.js'
7+
import Vant from 'vant'
8+
import 'vant/lib/index.css';
9+
Vue.use(Vant);
10+
import { Grid, GridItem } from 'vant';
11+
Vue.use(Grid);
12+
Vue.use(GridItem);
513

614
Vue.config.productionTip = false
15+
import wx from 'weixin-js-sdk'
16+
import { getConfig } from '@/api/jssdk'
17+
18+
const actions = ['openLocation','getLocation','updateAppMessageShareData', 'updateTimelineShareData', 'chooseImage', 'uploadImage', 'previewImage', 'getLocalImgData', 'downloadImage','closeWindow','hideMenuItems', 'scanQRCode','','', 'showMenuItems', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseWXPay']
19+
20+
router.afterEach(async (to, from) => {
21+
let _url = window.location.origin + to.fullPath
22+
// 非ios设备,切换路由时候进行重新签名
23+
if (window.__wxjs_is_wkwebview !== true) {
24+
let config = await getConfig(_url, actions)
25+
wx.config(config)
26+
}
27+
})
28+
29+
// ios 设备进入页面则进行js-sdk签名
30+
if (window.__wxjs_is_wkwebview === true) {
31+
let _url = window.location.href.split('#')[0]
32+
getConfig(_url, actions).then(function (res) {
33+
wx.config(res)
34+
})
35+
}
736

837
new Vue({
938
router,

src/minix/operate.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
methods: {
3+
onClickLeft(){
4+
this.$router.go(-1)
5+
}
6+
}
7+
}

src/permission.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import router from '@/router'
2+
import { saveToken } from '@/utils/cache'
3+
import wx from 'weixin-js-sdk'
4+
import store from '@/store'
5+
// import { getConfig } from '@/api/jssdk'
6+
import qs from 'qs'
7+
8+
router.beforeEach((to, from, next) => {
9+
const loginStatus = Number(store.getters.loginStatus)
10+
const token = store.getters.token
11+
if (loginStatus === 0) {
12+
// 微信未授权登录跳转到授权登录页面
13+
const url = window.location.href
14+
// 解决重复登录url添加重复的code与state问题
15+
const parseUrl = qs.parse(url.split('?')[1])
16+
// 无论拒绝还是授权都设置成1
17+
store.dispatch('user/setLoginStatus', 1)
18+
// 跳转到微信授权页面
19+
window.location.href = process.env.VUE_APP_REDIRECT +`?end_url=${url}`
20+
} else if (loginStatus === 1) {
21+
const url = window.location.href
22+
// 解决重复登录url添加重复的code与state问题
23+
const parseUrl = qs.parse(url.split('?')[1])
24+
saveToken(parseUrl.token)
25+
store.dispatch('user/setLoginStatus', 2)
26+
delete to.query.token
27+
next()
28+
} else {
29+
// 已登录直接进入
30+
next()
31+
}
32+
})

src/plugins/storage.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Vue from 'vue'
2+
import Storage from 'vue-ls';
3+
4+
// options = ;
5+
Vue.use(Storage, {
6+
namespace: 'vuejs__', // key键前缀
7+
name: 'ls', // 命名Vue变量.[ls]或this.[$ls],
8+
storage: 'local', // 存储名称: session, local, memory
9+
});

src/router/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ const routes = [
1717
// this generates a separate chunk (about.[hash].js) for this route
1818
// which is lazy-loaded when the route is visited.
1919
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
20+
},
21+
{
22+
path: '/img',
23+
name: 'img',
24+
component: () => import(/* webpackChunkName: "img" */ '../views/Img.vue')
25+
},
26+
{
27+
path: '/pay',
28+
name: 'pay',
29+
component: () => import(/* webpackChunkName: "img" */ '../views/Pay.vue')
2030
}
2131
]
2232

src/store/getters.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const getters = {
2+
// user
3+
token: state => state.user.token,
4+
loginStatus: state => state.user.loginStatus
5+
}
6+
export default getters

src/store/index.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3+
import getters from './getters'
4+
import app from './modules/app'
5+
import user from './modules/user'
36

47
Vue.use(Vuex)
58

6-
export default new Vuex.Store({
7-
state: {
8-
},
9-
mutations: {
10-
},
11-
actions: {
12-
},
9+
const store = new Vuex.Store({
1310
modules: {
14-
}
11+
app,
12+
user
13+
},
14+
getters
1515
})
16+
17+
export default store

0 commit comments

Comments
 (0)