cart.ts 8.1 KB

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