You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.3 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
5 months ago
5 months ago
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import {
  5. login,
  6. logout,
  7. getInfo
  8. } from '@/api/login'
  9. import {
  10. getToken,
  11. setToken,
  12. removeToken,
  13. getOpenIdKey,
  14. setOpenIdKey,
  15. } from '@/utils/auth'
  16. import {
  17. getOpenId,
  18. } from "@/api/system/user"
  19. const baseUrl = config.baseUrl
  20. const user = {
  21. state: {
  22. token: getToken(),
  23. name: storage.get(constant.name),
  24. avatar: storage.get(constant.avatar),
  25. roles: storage.get(constant.roles),
  26. permissions: storage.get(constant.permissions)
  27. },
  28. mutations: {
  29. SET_TOKEN: (state, token) => {
  30. state.token = token
  31. },
  32. SET_NAME: (state, name) => {
  33. state.name = name
  34. storage.set(constant.name, name)
  35. },
  36. SET_AVATAR: (state, avatar) => {
  37. state.avatar = avatar
  38. storage.set(constant.avatar, avatar)
  39. },
  40. SET_ROLES: (state, roles) => {
  41. state.roles = roles
  42. storage.set(constant.roles, roles)
  43. },
  44. SET_PERMISSIONS: (state, permissions) => {
  45. state.permissions = permissions
  46. storage.set(constant.permissions, permissions)
  47. }
  48. },
  49. actions: {
  50. // 登录
  51. Login({
  52. commit
  53. }, userInfo) {
  54. const username = userInfo.username.trim()
  55. const password = userInfo.password
  56. const code = userInfo.code
  57. const uuid = userInfo.uuid
  58. return new Promise((resolve, reject) => {
  59. login(username, password, code, uuid).then(res => {
  60. setToken(res.token)
  61. commit('SET_TOKEN', res.token)
  62. resolve()
  63. }).catch(error => {
  64. reject(error)
  65. })
  66. })
  67. },
  68. // 获取用户信息
  69. GetInfo({
  70. commit,
  71. state
  72. }) {
  73. return new Promise((resolve, reject) => {
  74. getInfo().then(res => {
  75. const user = res.user
  76. const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
  77. "": baseUrl + user.avatar
  78. const username = (user == null || user.userName == "" || user.userName ==
  79. null) ? "" : user.userName
  80. if (res.roles && res.roles.length > 0) {
  81. commit('SET_ROLES', res.roles)
  82. commit('SET_PERMISSIONS', res.permissions)
  83. } else {
  84. commit('SET_ROLES', ['ROLE_DEFAULT'])
  85. }
  86. commit('SET_NAME', username)
  87. commit('SET_AVATAR', avatar)
  88. resolve(res)
  89. }).catch(error => {
  90. reject(error)
  91. })
  92. })
  93. },
  94. // 退出系统
  95. LogOut({
  96. commit,
  97. state
  98. }) {
  99. return new Promise((resolve, reject) => {
  100. logout(state.token).then(() => {
  101. commit('SET_TOKEN', '')
  102. commit('SET_ROLES', [])
  103. commit('SET_PERMISSIONS', [])
  104. removeToken()
  105. storage.clean()
  106. resolve()
  107. }).catch(error => {
  108. reject(error)
  109. })
  110. })
  111. },
  112. login() {
  113. return new Promise((resolve, reject) => {
  114. uni.showLoading({
  115. title: '登录中...',
  116. zIndex : 9999999
  117. })
  118. uni.login({
  119. provider: 'weixin',
  120. success: (loginRes) => {
  121. getOpenId(loginRes.code).then(res => {
  122. uni.hideLoading()
  123. if (res.code == 200 && res.data) {
  124. let resData = JSON.parse(res.data)
  125. let token = resData.token;
  126. let openId = resData.openId;
  127. setOpenIdKey(openId)
  128. if (token) {
  129. setToken(token)
  130. resolve(token)
  131. }
  132. }
  133. }).catch(e => {
  134. uni.hideLoading()
  135. })
  136. },
  137. fail: function(error) {
  138. reject(error)
  139. uni.showToast('授权失败,请授权后再试')
  140. }
  141. });
  142. })
  143. },
  144. }
  145. }
  146. export default user