auth.js 3.9 KB

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