普兆健康管家前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.0 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex); //vue的插件机制
  4. import api from '@/api/api.js'
  5. import fetch from '@/api/fetch.js'
  6. //Vuex.Store 构造器选项
  7. const store = new Vuex.Store({
  8. state: {
  9. configList: {}, //配置列表
  10. shop : false,//身份判断如果不需要,可以删除
  11. userInfo : {}, //用户信息
  12. payOrderProduct: [], //支付订单中的商品applyServiceProduct
  13. applyServiceProduct: [], // 售后服务商品
  14. addressInfo: null,
  15. },
  16. getters: {
  17. // 角色 true为水洗店 false为酒店 : 身份判断如果不需要,可以删除
  18. userShop(state){
  19. return state.shop
  20. }
  21. },
  22. mutations: {
  23. // 初始化配置
  24. initConfig(state){
  25. api('getConfig', res => {
  26. const configList = {
  27. ...state.configList,
  28. }
  29. if (res.code == 200) {
  30. res.result.forEach(n => {
  31. configList[n.code] = n.content;
  32. });
  33. }
  34. state.configList = configList
  35. uni.$emit('initConfig', state.configList)
  36. })
  37. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  38. // config.forEach(k => {
  39. // api(k, res => {
  40. // if (res.code == 200) {
  41. // state.configList[k] = res.result
  42. // }
  43. // })
  44. // })
  45. },
  46. // 微信登录
  47. login(state){
  48. uni.showLoading({
  49. title: '登录中...'
  50. })
  51. uni.login({
  52. success(res) {
  53. if(res.errMsg != "login:ok"){
  54. return
  55. }
  56. api('wxLogin', {
  57. code : res.code
  58. }, res => {
  59. uni.hideLoading()
  60. if(res.code != 200){
  61. return
  62. }
  63. state.userInfo = res.result.userInfo
  64. uni.setStorageSync('token', res.result.token)
  65. if(!state.userInfo.name || !state.userInfo.avatar){
  66. uni.navigateTo({
  67. url: '/pages_order/auth/wxUserInfo'
  68. })
  69. }else{
  70. uni.navigateBack(-1)
  71. }
  72. })
  73. }
  74. })
  75. },
  76. // 获取用户个人信息
  77. getUserInfo(state){
  78. api('getInfo', res => {
  79. if(res.code == 200){
  80. state.userInfo = res.result
  81. }
  82. })
  83. },
  84. // 退出登录
  85. logout(state){
  86. uni.showModal({
  87. title: '退出登录',
  88. content: '确认要退出登录吗',
  89. cancelColor: '#393939',
  90. confirmColor: '#7451DE',
  91. success(r) {
  92. if(r.confirm){
  93. state.userInfo = {}
  94. state.role = false
  95. uni.removeStorageSync('token')
  96. uni.reLaunch({
  97. url: '/pages/index/index'
  98. })
  99. }
  100. }
  101. })
  102. },
  103. async addCart(state, data) {
  104. console.log('addCart', data)
  105. try {
  106. await fetch('addCart', { productId: data.id })
  107. uni.showToast({
  108. icon: 'success',
  109. title: '成功加入购物车',
  110. });
  111. } catch (err) {
  112. }
  113. },
  114. setAddressInfo(state, data) {
  115. state.addressInfo = data
  116. },
  117. // 设置支付订单中的商品
  118. setPayOrderProduct(state, data) {
  119. state.payOrderProduct = data
  120. },
  121. createOrder(state, data) {
  122. state.payOrderProduct = data
  123. // todo: create order
  124. uni.navigateTo({
  125. url: '/pages_order/order/orderConfirm/index'
  126. })
  127. },
  128. setApplyServiceProduct(state, data) {
  129. state.applyServiceProduct = data
  130. },
  131. },
  132. actions: {},
  133. })
  134. export default store