框架:uni-app
UI:uview
这里主要是对获取openid的方法的loginAPI和ajax请求进行async await和promise封装,并且写入缓存和vuex,第二次只读缓存并同步到vuex即可.至于获取用户信息同理,在user.vue发送请求获取用户信息
app.vue
export default {
onLaunch: function() {
this.getOpenid()
// 获取用户信息
let userInfo = uni.getStorageSync('userInfo') || null
this.$store.commit('setUserInfo', userInfo)
},
methods: {
// 获取openid
async getOpenid() {
let openid = this.$store.state.openid
if (!openid) {
// 登录
let code = await new Promise((resolve, reject) => {
uni.login({
success: res => {
resolve(res.code)
},
})
})
// 请求
let res = await this.$u.post('/api/.../get_openid', {
code: code