auth.js 3.9 KB

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