From 083b17d07939af29bcb04a27e564664c7280595a Mon Sep 17 00:00:00 2001 From: 王旭 <1377869194@qq.com> Date: 星期一, 10 十月 2022 15:40:58 +0800 Subject: [PATCH] 登录验证登出 --- src/store/index.js | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 86 insertions(+), 1 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 8fac372..088eb10 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,6 +1,10 @@ import Vue from 'vue'; import Vuex from 'vuex'; +import { getToken, setToken, removeToken } from "@/utils/auth"; +import { Loading, Message } from "element-ui"; +import { login, getPublicKey } from "@/api/api"; +var encrypt = new JSEncrypt(); Vue.use(Vuex); export default new Vuex.Store({ @@ -11,6 +15,12 @@ verCateNodes: {}, styleDirCateNodes: {}, styleDepCateNodes: {}, + token: getToken(), + //璇锋眰璁℃暟 + apiCount: 0, + //loading瀹炰緥 + loadingInstance: null, + key: null }, mutations: { // 鑾峰彇瀹屾暣闈㈠寘灞戣矾寰� @@ -34,7 +44,82 @@ styleDirChangeNode(state, msg) { state.styleDirCateNodes = msg; }, + /* 鎵撳紑Loading */ + START_LOADING(state, msg) { + state.loadingInstance = Loading.service({ + lock: true, + text: msg ? msg : "鍔犺浇涓�...", + background: "rgba(0, 0, 0, 0.7)", + }); + }, + /* 鍏抽棴loading */ + + CLOSE_LOADING(state) { + state.loadingInstance.close(); + }, + /* 鏇存柊璇锋眰绾跨▼姹� */ + UPDATE_API_COUNT(state, handle) { + if (handle == "add") { + state.apiCount++; + this.commit("START_LOADING"); + } else { + state.apiCount--; + if (state.apiCount <= 0) { + this.commit("CLOSE_LOADING"); + } + } + }, + SET_TOKEN(state, token) { + state.token = token; + }, + + SET_KEY(state, key) { + state.key = key; + }, }, - actions: {}, + actions: { + // 鐢ㄦ埛鐧诲綍 login + login({ commit, state }, userInfo) { + return new Promise((resolve, reject) => { + var data = { + uid: encrypt.encrypt(userInfo.uid), + pwd: encrypt.encrypt(userInfo.pwd) + }; + login(data) + .then((response) => { + const data = response; + if (data.code !== 200) { + return Message({ + message: data.msg, + type: "error", + duration: 5 * 1000, + }); + } + commit("SET_TOKEN", data.result.token); + setToken(data.result.token); + resolve(data); + }) + .catch((error) => { + reject(error); + }); + }); + }, + getpublickey({ commit, state }, userInfo) { + + return new Promise((resolve, reject) => { + getPublicKey(userInfo) + .then((response) => { + const data = response; + commit("SET_KEY", data.result); + + encrypt.setPublicKey(data.result); // 璁剧疆鍏挜 + resolve(); + }) + .catch((error) => { + reject(error); + }); + }); + } + }, modules: {}, }); -- Gitblit v1.9.3