index.tsx 5.5 KB

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