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

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