index.tsx 6.5 KB

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