user.js 7.8 KB

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