|
|
|
@ -109,46 +109,6 @@ export const copyClipboard = (data) => {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const replaceLogin = (msg) => { |
|
|
|
|
uni.hideLoading(); |
|
|
|
|
console.log('尝试开始重新登录') |
|
|
|
|
uni.showToast({ |
|
|
|
|
title: '重新登录中...', |
|
|
|
|
icon: 'none', |
|
|
|
|
duration: 2000 |
|
|
|
|
}); |
|
|
|
|
// 这里代表已经失去登录状态以及401强制推出登录了
|
|
|
|
|
store.commit('LOGOUT') |
|
|
|
|
if (Vue.prototype.$deviceType == 'routine') { |
|
|
|
|
console.log('当前是微信小程序,开始调用登录方法') |
|
|
|
|
// 如果是微信小程序,跳转到授权页
|
|
|
|
|
login({ |
|
|
|
|
fail: () => { |
|
|
|
|
console.log('自动登录失败,重定向授权页面') |
|
|
|
|
reLaunch({ |
|
|
|
|
path: '/pages/authorization/index', |
|
|
|
|
query: { |
|
|
|
|
redirect: `/${getCurrentPageUrl()}`, |
|
|
|
|
...parseQuery() |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
// 如果不是小程序跳转到登录页
|
|
|
|
|
console.log('当前是app,跳转到登录页面') |
|
|
|
|
push({ |
|
|
|
|
path: '/pages/user/Login/index', |
|
|
|
|
query: { |
|
|
|
|
redirect: `/${getCurrentPageUrl()}`, |
|
|
|
|
...parseQuery() |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const getProvider = (service) => { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
// 获取当前环境的服务商
|
|
|
|
@ -172,27 +132,25 @@ export const getProvider = (service) => {
|
|
|
|
|
|
|
|
|
|
export const authorize = (authorizeStr) => { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
uni.authorize({ |
|
|
|
|
scope: `scope.${authorizeStr}`, |
|
|
|
|
success() { |
|
|
|
|
console.log('检验授权', `scope.${authorizeStr}`) |
|
|
|
|
uni.getSetting({ |
|
|
|
|
success(res) { |
|
|
|
|
console.log(res.authSetting) |
|
|
|
|
if (res.authSetting[`scope.${authorizeStr}`]) { |
|
|
|
|
resolve('获取授权成功') |
|
|
|
|
} else { |
|
|
|
|
reject('获取授权失败') |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
fail() { |
|
|
|
|
|
|
|
|
|
console.log('授权失败跳转首页') |
|
|
|
|
switchTab({ |
|
|
|
|
path: '/pages/home/index', |
|
|
|
|
// query
|
|
|
|
|
}); |
|
|
|
|
reject('获取授权失败') |
|
|
|
|
reject('获取设置失败') |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}).catch(error => { |
|
|
|
|
console.log(error) |
|
|
|
|
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const login = (option) => { |
|
|
|
|
export const login = () => { |
|
|
|
|
console.log('开始登录 ————————————————————') |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
console.log('获取环境商') |
|
|
|
@ -215,62 +173,48 @@ export const login = (option) => {
|
|
|
|
|
success: function (user) { |
|
|
|
|
console.log('获取用户信息成功') |
|
|
|
|
console.log('开始调用登录接口') |
|
|
|
|
if (Vue.prototype.$deviceType == 'routine') { |
|
|
|
|
wxappAuth({ |
|
|
|
|
encryptedData: user.encryptedData, |
|
|
|
|
iv: user.iv, |
|
|
|
|
code: code, |
|
|
|
|
spread: cookie.get("spread") |
|
|
|
|
}).then(({ |
|
|
|
|
data |
|
|
|
|
}) => { |
|
|
|
|
}).then(({ data }) => { |
|
|
|
|
console.log('登录接口调用成功') |
|
|
|
|
console.log('开始处理登录信息保存,并获取用户详情') |
|
|
|
|
resolve(data) |
|
|
|
|
uni.hideLoading(); |
|
|
|
|
store.commit("LOGIN", data.token, dayjs(data.expires_time)); |
|
|
|
|
store.dispatch('USERINFO', true) |
|
|
|
|
console.log(store) |
|
|
|
|
handleGetUserInfo() |
|
|
|
|
getUser().then(user => { |
|
|
|
|
console.log('获取用户信息成功') |
|
|
|
|
store.dispatch('setUserInfo', user.data) |
|
|
|
|
resolve(user) |
|
|
|
|
}).catch(error => { |
|
|
|
|
console.log('获取用户信息失败') |
|
|
|
|
reject('获取用户信息失败') |
|
|
|
|
}); |
|
|
|
|
}).catch(error => { |
|
|
|
|
console.log('登录接口调用失败') |
|
|
|
|
reject() |
|
|
|
|
console.log(error) |
|
|
|
|
handleFail(option, '微信登录失败') |
|
|
|
|
reject('登录接口调用失败') |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
fail() { |
|
|
|
|
console.log('获取用户信息失败') |
|
|
|
|
// 获取用户信息失败
|
|
|
|
|
reject() |
|
|
|
|
handleFail(option, '获取用户信息失败') |
|
|
|
|
reject('获取用户信息失败') |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}).catch(error => { |
|
|
|
|
console.log('用户未授权') |
|
|
|
|
reject() |
|
|
|
|
console.log(error) |
|
|
|
|
handleFail(option, '用户未授权') |
|
|
|
|
reject('用户未授权') |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
fail() { |
|
|
|
|
console.log('调用登录接口失败') |
|
|
|
|
// 调用登录接口失败
|
|
|
|
|
reject() |
|
|
|
|
handleFail(option, '登录失败') |
|
|
|
|
reject('调用登录接口失败') |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}).catch(error => { |
|
|
|
|
handleFail(option, '获取环境服务商失败') |
|
|
|
|
reject() |
|
|
|
|
console.log(error) |
|
|
|
|
handleFail(option, '获取环境服务商失败') |
|
|
|
|
reject('获取环境服务商失败') |
|
|
|
|
}) |
|
|
|
|
}).catch(error => { |
|
|
|
|
handleFail(option, '登录失败') |
|
|
|
|
console.log(error) |
|
|
|
|
handleFail(option, '登录失败') |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -343,18 +287,13 @@ export const handleGetUserInfo = () => {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleFail = (option, msg) => { |
|
|
|
|
// 此处是处理登录失效的问题的
|
|
|
|
|
option && option.fail ? option.fail() : replaceLogin('登录失败,请重新登录') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function parseUrl(location) { |
|
|
|
|
if (typeof location === 'string') return location |
|
|
|
|
const { |
|
|
|
|
path, |
|
|
|
|
query |
|
|
|
|
} = location |
|
|
|
|
|
|
|
|
|
const queryStr = stringify(query) |
|
|
|
|
|
|
|
|
|
if (!queryStr) { |
|
|
|
@ -419,6 +358,8 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
|
|
|
|
// 是否可以访问
|
|
|
|
|
let isAuth = false |
|
|
|
|
|
|
|
|
|
console.log('即将跳转', location, parseUrl(location)) |
|
|
|
|
|
|
|
|
|
// 从 location 中获取当前url,location typeof string || object
|
|
|
|
|
let path = '' |
|
|
|
|
if (typeof location === 'string') { |
|
|
|
@ -427,6 +368,7 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
|
|
|
|
path = location.path |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 判断用户是否有token
|
|
|
|
|
if (!auth()) { |
|
|
|
|
page.map((item) => { |
|
|
|
|
if (item.path == path) { |
|
|
|
@ -439,7 +381,14 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
if (isAuth) { |
|
|
|
|
// 登录了有权限
|
|
|
|
|
// 有token
|
|
|
|
|
if (path == '/pages/home/index' || path == '/pages/shop/GoodsClass/index' || path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') { |
|
|
|
|
// switchTab({
|
|
|
|
|
// path: parseUrl(location),
|
|
|
|
|
// })
|
|
|
|
|
// return
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resolve({ |
|
|
|
|
url: parseUrl(location), |
|
|
|
|
complete, |
|
|
|
@ -447,8 +396,8 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
|
|
|
|
success |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
|
// 未登录没有权限
|
|
|
|
|
replaceLogin() |
|
|
|
|
// 没有token,先校验用户是否授权,如果授权了,进行自动登录
|
|
|
|
|
routerPermissions(parseUrl(location)) |
|
|
|
|
reject() |
|
|
|
|
} |
|
|
|
|
}).catch(error => { |
|
|
|
@ -456,6 +405,74 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// export function checkPermissions(){
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
export function routerPermissions(url, type) { |
|
|
|
|
console.log('routerPermissions', url) |
|
|
|
|
let path = url |
|
|
|
|
if (!path) { |
|
|
|
|
path = getCurrentPageUrlWithArgs() |
|
|
|
|
} |
|
|
|
|
if (Vue.prototype.$deviceType == 'routine') { |
|
|
|
|
console.log('当前是微信小程序,开始处理小程序登录方法') |
|
|
|
|
// 如果是微信小程序,跳转到授权页
|
|
|
|
|
// 先校验用户是否授权,如果授权了,进行自动登录
|
|
|
|
|
authorize('userInfo').then(() => { |
|
|
|
|
// 自动登录
|
|
|
|
|
login().then(res => { |
|
|
|
|
// 登录成功,跳转到需要跳转的页面
|
|
|
|
|
console.log('登录成功,跳转页面') |
|
|
|
|
store.commit("UPDATE_AUTHORIZATIONPAGE", false); |
|
|
|
|
if (path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (type == 'reLaunch') { |
|
|
|
|
reLaunch({ |
|
|
|
|
path, |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
|
push({ |
|
|
|
|
path, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}).catch(error => { |
|
|
|
|
uni.showToast({ |
|
|
|
|
title: error, |
|
|
|
|
icon: "none", |
|
|
|
|
duration: 2000 |
|
|
|
|
}); |
|
|
|
|
reLaunch({ |
|
|
|
|
path: '/pages/authorization/index', |
|
|
|
|
}) |
|
|
|
|
cookie.set('redirect', path) |
|
|
|
|
}) |
|
|
|
|
}).catch(error => { |
|
|
|
|
// 跳转到登录页面或者授权页面
|
|
|
|
|
if (path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') { |
|
|
|
|
switchTab({ |
|
|
|
|
path, |
|
|
|
|
}) |
|
|
|
|
store.commit("UPDATE_AUTHORIZATIONPAGE", false); |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
reLaunch({ |
|
|
|
|
path: '/pages/authorization/index', |
|
|
|
|
}) |
|
|
|
|
cookie.set('redirect', path) |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
|
// 如果不是小程序跳转到登录页
|
|
|
|
|
console.log('当前是app,开始处理app登录方法') |
|
|
|
|
push({ |
|
|
|
|
path: '/pages/user/Login/index', |
|
|
|
|
}) |
|
|
|
|
cookie.set('redirect', path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function push(location, complete, fail, success) { |
|
|
|
|
handleLoginStatus(location, complete, fail, success).then(params => { |
|
|
|
|
uni.navigateTo(params) |
|
|
|
@ -662,3 +679,98 @@ export const PosterCanvas = (store, successCallBack) => {
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const handleLoginFailure = () => { |
|
|
|
|
store.commit("LOGOUT"); |
|
|
|
|
store.commit("UPDATE_AUTHORIZATION", false); |
|
|
|
|
|
|
|
|
|
// token 失效
|
|
|
|
|
// 判断当前是不是已经在登录页面或者授权页,防止二次跳转
|
|
|
|
|
if (store.getters.isAuthorizationPage || getCurrentPageUrl() == '/pages/user/Login/index') { |
|
|
|
|
console.log('已经是登录页面或者授权页面,跳出方法') |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('当前是授权页面') |
|
|
|
|
console.log(store.getters) |
|
|
|
|
store.commit("UPDATE_AUTHORIZATIONPAGE", true); |
|
|
|
|
|
|
|
|
|
let path = getCurrentPageUrlWithArgs() |
|
|
|
|
|
|
|
|
|
// 判断是不是拼团进来的
|
|
|
|
|
if (getCurrentPageUrl() == 'pages/activity/GroupRule/index' && handleQrCode()) { |
|
|
|
|
console.log('是拼团进来的') |
|
|
|
|
let url = handleQrCode(); |
|
|
|
|
console.log(url) |
|
|
|
|
if (url) { |
|
|
|
|
path = parseUrl({ |
|
|
|
|
path: `/${getCurrentPageUrl()}`, |
|
|
|
|
query: { |
|
|
|
|
id: url.pinkId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
// cookie.set("spread", url.spread || 0);
|
|
|
|
|
} else { |
|
|
|
|
console.log('是拼团进来的,但是没有获取到参数') |
|
|
|
|
handleNoParameters() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 判断是不是扫描的砍价海报进来的
|
|
|
|
|
if (getCurrentPageUrl() == 'pages/activity/DargainDetails/index' && handleQrCode()) { |
|
|
|
|
console.log('是扫描的砍价海报进来的') |
|
|
|
|
let url = handleQrCode(); |
|
|
|
|
if (url) { |
|
|
|
|
path = parseUrl({ |
|
|
|
|
path: `/${getCurrentPageUrl()}`, |
|
|
|
|
query: { |
|
|
|
|
id: url.bargainId, |
|
|
|
|
partake: url.uid |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
// cookie.set("spread", url.spread || 0);
|
|
|
|
|
} else { |
|
|
|
|
handleNoParameters() |
|
|
|
|
console.log('是扫描的砍价海报进来的,但是没有获取到参数') |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (getCurrentPageUrl() == 'pages/shop/GoodsCon/index' && handleQrCode()) { |
|
|
|
|
console.log('是扫描的商品详情') |
|
|
|
|
let url = handleQrCode(); |
|
|
|
|
console.log(url) |
|
|
|
|
if (url) { |
|
|
|
|
path = parseUrl({ |
|
|
|
|
path: `/${getCurrentPageUrl()}`, |
|
|
|
|
query: { |
|
|
|
|
id: url.productId, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
cookie.set("spread", url.spread || 0); |
|
|
|
|
} else { |
|
|
|
|
handleNoParameters() |
|
|
|
|
console.log('是扫描的商品详情进来的,但是没有获取到参数') |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
routerPermissions(path, 'reLaunch') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const handleNoParameters = () => { |
|
|
|
|
uni.showToast({ |
|
|
|
|
title: '未获取到必要参数,即将跳转首页', |
|
|
|
|
icon: 'success', |
|
|
|
|
duration: 2000 |
|
|
|
|
}) |
|
|
|
|
setTimeout(() => { |
|
|
|
|
clearTimeout() |
|
|
|
|
switchTab({ |
|
|
|
|
path: '/pages/home/index', |
|
|
|
|
}); |
|
|
|
|
}, 1500) |
|
|
|
|
} |
|
|
|
|