Files
sexygoat 6bf56a4b4c first
2026-01-22 17:25:30 +08:00

435 lines
9.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 用户状态管理模块
*/
import {
reactive,
computed
} from 'vue';
import userApi from '@/api/user.js';
// 用户状态数据
const state = reactive({
userInfo: {
nickname: '单卡用户', // 昵称
avatar: 'https://img1.baidu.com/it/u=2462918877,1866131262&fm=253&fmt=auto&app=138&f=JPEG?w=506&h=500', // 头像
deviceId: '' // 设备ID
},
// 用户信息
isLoggedIn: false, // 登录状态
error: null // 错误信息
});
// 计算属性
const getters = {
// 是否已登录
isLoggedIn: computed(() => state.isLoggedIn && !!state.userInfo.deviceId),
// 用户信息
userInfo: computed(() => state.userInfo)
};
// 操作方法
const actions = {
// 获取Cookies
async getCookie() {
try {
return userApi.getCookie();
} catch (error) {
console.error('获取微信授权失败:', error);
state.error = error.message;
throw error;
}
},
// 登录
async login(iccidMark) {
try {
// 调用login接口
const result = await userApi.login(iccidMark);
// 如果登录成功,设置登录状态和用户信息
if (!result.system_result_message_key) {
state.isLoggedIn = true;
state.userInfo.deviceId = iccidMark;
// 同步到本地存储
uni.setStorageSync('device_id', iccidMark);
uni.setStorageSync('isLoggedIn', true);
console.log('登录成功,已设置登录状态');
}
return result;
} catch (error) {
console.error('登录失败:', error);
state.error = error.message;
state.isLoggedIn = false;
throw error;
}
},
// 获取用户实名信息
async getRealNameInfo() {
try {
return userApi.getRealNameInfo();
} catch (error) {
console.error('获取用户实名信息失败:', error);
state.error = error.message;
throw error;
}
},
// 获取用户信息
async getUserInfo() {
try {
const result = await userApi.getUserInfo();
if (result?.entity) {
state.userInfo.avatar = result.entity.headimgurl
state.userInfo.nickname = result.entity.nickname
} else {
const result_once = await userApi.getUserInfo();
if (result_once?.entity) {
state.userInfo.avatar = result_once.entity.headimgurl
state.userInfo.nickname = result_once.entity.nickname
}
}
return result
} catch (error) {
console.error('获取用户信息失败:', error);
state.error = error.message;
throw error;
}
},
// 获取wxUrl
async getWxUrl() {
try {
return userApi.getWxUrl();
} catch (error) {
console.error('获取获取wxUrl失败:', error);
state.error = error.message;
throw error;
}
},
// 获取getOpenId
async getOpenId() {
try {
return userApi.getOpenId();
} catch (error) {
console.error('获取获取getOpenId失败:', error);
state.error = error.message;
throw error;
}
},
// 微信授权
async getAuthorize(code) {
try {
return userApi.getAuthorize(code);
} catch (error) {
console.error('获取微信授权失败:', error);
state.error = error.message;
throw error;
}
},
// 获取微信支付签名
async getWxPaySign() {
try {
return userApi.getWxPaySign();
} catch (error) {
console.error('获取微信支付签名失败:', error);
state.error = error.message;
throw error;
}
},
// 恢复出厂设置
async restDevice(deviceId) {
try {
return userApi.restDevice(deviceId);
} catch (error) {
console.error('恢复出厂设置失败:', error);
state.error = error.message;
throw error;
}
},
// 重启设备
async restartDevice(deviceId) {
try {
return userApi.restartDevice(deviceId);
} catch (error) {
console.error('重启设备失败:', error);
state.error = error.message;
throw error;
}
},
// 获取设备信息
async getCardInfo() {
try {
return userApi.getCardInfo();
} catch (error) {
console.error('获取设备信息失败:', error);
state.error = error.message;
throw error;
}
},
// 获取设备信息(管理平台)
async getDeviceInfoAdmin(device_id) {
try {
return userApi.getDeviceInfoAdmin(device_id);
} catch (error) {
console.error('获取设备信息(管理平台)', error);
state.error = error.message;
throw error;
}
},
// 获取首页信息-设备列表
async getSwitchCardList() {
try {
return userApi.getSwitchCardList();
} catch (error) {
console.error('获取首页信息-设备列表失败:', error);
state.error = error.message;
throw error;
}
},
// 修改WiFi
async modifyWifi(data) {
try {
return userApi.modifyWifi(data);
} catch (error) {
console.error('修改WiFi失败:', error);
state.error = error.message;
throw error;
}
},
// 获取套餐列表
async getPackageList() {
try {
return userApi.getPackageList();
} catch (error) {
console.error('获取套餐列表失败:', error);
state.error = error.message;
throw error;
}
},
// 智能诊断
async intelligentDiagnosis() {
try {
return userApi.intelligentDiagnosis();
} catch (error) {
console.error('智能诊断失败:', error);
state.error = error.message;
throw error;
}
},
// 获取绑定手机号
async getDeviceBindPhone() {
try {
return userApi.getDeviceBindPhone();
} catch (error) {
console.error('获取绑定手机号失败:', error);
state.error = error.message;
throw error;
}
},
// 获取验证码
async getSmsNumber(mobile) {
try {
return userApi.getSmsNumber(mobile);
} catch (error) {
console.error('获取验证码失败:', error);
state.error = error.message;
throw error;
}
},
// 绑定手机号
async bindCardPhone(mobile, code) {
try {
return userApi.bindCardPhone(mobile, code);
} catch (error) {
console.error('绑定手机号失败:', error);
state.error = error.message;
throw error;
}
},
// 是否显示主号
async getIsOnlyMainCard(iccid) {
try {
return userApi.getIsOnlyMainCard(iccid);
} catch (error) {
console.error('是否显示主号失败:', error);
state.error = error.message;
throw error;
}
},
// 获取实名地址
async getRealNameAddress(iccid) {
try {
return userApi.getRealNameAddress(iccid);
} catch (error) {
console.error('获取实名地址失败:', error);
state.error = error.message;
throw error;
}
},
// 切换运营商
async changeOperator(esim, iccid) {
try {
return userApi.changeOperator(esim, iccid);
} catch (error) {
console.error('切换运营商失败:', error);
state.error = error.message;
throw error;
}
},
// 1. checkHasGzhOpenId 判断当前用户是否已经绑定了微信公众号的
async checkHasGzhOpenId() {
try {
return userApi.checkHasGzhOpenId();
} catch (error) {
console.error('checkHasGzhOpenId 失败:', error);
state.error = error.message;
throw error;
}
},
// 2. checkYgosWxInfo 检查当前用户是否具有与微信支付相关的特定信息
async checkYgosWxInfo() {
try {
return userApi.checkYgosWxInfo();
} catch (error) {
console.error('checkYgosWxInfo 失败:', error);
state.error = error.message;
throw error;
}
},
// 3. checkXwzfWxInfo 检查当前用户是否具有与微信支付相关的特定信息
async checkXwzfWxInfo() {
try {
return userApi.checkXwzfWxInfo();
} catch (error) {
console.error('checkXwzfWxInfo 失败:', error);
state.error = error.message;
throw error;
}
},
// 4. 获取支付列表
async getPayList(mealId) {
try {
return userApi.getPayList(mealId);
} catch (error) {
console.error('获取支付列表失败:', error);
state.error = error.message;
throw error;
}
},
// 5. createOrder 创建订单 并拉起支付 这个会返回微信支付参数
async createOrder(data) {
try {
return userApi.createOrder(data);
} catch (error) {
console.error('createOrder 失败:', error);
state.error = error.message;
throw error;
}
},
// 登出清除Cookie和状态
async logout() {
try {
await userApi.logout();
// 重置状态
state.isLoggedIn = false;
state.userInfo = {
nickname: '',
avatar: '',
deviceId: ''
};
state.error = null;
// 清除本地存储
uni.removeStorageSync('device_id');
uni.removeStorageSync('isLoggedIn');
console.log('退出登录成功,已清除登录状态');
// 跳转到登录页
uni.redirectTo({
url: '/pages/login/login'
});
} catch (error) {
console.error('登出失败:', error);
state.error = error.message;
// 即使服务端退出失败,也要清除本地状态
state.isLoggedIn = false;
state.userInfo = {
nickname: '',
avatar: '',
deviceId: ''
};
uni.removeStorageSync('device_id');
uni.removeStorageSync('isLoggedIn');
// 跳转到登录页
uni.redirectTo({
url: '/pages/login/login'
});
}
},
// 重置错误状态
clearError() {
state.error = null;
},
// 初始化登录状态(从本地存储恢复)
initLoginState() {
try {
const deviceId = uni.getStorageSync('device_id');
const isLoggedIn = uni.getStorageSync('isLoggedIn');
if (deviceId && isLoggedIn) {
state.isLoggedIn = true;
state.userInfo.deviceId = deviceId;
console.log('从本地存储恢复登录状态:', deviceId);
} else {
state.isLoggedIn = false;
state.userInfo.deviceId = '';
console.log('未找到有效的登录状态');
}
} catch (error) {
console.error('初始化登录状态失败:', error);
state.isLoggedIn = false;
state.userInfo.deviceId = '';
}
},
};
// 创建用户store
const userStore = {
state,
getters,
actions
};
export default userStore;