smartmeter-app/common/http.api.js
2023-12-05 16:45:28 +08:00

119 lines
3.8 KiB
JavaScript

const install = (Vue, vm) => {
let login = (params = {}) => vm.$u.post('/user/login',params).then(res=>{
console.log("res",res)
console.log("res.code",res.code == 10003)
if (res.code == 10003) {
console.log("新用户登录")
uni.setStorageSync('token', res.data.token);
wx.showModal({
title: '温馨提示',
content: '您还未登陆,需要获取您的个人信息',
success(res) {
if (res.confirm) {
wx.getUserProfile({
desc: "获取你的昵称、头像、地区及性别",
success: res => {
console.log(res)
vm.$u.post('/user/addVxUser',{
nickName: res.userInfo.nickName,
userAvatarUrl: res.userInfo.avatarUrl,
sex: res.userInfo.gender
}).then(res=>{
console.log("看看结果")
console.log(res)
if (res.code == 200) {
//这就代表ok
getApp().globalData.userInfo = res.data
getApp().globalData.isLogin = true
uni.setStorage({
key: 'userInfo',
data: res.data,
success: function () {
console.log('缓存用户信息 success');
},
fail:function(){
console.log('缓存用户信息 fail');
}
});
}else if (res.code == 701) {
//添加失败
uni.showToast({
title: '登陆失败',
duration: 2000
})
}
})
},
fail: res => {
//拒绝授权
// uni.showErrorModal('您拒绝了请求');
uni.showToast({
title:"您拒绝了请求",
icon:"none",
duration:5000
})
return;
}
})} else if (res.cancel) {
//拒绝授权 showErrorModal是自定义的提示
// uni.showErrorModal('您拒绝了请求');
uni.showToast({
title:"您拒绝了请求",
icon:"none",
duration:5000
})
return;
}
}
})
} else if (res.code == 200) {
console.log("老用户登录",res.data)
uni.setStorageSync('token', res.data.token);
//这里需要做缓存
getApp().globalData.userInfo = res.data
getApp().globalData.isLogin = true
console.log("globalData.userInfo",getApp().globalData.userInfo)
uni.setStorage({
key: 'userInfo',
data: res.data,
success: function () {
console.log('缓存用户信息 success');
},
fail:function(){
console.log('缓存用户信息 fail');
}
});
}
});
let loginWithoutUser = (params = {}) => vm.$u.post('/mini/user/login',params).then(res=>{
console.log("res",res)
console.log("res.code",res.code == 10003)
if (res.code == 10003) {
console.log("新用户登录")
} else if (res.code == 200) {
console.log("老用户登录",res.data)
//这里需要做缓存
getApp().globalData.userInfo = res.data
getApp().globalData.isLogin = true
console.log("globalData.userInfo",getApp().globalData.userInfo)
uni.setStorage({
key: 'userInfo',
data: res.data,
success: function () {
console.log('缓存用户信息 success');
},
fail:function(){
console.log('缓存用户信息 fail');
}
});
}
});
vm.$u.api = {login,loginWithoutUser};
}
export default {
install
}