globalMixins.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { mapGetters,mapActions } from 'vuex'
  2. import { pageTo } from '@/utils/util.js'
  3. import { getUser, wxLoginFn } from '@/utils/auth'
  4. import { getDictionaries } from '@/api/mini'
  5. import { hasTokenAutoLogin } from '@/api/common'
  6. import constant from '@/constant'
  7. export default {
  8. data() {
  9. return {
  10. // 全局混入,插入链接参数
  11. option: null,
  12. // 全局混入,插入全局常量
  13. constant: this.$constant,
  14. // 全局混入,插入全局工具函数
  15. $util: this.$util,
  16. // 全局混入,插入全局跳转函数
  17. pageTo: pageTo,
  18. shopAdminId: null,
  19. MODEL_TITLE: '',
  20. EXPER_AUTH_SHOW: false
  21. }
  22. },
  23. computed: {
  24. ...mapGetters({ loginInfo: "getLoginInfo" }),
  25. ...mapGetters(["getLoginInfo"])
  26. },
  27. onLoad: function (option) {
  28. this.option = option || null
  29. if (option.token) {
  30. uni.setStorageSync('token', option.token)
  31. }
  32. if (option.account) {
  33. uni.setStorageSync('account', option.account)
  34. }
  35. uni.setStorageSync('currentQuery', JSON.stringify(option))
  36. if (this.$util.isEmpty(this.getLoginInfo)) {
  37. // #ifdef MP-WEIXIN
  38. //微信小程序第一次打开小程序时尝试登录一次
  39. var pages = getCurrentPages()
  40. let currentRoute = pages[pages.length-1].route;
  41. let token = uni.getStorageSync('token')
  42. if(this.$util.isEmpty(token)){
  43. if(currentRoute == 'admin/home/workbench' || currentRoute == 'pagesPurchase/ghsProduct'){
  44. wxLoginFn(true).then(res => {
  45. let token = res.data.token || ''
  46. //用于开启小程序自动登录
  47. let currentMiniOpenId = res.data.currentMiniOpenId || ''
  48. uni.setStorageSync('currentMiniOpenId', currentMiniOpenId)
  49. if (!this.$util.isEmpty(token)) {
  50. getUser(true)
  51. this.setUserShopAll()
  52. }
  53. if (this.init) {
  54. this.init()
  55. }
  56. })
  57. }
  58. }else{
  59. let that = this
  60. hasTokenAutoLogin().then(subRes=>{
  61. //有几个地方涉及登陆,需要统一方法,请搜索关键词 uni_login
  62. if(subRes.code == 1){
  63. uni.setStorageSync('shopId', subRes.data.admin.currentShopId)
  64. getDictionaries({ token: subRes.data.token }).then(res => {
  65. if (that.$util.isEmpty(res)) {
  66. return false
  67. }
  68. that.$store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data })
  69. that.$store.commit('setMyShopInfo', { ...res.data.shop, ...res.data })
  70. })
  71. that.$store.commit('setLoginInfo', { ...subRes.data.admin, ...subRes.data })
  72. getUser(true).then(res => {
  73. uni.setNavigationBarTitle({ title: res.name })
  74. })
  75. that.setUserShopAll()
  76. }
  77. })
  78. }
  79. // #endif
  80. }
  81. },
  82. onShow(){
  83. if (this.init) {
  84. this.init()
  85. }
  86. },
  87. methods: {
  88. ...mapActions(['setUserShopAll'])
  89. },
  90. mounted() {
  91. if (this.mInit) {
  92. this.mInit()
  93. }
  94. }
  95. }