store.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { defineStore } from 'pinia'
  2. import cookie from '@/utils/cookie'
  3. import { navigateTo } from '@/utils/router'
  4. export const useMainStore = defineStore('main', {
  5. state: () => ({
  6. websocket: {},
  7. isMer: 0,
  8. store: {},
  9. cart: [],
  10. orderType: 'takein',
  11. address: {},
  12. addresses: {},
  13. member: {
  14. },
  15. openid:"",
  16. token:"",
  17. lang: 'zh-cn',
  18. cookieKey:'YSESSID=yshop-e4dk4o2utr3c0n95tp42p745ai',
  19. // 默认地为你为北京地址
  20. location: {},
  21. mycoupon: {},
  22. desk: {},
  23. isScan: false,
  24. uidType: 'user',
  25. merchartShop: {} //商家店铺
  26. }),
  27. getters: {
  28. isLogin(state) {//是否登录
  29. return Object.keys(state.member).length > 0
  30. //return cookie.get('accessToken') ? true : false
  31. }
  32. //isLogin: state => Object.keys(state.member).length > 0 //是否登录
  33. },
  34. persist: {
  35. enabled: true, // 启用持久化
  36. strategies: [
  37. {
  38. storage: {
  39. setItem(key, value) {
  40. uni.setStorageSync(key, value)
  41. },
  42. getItem(key) {
  43. return uni.getStorageSync(key)
  44. },
  45. },
  46. paths: ['member'] // 持久化这个字段
  47. }
  48. ]
  49. },
  50. actions: {
  51. SET_WEB_SOCKET(val) {
  52. this.websocket = val
  53. },
  54. SET_UID_TYPE(val) {
  55. this.uidType = val
  56. },
  57. SET_MER(val) {
  58. this.isMer = val
  59. },
  60. SET_MERCHART_SHOP(shop) {
  61. this.merchartShop = shop
  62. },
  63. SET_DESK(desk) {
  64. this.desk = desk
  65. this.isScan = true
  66. },
  67. DEL_DESK() {
  68. this.desk = {}
  69. this.isScan = false
  70. },
  71. SET_COUPON(coupon) {
  72. this.mycoupon = coupon
  73. },
  74. DEL_COUPON() {
  75. this.mycoupon = {}
  76. },
  77. SET_ORDER_TYPE(type) {
  78. this.orderType = type
  79. },
  80. SET_MEMBER(member) {
  81. this.member = member
  82. cookie.set('userinfo', member)
  83. },
  84. SET_ADDRESS(address) {
  85. this.address = address
  86. },
  87. SET_ADDRESSES(addresses) {
  88. this.addresses = addresses
  89. },
  90. SET_STORE(store) {
  91. this.store = store
  92. },
  93. SET_CART(cart) {
  94. this.cart = cart
  95. },
  96. REMOVE_CART(state) {
  97. this.cart = []
  98. },
  99. setCookie(state, provider) {
  100. state.cookie = provider;
  101. uni.setStorage({
  102. key: 'cookieKey',
  103. data: provider
  104. });
  105. },
  106. SET_LOCATION(location) {
  107. this.location = location;
  108. },
  109. SET_OPENID(openid) {
  110. this.openid = openid;
  111. },
  112. SET_TOKEN(token) {
  113. this.token = token;
  114. cookie.set('accessToken', token)
  115. },
  116. setAccessToken(user) {
  117. cookie.set('accessToken', user)
  118. // return getUserInfo()
  119. },
  120. setSelectAddress(id) {
  121. console.log('--> % setSelectAddress % id:\n', id)
  122. this.selectAddress = this.address.filter(item => item.id == id)[0]
  123. },
  124. init() {
  125. let accessToken = cookie.get('accessToken')
  126. if (accessToken) {
  127. //return getUserInfo()
  128. }
  129. return null
  130. },
  131. logout() {
  132. this.user = null
  133. this.address = []
  134. this.areaList = []
  135. this.selectAddress = null
  136. navigateTo('/pages/components/pages/login/login')
  137. },
  138. },
  139. })