| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- // 用户数据模块
- import {
- login as loginApi,
- wxLogin,
- logout
- } from '@/config/api.js'
- const state = {
- access_token: uni.getStorageSync("access_token") || "",
- refresh_token: uni.getStorageSync("refresh_token") || "",
- isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
- userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
- }
- const getters = {
- access_token: state => state.access_token,
- refresh_token: state => state.refresh_token,
- isLogin: state => state.isLogin,
- userInfo: state => state.userInfo
- }
- const actions = {
- // 设置token
- setToken({
- commit
- }, token) {
- commit('access_token', token);
- commit('isLogin', true);
- },
- // 设置用户信息
- setUserInfo({
- commit
- }, userInfo) {
- commit('userInfo', userInfo);
- },
- // 登录
- login({
- commit
- }, userInfo = {}) {
- return new Promise((resolve, reject) => {
- loginApi({
- account: userInfo.email,
- password: userInfo.password,
- type: userInfo.type,
- }).then(res => {
- if (!res.user_id) return uni.$u.toast(res.error_description)
- commit('isLogin', true);
- commit('access_token', res.access_token);
- commit('refresh_token', res.refresh_token);
- commit('userInfo', res);
- uni.$u.toast('登录成功')
- resolve(res)
- }).catch(e => {
- reject(e)
- })
- })
- },
- // 刷新token
- refreshToken({
- commit
- }, userInfo = {}) {
- return new Promise((resolve, reject) => {
- wxLogin({
- refresh_token: userInfo.refreshToken,
- grant_type: 'refresh_token'
- }).then(res => {
- if (!res.user_id) return uni.$u.toast(res.error_description)
- commit('access_token', res.access_token);
- commit('refresh_token', res.refresh_token);
- commit('userInfo', res);
- resolve(res)
- }).catch(e => {
- reject(e)
- })
- })
- },
- // 退出登录
- logout({
- commit,
- dispatch
- }) {
- uni.getStorageSync('access_token') && logout()
- commit('access_token', '');
- commit('refresh_token', '');
- commit('isLogin', false);
- commit('userInfo', {});
- uni.$u.toast('退出登录成功')
- },
- // 处理未登录状态
- handleUnauthorized({
- commit
- }, redirectPath = '') {
- // 清除用户状态
- commit('access_token', '');
- commit('refresh_token', '');
- commit('isLogin', false);
- commit('userInfo', {});
- // 如果没有传入重定向路径,则获取当前页面路径
- if (!redirectPath) {
- const pages = getCurrentPages();
- const currentPage = pages[pages.length - 1];
- if (currentPage) {
- redirectPath = currentPage.route;
- }
- }
- // 使用 reLaunch 重新加载到登录页
- const redirectUrl = redirectPath ? encodeURIComponent(redirectPath) : '';
- uni.reLaunch({
- url: `/pages/user/login?redirect=/${redirectUrl}`,
- complete: () => {
- uni.$u.toast('请登录后操作');
- }
- });
- },
- }
- const mutations = {
- access_token(state, payload) {
- state.access_token = payload;
- uni.setStorageSync("access_token", payload);
- },
- refresh_token(state, payload) {
- state.refresh_token = payload;
- uni.setStorageSync("refresh_token", payload);
- },
- // 登录态
- isLogin(state, data) {
- state.isLogin = data;
- uni.setStorageSync('isLogin', data);
- },
- // 用户信息
- userInfo(state, data) {
- state.userInfo = data;
- uni.setStorageSync("userInfo", data);
- },
- }
- export default {
- state,
- mutations,
- actions,
- getters
- }
|