auth.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import store from '@/store'
  2. // import { share } from '@/mixins'
  3. import CONSTANT from '@/constant'
  4. import UTIL from '@/utils/util'
  5. import {
  6. getStaffApi,
  7. getUserApi,
  8. getCustomApi
  9. } from '@/api/common'
  10. import { postWxCode } from '@/api/mini'
  11. import util from './util'
  12. /**
  13. * 获取商城端用户信息
  14. * @parmas update为true时会重新触发请求,重置vuex的数据
  15. */
  16. export async function getShopUser(update) {
  17. let userInfo = store.getters.getShopUser
  18. if (!UTIL.isEmpty(userInfo) && !update) {
  19. return userInfo
  20. }
  21. await getCustomApi().then((res) => {
  22. userInfo = res.data
  23. store.dispatch('setShopUser', userInfo)
  24. }).catch(() => {
  25. uni.removeStorageSync('token')
  26. })
  27. return userInfo
  28. }
  29. /**
  30. * 获取后台商户用户信息
  31. * @parmas update为true时会重新触发请求,重置vuex的数据
  32. */
  33. export async function getUser(update) {
  34. let userInfo = store.getters.getUser
  35. if (!UTIL.isEmpty(userInfo) && !update) {
  36. return userInfo
  37. }
  38. await getUserApi().then((res) => {
  39. userInfo = res.data
  40. store.dispatch('setUser', userInfo)
  41. }).catch(() => {
  42. uni.removeStorageSync('token')
  43. })
  44. return userInfo
  45. }
  46. /**
  47. * 获取登录员工的基本信息
  48. * @parmas update为true时会重新触发请求,重置vuex的数据
  49. */
  50. export async function getSelfInfo(update) {
  51. let userInfo = store.getters.getSelfUser
  52. if (!util.isEmpty(userInfo) && !update) {
  53. return userInfo
  54. }
  55. await getStaffApi().then(res => {
  56. userInfo = res.data
  57. store.dispatch('setSelfInfo', userInfo)
  58. }).catch(() => {
  59. return false
  60. })
  61. return userInfo
  62. }
  63. /**
  64. * 微信小程序登录
  65. * @parmas update为true时会重新触发请求,重置vuex的数据
  66. */
  67. export async function wxLoginFn(update) {
  68. let dataInfo = null
  69. let loginInfo = store.state.login.loginInfo
  70. if (!util.isEmpty(loginInfo) && !update) {
  71. return loginInfo
  72. }
  73. let getObj = await new Promise(resolve => {
  74. uni.login({
  75. provider: 'weixin',
  76. success: res => {
  77. uni.setStorageSync('code', res.code)
  78. resolve(res)
  79. }
  80. })
  81. })
  82. await postWxCode({ code: getObj.code }).then(subRes => {
  83. if (UTIL.isEmpty(subRes)) {
  84. return false
  85. }
  86. uni.setStorageSync('token', subRes.data.token)
  87. store.commit('setLoginInfo', subRes.data.user)
  88. //启动更新逻辑 shish 2020.5.18
  89. if (subRes.data.update == 1) {
  90. const updateManager = uni.getUpdateManager();
  91. updateManager.onCheckForUpdate(function(res) {
  92. // 请求完新版本信息的回调
  93. if (res.hasUpdate) {
  94. updateManager.onUpdateReady(function(res2) {
  95. uni.showModal({
  96. title: '更新提示',
  97. content: '发现新版本,请更新应用',
  98. cancelColor:'#DDDDDD',
  99. confirmColor:'#FF0000',
  100. success(res2) {
  101. if (res2.confirm) {
  102. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  103. updateManager.applyUpdate();
  104. }
  105. }
  106. });
  107. });
  108. }
  109. });
  110. updateManager.onUpdateFailed(function(res) {
  111. // 新的版本下载失败
  112. uni.showModal({
  113. title: '提示',
  114. content: '检查到有新版本,但下载失败,请检查网络连接',
  115. success(res) {
  116. if (res.confirm) {
  117. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  118. updateManager.applyUpdate();
  119. }
  120. }
  121. });
  122. });
  123. }
  124. })
  125. return dataInfo
  126. }
  127. // export default {
  128. // getSelfInfo,
  129. // getUser,
  130. // resetAllUser,
  131. // getRoleReal
  132. // }