Files
yunxinwei-uniapp/App.vue
messi 4810dc79e3 fix
2024-08-19 17:17:46 +08:00

242 lines
5.9 KiB
Vue

<script>
import store from '@/store'
import storage from '@/utils/storage'
import {
ACCESS_TOKEN,
USER_ID
} from '@/store/mutation-types'
import SettingModel from '@/common/model/Setting'
import {
getSceneData
} from './core/app'
import {
getCaptcha,
getAuthCode,
login,
getPhoneNumber,
register,
alipayLogin
} from '@/websoft/api/login.js'
import {
tenantId,
roleId,
userId,
username,
password
} from '@/config.js';
import {
getUser
} from './websoft/api/user'
import http from './websoft/api'
export default {
/**
* 全局变量
*/
globalData: {},
/**
* 初始化完成时触发
*/
async onLaunch({
path,
query,
scene
}) {
// 程序初始化
// await this.init(path,query)
// 获取头像昵称
// await this.getAuthCode()
// 小程序主动更新
// this.updateManager()
// app启动参数
// this.onStartupQuery(query)
// 设置全局自定义主题
// this.setAppTheme()
},
onShow() {},
methods: {
// 程序初始化
async init(path,query) {
console.log("程序初始化:>>>> ");
const app = this
// 1. 登录获取token
const token = storage.get(ACCESS_TOKEN)
if(!token){
app.doAlipayLogin()
// await app.getAuthCode()
}
if(query && query.equipmentId){
this.$navTo(path,query)
}
console.log("初始化结束:>>>> ");
},
// 执行支付宝无感登录
doAlipayLogin() {
const app = this
// 游客身份登录获取token
login({username, password}).then(res => {
// 登录成功
store.dispatch('setUserId', res.data.user.userId)
store.dispatch('setToken', res.data.access_token)
const expiryTime = 30 * 86400 // 过期时间30天
storage.set(ACCESS_TOKEN, res.data.access_token, expiryTime)
storage.set(USER_ID, res.data.user.userId, expiryTime)
// 配置请求头
// http.setConfig((config) => {
// config.header = {
// Authorization: res.data.access_token
// }
// return config
// })
})
},
// 获取头像昵称用户名
getAuthCode(){
const app = this
my.getAuthCode({
scopes: 'auth_user',
success: ({ authCode }) => {
console.log("获取支付宝授权码: ",authCode);
// 跟进授权码获取支付宝用户ID
getAuthCode({ authCode })
.then(res => {
console.log("userId: ",res.data);
// 拿到userId后执行登录事件
app.onAuthSuccess(res.data)
})
.catch(e => {
app.$error(e.message)
})
}
});
},
// 执行登录事件
onAuthSuccess(userId) {
const app = this
// 提交到后端
alipayLogin({
username: userId,
password: userId
})
.then(res => {
console.log("老用户登录成功: ", res);
// 老用户登录成功
store.dispatch('setUserInfo',res.data.user)
store.dispatch('setUserId',res.data.user.userId)
store.dispatch('setToken',res.data.access_token)
const expiryTime = 30 * 86400 // 过期时间30天
storage.set(ACCESS_TOKEN, res.data.access_token, expiryTime)
storage.set(USER_ID, res.data.user.userId, expiryTime)
// 配置请求头
http.setConfig((config) => {
config.header = {
Authorization: res.data.access_token
}
return config
})
// 显示登录成功
app.$toast(res.message)
// 跳转回原页面
setTimeout(() => {
app.onNavigateBack(1)
}, 2000)
})
.catch(err => {
console.log("err: ",err);
// 新用户自动注册(默认角色roleId为注册用户81)
register({
username: userId,
roleId
}).then(res => {
console.log("新用户登录成功: ", res);
store.dispatch('setUserInfo',res.data.user)
store.dispatch('setUserId',res.data.user.userId)
store.dispatch('setToken',res.data.access_token)
// 过期时间30天
const expiryTime = 30 * 86400
storage.set(ACCESS_TOKEN, res.data.access_token, expiryTime)
storage.set(USER_ID, res.data.user.userId, expiryTime)
// 配置请求头
http.setConfig((config) => {
config.header = {
Authorization: res.data.access_token
}
return config
})
// 显示登录成功
app.$toast(res.message)
// 跳转回原页面
setTimeout(() => {
app.onNavigateBack(1)
}, 2000)
})
})
},
// app启动参数
onStartupQuery(query) {
// 获取二维码场景值
const scene = getSceneData(query)
// 记录推荐人ID
const refereeId = query.refereeId ? query.refereeId : scene.uid
refereeId > 0 && (this.saveRefereeId(refereeId))
},
// 记录推荐人ID
saveRefereeId(refereeId) {
store.dispatch('SaveRefereeId', refereeId)
},
// 设置全局自定义主题
setAppTheme() {
SettingModel.setAppTheme()
},
// 小程序主动更新
updateManager() {
const updateManager = uni.getUpdateManager()
updateManager.onCheckForUpdate(res => {
// 请求完新版本信息的回调
// console.log(res.hasUpdate)
})
updateManager.onUpdateReady(() => {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,即将重启应用',
showCancel: false,
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(() => {
// 新的版本下载失败
uni.showModal({
title: '更新提示',
content: '新版本下载失败',
showCancel: false
})
})
}
}
}
</script>
<style lang="scss">
/* uView库样式 */
@import "uview-ui/index.scss";
/* 项目基础样式 */
@import "./app.scss";
/* iconfont图标库 */
@import "/utils/iconfont.scss";
</style>