import $http from "./request.js" export default { // 用户token token: false, // 用户信息 userinfo: false, // 绑定第三方登录情况 userbind: false, latitude: 0, longitude: 0, curcity: false, agreeagent: false, sysnotify: false, // 初始化 __init(){ // 获取用户信息 this.userinfo = uni.getStorageSync("userinfo"); this.token = uni.getStorageSync("token"); this.counts = uni.getStorageSync("counts"); this.userbind = uni.getStorageSync("userbind"); this.curcity = uni.getStorageSync("curcity"); this.agreeagent = uni.getStorageSync("agreeagent"); this.sysnotify = uni.getStorageSync("sysnotify"); }, // 权限验证跳转 navigate(options,type = "navigateTo",isCheck){ // 是否登录验证 if (!$http.checkToken(isCheck)) return; // 验证是否绑定手机号 // if (!$http.checkAuth(true)) return; // 跳转 switch (type){ case "navigateTo": uni.navigateTo(options); break; case "redirectTo": uni.redirectTo(options); break; case "reLaunch": uni.reLaunch(options); break; case "switchTab": uni.switchTab(options); break; } }, // 登录 async login(options ={}){ uni.showLoading({ title: '登录中...', mask: true }); // 请求登录 let [err,res] = await $http.post(options.url,options.data); // 登录失败 if (!$http.errorCheck(err,res)){ uni.hideLoading(); return false; } console.log("res", res); // 登录成功 保存状态 this.token = res.data.data.token; // this.userinfo = this.__formatUserinfo(res.data.data); // 本地存储 // uni.setStorageSync("userinfo", this.userinfo); uni.setStorageSync("token", this.token); // 成功提示 uni.hideLoading(); uni.showToast({ title: '登录成功' }); // 返回上一步 // if (!options.Noback) { // uni.navigateBack({ delta: 1 }); // } return true; }, // 退出登录 async logout(showToast = true){ // 退出登录 await $http.post('/user/logout',{},{ token:true, checkToken:true , }); // 清除缓存 uni.removeStorageSync('userinfo'); uni.removeStorageSync('token'); uni.removeStorageSync('counts'); // 清除状态 this.token = false; this.userinfo = false; this.userbind = false; this.counts = {}; // 返回home页面 uni.switchTab({ url:"/pages/home/home" }) // 退出成功 if (showToast) { return uni.showToast({ title: '退出登录成功' }); } }, // 获取当前用户第三方绑定情况 // async getUserBind(){ // let [err,res] =await $http.get('/user/getuserbind',{},{ // token: true, // checkToken: true // }); // if (!$http.errorCheck(err,res)) return false; // this.userbind = res.data.data; // // 存储缓存 // uni.setStorageSync("userbind", this.userbind); // return true; // }, // 转换第三方登录格式 // __formatOtherLogin(provider,options){ // return { // provider:provider, // openid:options.userInfo.unionId || options.userInfo.openId, // expires_in:options.authResult.expires_in, // nickName:options.userInfo.nickName, // avatarUrl:options.userInfo.avatarUrl, // } // }, setCurCity(city) { uni.setStorageSync("curcity", city); this.curcity = city }, setUserInfo(userinfo) { this.userinfo = userinfo; // 本地存储 uni.setStorageSync("userinfo", this.userinfo); }, setAgreeAgent(value) { // 本地存储 uni.setStorageSync("agreeagent", value); this.agreeagent = value }, setSysNotify(value) { // 本地存储 uni.setStorageSync("sysnotify", value) this.sysnotify = value } }