user.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import { login, logout, getInfo } from '@/api/login'
  5. import { getToken, setToken, removeToken } from '@/utils/auth'
  6. const baseUrl = config.baseUrl
  7. const Clientid = config.Clientid
  8. const grantType = config.grantType
  9. const user = {
  10. state: {
  11. token: getToken(),
  12. name: storage.get(constant.name),
  13. nickName: storage.get(constant.nickName),
  14. avatar: storage.get(constant.avatar),
  15. roles: storage.get(constant.roles),
  16. roleName: storage.get(constant.roleName),
  17. permissions: storage.get(constant.permissions),
  18. wgtcode:storage.get(constant.wgtcode),
  19. autologin:storage.get(constant.autologin),
  20. userId: storage.get(constant.userId),
  21. phonenumber: storage.get(constant.phonenumber),
  22. deptId: storage.get(constant.deptId),
  23. deptName: storage.get(constant.deptName),
  24. loading: false,
  25. tenantId:storage.get(constant.tenantId),
  26. initFace: storage.get(constant.initFace),
  27. },
  28. mutations: {
  29. //tf作为主动控制的参数
  30. switch_loading(state,tf){
  31. /* if(tf){
  32. state.loading = tf;
  33. }else{
  34. state.loading = !state.loading
  35. } */
  36. state.loading = tf;
  37. },
  38. SET_TENANID: (state, tenantId) => {
  39. state.tenantId = tenantId
  40. storage.set(constant.tenantId, tenantId)
  41. },
  42. SET_TOKEN: (state, token) => {
  43. state.token = token
  44. },
  45. SET_NAME: (state, name) => {
  46. state.name = name
  47. storage.set(constant.name, name)
  48. },
  49. SET_NICKNAME: (state, nickName) => {
  50. state.nickName = nickName
  51. storage.set(constant.nickName, nickName)
  52. },
  53. SET_DEPTID: (state, deptId) => {
  54. state.deptId = deptId
  55. storage.set(constant.deptId, deptId)
  56. },
  57. SET_DEPNAME: (state, deptName) => {
  58. state.deptName = deptName
  59. storage.set(constant.deptName, deptName)
  60. },
  61. SET_AVATAR: (state, avatar) => {
  62. state.avatar = avatar
  63. storage.set(constant.avatar, avatar)
  64. },
  65. SET_ROLES: (state, roles) => {
  66. state.roles = roles
  67. storage.set(constant.roles, roles)
  68. },
  69. SET_ROLESNAME: (state, roleName) => {
  70. state.roleName = roleName
  71. storage.set(constant.roleName, roleName)
  72. },
  73. SET_PERMISSIONS: (state, permissions) => {
  74. state.permissions = permissions
  75. storage.set(constant.permissions, permissions)
  76. },
  77. SET_WGTCODE: (state, wgtcode) => {
  78. state.wgtcode = wgtcode
  79. storage.set(constant.wgtcode, wgtcode)
  80. },
  81. SET_AUTOLOGIN: (state, autologin) => {
  82. state.autologin = autologin
  83. storage.set(constant.autologin, autologin)
  84. },
  85. SET_USERID: (state, userId) => {
  86. state.userId = userId
  87. storage.set(constant.userId, userId)
  88. },
  89. SET_PHONENUMBER: (state, phonenumber) => {
  90. state.phonenumber = phonenumber
  91. storage.set(constant.phonenumber, phonenumber)
  92. },
  93. SET_INITFACE: (state, initFace) => {
  94. state.initFace = initFace
  95. storage.set(constant.initFace, initFace)
  96. },
  97. },
  98. actions: {
  99. // 版本号
  100. SetwgtFn({ commit}, code ){
  101. commit('SET_WGTCODE',code)
  102. },
  103. //修改认证状态
  104. checkInitFace({commit},data){
  105. commit('SET_INITFACE', data)
  106. },
  107. //修改token
  108. checkToken({commit},data){
  109. setToken(data)
  110. commit('SET_TOKEN', data)
  111. },
  112. // 登录
  113. Login({ commit }, userInfo) {
  114. const username = userInfo.username.trim()
  115. const password = userInfo.password
  116. const code = userInfo.code
  117. const uuid = userInfo.uuid
  118. const strfrom = userInfo.strfrom||""
  119. // const tenantId = '000000'
  120. const tenantId = ''
  121. const clientId = Clientid
  122. const grantType = userInfo.grantType
  123. const phonenumber=userInfo.username.trim()
  124. const smsCode=userInfo.code
  125. return new Promise((resolve, reject) => {
  126. login(username, password, code, uuid,tenantId,clientId,grantType,phonenumber,smsCode).then(res => {
  127. setToken(res.data.access_token)
  128. commit('SET_TOKEN', res.data.access_token)
  129. // setToken(res.token)
  130. // commit('SET_TOKEN', res.token)
  131. commit('SET_AUTOLOGIN',true)
  132. resolve()
  133. }).catch(error => {
  134. if(error==500&&strfrom=='request'){
  135. uni.hideLoading()
  136. // 清空数据
  137. uni.showModal({
  138. title: '提示',
  139. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  140. cancelText: '取消',
  141. confirmText: '确定',
  142. success: function(res) {
  143. if (res.confirm) {
  144. commit('SET_TOKEN', '')
  145. commit('SET_ROLES', [])
  146. commit('SET_ROLESNAME', [])
  147. commit('SET_PERMISSIONS', [])
  148. removeToken()
  149. storage.clean()
  150. uni.reLaunch({ url: '/pages/login' })
  151. }else{
  152. commit('SET_AUTOLOGIN',false)
  153. }
  154. }
  155. })
  156. }
  157. reject(error)
  158. })
  159. })
  160. },
  161. // 获取用户信息require("@/static/images/profile.jpg")
  162. GetInfo({ commit, state }) {
  163. return new Promise((resolve, reject) => {
  164. getInfo().then(resd => {
  165. const res = resd.data
  166. // const res = resd;
  167. const user = res.user
  168. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? '' : user.avatar
  169. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  170. const nickName = (user == null || user.nickName == "" || user.nickName == null) ? "" : user.nickName
  171. const userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
  172. const tenantId = (user == null || user.tenantId == "" || user.tenantId == null) ? "" : user.tenantId
  173. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  174. const initFace = (res == null || res.authenticationUser == "" || res.authenticationUser == null) ? "" : res.authenticationUser
  175. const deptId = (user == null || user.deptId == "" || user.deptId == null) ? "" : user.deptId
  176. const deptName = (user == null || user.dept == "" || user.dept == null||user.dept.deptName==''||user.dept.deptName==null) ? "" : user.dept.deptName
  177. // 根据角色来储存权限
  178. if (res.user.roles && res.user.roles.length > 0) {
  179. var newArr=[]
  180. newArr = res.user.roles.map(v => {
  181. return {
  182. dictValue: v.roleKey,
  183. dictLabel:v.roleName,
  184. // permissions: v.permissions
  185. }
  186. })
  187. commit('SET_ROLESNAME', newArr)
  188. }
  189. if (res.roles && res.roles.length > 0) {
  190. commit('SET_ROLES', res.roles)
  191. commit('SET_PERMISSIONS', res.permissions||[])
  192. } else {
  193. commit('SET_ROLES', ['ROLE_DEFAULT'])
  194. commit('SET_PERMISSIONS', [])
  195. }
  196. commit('SET_PERMISSIONS', res.permissions||[])
  197. commit('SET_NAME', username)
  198. commit('SET_NICKNAME', nickName)
  199. commit('SET_AVATAR', avatar)
  200. commit('SET_USERID', userId)
  201. commit('SET_TENANID', tenantId)
  202. commit('SET_PHONENUMBER', phonenumber)
  203. commit('SET_INITFACE', initFace)
  204. commit('SET_DEPTID', deptId)
  205. commit('SET_DEPNAME', deptName)
  206. resolve(res)
  207. }).catch(error => {
  208. reject(error)
  209. })
  210. })
  211. },
  212. // 退出系统
  213. LogOut({ commit, state }) {
  214. return new Promise((resolve, reject) => {
  215. logout(state.token).then(() => {
  216. commit('SET_TOKEN', '')
  217. commit('SET_ROLES', [])
  218. commit('SET_PERMISSIONS', [])
  219. removeToken()
  220. storage.clean()
  221. resolve()
  222. }).catch(error => {
  223. reject(error)
  224. })
  225. })
  226. }
  227. }
  228. }
  229. export default user