index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { defineComponent, ref } from 'vue'
  2. import ColHeader from '@/components/col-header'
  3. import styles from './index.module.less'
  4. import {
  5. Checkbox,
  6. SubmitBar,
  7. Card,
  8. Stepper,
  9. CheckboxGroup,
  10. Dialog,
  11. Toast,
  12. Image
  13. } from 'vant'
  14. import request from '@/helpers/request'
  15. import { cartConfirm, formateAttr } from './cart'
  16. import ColResult from '@/components/col-result'
  17. import { moneyFormat } from '@/helpers/utils'
  18. import { postMessage } from '@/helpers/native-message'
  19. import iconSellOut from '../shop-mall/images/icon-sell-out.png'
  20. import { getCartCount } from '../shop-mall/shop-mall'
  21. export default defineComponent({
  22. name: 'cart',
  23. data() {
  24. return {
  25. loading: true,
  26. dataShow: false,
  27. isManage: false,
  28. cartList: [],
  29. selectItems: []
  30. }
  31. },
  32. computed: {
  33. checkAll() {
  34. let selectLen: number = this.selectItems.length
  35. let cartLen: number = this.cartList.length
  36. if (!cartLen) return false
  37. return selectLen === cartLen
  38. },
  39. len() {
  40. let n: number = this.selectItems.length
  41. return n
  42. },
  43. totalPrice() {
  44. let price = 0
  45. const items = this.selectItems as any
  46. this.cartList.forEach((n: any) => {
  47. if (items.includes(n.id) && typeof n.price === 'number') {
  48. price += n.price * n.quantity
  49. }
  50. })
  51. return price * 100
  52. }
  53. },
  54. mounted() {
  55. this.getCartList()
  56. },
  57. methods: {
  58. async getCartList() {
  59. this.loading = true
  60. this.cartList = []
  61. try {
  62. let { code, data } = await request.get('/api-mall-portal/cart/list')
  63. if (code === 200) {
  64. this.cartList = data.map(n => {
  65. n.stock = n.stock - n.lockStock
  66. n.stock = n.stock <= 0 ? 0 : n.stock
  67. return n
  68. })
  69. }
  70. } catch (err) {}
  71. this.dataShow = true
  72. this.loading = false
  73. },
  74. setCheckAll() {
  75. const selectItems = [] as any
  76. if (!this.checkAll) {
  77. this.cartList.forEach((n: any) => {
  78. if (!this.isManage) {
  79. if (n.stock > 0) {
  80. selectItems.push(n.id)
  81. }
  82. } else {
  83. selectItems.push(n.id)
  84. }
  85. })
  86. }
  87. this.selectItems = selectItems
  88. },
  89. async setCartItem(item: any) {
  90. try {
  91. let { code, data } = await request.get(
  92. '/api-mall-portal/cart/update/quantity',
  93. {
  94. params: {
  95. id: item.id,
  96. quantity: item.quantity
  97. }
  98. }
  99. )
  100. item.stock = data
  101. if (item.quantity > data) {
  102. item.quantity = data
  103. setTimeout(() => {
  104. Toast('库存不足')
  105. }, 500)
  106. }
  107. } catch (err) {}
  108. },
  109. onChecked(id: number) {
  110. const items = this.selectItems as number[]
  111. if (items.includes(id)) {
  112. items.splice(items.indexOf(id), 1)
  113. } else {
  114. items.push(id)
  115. }
  116. this.selectItems = items as []
  117. },
  118. async onDeleteCartItem() {
  119. Dialog.confirm({
  120. message: '是否确认删除选中的商品?'
  121. }).then(async () => {
  122. const ids = this.selectItems.join(',')
  123. try {
  124. let { code, data } = await request.post(
  125. '/api-mall-portal/cart/delete?ids=' + ids
  126. )
  127. if (code === 200) {
  128. this.getCartList()
  129. this.selectItems = []
  130. this.isManage = false
  131. getCartCount()
  132. }
  133. } catch (error) {}
  134. })
  135. },
  136. //生成确认订单
  137. async generateConfirmOrder() {
  138. this.$router.push({
  139. path: '/cartConfirm',
  140. query: {
  141. cartIds: this.selectItems.join(',')
  142. }
  143. })
  144. // return
  145. // const ids: number[] = [...this.selectItems]
  146. // try {
  147. // let { code, data } = await request.post(
  148. // '/api-mall-portal/order/generateConfirmOrder',
  149. // {
  150. // params: {
  151. // cartIds: ids.join(',')
  152. // }
  153. // }
  154. // )
  155. // if (code === 200) {
  156. // cartConfirm.calcAmount = data.calcAmount
  157. // cartConfirm.cartPromotionItemList = data.cartPromotionItemList
  158. // cartConfirm.memberReceiveAddressList = data.memberReceiveAddressList
  159. // // alert(JSON.stringify(data.memberReceiveAddressList))
  160. // this.$router.push({
  161. // path: '/cartConfirm'
  162. // })
  163. // }
  164. // } catch (error) {}
  165. },
  166. gotoShopMall() {
  167. postMessage({
  168. api: 'back'
  169. })
  170. }
  171. },
  172. render() {
  173. return (
  174. <>
  175. {this.dataShow ? (
  176. <div>
  177. <ColHeader
  178. onClickRight={() => {
  179. this.isManage = !this.isManage
  180. if (!this.isManage) {
  181. this.cartList.forEach((n: any) => {
  182. if (n.stock <= 0) {
  183. this.selectItems = this.selectItems.filter(
  184. (item: any) => item != n.id
  185. )
  186. }
  187. })
  188. }
  189. }}
  190. v-slots={{
  191. right: () => (
  192. <span style={{ color: '#333', fontSize: '14px' }}>
  193. {this.isManage ? '完成' : '管理'}
  194. </span>
  195. )
  196. }}
  197. ></ColHeader>
  198. <div class={styles.cartBox}>
  199. {this.cartList.length ? (
  200. <CheckboxGroup v-model={this.selectItems}>
  201. {this.cartList.map((item: any) => (
  202. <div class={styles.cartItem}>
  203. <Checkbox
  204. name={item.id}
  205. disabled={item.stock <= 0 && !this.isManage}
  206. >
  207. <Card
  208. price={moneyFormat(item.price)}
  209. desc={formateAttr(item.productAttr)}
  210. title={item.productName}
  211. thumb={item.productPic}
  212. v-slots={{
  213. num: () => (
  214. <Stepper
  215. longPress={false}
  216. v-model={item.quantity}
  217. onClick={e => {
  218. e.stopPropagation()
  219. }}
  220. onChange={() => {
  221. if (item.quantity) {
  222. this.setCartItem(item)
  223. }
  224. }}
  225. inputWidth="50px"
  226. buttonSize="24px"
  227. min={1}
  228. max={item.stock > 200 ? 200 : item.stock}
  229. integer
  230. />
  231. ),
  232. thumb: () => (
  233. <div>
  234. <Image src={item.productPic}></Image>
  235. {item.stock <= 0 && (
  236. <div class={styles.sellOut}>
  237. <Image
  238. src={iconSellOut}
  239. fit="cover"
  240. class={styles.sellOutImg}
  241. />
  242. </div>
  243. )}
  244. </div>
  245. )
  246. }}
  247. ></Card>
  248. </Checkbox>
  249. </div>
  250. ))}
  251. </CheckboxGroup>
  252. ) : null}
  253. {!this.loading && !this.cartList.length && (
  254. <ColResult
  255. tips="购物车空空如也"
  256. buttonText="去商城逛逛"
  257. onClick={() => this.gotoShopMall()}
  258. ></ColResult>
  259. )}
  260. <div style={{ height: 'var(--van-submit-bar-height)' }}></div>
  261. {this.isManage ? (
  262. <div class={styles.delete}>
  263. <SubmitBar
  264. buttonText="删除"
  265. buttonColor="var(--van-primary)"
  266. disabled={this.len === 0}
  267. onSubmit={() => this.onDeleteCartItem()}
  268. >
  269. <Checkbox
  270. modelValue={this.checkAll}
  271. onClick={value => this.setCheckAll()}
  272. >
  273. 全选
  274. </Checkbox>
  275. </SubmitBar>
  276. </div>
  277. ) : (
  278. <div class={styles.submit}>
  279. <SubmitBar
  280. price={this.totalPrice}
  281. buttonText={`结算(${this.len})`}
  282. buttonColor="var(--van-primary)"
  283. disabled={this.len === 0}
  284. onSubmit={() => this.generateConfirmOrder()}
  285. >
  286. <Checkbox
  287. modelValue={this.checkAll}
  288. onClick={value => this.setCheckAll()}
  289. >
  290. 全选
  291. </Checkbox>
  292. </SubmitBar>
  293. </div>
  294. )}
  295. </div>
  296. </div>
  297. ) : null}
  298. </>
  299. )
  300. }
  301. })