You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.7 KiB

2 years ago
import store from "../store.js"
import request from './request.js'
import alert from './alert.js'
const login = {
checkAuth(callback) {
console.log(1);
if (this.getLocalUserInfo()) {
console.log(2);
callback(true)
} else {
console.log(3);
callback(false);
console.log('未获取到用户本地数据,去获取授权设置', 22222222)
this.askLogin()
}
},
getLocalUserInfo() {
const user = uni.getStorageSync("token");
console.log('从本地存储中获取用户数据', user, 11111111);
if (!user) return false;
if (!user.token) return false;
store.commit('saveToken', user);
return true
},
noCase() {
alert.showError('案件不存在');
setTimeout(() => {
uni.switchTab({
url: '/pages/case/caseList/caseList'
});
}, 1500);
},
askLogin() {
uni.showModal({
title: '尚未登录',
content: '前往授权登录页面吗?',
success(res) {
console.log(res)
if (res.confirm) {
uni.navigateTo({
url: '/pages/login/login'
})
}
}
})
},
getUserProfile(data) {
const that = this;
2 years ago
console.log(data);
// return
2 years ago
wx.getUserProfile({
lang: 'zh_CN',
desc: '用于完善会员资料',
success (res) {
that.login(data,res)
},
fail (e) {
console.error('获取用户身份信息失败了', e);
alert.showError('获取失败')
}
})
},
2 years ago
login(data) {
2 years ago
const that = this;
uni.showLoading({
title: '登录中',
2 years ago
});
2 years ago
wx.login({
success: res => {
console.log(res, '----------- login获取的 -----------')
const code = res.code;
2 years ago
console.log(res.code);
request.post('/api/Accounts/SignIn', {
wechatToken:code,
username:data.name,
password:data.password
2 years ago
}, function(data, res) {
uni.hideLoading();
2 years ago
console.log(data, res ,'----------后端传回来的----------')
if(res.status==500){
return alert.showError("用户名或密码错误")
2 years ago
}
2 years ago
uni.setStorageSync("token", res)
store.commit('saveToken', res)
console.log(store.state.token);
request.get("/api/Accounts/Profile", {}, (data, res) => {
console.log(res);
uni.setStorageSync("user", res)
uni.reLaunch({
url: "/pages/index/index",
})
})
2 years ago
})
},
fail: err => {
console.error(err, 8877897)
}
})
},
logout() {
store.state.user = {
user: null,
2 years ago
path: '/pages/login/login'
2 years ago
}
2 years ago
store.commit('saveToken', null)
// uni.setStorageSync("token",null);
2 years ago
uni.clearStorage();
uni.reLaunch({
2 years ago
url: '/pages/login/login'
2 years ago
})
console.log('退出登录。。。', this.user)
},
}
export default login