auth.js 2.9 KB

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