auth.js 3.1 KB

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