auth.js 3.2 KB

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