cart.ts 7.8 KB

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