初始化项目模板
This commit is contained in:
1 parent
80fd6a1d4b
commit
b3a69a1f27
69 files changed
+11088
No files matched your search
+159
@@ -0,0 +1,159 @@
|
||||
import {
|
||||
isEmpty
|
||||
} from '@/common/common.js'
|
||||
import {
|
||||
useUserStore
|
||||
} from '@/store/user.js'
|
||||
import { request } from '@/utils/request.js'
|
||||
|
||||
/*
|
||||
@desc 多端一键登录授权
|
||||
@desc wx:自2023年8月26日起,手机号快速验证组件将需要付费使用。标准单价为:每次组件调用成功,收费0.03元。
|
||||
zfb:用户同意授权后,scope为长期有效,需用户自行手动解除授权。
|
||||
@params e 为getPhoneNumber原生按钮的回调值
|
||||
*/
|
||||
export function uniLogin(e) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (e.detail.errMsg !== 'getPhoneNumber:ok') {
|
||||
return
|
||||
}
|
||||
// 获取当前操作端
|
||||
uni.getProvider({
|
||||
service: 'oauth',
|
||||
success: (res) => {
|
||||
if (res.provider.includes('weixin') || res.provider.includes('alipay')) {
|
||||
// 登录
|
||||
uni.login({
|
||||
provider: res.provider[0],
|
||||
success: (loginRes) => {
|
||||
if (loginRes.code) {
|
||||
// 请求后端接口解密code,拿到真实电话号码,并缓存,然后关闭弹窗,渲染页面数据
|
||||
var params = {
|
||||
codeType: 'mpMobile',
|
||||
codeValue: JSON.stringify({
|
||||
...e.detail,
|
||||
...{
|
||||
loginData: loginRes,
|
||||
},
|
||||
}),
|
||||
}
|
||||
request({
|
||||
method: 'POST',
|
||||
url: '/sale/login/login',
|
||||
data: params,
|
||||
}).then((resp) => {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
mask: true,
|
||||
title: '登录成功',
|
||||
})
|
||||
if (resp.success) {
|
||||
const userStore = useUserStore()
|
||||
userStore.userToken = resp.data.token;
|
||||
userStore.isLogin = resp.data.isLogin;
|
||||
userStore.setProfile({
|
||||
...resp.data.userData,
|
||||
});
|
||||
resolve(true)
|
||||
} else {
|
||||
// 失败的操作
|
||||
console.log('request-fail', resp)
|
||||
uni.showToast({
|
||||
title: '授权登录失败',
|
||||
icon: 'none',
|
||||
})
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log({ err })
|
||||
uni.showToast({
|
||||
title: '授权失败',
|
||||
icon: 'none',
|
||||
})
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @desc 获取当前小程序端的登录授权码code
|
||||
*/
|
||||
export function getLoginCode() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef MP-WEIXIN || MP-ALIPAY
|
||||
uni.getProvider({
|
||||
service: 'oauth',
|
||||
success: function(res) {
|
||||
if (~res.provider.indexOf('weixin') || ~res.provider.indexOf('alipay')) {
|
||||
// 登录
|
||||
uni.login({
|
||||
provider: res.provider[0],
|
||||
success: function(loginRes) {
|
||||
resolve(loginRes);
|
||||
},
|
||||
fail(err) {
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
// #endif
|
||||
// #ifdef MP-BAIDU
|
||||
swan.getLoginCode({
|
||||
success(loginRes) {
|
||||
resolve(loginRes);
|
||||
},
|
||||
fail(err) {
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
// #endif
|
||||
// #ifdef APP-PLUS || H5
|
||||
resolve();
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc 退出登录,清除token,手机号缓存
|
||||
*/
|
||||
export function loginOut(status) {
|
||||
const userStore = useUserStore();
|
||||
userStore.clearProfile();
|
||||
userStore.userToken = '';
|
||||
uni.showLoading({
|
||||
mask: true,
|
||||
title: 'loading...'
|
||||
})
|
||||
setTimeout(() => {
|
||||
if (status) {
|
||||
uni.switchTab({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}
|
||||
uni.hideLoading()
|
||||
}, 800)
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc 检查是否存在token等用户信息
|
||||
* @returns false 不存在token 、true存在token
|
||||
*/
|
||||
export function checkLoginStatus() {
|
||||
const userStore = useUserStore();
|
||||
const token = userStore.userToken;
|
||||
if (isEmpty(token)) {
|
||||
loginOut(false)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user