index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import request from '@/helpers/request'
  2. import { state } from '@/state'
  3. import { Cell, Popup } from 'vant'
  4. import { defineComponent } from 'vue'
  5. import ChoiceCoupon from './choice-coupon'
  6. import styles from './index.module.less'
  7. /*
  8. * 订单类型对应优惠券类型
  9. */
  10. export const couponEnum = {
  11. UNIVERSAL: 'UNIVERSAL',
  12. VIP: 'VIP',
  13. PINAO_ROOM: 'PIANO',
  14. GOODS: 'MALL',
  15. MUSIC: 'MUSIC',
  16. PRACTICE: 'SPARRING',
  17. LIVE: 'LIVE',
  18. VIDEO: 'VIDEO'
  19. }
  20. export default defineComponent({
  21. name: 'use-conpon',
  22. props: {
  23. disabled: {
  24. type: Boolean,
  25. default: false
  26. },
  27. orderAmount: {
  28. type: Number,
  29. default: 0
  30. },
  31. orderType: {
  32. type: String,
  33. default: ''
  34. },
  35. discountPrice: {
  36. // 优惠券使用金额
  37. type: Number,
  38. default: 0
  39. }
  40. },
  41. emits: ['couponSelect'],
  42. data() {
  43. return {
  44. popupStatus: false,
  45. popupLoading: false,
  46. useCouponList: [] as any,
  47. useCouponLoading: false,
  48. useCouponCount: 0,
  49. dataLoading: false,
  50. list: [] as any
  51. }
  52. },
  53. computed: {
  54. couponCount() {
  55. const limitCount = this.useCouponList.map((list: any) => {
  56. return Number(list.discountPrice || 0)
  57. })
  58. let count = 0
  59. if (this.disabled) {
  60. count = this.discountPrice
  61. } else {
  62. count =
  63. limitCount.length > 0
  64. ? limitCount.reduce((sum: any, list: any) => {
  65. return sum + list
  66. })
  67. : 0
  68. }
  69. return count
  70. },
  71. couponCategory() {
  72. // 如果订单类型不在优惠券类型里面,则默认查询通用券
  73. return couponEnum[this.orderType] || 'UNIVERSAL'
  74. }
  75. },
  76. mounted() {
  77. // this.getUseableCoupon()
  78. this.getList()
  79. },
  80. methods: {
  81. async getList() {
  82. if (this.dataLoading) return
  83. this.dataLoading = true
  84. try {
  85. const res = await request.post(`${state.platformApi}/couponInfo/page`, {
  86. data: {
  87. couponCategory: this.couponCategory,
  88. couponType: 'FULL_DISCOUNT',
  89. useState: 'USABLE',
  90. page: 1,
  91. rows: 100
  92. }
  93. })
  94. this.dataLoading = false
  95. const result = res.data || {}
  96. // 处理重复请求数据
  97. if (this.list.length > 0 && result.pageNo === 1) return
  98. this.list = result.rows || []
  99. // 处理可用优惠券是否支付使用
  100. this.list.forEach((item: any) => {
  101. item.checked = false
  102. // 如果使用金额大于订单金额则优惠券不可用
  103. if (item.useLimit > this.orderAmount) {
  104. item.disabled = true
  105. } else {
  106. item.disabled = false
  107. }
  108. })
  109. let count = 0
  110. this.list.forEach((item: any) => {
  111. if (!item.disabled) {
  112. count++
  113. }
  114. })
  115. console.log(this.list, 'list')
  116. this.useCouponCount = count
  117. } catch {
  118. //
  119. }
  120. },
  121. // async getUseableCoupon() {
  122. // try {
  123. // this.useCouponLoading = true
  124. // // 判断是哪个端
  125. // const url =
  126. // state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  127. // const res = await request.get(`${url}/couponInfo/statInfo`)
  128. // this.useCouponLoading = false
  129. // const result = (res.data || []).find(
  130. // result => result.useState === 'USABLE'
  131. // )
  132. // this.useCouponCount = result.total || 0
  133. // } catch {
  134. // // TODO: handle
  135. // }
  136. // },
  137. onSubmit(item: any) {
  138. // useCouponList
  139. this.useCouponList = item
  140. this.$emit('couponSelect', item)
  141. this.popupStatus = false
  142. this.popupLoading = false
  143. }
  144. },
  145. render() {
  146. return (
  147. <>
  148. <Cell
  149. title="优惠券"
  150. class={styles.useCoupon}
  151. style={{ borderRadius: '8px' }}
  152. isLink={!this.disabled}
  153. clickable={false}
  154. v-slots={{
  155. value: () =>
  156. !this.useCouponLoading && (
  157. <>
  158. {/* 判断是否有选择优惠券 */}
  159. {this.couponCount > 0 ? (
  160. <span class={styles.couponCount}>
  161. <i>-¥</i>
  162. {this.couponCount}
  163. </span>
  164. ) : (
  165. <>
  166. {/* 判断是否有可以的优惠券 */}
  167. {this.useCouponCount > 0
  168. ? `${this.useCouponCount}张可使用`
  169. : '暂无可使用优惠券'}
  170. </>
  171. )}
  172. </>
  173. )
  174. }}
  175. onClick={() => {
  176. if (this.disabled) return
  177. this.popupStatus = true
  178. this.popupLoading = true
  179. }}
  180. ></Cell>
  181. <Popup
  182. v-model:show={this.popupStatus}
  183. position="bottom"
  184. round
  185. safeAreaInsetBottom={true}
  186. style={{ height: '75%' }}
  187. onClosed={() => {
  188. this.popupLoading = false
  189. }}
  190. >
  191. {/* 优化体验 */}
  192. {this.popupLoading && (
  193. <ChoiceCoupon
  194. couponCategory={this.couponCategory}
  195. useCoupon={this.useCouponList}
  196. orderAmount={this.orderAmount}
  197. couponList={this.list}
  198. onClose={() => (this.popupStatus = false)}
  199. onSubmit={(item: any) => this.onSubmit(item)}
  200. />
  201. )}
  202. </Popup>
  203. </>
  204. )
  205. }
  206. })