普兆健康管家前端代码仓库
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.

211 lines
4.3 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 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. paperInfo: null,
  16. },
  17. getters: {
  18. // 角色 true为水洗店 false为酒店 : 身份判断如果不需要,可以删除
  19. userShop(state){
  20. return state.shop
  21. }
  22. },
  23. mutations: {
  24. // 初始化配置
  25. initConfig(state){
  26. api('getConfig', res => {
  27. const configList = {
  28. ...state.configList,
  29. }
  30. if (res.code == 200) {
  31. res.result.forEach(n => {
  32. configList[n.code] = n.content;
  33. });
  34. }
  35. state.configList = configList
  36. uni.$emit('initConfig', state.configList)
  37. })
  38. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  39. // config.forEach(k => {
  40. // api(k, res => {
  41. // if (res.code == 200) {
  42. // state.configList[k] = res.result
  43. // }
  44. // })
  45. // })
  46. },
  47. // 微信登录
  48. login(state){
  49. uni.showLoading({
  50. title: '登录中...'
  51. })
  52. uni.login({
  53. success(res) {
  54. if(res.errMsg != "login:ok"){
  55. return
  56. }
  57. api('wxLogin', {
  58. code : res.code
  59. }, res => {
  60. uni.hideLoading()
  61. if(res.code != 200){
  62. return
  63. }
  64. state.userInfo = res.result.userInfo
  65. uni.setStorageSync('token', res.result.token)
  66. if(!state.userInfo.name || !state.userInfo.avatar){
  67. uni.navigateTo({
  68. url: '/pages_order/auth/wxUserInfo'
  69. })
  70. }else{
  71. uni.navigateBack(-1)
  72. }
  73. })
  74. }
  75. })
  76. },
  77. // 获取用户个人信息
  78. getUserInfo(state){
  79. api('getInfo', res => {
  80. if(res.code == 200){
  81. state.userInfo = res.result
  82. }
  83. })
  84. },
  85. // 退出登录
  86. logout(state){
  87. uni.showModal({
  88. title: '退出登录',
  89. content: '确认要退出登录吗',
  90. cancelColor: '#393939',
  91. confirmColor: '#7451DE',
  92. success(r) {
  93. if(r.confirm){
  94. state.userInfo = {}
  95. state.role = false
  96. uni.removeStorageSync('token')
  97. uni.reLaunch({
  98. url: '/pages/index/index'
  99. })
  100. }
  101. }
  102. })
  103. },
  104. setAddressInfo(state, data) {
  105. state.addressInfo = data
  106. },
  107. // 设置支付订单中的商品
  108. setPayOrderProduct(state, data) {
  109. state.payOrderProduct = data
  110. },
  111. createOrder(state, data) {
  112. console.log('createOrder', data)
  113. state.payOrderProduct = data
  114. // todo: create order?
  115. uni.navigateTo({
  116. url: '/pages_order/order/orderConfirm/index'
  117. })
  118. },
  119. setApplyServiceProduct(state, data) {
  120. state.applyServiceProduct = data
  121. },
  122. setPaperInfo(state, data) {
  123. state.paperInfo = data
  124. },
  125. },
  126. actions: {
  127. async addCart(state, data) {
  128. console.log('addCart', data)
  129. try {
  130. const { id: productId, specId, specs } = data
  131. let skuId
  132. if (specId) {
  133. skuId = specId
  134. } else {
  135. let arr = specs?.length ? specs : await fetch('getProductSpec', { productId })
  136. arr?.sort?.((a, b) => a.sortOrder - b.sortOrder)
  137. skuId = arr?.[0]?.id
  138. }
  139. await fetch('addCart', { productId, skuId })
  140. uni.showToast({
  141. icon: 'success',
  142. title: '成功加入购物车',
  143. });
  144. return true
  145. } catch (err) {
  146. console.log('addCart err', err)
  147. return false
  148. }
  149. },
  150. async addCartBatch(state, arr) {
  151. console.log('addCartBatch', arr)
  152. try {
  153. const { id: productId, specId, specs } = data
  154. let skuId
  155. if (specId) {
  156. skuId = specId
  157. } else {
  158. let arr = specs?.length ? specs : await fetch('getProductSpec', { productId })
  159. arr?.sort?.((a, b) => a.sortOrder - b.sortOrder)
  160. skuId = arr?.[0]?.id
  161. }
  162. const list = arr.map(item => {
  163. const { id: productId, specId, specs } = item
  164. return {
  165. productId,
  166. skuId: specId || specs?.[0]?.id
  167. }
  168. })
  169. await fetch('addCartBatch', { list: JSON.stringify(list) })
  170. uni.showToast({
  171. icon: 'success',
  172. title: '成功加入购物车',
  173. });
  174. return true
  175. } catch (err) {
  176. console.log('addCart err', err)
  177. return false
  178. }
  179. },
  180. },
  181. })
  182. export default store