globalMixins.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import store from '@/store'
  2. import { mapGetters,mapActions } from 'vuex'
  3. import { pageTo, isLogin } from '@/utils/util.js'
  4. import { getUser, wxMiniLogin } from '@/utils/auth'
  5. import { getDictionaries } from '@/api/mini'
  6. import { hasTokenAutoLogin } from '@/api/common'
  7. export default {
  8. mixins: [],
  9. data() {
  10. return {
  11. // 全局混入,插入链接参数
  12. option: null,
  13. // 全局混入,插入全局常量
  14. constant: this.$constant,
  15. // 全局混入,插入全局工具函数
  16. $util: this.$util,
  17. // 全局混入,插入全局跳转函数
  18. pageTo: pageTo,
  19. shopAdminId: null,
  20. //所有花材列表
  21. allProduct:[],
  22. //零售店对应的id
  23. skCustomId:0
  24. }
  25. },
  26. computed: {
  27. ...mapGetters(["getLoginInfo","getSelectInfo"]),
  28. },
  29. onLoad: function (option) {
  30. this.option = option || null
  31. if (option.token) {
  32. uni.setStorageSync('token', option.token)
  33. }
  34. if (option.account) {
  35. uni.setStorageSync('account', option.account)
  36. }
  37. uni.setStorageSync('currentQuery', JSON.stringify(option))
  38. //pda监听获取不到option,通过存到storage传进去
  39. if(option.customId){
  40. uni.setStorageSync('pdaCustomId', option.customId)
  41. }else{
  42. uni.removeStorageSync('pdaCustomId')
  43. }
  44. if(this.$util.isEmpty(this.getLoginInfo)){
  45. var pages = getCurrentPages()
  46. let currentRoute = pages[pages.length-1].route;
  47. let token = uni.getStorageSync('token')
  48. if(this.$util.isEmpty(token)){
  49. if(currentRoute == 'pages/home/cash'){
  50. // #ifdef MP-WEIXIN
  51. wxMiniLogin(true).then(res => {
  52. //用于开启小程序自动登录
  53. let currentMiniOpenId = res.data.currentMiniOpenId || ''
  54. uni.setStorageSync('currentMiniOpenId', currentMiniOpenId)
  55. let token = res.token || ''
  56. if (!this.$util.isEmpty(token)) {
  57. getUser(true)
  58. }
  59. if (this.init) {
  60. this.init()
  61. }
  62. })
  63. // #endif
  64. }
  65. }else{
  66. let that = this
  67. hasTokenAutoLogin().then(subRes=>{
  68. if(subRes.code != 1){
  69. return
  70. }
  71. getDictionaries({ token: subRes.data.token }).then(res => {
  72. if (!that.$util.isEmpty(res)) {
  73. store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data })
  74. store.commit('setMyShopInfo', { ...res.data.shop, ...res.data })
  75. }
  76. })
  77. store.commit('setLoginInfo', { ...subRes.data.admin, ...subRes.data })
  78. this.skCustomId = subRes.data.skCustomId
  79. if (this.init) {
  80. this.init()
  81. }
  82. })
  83. }
  84. }
  85. },
  86. onShow(){
  87. if (isLogin()&&!this.$util.isEmpty(this.getLoginInfo)) {
  88. if (this.init) {
  89. this.init()
  90. }
  91. }
  92. },
  93. methods: {
  94. }
  95. }