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; // console.log(data); wx.getUserProfile({ lang: 'zh_CN', desc: '用于完善会员资料', success (res) { that.login(data,res) }, fail (e) { console.error('获取用户身份信息失败了', e); alert.showError('获取失败') } }) }, login(data,userInfo) { const that = this; uni.showLoading({ title: '登录中', }); console.log(userInfo, 1111) wx.login({ success: res => { console.log(res, '----------- login获取的 -----------') const code = res.code; request.post('/login', { code, encryptedData: userInfo.encryptedData, iv: userInfo.iv }, function(data, res) { console.log(data, res ,'----------后端传回来的----------') return const user = { token: data.token, who: data.lawyerInfo.who, expires_at: data.expires_at, lawyerInfo: data.lawyerInfo, clientInfo: data.clientInfo, info: res.userInfo, login_type: 0 }; store.commit('saveUser', user) uni.setStorageSync('user', user); uni.hideLoading(); if (store.state.path) { let path = store.state.path; console.log(path.startsWith('/')) // 判断是否path前面有/ if (!path.startsWith('/')) { path = '/' + path } uni.reLaunch({ url: path, success: function(res) { console.log(res) }, fail: function(res) { console.log(res) } }); } }) }, fail: err => { console.error(err, 8877897) } }) }, logout() { store.state.user = { user: null, path: '/pages/login/login' } store.commit('saveToken', null) // uni.setStorageSync("token",null); uni.clearStorage(); uni.reLaunch({ url: '/pages/login/login' }) console.log('退出登录。。。', this.user) }, } export default login