123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import {
- setPubKey,
- getPubKey,
- getToken,
- setToken,
- setUid,
- getUid,
- removeAll
- } from '@/utils/auth.js'
- import {
- encrypt
- } from '@/utils/jsencrypt.js'
- const user = {
- state: {
- // 用户认证token
- token: getToken(),
- // 用户ID
- userId: getUid(),
- // 用户名
- userName: null,
- // 用户权限
- roleList: null,
- // 最后登陆时间
- lastLoginTime: null,
- // 验证码
- codeUrl: null,
- // 验证码登录参数
- uid: null,
- // 公钥
- publicKey: getPubKey(),
- },
- mutations: {
- SET_TOKEN: (state, value) => {
- state.token = value
- },
- SET_USER_ID: (state, value) => {
- state.userId = value
- },
- SET_USER_NAME: (state, value) => {
- state.userName = value
- },
- SET_ROLE_LIST: (state, value) => {
- state.roleList = value
- },
- SET_LAST_LOGINTIME: (state, value) => {
- state.lastLoginTime = value
- },
- SET_CODE_IMG: (state, value) => {
- state.codeUrl = 'data:image/gif;base64,' + value
- },
- SET_UID: (state, value) => {
- state.uid = value
- },
- SET_PUBKEY: (state, value) => {
- state.publicKey = value
- },
- },
- actions: {
- // 获取验证码
- getCodeImage({
- commit,
- dispatch
- }) {
- return new Promise((resolve, reject) => {
- uni.$http.get('/user/getCodeImage').then(res => {
- if (res.data.code === 200) {
- const {
- verifyCode,
- uid
- } = res.data.data
- commit('SET_CODE_IMG', verifyCode)
- commit('SET_UID', uid)
- dispatch('getPubKey')
- resolve(res)
- }
- })
- })
- },
- // 获取公钥
- getPubKey({
- commit
- }) {
- return new Promise((resolve, reject) => {
- uni.$http.get('/user/getPublicKey').then(res => {
- if (res.data.code === 200) {
- const key = res.data.data.replace('\r\n', '')
- commit('SET_PUBKEY', key)
- setPubKey(key)
- resolve(res)
- }
- })
- })
- },
- // 用户登录
- login({
- commit,
- state
- }, data) {
- const pwd = encrypt(data.password, data.publicKey)
- const obj = {
- userName: data.userName,
- password: pwd,
- verifyCode: data.verifyCode,
- uid: state.uid
- }
- return new Promise((resolve, reject) => {
- uni.$http.post('/user/phoneUserLogin', obj).then(res => {
- if (res.data.code === 200) {
- const token = res.data.data.token
- const {
- userId,
- userName,
- lastLoginTime,
- roleList
- } = res.data.data.user
- commit('SET_TOKEN', token)
- setToken(token)
- commit('SET_USER_ID', userId)
- setUid(userId)
- commit('SET_USER_NAME', userName)
- commit('SET_ROLE_LIST', roleList)
- commit('SET_LAST_LOGINTIME', lastLoginTime)
- }
- resolve(res)
- }).catch(err => {
- reject(err)
- })
- })
- },
- // 退出登录
- logout({
- commit
- }) {
- return new Promise((resolve, reject) => {
- uni.$http.post('/user/userLoginOut').then(res => {
- if (res.data.code === 200) {
- commit('SET_TOKEN', '')
- commit('SET_USER_ID', '')
- removeAll()
- }
- resolve(res)
- }).catch(err => {
- reject(err)
- })
- })
- },
- //修改密码
- updatePassword({
- state,
- commit
- }, data) {
- const pwd = encrypt(data.password, state.publicKey)
- const newPwd = encrypt(data.newPassword, state.publicKey)
- const obj = {
- password: pwd,
- newPassword: newPwd,
- userId: state.userId,
- userName: state.userName
- }
- return new Promise((resolve, reject) => {
- uni.$http.post('/user/updatePassWord', obj).then(res => {
- if (res.data.code === 200) {
- commit('SET_TOKEN', '')
- commit('SET_USER_ID', '')
- removeAll()
- }
- resolve(res)
- }).catch(err => {
- reject(err)
- })
- })
- },
- }
- }
- const {
- state,
- mutations,
- actions
- } = user
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|