user.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // 登录
  108. Login({ commit }, userInfo) {
  109. const username = userInfo.username.trim()
  110. const password = userInfo.password
  111. const code = userInfo.code
  112. const uuid = userInfo.uuid
  113. const strfrom = userInfo.strfrom||""
  114. // const tenantId = '000000'
  115. const tenantId = ''
  116. const clientId = Clientid
  117. const grantType = userInfo.grantType
  118. const phonenumber=userInfo.username.trim()
  119. const smsCode=userInfo.code
  120. return new Promise((resolve, reject) => {
  121. login(username, password, code, uuid,tenantId,clientId,grantType,phonenumber,smsCode).then(res => {
  122. setToken(res.data.access_token)
  123. commit('SET_TOKEN', res.data.access_token)
  124. // setToken(res.token)
  125. // commit('SET_TOKEN', res.token)
  126. commit('SET_AUTOLOGIN',true)
  127. resolve()
  128. }).catch(error => {
  129. if(error==500&&strfrom=='request'){
  130. uni.hideLoading()
  131. // 清空数据
  132. uni.showModal({
  133. title: '提示',
  134. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  135. cancelText: '取消',
  136. confirmText: '确定',
  137. success: function(res) {
  138. if (res.confirm) {
  139. commit('SET_TOKEN', '')
  140. commit('SET_ROLES', [])
  141. commit('SET_ROLESNAME', [])
  142. commit('SET_PERMISSIONS', [])
  143. removeToken()
  144. storage.clean()
  145. uni.reLaunch({ url: '/pages/login' })
  146. }else{
  147. commit('SET_AUTOLOGIN',false)
  148. }
  149. }
  150. })
  151. }
  152. reject(error)
  153. })
  154. })
  155. },
  156. // 获取用户信息require("@/static/images/profile.jpg")
  157. GetInfo({ commit, state }) {
  158. return new Promise((resolve, reject) => {
  159. getInfo().then(resd => {
  160. const res = resd.data
  161. // const res = resd;
  162. const user = res.user
  163. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? '' : user.avatar
  164. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  165. const nickName = (user == null || user.nickName == "" || user.nickName == null) ? "" : user.nickName
  166. const userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
  167. const tenantId = (user == null || user.tenantId == "" || user.tenantId == null) ? "" : user.tenantId
  168. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  169. const initFace = (res == null || res.authenticationUser == "" || res.authenticationUser == null) ? "" : res.authenticationUser
  170. const deptId = (user == null || user.deptId == "" || user.deptId == null) ? "" : user.deptId
  171. const deptName = (user == null || user.dept == "" || user.dept == null||user.dept.deptName==''||user.dept.deptName==null) ? "" : user.dept.deptName
  172. // 根据角色来储存权限
  173. if (res.user.roles && res.user.roles.length > 0) {
  174. var newArr=[]
  175. newArr = res.user.roles.map(v => {
  176. return {
  177. dictValue: v.roleKey,
  178. dictLabel:v.roleName,
  179. // permissions: v.permissions
  180. }
  181. })
  182. commit('SET_ROLESNAME', newArr)
  183. }
  184. if (res.roles && res.roles.length > 0) {
  185. commit('SET_ROLES', res.roles)
  186. commit('SET_PERMISSIONS', res.permissions||[])
  187. } else {
  188. commit('SET_ROLES', ['ROLE_DEFAULT'])
  189. commit('SET_PERMISSIONS', [])
  190. }
  191. commit('SET_NAME', username)
  192. commit('SET_NICKNAME', nickName)
  193. commit('SET_AVATAR', avatar)
  194. commit('SET_USERID', userId)
  195. commit('SET_TENANID', tenantId)
  196. commit('SET_PHONENUMBER', phonenumber)
  197. commit('SET_INITFACE', initFace)
  198. commit('SET_DEPTID', deptId)
  199. commit('SET_DEPNAME', deptName)
  200. resolve(res)
  201. }).catch(error => {
  202. reject(error)
  203. })
  204. })
  205. },
  206. // 退出系统
  207. LogOut({ commit, state }) {
  208. return new Promise((resolve, reject) => {
  209. logout(state.token).then(() => {
  210. commit('SET_TOKEN', '')
  211. commit('SET_ROLES', [])
  212. commit('SET_PERMISSIONS', [])
  213. removeToken()
  214. storage.clean()
  215. resolve()
  216. }).catch(error => {
  217. reject(error)
  218. })
  219. })
  220. }
  221. }
  222. }
  223. export default user