index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {
  2. computed,
  3. ref,
  4. defineComponent,
  5. onMounted,
  6. reactive,
  7. onUnmounted,
  8. watch
  9. } from 'vue'
  10. import { Card, Cell, CellGroup, Popup, SubmitBar, Toast } from 'vant'
  11. import { addressType, cartConfirm, formateAttr } from '../cart'
  12. import styles from '../index.module.less'
  13. import Address from '../components/address'
  14. import request from '@/helpers/request'
  15. import { useRoute, useRouter } from 'vue-router'
  16. import ColProtocol from '@/components/col-protocol'
  17. import Payment from '@/views/order-detail/payment'
  18. import { state } from '@/state'
  19. import ColPopup from '@/components/col-popup'
  20. import UserAuth from '@/views/order-detail/userAuth'
  21. import ColResult from '@/components/col-result'
  22. import { moneyFormat } from '@/helpers/utils'
  23. import {
  24. listenerMessage,
  25. postMessage,
  26. removeListenerMessage
  27. } from '@/helpers/native-message'
  28. import UseCoupons, { couponEnum } from '@/views/order-detail/use-coupons'
  29. import ColHeader from '@/components/col-header'
  30. export default defineComponent({
  31. name: 'cartConfirm',
  32. setup() {
  33. const loading = ref(true)
  34. const route = useRoute()
  35. const address = ref<addressType>()
  36. onMounted(async () => {
  37. loading.value = true
  38. if (route.query.cartIds) {
  39. try {
  40. const { code, data } = await request.post(
  41. '/api-mall-portal/order/generateConfirmOrder',
  42. {
  43. params: {
  44. cartIds: route.query.cartIds
  45. }
  46. }
  47. )
  48. if (code === 200) {
  49. cartConfirm.calcAmount = data.calcAmount
  50. cartConfirm.cartPromotionItemList = data.cartPromotionItemList
  51. cartConfirm.memberReceiveAddressList = data.memberReceiveAddressList
  52. }
  53. } catch (error) {}
  54. }
  55. loading.value = false
  56. if (cartConfirm.memberReceiveAddressList.length) {
  57. const a =
  58. cartConfirm.memberReceiveAddressList.find(
  59. (n: any) => n.defaultStatus
  60. ) || cartConfirm.memberReceiveAddressList[0]
  61. if (a) address.value = a
  62. }
  63. })
  64. const setAddress = (result: addressType) => {
  65. address.value = result
  66. }
  67. onMounted(() => {
  68. listenerMessage('getAddress', result => {
  69. setAddress(result?.content || {})
  70. })
  71. })
  72. onUnmounted(() => {
  73. removeListenerMessage('getAddress', () => {})
  74. })
  75. const agreeStatus = ref(false)
  76. const paymentPopup = ref(false)
  77. const authPopup = ref(false)
  78. const orderInfo = reactive({
  79. orderNo: '',
  80. actualPrice: 0,
  81. couponId: ''
  82. })
  83. //修复实名认证头部问题
  84. watch(authPopup, (value, oldValue) => {
  85. if (authPopup.value) {
  86. // 设置是否显示导航栏 0 显示 1 不显示
  87. postMessage({ api: 'setBarStatus', content: { status: 0 } })
  88. } else {
  89. postMessage({ api: 'setBarStatus', content: { status: 1 } })
  90. }
  91. })
  92. // 提交
  93. const onSubmit = () => {
  94. if (!address.value?.id) {
  95. Toast('请选择收货地址')
  96. return
  97. }
  98. if (!agreeStatus.value) {
  99. Toast('请先阅读并同意《酷乐秀平台服务协议》')
  100. return
  101. }
  102. const users = state.user.data
  103. // 判断是否需要实名认证
  104. if (!users?.realName || !users?.idCardNo) {
  105. authPopup.value = true
  106. return
  107. }
  108. // 判断是否有订单号
  109. if (orderInfo.orderNo) {
  110. paymentPopup.value = true
  111. return
  112. }
  113. createOrder()
  114. }
  115. const router = useRouter()
  116. //创建订单
  117. const createOrder = async () => {
  118. const ids = cartConfirm.cartPromotionItemList.reduce(
  119. (arr, value: any) => {
  120. arr.push(value.id)
  121. return arr
  122. },
  123. []
  124. )
  125. const payAmount =
  126. cartConfirm.calcAmount.payAmount -
  127. cartConfirm.calcAmount.promotionAmount
  128. const body = {
  129. cartIds: ids,
  130. memberReceiveAddressId: address.value?.id,
  131. platformType: state.platformType,
  132. orderAmount: Number((payAmount >= 0 ? payAmount : 0).toFixed(2)),
  133. couponId: orderInfo.couponId
  134. }
  135. try {
  136. const { code, data } = await request.post(
  137. '/api-mall-portal/order/generateOrder',
  138. { data: body }
  139. )
  140. if (code === 200) {
  141. if (data.order.status === 1) {
  142. router.replace({
  143. path: `/shopTrade`,
  144. query: {
  145. orderNo: data?.order.orderSn,
  146. id: data?.order.id
  147. }
  148. })
  149. return
  150. }
  151. paymentPopup.value = true
  152. orderInfo.orderNo = data?.order.orderSn || ''
  153. orderInfo.actualPrice = data?.order.payAmount || 0
  154. }
  155. } catch (error) {}
  156. }
  157. //认证成功
  158. const onAuthSuccess = () => {
  159. authPopup.value = false
  160. onSubmit() // 实名成功后自动支付
  161. }
  162. return () => (
  163. <>
  164. <ColHeader />
  165. {loading.value ? null : (
  166. <div>
  167. {cartConfirm.cartPromotionItemList.length ? (
  168. <div class={styles.cartConfirm}>
  169. <div class={styles.cartConfirmBox}>
  170. <Address item={address.value} setAddress={setAddress} />
  171. </div>
  172. <div
  173. style={{ marginTop: '20px' }}
  174. class={[styles.cartBox, styles.cartConfirmBox]}
  175. >
  176. <div class={styles.shopBox}>
  177. {cartConfirm.cartPromotionItemList.map((item: any) => (
  178. <div
  179. class={[styles.cartItem]}
  180. style={{ marginBottom: '10px' }}
  181. >
  182. <Card
  183. price={moneyFormat(item.price)}
  184. desc={formateAttr(item.productAttr)}
  185. title={item.productName}
  186. thumb={item.productPic}
  187. num={item.quantity}
  188. ></Card>
  189. </div>
  190. ))}
  191. </div>
  192. <CellGroup border={false}>
  193. {/* <Cell
  194. border={false}
  195. title="运费"
  196. value={moneyFormat(cartConfirm.calcAmount.freightAmount)}
  197. ></Cell>
  198. {/* <Cell border={false} title="优惠卷" value="暂无可用优惠卷"></Cell>
  199. <Cell border={false} title="乐乐币抵扣" value={"-¥" + cartConfirm.calcAmount.promotionAmount}></Cell> */}
  200. <UseCoupons
  201. orderType="GOODS"
  202. orderAmount={cartConfirm.calcAmount.totalAmount}
  203. onCouponSelect={coupon => {
  204. const discountPrice = coupon
  205. .map(n => n.discountPrice)
  206. .reduce((total, n) => {
  207. return total + n
  208. }, 0)
  209. const couponId = coupon
  210. .map(n => n.couponIssueId)
  211. .join(',')
  212. cartConfirm.calcAmount.promotionAmount = discountPrice
  213. orderInfo.couponId = couponId
  214. }}
  215. />
  216. {/* <Cell
  217. border={false}
  218. title="优惠"
  219. value={
  220. '-¥ ' +
  221. moneyFormat(cartConfirm.calcAmount.promotionAmount)
  222. }
  223. ></Cell> */}
  224. <Cell
  225. border={false}
  226. title="总额"
  227. value={
  228. '¥ ' + moneyFormat(cartConfirm.calcAmount.totalAmount)
  229. }
  230. ></Cell>
  231. </CellGroup>
  232. </div>
  233. <div class={styles.payProtocol}>
  234. <ColProtocol v-model={agreeStatus.value}></ColProtocol>
  235. <SubmitBar
  236. buttonText={`结算(${cartConfirm.cartPromotionItemList.length})`}
  237. buttonColor="var(--van-primary)"
  238. disabled={cartConfirm.cartPromotionItemList.length === 0}
  239. onSubmit={() => onSubmit()}
  240. >
  241. <div class={styles.confirmBottom}>
  242. 合计{' '}
  243. <span class={styles['price-des']}>
  244. ¥
  245. {moneyFormat(
  246. cartConfirm.calcAmount.payAmount -
  247. cartConfirm.calcAmount.promotionAmount >=
  248. 0
  249. ? cartConfirm.calcAmount.payAmount -
  250. cartConfirm.calcAmount.promotionAmount
  251. : 0
  252. )}
  253. </span>
  254. </div>
  255. </SubmitBar>
  256. </div>
  257. <div style={{ height: 'var(--van-submit-bar-height)' }}></div>
  258. <ColPopup v-model={authPopup.value}>
  259. <UserAuth onSuccess={onAuthSuccess} />
  260. </ColPopup>
  261. <Popup
  262. show={paymentPopup.value}
  263. closeOnClickOverlay={false}
  264. position="bottom"
  265. round
  266. closeOnPopstate
  267. safeAreaInsetBottom
  268. style={{ minHeight: '30%' }}
  269. >
  270. <Payment
  271. v-model={paymentPopup.value}
  272. orderInfo={orderInfo}
  273. paymentType="goodsPay"
  274. onBackOut={() => (paymentPopup.value = false)}
  275. />
  276. </Popup>
  277. </div>
  278. ) : (
  279. <ColResult
  280. buttonText="去购物车"
  281. onClick={() => {
  282. router.push({ path: '/cart' })
  283. }}
  284. ></ColResult>
  285. )}
  286. </div>
  287. )}
  288. </>
  289. )
  290. }
  291. })