auth.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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,getDictionaries } 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,reject) => {
  78. uni.login({
  79. provider: 'weixin',
  80. success: res => {
  81. console.log('res',res)
  82. uni.setStorageSync('code', res.code)
  83. postWxCode({ code: res.code }).then(subRes => {
  84. if (util.isEmpty(subRes)) {
  85. return false
  86. }
  87. var pages = getCurrentPages();
  88. var page = (pages[pages.length - 1]).route;
  89. resolve({ redirectPage: page, ...subRes })
  90. uni.setStorageSync('token', subRes.data.token)
  91. uni.setStorageSync('shopId', subRes.data.admin.currentShopId)
  92. getDictionaries({ token: subRes.data.token }).then(res => {
  93. if (util.isEmpty(res)) {
  94. return false
  95. }
  96. store.commit('setDictionariesInfo', {
  97. ...res.data.dict,
  98. ...res.data
  99. })
  100. store.commit('setMyShopInfo', {
  101. ...res.data.shop,
  102. ...res.data
  103. })
  104. })
  105. store.commit('setLoginInfo', {
  106. ...subRes.data.admin,
  107. ...subRes.data
  108. })
  109. //启动更新逻辑 shish 2020.5.18
  110. if (subRes.data.update == 1) {
  111. const updateManager = wx.getUpdateManager()
  112. console.log('请求完新版本信息的回调')
  113. updateManager.onCheckForUpdate(function (res) {
  114. // 请求完新版本信息的回调
  115. console.log(res.hasUpdate)
  116. })
  117. updateManager.onUpdateReady(function () {
  118. wx.showModal({
  119. title: '更新提示',
  120. content: '新版本已经准备好,是否重启应用?',
  121. success(res) {
  122. if (res.confirm) {
  123. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  124. updateManager.applyUpdate()
  125. }
  126. }
  127. })
  128. })
  129. updateManager.onUpdateFailed(function () {
  130. // 新版本下载失败
  131. })
  132. }
  133. })
  134. },
  135. fail:err=>{
  136. reject(err)
  137. }
  138. })
  139. })
  140. }
  141. // export default {
  142. // getSelfInfo,
  143. // getUser,
  144. // resetAllUser,
  145. // getRoleReal
  146. // }