auth.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import store from '@/store'
  2. import {getStaffApi,getShopInfoApi,getShopUserApi} from '@/api/common'
  3. import { postWxCode,getDictionaries } from '@/api/mini'
  4. import util from './util'
  5. /**
  6. * 获取商城端用户信息
  7. * @parmas update为true时会重新触发请求,重置vuex的数据
  8. */
  9. export async function getShopUser(update) {
  10. let userInfo = store.getters.getShopUser
  11. if (!util.isEmpty(userInfo) && !update) {
  12. return userInfo
  13. }
  14. await getShopUserApi().then((res) => {
  15. userInfo = res.data
  16. store.dispatch('setShopUser', userInfo)
  17. }).catch(() => {})
  18. return userInfo
  19. }
  20. /**
  21. * 获取后台商户用户信息
  22. * @parmas update为true时会重新触发请求,重置vuex的数据
  23. */
  24. export async function getShopInfo(update) {
  25. let userInfo = store.getters.getUser
  26. if (!util.isEmpty(userInfo) && !update) {
  27. return userInfo
  28. }
  29. await getShopInfoApi().then((res) => {
  30. userInfo = res.data
  31. store.dispatch('setUser', userInfo)
  32. }).catch(() => {})
  33. return userInfo
  34. }
  35. /**
  36. * 获取登录员工的基本信息
  37. * @parmas update为true时会重新触发请求,重置vuex的数据
  38. */
  39. export async function getSelfInfo(update) {
  40. let userInfo = store.getters.getSelfUser
  41. if (!util.isEmpty(userInfo) && !update) {
  42. return userInfo
  43. }
  44. await getStaffApi().then(res => {
  45. userInfo = res.data
  46. store.dispatch('setSelfInfo', userInfo)
  47. }).catch(() => {
  48. return false
  49. })
  50. return userInfo
  51. }
  52. /**
  53. * 微信小程序登录
  54. * update为true时会重新触发请求,重置vuex的数据
  55. */
  56. export async function wxMiniLogin(update) {
  57. let loginInfo = store.state.login.loginInfo
  58. if (!util.isEmpty(loginInfo) && !update) {
  59. return loginInfo
  60. }
  61. //有几个地方涉及登陆,需要统一方法,请搜索关键词 uni_login
  62. return new Promise((resolve,reject) => {
  63. uni.login({
  64. provider: 'weixin',
  65. success: res => {
  66. uni.setStorageSync('code', res.code)
  67. postWxCode({ code: res.code }).then(subRes => {
  68. if (util.isEmpty(subRes)) {
  69. return false
  70. }
  71. var pages = getCurrentPages();
  72. var page = (pages[pages.length - 1]).route;
  73. resolve({ redirectPage: page, ...subRes })
  74. let token = subRes.data.token || '';
  75. if (util.isEmpty(token)) {
  76. return false
  77. }
  78. uni.setStorageSync('token', subRes.data.token)
  79. uni.setStorageSync('shopId', subRes.data.admin.currentGhsShopId)
  80. getDictionaries({ token: subRes.data.token }).then(res => {
  81. if (util.isEmpty(res)) {
  82. return false
  83. }
  84. store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data })
  85. store.commit('setMyShopInfo', { ...res.data.shop, ...res.data })
  86. })
  87. store.commit('setLoginInfo', { ...subRes.data.admin, ...subRes.data })
  88. //启动更新逻辑 shish 2020.5.18
  89. if (subRes.data.update == 1) {
  90. // #ifdef MP-WEIXIN
  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. // #endif
  125. }
  126. })
  127. },
  128. fail:err=>{
  129. reject(err)
  130. }
  131. })
  132. })
  133. }