index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { defineComponent, ref } from 'vue'
  2. import ColHeader from '@/components/col-header'
  3. import styles from './index.module.less'
  4. import { Checkbox, SubmitBar, Card, Stepper, CheckboxGroup } from 'vant'
  5. import request from '@/helpers/request'
  6. import { cartConfirm } from './cart'
  7. import ColResult from '@/components/col-result'
  8. import { moneyFormat } from '@/helpers/utils'
  9. export default defineComponent({
  10. name: 'cart',
  11. data() {
  12. return {
  13. dataShow: false,
  14. isManage: false,
  15. cartList: [],
  16. selectItems: []
  17. }
  18. },
  19. computed: {
  20. checkAll() {
  21. let selectLen = this.selectItems.length as any
  22. let cartLen = this.cartList.length as any
  23. return selectLen === cartLen
  24. },
  25. len() {
  26. let n = this.selectItems.length as any
  27. return n
  28. },
  29. totalPrice() {
  30. let price = 0
  31. const items = this.selectItems as any
  32. this.cartList.forEach((n: any) => {
  33. if (items.includes(n.id) && typeof n.price === 'number') {
  34. price += n.price * n.quantity
  35. }
  36. })
  37. return price * 100
  38. }
  39. },
  40. mounted() {
  41. this.getCartList()
  42. },
  43. methods: {
  44. async getCartList() {
  45. this.cartList = []
  46. try {
  47. let { code, data } = await request.get('/api-mall-portal/cart/list')
  48. code === 200 && (this.cartList = data)
  49. } catch (err) {}
  50. this.dataShow = true
  51. },
  52. setCheckAll() {
  53. const selectItems = [] as any
  54. if (!this.checkAll) {
  55. this.cartList.forEach((n: any) => {
  56. selectItems.push(n.id)
  57. })
  58. }
  59. this.selectItems = selectItems
  60. },
  61. async setCartItem(item: any) {
  62. try {
  63. let { code, data } = await request.get(
  64. '/api-mall-portal/cart/update/quantity',
  65. {
  66. params: {
  67. id: item.id,
  68. quantity: item.quantity
  69. },
  70. hideLoading: true
  71. }
  72. )
  73. } catch (err) {}
  74. },
  75. onChecked(id: number) {
  76. const items = this.selectItems as number[]
  77. if (items.includes(id)) {
  78. items.splice(items.indexOf(id), 1)
  79. } else {
  80. items.push(id)
  81. }
  82. this.selectItems = items as []
  83. },
  84. async onDeleteCartItem() {
  85. const ids = this.selectItems.join(',')
  86. try {
  87. let { code, data } = await request.post(
  88. '/api-mall-portal/cart/delete?ids=' + ids
  89. )
  90. if (code === 200) {
  91. this.getCartList()
  92. this.selectItems = []
  93. this.isManage = false
  94. }
  95. } catch (error) {}
  96. },
  97. //生成确认订单
  98. async generateConfirmOrder() {
  99. const ids: number[] = [...this.selectItems]
  100. try {
  101. let { code, data } = await request.post(
  102. '/api-mall-portal/order/generateConfirmOrder',
  103. {
  104. params: {
  105. cartIds: ids.join(',')
  106. }
  107. }
  108. )
  109. if (code === 200) {
  110. cartConfirm.calcAmount = data.calcAmount
  111. cartConfirm.cartPromotionItemList = data.cartPromotionItemList
  112. cartConfirm.memberReceiveAddressList = data.memberReceiveAddressList
  113. // alert(JSON.stringify(data.memberReceiveAddressList))
  114. this.$router.push({
  115. path: '/cartConfirm'
  116. })
  117. }
  118. } catch (error) {}
  119. },
  120. gotoShopMall() {
  121. this.$router.push({
  122. path: '/shopMall'
  123. })
  124. }
  125. },
  126. render() {
  127. return (
  128. <>
  129. {this.dataShow ? (
  130. <div>
  131. {!this.cartList.length ? (
  132. <ColResult
  133. tips="购物车空空如也"
  134. buttonText="去商城逛逛"
  135. onClick={() => this.gotoShopMall()}
  136. ></ColResult>
  137. ) : (
  138. <>
  139. <ColHeader
  140. onClickRight={() => (this.isManage = !this.isManage)}
  141. v-slots={{
  142. right: () => (
  143. <span style={{ color: '#333', fontSize: '14px' }}>
  144. {this.isManage ? '完成' : '管理'}
  145. </span>
  146. )
  147. }}
  148. ></ColHeader>
  149. <div class={styles.cartBox}>
  150. <CheckboxGroup v-model={this.selectItems}>
  151. {this.cartList.map((item: any) => (
  152. <div class={styles.cartItem}>
  153. <Checkbox name={item.id}>
  154. <Card
  155. price={moneyFormat(item.price)}
  156. desc={item.productAttr}
  157. title={item.productName}
  158. thumb={item.productPic}
  159. v-slots={{
  160. num: () => (
  161. <Stepper
  162. v-model={item.quantity}
  163. onClick={e => {
  164. e.stopPropagation()
  165. }}
  166. onChange={() => this.setCartItem(item)}
  167. inputWidth="50px"
  168. buttonSize="24px"
  169. min={1}
  170. />
  171. )
  172. }}
  173. ></Card>
  174. </Checkbox>
  175. </div>
  176. ))}
  177. </CheckboxGroup>
  178. <div style={{ height: 'var(--van-submit-bar-height)' }}></div>
  179. {this.isManage ? (
  180. <div class={styles.delete}>
  181. <SubmitBar
  182. buttonText="删除"
  183. buttonColor="var(--van-primary)"
  184. disabled={this.totalPrice === 0}
  185. onSubmit={() => this.onDeleteCartItem()}
  186. >
  187. <Checkbox
  188. modelValue={this.checkAll}
  189. onClick={value => this.setCheckAll()}
  190. >
  191. 全选
  192. </Checkbox>
  193. </SubmitBar>
  194. </div>
  195. ) : (
  196. <div class={styles.submit}>
  197. <SubmitBar
  198. price={this.totalPrice}
  199. buttonText={`结算(${this.len})`}
  200. buttonColor="var(--van-primary)"
  201. disabled={this.totalPrice === 0}
  202. onSubmit={() => this.generateConfirmOrder()}
  203. >
  204. <Checkbox
  205. modelValue={this.checkAll}
  206. onClick={value => this.setCheckAll()}
  207. >
  208. 全选
  209. </Checkbox>
  210. </SubmitBar>
  211. </div>
  212. )}
  213. </div>
  214. </>
  215. )}
  216. </div>
  217. ) : null}
  218. </>
  219. )
  220. }
  221. })