cart.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { defineStore } from 'pinia'
  2. import router from '@/router'
  3. interface cartState {
  4. cartList: Api.myShoppingCart[]
  5. /**
  6. * 是否正在加入购物车
  7. */
  8. isAddingCart: boolean
  9. /**
  10. * 星闪豹购物车全选按钮状态
  11. */
  12. isCartAllChecked: boolean
  13. totalProduct: Api.shoppingCartOrderConfirm | null
  14. }
  15. export const useSmqjhCartStore = defineStore('smqjh-cart', {
  16. state: (): cartState => ({
  17. cartList: [],
  18. isAddingCart: false,
  19. isCartAllChecked: false,
  20. totalProduct: null,
  21. }),
  22. getters: {
  23. /**
  24. * 计算星闪豹购物车商品总数量
  25. * @returns 购物车商品总数量
  26. */
  27. getTotalNum(): number {
  28. return this.cartList.map(it => it.skuList.map(its => its.num)).reduce((a, b) => a.concat(b), []).reduce((a, b) => a + b, 0)
  29. },
  30. },
  31. actions: {
  32. /**
  33. * 星闪豹加入购物车逻辑
  34. * @param skuId 商品skuId
  35. * @param num 商品数量
  36. * @param shopId 门店id
  37. * @param businessType 是否是加入购物车
  38. */
  39. async addCart(skuId: number, num: number, shopId: number, businessType: string) {
  40. return new Promise((resolve, reject) => {
  41. if (!skuId) {
  42. useGlobalToast().show({ msg: '请选择商品规格' })
  43. return reject(new Error('请选择商品规格'))
  44. }
  45. const { userInfo } = storeToRefs(useUserStore())
  46. useUserStore().checkLogin().then(() => {
  47. this.isAddingCart = true
  48. Apis.common.addShoppingCart({
  49. data: {
  50. businessType,
  51. skuId,
  52. num,
  53. shopId,
  54. channelId: Number(userInfo.value.channelId),
  55. },
  56. }).then((res) => {
  57. this.getCartList('XSB')
  58. resolve(res)
  59. }).finally(() => {
  60. this.isAddingCart = false
  61. })
  62. }).catch(err => reject(err))
  63. })
  64. },
  65. /**
  66. * 星闪豹获取购物车列表
  67. */
  68. async getCartList(businessType: string) {
  69. const { userInfo, token } = storeToRefs(useUserStore())
  70. if (!unref(token)) {
  71. return
  72. }
  73. const res = await Apis.common.myShoppingCart({
  74. data: {
  75. channelId: unref(userInfo).channelId,
  76. businessType,
  77. },
  78. })
  79. this.cartList = res.data.map((it) => {
  80. return {
  81. ...it,
  82. allGoods: [],
  83. }
  84. })
  85. this.isCartAllChecked = false
  86. this.totalProduct = null
  87. useTabbar().setTabbarItem('smqjh-cart', this.getTotalNum)
  88. },
  89. /**
  90. *
  91. * @param ids
  92. * @returns 计算选中商品总价
  93. */
  94. async getCartAddGoodsPrice(ids: string): Promise<Api.shoppingCartOrderConfirm> {
  95. return new Promise((resolve, reject) => {
  96. uni.showLoading({ mask: true })
  97. Apis.common.shoppingCartOrderConfirm({
  98. pathParams: {
  99. ids,
  100. },
  101. }).then((res) => {
  102. resolve(res.data)
  103. }).catch(err => reject(err)).finally(() => uni.hideLoading())
  104. })
  105. },
  106. /**
  107. * 购物车店铺全选按钮点击逻辑
  108. */
  109. async cartStoreAllChecked(e: { value: boolean }, shop: Api.myShoppingCart) {
  110. if (e.value) {
  111. shop.allGoods = shop.skuList.filter(it => it.shopSkuStocks !== '0' && it.isDelete !== '1').map(it => Number(it.id))
  112. }
  113. else {
  114. shop.allGoods = []
  115. this.totalProduct = null
  116. }
  117. },
  118. /**
  119. * 购物车商品选中逻辑
  120. */
  121. cartGoodsChecked(e: { value: number[] }, shop: Api.myShoppingCart) {
  122. if (e.value.length === shop.skuList.length) {
  123. shop.AllShopGoods = true
  124. }
  125. else {
  126. shop.AllShopGoods = false
  127. this.totalProduct = null
  128. }
  129. },
  130. /**
  131. * 购物车底部全选
  132. */
  133. cartAllChecked(e: { value: boolean }) {
  134. if (e.value) {
  135. this.cartList = this.cartList.map((it) => {
  136. return {
  137. ...it,
  138. AllShopGoods: true,
  139. allGoods: it.skuList.map(it => Number(it.id)),
  140. }
  141. })
  142. }
  143. else {
  144. this.cartList = this.cartList.map((it) => {
  145. return {
  146. ...it,
  147. AllShopGoods: false,
  148. allGoods: [],
  149. }
  150. })
  151. this.totalProduct = null
  152. }
  153. },
  154. /**
  155. * 购物车删除商品逻辑
  156. */
  157. cartDeleteGoods() {
  158. useGlobalMessage().confirm({
  159. title: '删除商品',
  160. msg: '是否删除选中的商品?',
  161. success: async () => {
  162. const delGoods = this.cartList.filter(it => it.allGoods?.length > 0)
  163. if (delGoods.length) {
  164. await Apis.common.deleteShoppingCart({
  165. pathParams: {
  166. ids: delGoods.map(it => it.allGoods).join(','),
  167. },
  168. })
  169. this.getCartList('XSB')
  170. useGlobalToast().show({ msg: '删除成功!' })
  171. }
  172. else {
  173. useGlobalToast().show({ msg: '请选择要删除的商品' })
  174. }
  175. },
  176. })
  177. },
  178. /**
  179. * 购物车减按钮逻辑
  180. */
  181. async cartSubGoods(item: Api.CartSkuVo) {
  182. if (this.isAddingCart) {
  183. useGlobalToast().show({ msg: '移除商品中,请稍后' })
  184. return
  185. }
  186. if (item.num === 1) {
  187. useGlobalMessage().confirm({
  188. msg: '是否删除该商品?',
  189. success: async () => {
  190. await this.addCart(item.skuId, -1, item.shopId, 'XSB')
  191. },
  192. })
  193. }
  194. else {
  195. await this.addCart(item.skuId, -1, item.shopId, 'XSB')
  196. }
  197. },
  198. /**
  199. * 购物车加按钮逻辑
  200. */
  201. async cartAddGoods(item: Api.CartSkuVo) {
  202. if (this.isAddingCart) {
  203. useGlobalToast().show({ msg: '加入购物车中,请稍后' })
  204. return
  205. }
  206. await this.addCart(item.skuId, 1, item.shopId, 'XSB')
  207. },
  208. /**
  209. * 统一结算按钮逻辑
  210. */
  211. async cartOrderConfirm() {
  212. if (!this.totalProduct) {
  213. useGlobalToast().show({ msg: '请选择商品' })
  214. return
  215. }
  216. const list = this.cartList.filter(it => it.allGoods.length)
  217. if (list.length > 1) {
  218. useGlobalToast().show({ msg: '不能同时选中两个店铺的商品' })
  219. return
  220. }
  221. const shopSkuStock = this.totalProduct.skuList.filter(it => it.num > Number(it.shopSkuStocks))
  222. if (shopSkuStock.length) {
  223. useGlobalToast().show({ msg: `${shopSkuStock[0].skuName}库存不足` })
  224. }
  225. else {
  226. router.push({
  227. name: 'xsb-confirmOrder',
  228. params: { data: JSON.stringify(this.totalProduct) },
  229. })
  230. }
  231. },
  232. /**
  233. * 统一获取价格逻辑
  234. */
  235. async getCartTotalPrice() {
  236. const ids = this.cartList.filter(it => it.allGoods.length)
  237. if (ids.length) {
  238. const shopIds = ids.map(it => it.allGoods).flat()
  239. const id = shopIds.join(',')
  240. const res = await this.getCartAddGoodsPrice(id)
  241. this.totalProduct = res
  242. }
  243. if (ids.length === this.cartList.length) {
  244. const goodsindex = ids.filter(it => it.allGoods.length === it.skuList.length)
  245. if (goodsindex.length === this.cartList.length) {
  246. this.isCartAllChecked = true
  247. }
  248. else {
  249. this.isCartAllChecked = false
  250. }
  251. }
  252. else {
  253. this.isCartAllChecked = false
  254. }
  255. },
  256. },
  257. })