auth.js 3.3 KB

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