index.tsx 7.0 KB

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