import { defineStore } from 'pinia' import api from '@/api/index' // import config from '../config' // useStore 可以是 useUser、useCart 之类的任何东西 // 第一个参数是应用程序中 store 的唯一 id export const useStore = defineStore('main2', { state: () => { return { // 所有这些属性都将自动推断其类型 isLogin: false, userInfo:null, access_token:'', historySearchWords:'', course_detail_id:0, timer:null, surplus_seconds:null,// 倒计时 test_time:0,//测试用时 test_timer:null, total_score:0, status_bar_height: 0, debounceTimer:null, systemInfo:null, cartList:[], cartChange:false, remark_text:'', address:'', operation_info:null, // 手术信息 bgList:[], // 测试结果背景图 } }, actions:{ //应用初始化,在这里获取必要的基础信息 appInit() { //获取设备信息 const { statusBarHeight } = uni.getSystemInfoSync(); this.status_bar_height = statusBarHeight }, countdownFunc(minutes){ // 计时 this.surplus_seconds = minutes*60 }, saveToken(token){ // 保存token uni.setStorage({ key: 'access_token', data: token, success: function () { console.log('token was saved'); } }); }, saveVisitState(){ // 保存浏览状态 uni.setStorage({ key: 'visitState', data: 1, success: function () { console.log('visitState was saved'); } }); }, checkLogin(){ // 检查是否登录 // let access_token = uni.getStorageSync('access_token') let u_info = uni.getStorageSync('userInfo') // console.log("u_info is ",u_info) if(u_info) { this.access_token = uni.getStorageSync('access_token') this.isLogin = true this.userInfo = uni.getStorageSync('userInfo') this.operation_info = uni.getStorageSync('operation_info') // this.getUserInfo() } else{ this.access_token = null this.isLogin = false this.userInfo = null // 清空缓存 // uni.removeStorageSync('userInfo'); // uni.removeStorageSync('access_token'); // uni.removeStorageSync('avatarUrl'); } // else{ // if(!visitState) { // // uni.navigateTo({ // // url:`/pages/user/login` // // }); // } // } }, async getUserInfo() {// 获取用户信息 const _this = this //测试 // 拦截器 // uni.addInterceptor('request',{ // complete(res) { // // console.log('拦截信息:',res) // if(res.data.data.tag === 'user') { // user接口 // // console.log('interceptor-complete',res) // if(res.data.code === 1) { // _this.userInfo = res.data.data.user // } // if(res.data.code === 7 ) { // token过期 清除token缓存 // _this.access_token = '' // _this.isLogin = false // uni.removeStorageSync('access_token'); // uni.removeStorageSync('visitState'); // } // if(res.header.authorization) { // _this.saveToken(res.header.authorization) // _this.isLogin = true // } // uni.removeInterceptor('request') // } // } // }) //测试 结束 const res = await api.getUserInfo() // console.log('用户信息:',res) if(res.code == 1) { this.userInfo = res.data uni.setStorageSync('userInfo',res.data); } // else if(res.code == 7){ // token过期 清除token缓存 // this.access_token = '' // this.isLogin = false // uni.removeStorageSync('access_token'); // uni.removeStorageSync('visitState'); // } }, logout() { // 登出 this.userInfo = null this.isLogin = false uni.removeStorageSync('access_token'); uni.removeStorageSync('visitState'); }, clearStorageFunc() { // 清除倒计时缓存 uni.removeStorageSync('surplus_seconds') uni.removeStorageSync('test_time') uni.removeStorageSync('saved_right_indexs_storage') // 清除正确题目的提交记录 }, toLogin() { // 跳转登录页 uni.navigateTo({ url:'/pages/user/login' }); }, showMsg(msg,type){ uni.showToast({ title: msg, icon :type || 'none', duration: 2000 }); }, async getCartList(){ const res = await api.getCartList() // console.log(res) if(res.code === 1) { // if(res.data.length >0) { this.cartList = res.data // cart_list.value = res.data // } } }, async getBgList(){ const res = await api.getBgList({page:1,pageSize:100}) // console.log(res) if(res.code == 0) { if(res.data && res.data.list && res.data.list.length>0 ) { this.bgList = res.data.list // console.log(this.bgList) } } }, afterFailLogin(duration){ setTimeout(()=>{ uni.removeStorageSync('userInfo'); uni.removeStorageSync('access_token'); uni.removeStorageSync('avatarUrl'); this.userInfo = null this.operation_info = null uni.switchTab({ url:'/pages/user/index' }) },duration) }, async getAccess_token(params={}) { //清除token缓存 wx.clearStorageSync(); //访问接口 const data={ username:params.code,//手机号按钮获取的code type:'wechat_mini_app', identity:'user', password:'111' } // console.log('登录参数:',data);return; const res = await api.getToken(data) if(res.code === 200) { // newUser.value = res.data.newUser // res.data.newUser = true // 开发阶段专用,正式版请删除或注释 if(res.data.newUser) { // 新用户,不做任何缓存动作,跳转登录页 let userId = res.data.userId let res_p=await new Promise(resolve=>{ uni.login({ provider: 'weixin', //使用微信登录 success: function (loginRes) { resolve(loginRes); // console.log(loginRes,'微信登录返回信息'); } }); }); // 正式版记住放开下面这行注释掉的代码 uni.setStorageSync('access_token','Bearer '+res.data.access_token);// 为了wx登录成功,必须token单独缓存一次 let res2= await api.wx_login({code:res_p.code},userId); if(res2.code === 0) { // this.checkLogin() // this.toLogin(encodeURIComponent(JSON.stringify(res.data))) // 跳转登录页,但是我不想这么做 this.toProfile(encodeURIComponent(JSON.stringify(res.data))) } } else{ let res_form = {} uni.setStorageSync('avatarUrl', res.data.avatar);//avatarUrl res_form = res.data res_form.access_token = 'Bearer '+res.data.access_token let userId = res.data.userId uni.setStorageSync('access_token',res_form.access_token);// token单独缓存一次 this.wx_login(userId, res_form) } } else{ uni.showToast({ icon:"error", title:res.msg, duration:2000 }) } }, async wx_login(userId, res_form) { let res=await new Promise(resolve=>{ uni.login({ provider: 'weixin', //使用微信登录 success: function (loginRes) { resolve(loginRes); } }); }); let res2 = await api.wx_login({code:res.code},userId); if(res2.code === 0) { uni.setStorageSync('userInfo',JSON.stringify(res_form)) this.checkLogin() } else{ uni.clearStorage(); } }, toLogin(token_info) { uni.navigateTo({ url:"/pages/login/login?token_info="+token_info }) }, toProfile(token_info = null) { uni.navigateTo({ url:"/pages/user/userInfo?token_info="+token_info }) } } })