index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { computed, defineComponent, onMounted, reactive } from 'vue'
  2. import styles from './index.module.less'
  3. import ColHeader from '@/components/col-header'
  4. import { useEventListener } from '@vant/use'
  5. import iconStudent from '@common/images/icon_student.png'
  6. import { Button, Image, Popup } from 'vant'
  7. import { state as baseState, setLogin } from '@/state'
  8. import iconMemo from './images/memo.png'
  9. import iconTip from './images/tip.png'
  10. import iconTitle1 from './images/title1.png'
  11. import TheSticky from '@/components/the-sticky'
  12. import request from '@/helpers/request'
  13. import { tradeOrder } from '../trade/tradeOrder'
  14. import { useRouter } from 'vue-router'
  15. import dayjs from 'dayjs'
  16. import { orderStatus } from '@/views/order-detail/orderStatus'
  17. export default defineComponent({
  18. name: 'discount-card',
  19. setup() {
  20. const router = useRouter()
  21. const state = reactive({
  22. discountDetail: {} as any,
  23. titleOpacity: 0,
  24. orderVisible: false,
  25. orderDetail: {} as any
  26. })
  27. const userInfo = computed(() => {
  28. const users = baseState.user.data
  29. return {
  30. username: users?.username,
  31. phone: users?.phone,
  32. avatar: users?.heardUrl,
  33. id: users?.userId,
  34. discountCardFlag: users?.discountCardFlag,
  35. discountEndTime: users?.discountEndTime,
  36. userVip: users?.userVip
  37. }
  38. })
  39. useEventListener('scroll', () => {
  40. const height =
  41. window.scrollY ||
  42. window.pageYOffset ||
  43. document.documentElement.scrollTop
  44. state.titleOpacity = height > 30 ? 1 : 0
  45. })
  46. // 取消支付
  47. const onCancelOrder = async () => {
  48. try {
  49. await request.post(`${baseState.platformApi}/userOrder/orderCancel`, {
  50. data: { orderNo: state.orderDetail.orderNo }
  51. })
  52. state.orderVisible = false
  53. } catch {
  54. //
  55. }
  56. }
  57. // 继续支付
  58. const onContinueOrder = async () => {
  59. const orderDetail = state.orderDetail || {}
  60. tradeOrder(orderDetail, () => {
  61. router.push({
  62. path: '/orderDetail',
  63. query: {
  64. orderType: orderDetail.orderType
  65. }
  66. })
  67. })
  68. }
  69. const onSubmit = async () => {
  70. try {
  71. // 判断是否有待支付订单
  72. const resPadding = await request.post(
  73. `${baseState.platformApi}/userOrder/getPendingOrder`,
  74. {
  75. data: { goodType: 'DISCOUNT' }
  76. }
  77. )
  78. console.log(resPadding, 'resPadding')
  79. if (resPadding?.data?.id) {
  80. state.orderVisible = true
  81. state.orderDetail = resPadding.data || {}
  82. return
  83. }
  84. let startTime = new Date()
  85. if(userInfo.value.discountCardFlag) {
  86. startTime = dayjs(userInfo.value.discountEndTime || new Date()).toDate()
  87. }
  88. let endTime = new Date()
  89. if (state.discountDetail.period === 'MONTH') {
  90. endTime = dayjs(startTime).add(1, 'month').toDate()
  91. } else if (state.discountDetail.period === 'QUARTERLY') {
  92. endTime = dayjs(startTime).add(3, 'month').toDate()
  93. } else if (state.discountDetail.period === 'YEAR_HALF') {
  94. endTime = dayjs(startTime).add(6, 'month').toDate()
  95. } else if (state.discountDetail.period === 'YEAR') {
  96. endTime = dayjs(startTime).add(1, 'year').toDate()
  97. }
  98. orderStatus.orderObject.orderType = 'DISCOUNT'
  99. orderStatus.orderObject.orderName = `畅学卡`
  100. orderStatus.orderObject.orderDesc = `畅学卡`
  101. orderStatus.orderObject.actualPrice = state.discountDetail.salePrice || 0
  102. orderStatus.orderObject.recomUserId = null
  103. orderStatus.orderObject.activityId = null
  104. orderStatus.orderObject.orderNo = ''
  105. orderStatus.orderObject.orderList = [
  106. {
  107. orderType: 'DISCOUNT',
  108. goodsName: `畅学卡`,
  109. id: state.discountDetail.id,
  110. title: '畅学卡',
  111. num: 1, // 购买个数
  112. salePrice: state.discountDetail.salePrice,
  113. period: state.discountDetail.period,
  114. price: state.discountDetail.salePrice,
  115. startTime: dayjs(startTime).format('YYYY-MM-DD'),
  116. endTime: dayjs(endTime).format('YYYY-MM-DD')
  117. }
  118. ]
  119. router.push({
  120. path: '/orderDetail',
  121. query: {
  122. orderType: 'DISCOUNT'
  123. }
  124. })
  125. } catch {
  126. //
  127. }
  128. }
  129. onMounted(async () => {
  130. try {
  131. const userInfo = await request.get(
  132. baseState.platformType === 'TEACHER'
  133. ? '/api-teacher/teacher/queryUserInfo'
  134. : '/api-student/student/queryUserInfo'
  135. )
  136. setLogin(userInfo.data)
  137. const { data } = await request.get(
  138. `${baseState.platformApi}/memberPriceSettings/getDiscount`
  139. )
  140. state.discountDetail = data
  141. } catch {
  142. //
  143. }
  144. })
  145. return () => (
  146. <div class={styles.discountCardContainer}>
  147. <ColHeader
  148. background={`rgba(255,255,255, ${state.titleOpacity})`}
  149. backIconColor="black"
  150. hideHeader={false}
  151. border={false}
  152. />
  153. <div class={styles.cardContainer}>
  154. <div class={styles.userSection}>
  155. <div
  156. class={[
  157. styles.userImgSection,
  158. (userInfo.value.userVip.vipType === 'SVIP' ||
  159. userInfo.value.userVip.vipType === 'PERMANENT_SVIP') &&
  160. styles.userSVip,
  161. userInfo.value.userVip.vipType === 'VIP' && styles.userVip,
  162. userInfo.value.userVip.svipEndDays > 0 ||
  163. userInfo.value.userVip.vipEndDays > 0
  164. ? styles.isVip
  165. : ''
  166. ]}
  167. >
  168. <Image
  169. class={styles.userImg}
  170. src={userInfo.value.avatar || iconStudent}
  171. fit="cover"
  172. />
  173. <i class={styles.showMemeber}></i>
  174. </div>
  175. <div class={styles.userInfo}>
  176. <div class={styles.userName}>
  177. <span class={styles.name}>{userInfo.value.username}</span>
  178. {userInfo.value.phone && (
  179. <span class={styles.phone}>({userInfo.value.phone})</span>
  180. )}
  181. </div>
  182. <div class={styles.member_time}>
  183. {userInfo.value.discountCardFlag ? <>有效期至<span>{dayjs(userInfo.value.discountEndTime).format('YYYY-MM-DD')}</span></> : '您当前尚未开通畅学卡'}
  184. </div>
  185. </div>
  186. </div>
  187. <div class={styles.cardSection}>
  188. <div class={styles.top}>
  189. <img src={iconTitle1} class={styles.iconTitle1} />
  190. <div class={styles.priceSection}>
  191. <div class={styles.currentPrice}>
  192. <div class={styles.l}>¥</div>
  193. <div class={styles.c}>{state.discountDetail.salePrice || 0}</div>
  194. <div class={styles.r}>/年</div>
  195. </div>
  196. <del class={styles.originPrice}>原价¥{state.discountDetail.originalPrice}/年</del>
  197. </div>
  198. </div>
  199. <div class={styles.chapter}>
  200. <div class={styles.chapterTop}>课程全部</div>
  201. <div class={styles.chapterBottom}>
  202. {(state.discountDetail.discountRate || 0) * 100}<i>折</i>
  203. </div>
  204. </div>
  205. </div>
  206. <div class={styles.imgSection}>
  207. <img src={iconMemo} class={styles.iconMemo} />
  208. <img src={iconTip} class={styles.iconTip} />
  209. </div>
  210. </div>
  211. <TheSticky position="bottom">
  212. <div class={styles.btnGroup}>
  213. <div class={styles.submitBtn} onClick={onSubmit}>
  214. {userInfo.value.discountCardFlag ? '立即续费' : <><i>¥</i>{state.discountDetail.salePrice||0}元立即开通</>}
  215. </div>
  216. </div>
  217. </TheSticky>
  218. <Popup
  219. v-model:show={state.orderVisible}
  220. style={{ background: 'transparent' }}
  221. closeOnClickOverlay={false}
  222. >
  223. <div class={styles.dialogContainer}>
  224. <div class={styles.dialogTitle}>提示</div>
  225. <div class={styles.dialogContent}>
  226. 您有待支付的订单,是否继续支付
  227. </div>
  228. <div class={[styles.dialogBtnGroup, styles.orderGroup]}>
  229. <Button round type="default" plain block onClick={onCancelOrder}>
  230. 取消订单
  231. </Button>
  232. <Button
  233. round
  234. type="primary"
  235. block
  236. class={styles.dialogBtn}
  237. onClick={onContinueOrder}
  238. >
  239. 继续支付
  240. </Button>
  241. </div>
  242. </div>
  243. </Popup>
  244. </div>
  245. )
  246. }
  247. })