123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- import config from '@/config'
- import storage from '@/utils/storage'
- import constant from '@/utils/constant'
- import {
- login,
- logout,
- getInfo,
- getUserInfo,
- getDepartment,
- streetPage,//街道信息
- listSimpleDictDatas
- } from '@/api/login'
- import {
- setToken,
- removeToken,
- setUserId,
- removeUserId,
- getUserId
- } from '@/utils/auth'
- const baseUrl = config.baseUrl
- const baseUrlImg=config.baseUrlImg
- const user = {
- state: {
- id: storage.get(constant.id), // 用户编号
- orgId: storage.get(constant.orgId), // 单位id
- name: storage.get(constant.name),
- avatar: storage.get(constant.avatar),
- roles: storage.get(constant.roles),
- permissions: storage.get(constant.permissions)
- },
- mutations: {
- SET_ID: (state, id) => {
- state.id = id
- storage.set(constant.id, id)
- },
- SET_ORGID: (state, orgId) => {
- state.orgId = orgId
- storage.set(constant.orgId, orgId)
- },
- SET_NAME: (state, name) => {
- state.name = name
- storage.set(constant.name, name)
- },
- SET_AVATAR: (state, avatar) => {
- state.avatar = avatar
- storage.set(constant.avatar, avatar)
- },
- SET_ROLES: (state, roles) => {
- state.roles = roles
- storage.set(constant.roles, roles)
- },
- SET_PERMISSIONS: (state, permissions) => {
- state.permissions = permissions
- storage.set(constant.permissions, permissions)
- },
- },
- actions: {
- // 登录
- Login({
- commit
- }, userInfo) {
- const username = userInfo.username.trim()
- const password = userInfo.password
- const socialType = userInfo.socialType
- const socialCode = userInfo.socialCode
- const socialState=userInfo.socialState
- const captchaVerification = userInfo.captchaVerification
- return new Promise((resolve, reject) => {
- login(username, password,socialCode,socialType,socialState,captchaVerification).then(res => {
- res = res.data;
- // 设置 token
- setToken(res)
- // 设置UserId
- setUserId(res)
- resolve()
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 获取用户信息
- GetInfo({
- commit,
- state
- }) {
- return new Promise((resolve, reject) => {
- getInfo({
- id: getUserId()
- }).then(res => {
- const user = res.data.user
- const avatar = (user == null || user.avatar === "" || user.avatar == null) ?
- `${baseUrlImg}/bianjiziliao.png`: user.avatar
- const nickname = (user == null || user.nickname === "" || user.nickname ==
- null) ? "" : user.nickname
- const id = (user == null || user.id === "" || user.id == null) ? "" : user.id
- if (res.roles && res.roles.length > 0) {
- commit('SET_ROLES', res.roles)
- commit('SET_PERMISSIONS', res.permissions)
- } else {
- commit('SET_ROLES', ['ROLE_DEFAULT'])
- }
- commit('SET_NAME', nickname)
- commit('SET_AVATAR', avatar)
- commit('SET_ID', id)
- // 本地存储getUserInfo 用户信息
- getUserInfo({
- id: getUserId()
- }).then(res => {
- try {
- uni.setStorageSync('getUserInfo_key', res.data);
- } catch (e) {
- // error
- }
-
- // 本地存储department 部门信息
- streetPage({pageNo:2,pageSize:99}).then(res => {
- try {
- uni.setStorageSync('getDepartment_key', res.data.list);
- } catch (e) {
- // error
- }
- })
-
- // 本地存储listSimpleDictDatas 数据字典信息
- listSimpleDictDatas({}).then(res => {
- try {
- uni.setStorageSync('getlistSimpleDict_key', res.data);
- } catch (e) {
- // error
- }
- })
-
- // Orgid的值
- const orgId = (res.data == null || res.data === "" || res.data ==
- undefined) ? [] : res.data.userInfo.orgId
- commit('SET_ORGID', orgId)
- resolve(res)
- }).catch(error => {
- reject(error)
- })
- resolve(res)
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 退出系统
- LogOut({
- commit,
- state
- }) {
- return new Promise((resolve, reject) => {
- logout(state.token).then(() => {
- commit('SET_ROLES', [])
- commit('SET_PERMISSIONS', [])
- removeToken()
- removeUserId()
- storage.clean()
- resolve()
- }).catch(error => {
- reject(error)
- })
- })
- }
- }
- }
- export default user
|