Commit 89385bfb authored by Damon's avatar Damon

feat: 登录授权

parent 9d446897
let commonApiFun = function(vm) {
let loginWechatAuth = async (params = {}) => await vm.$u.post('/authorization', params)
let loginWechatAuth = async (params = {}) => await vm.$u.post('/wxapp/unify/authorization', params)
const commonApi = {
loginWechatAuth
......@@ -8,4 +8,4 @@ let commonApiFun = function(vm) {
}
module.exports = {
commonApiFun
}
}
import { commonApiFun } from './common.js';
import { commonApiFun } from './common.js'
const apiFun = function(vm) {
let commonApi = commonApiFun(vm);
let apiUrl = {
...commonApi,
}
return apiUrl
let commonApi = commonApiFun(vm)
let apiUrl = {
...commonApi,
}
return apiUrl
}
module.exports = {
apiFun
apiFun,
}
......@@ -9,7 +9,8 @@ const install = (Vue, vm) => {
channel: 'Applets',
'content-type': 'application/json; charset=utf-8',
},
originalData: true
loadingTime: 0,
originalData: true,
})
// 请求拦截,配置Token等参数
......@@ -34,30 +35,30 @@ const install = (Vue, vm) => {
// 响应拦截,判断状态码是否通过
Vue.prototype.$u.http.interceptor.response = (res) => {
const data = res.data
if (res.statusCode === 200) {
if (data.code === 0) {
// res为服务端返回值,可能有code,result等字段
return data.data
} else if (data.code === 401) {
vm.$u.toast(data.msg)
let pages = getCurrentPages()
let route = pages[pages.length - 1].route
// 如果不是在登录页,则跳转到登录页
if (route !== 'pages/login/index') {
setTimeout(() => {
vm.$u.route('/pages/login/index')
}, 1500)
}
return false
} else {
// 如果返回false,则会调用Promise的reject回调,
vm.$u.toast(data.msg)
return false
}
} else { // HTTP状态非200
vm.$u.toast(data.msg || '服务器错误')
}
const data = res.data
if (res.statusCode === 200) {
if (data.code === 0) {
return data.data
} else if (data.code === 401) {
vm.$u.toast(data.msg)
let pages = getCurrentPages()
let route = pages[pages.length - 1].route
// 如果不是在登录页,则跳转到登录页
if (route !== 'pages/login/index') {
setTimeout(() => {
vm.$u.route('/pages/login/index')
}, 1500)
}
return false
} else {
// 如果返回false,则会调用Promise的reject回调,
vm.$u.toast(data.msg)
return false
}
} else {
// HTTP状态非200
vm.$u.toast(data.msg || '服务器错误')
}
}
}
......
......@@ -17,8 +17,7 @@ export default {
host: '', //配置为自己的额服务器即可
}
},
onShow() {
},
onShow() {},
methods: {
wxLogin() {
// 获取code 小程序专有,用户登录凭证。
......@@ -31,12 +30,19 @@ export default {
provider: 'weixin',
success: (code_res) => {
if (code_res.errMsg == 'login:ok') {
let code = code_res.code
console.log('user, code', user, code)
this.$u.vuex('vuex_token', code)
this.$u.vuex('userInfo', user.userInfo)
this.$u.route({
type: 'back'
const code = code_res.code
const userInfo = user.userInfo
const data = {
code: code,
avatar: userInfo.avatarUrl,
nickname: userInfo.nickName,
}
this.$u.api.loginWechatAuth(data).then((res) => {
this.$u.vuex('vuex_token', res.token)
this.$u.vuex('userInfo', userInfo)
this.$u.route({
type: 'back',
})
})
}
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment