auth.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = wx.getUpdateManager()
  91. console.log('请求完新版本信息的回调')
  92. updateManager.onCheckForUpdate(function (res) {
  93. // 请求完新版本信息的回调
  94. console.log(res.hasUpdate)
  95. })
  96. updateManager.onUpdateReady(function () {
  97. wx.showModal({
  98. title: '更新提示',
  99. content: '新版本已经准备好,是否重启应用?',
  100. success(res) {
  101. if (res.confirm) {
  102. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  103. updateManager.applyUpdate()
  104. }
  105. }
  106. })
  107. })
  108. updateManager.onUpdateFailed(function () {
  109. // 新版本下载失败
  110. })
  111. }
  112. })
  113. return dataInfo
  114. }
  115. // export default {
  116. // getSelfInfo,
  117. // getUser,
  118. // resetAllUser,
  119. // getRoleReal
  120. // }