186 lines
5.0 KiB
JavaScript
186 lines
5.0 KiB
JavaScript
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 visitState = uni.getStorageSync('visitState')
|
|
console.log(access_token)
|
|
if(access_token) {
|
|
this.access_token = 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)
|
|
|
|
}
|
|
}
|
|
}) |