index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import request from '@/helpers/request'
  2. import { state } from '@/state'
  3. import { Cell, Popup } from 'vant'
  4. import { defineComponent, PropType } 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. SVIP: 'SVIP',
  14. PIANO_ROOM: 'PIANO',
  15. GOODS: 'MALL',
  16. MUSIC: 'MUSIC',
  17. VIP_COURSE: 'VIP_COURSE',
  18. DISCOUNT: 'DISCOUNT',
  19. PRACTICE: 'SPARRING',
  20. LIVE: 'LIVE',
  21. GROUP: 'GROUP',
  22. VIDEO: 'VIDEO',
  23. ALBUM: 'ALBUM'
  24. }
  25. /*
  26. * 优惠券类型对应订单类型
  27. */
  28. export const couponToOrderTypeEnum = {
  29. UNIVERSAL: 'UNIVERSAL',
  30. VIP: 'VIP',
  31. SVIP: 'SVIP',
  32. PIANO: 'PIANO_ROOM',
  33. MALL: 'GOODS',
  34. MUSIC: 'MUSIC',
  35. VIP_COURSE: 'VIP_COURSE',
  36. DISCOUNT: 'DISCOUNT',
  37. SPARRING: 'PRACTICE',
  38. LIVE: 'LIVE',
  39. GROUP: 'GROUP',
  40. VIDEO: 'VIDEO',
  41. ALBUM: 'ALBUM'
  42. }
  43. export default defineComponent({
  44. name: 'use-conpon',
  45. props: {
  46. disabled: {
  47. type: Boolean,
  48. default: false
  49. },
  50. orderAmount: {
  51. type: Number,
  52. default: 0
  53. },
  54. orderType: {
  55. type: String,
  56. default: ''
  57. },
  58. orderGoodsType: {
  59. type: Array,
  60. default: () => []
  61. },
  62. discountPrice: {
  63. // 优惠券使用金额
  64. type: Number,
  65. default: 0
  66. },
  67. // 选择的优惠券
  68. couponId: {
  69. type: String,
  70. default: ''
  71. },
  72. usedLength: {
  73. type: String as PropType<'SINGLE' | 'MULTIPLE'>,
  74. default: 'SINGLE'
  75. }
  76. },
  77. emits: ['couponSelect'],
  78. data() {
  79. return {
  80. popupStatus: false,
  81. popupLoading: false,
  82. useCouponList: [] as any,
  83. useCouponLoading: false,
  84. useCouponCount: 0,
  85. dataLoading: false,
  86. list: [] as any
  87. }
  88. },
  89. computed: {
  90. couponCount() {
  91. const limitCount = this.useCouponList.map((list: any) => {
  92. return Number(list.discountPrice || 0)
  93. })
  94. let count = 0
  95. if (this.disabled) {
  96. count = this.discountPrice
  97. } else {
  98. count =
  99. limitCount.length > 0
  100. ? limitCount.reduce((sum: any, list: any) => {
  101. return sum + list
  102. })
  103. : 0
  104. }
  105. return count
  106. },
  107. couponCategory() {
  108. // 如果订单类型不在优惠券类型里面,则默认查询通用券
  109. const temp: any[] = []
  110. this.orderGoodsType.forEach((item: any) => {
  111. if(couponEnum[item.orderType]) {
  112. temp.push(couponEnum[item.orderType])
  113. }
  114. })
  115. return temp.join(',') + (temp.length ? ',UNIVERSAL' : 'UNIVERSAL')
  116. }
  117. },
  118. watch: {
  119. couponId(val: any) {
  120. // 为
  121. const ids = val ? val.split(',').map(i => Number(i)) : []
  122. const selectCouponList: any[] = []
  123. this.useCouponList.forEach((item: any) => {
  124. if(ids.includes(item.couponIssueId)) {
  125. selectCouponList.push(item)
  126. }
  127. });
  128. this.useCouponList = selectCouponList
  129. }
  130. },
  131. mounted() {
  132. this.getList()
  133. },
  134. methods: {
  135. resetCouponList() {
  136. this.list = []
  137. this.getList()
  138. },
  139. async getList() {
  140. if (this.dataLoading) return
  141. this.dataLoading = true
  142. try {
  143. const res = await request.post(`${state.platformApi}/couponInfo/page`, {
  144. data: {
  145. couponCategory: this.couponCategory,
  146. couponType: 'FULL_DISCOUNT',
  147. useState: 'USABLE',
  148. orderUse: 1,
  149. page: 1,
  150. rows: 100
  151. }
  152. })
  153. this.dataLoading = false
  154. const result = res.data || {}
  155. // 处理重复请求数据
  156. if (this.list.length > 0 && result.pageNo === 1) return
  157. this.list = result.rows || []
  158. // 处理可用优惠券是否支付使用
  159. this.list.forEach((item: any) => {
  160. item.checked = false
  161. if(item.couponCategory === 'UNIVERSAL') {
  162. let allAmount = 0
  163. this.orderGoodsType.forEach((goods: any) => {
  164. allAmount += Number(goods.price)
  165. })
  166. console.log(allAmount, '1212')
  167. item.disabled = item.useLimit <= allAmount ? false : true
  168. } else {
  169. const disItem: any = this.orderGoodsType.find((goods: any) => couponEnum[goods.orderType] === item.couponCategory)
  170. const useLastAmount = disItem.price || 0
  171. // 如果使用金额大于订单金额则优惠券不可用
  172. if (item.useLimit > useLastAmount) {
  173. item.disabled = true
  174. } else {
  175. item.disabled = false
  176. }
  177. }
  178. })
  179. let count = 0
  180. this.list.forEach((item: any) => {
  181. if (!item.disabled) {
  182. count++
  183. }
  184. })
  185. // console.log(this.list, 'list')
  186. this.useCouponCount = count
  187. } catch {
  188. //
  189. }
  190. },
  191. // async getUseableCoupon() {
  192. // try {
  193. // this.useCouponLoading = true
  194. // // 判断是哪个端
  195. // const url =
  196. // state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  197. // const res = await request.get(`${url}/couponInfo/statInfo`)
  198. // this.useCouponLoading = false
  199. // const result = (res.data || []).find(
  200. // result => result.useState === 'USABLE'
  201. // )
  202. // this.useCouponCount = result.total || 0
  203. // } catch {
  204. // // TODO: handle
  205. // }
  206. // },
  207. onSubmit(item: any) {
  208. // useCouponList
  209. this.useCouponList = item
  210. this.$emit('couponSelect', item)
  211. this.popupStatus = false
  212. this.popupLoading = false
  213. }
  214. },
  215. render() {
  216. return (
  217. <>
  218. <Cell
  219. title="优惠券"
  220. class={styles.useCoupon}
  221. isLink={!this.disabled}
  222. clickable={false}
  223. v-slots={{
  224. value: () =>
  225. !this.useCouponLoading && (
  226. <>
  227. {/* 判断是否有选择优惠券 */}
  228. {this.couponCount > 0 ? (
  229. <span class={styles.couponCount}>
  230. <i>-¥</i>
  231. {this.couponCount}
  232. </span>
  233. ) : (
  234. <>
  235. {/* 判断是否有可以的优惠券 */}
  236. {this.useCouponCount > 0
  237. ? `${this.useCouponCount}张可使用`
  238. : '暂无可使用优惠券'}
  239. </>
  240. )}
  241. </>
  242. )
  243. }}
  244. onClick={() => {
  245. if (this.disabled) return
  246. this.popupStatus = true
  247. this.popupLoading = true
  248. }}
  249. ></Cell>
  250. <Popup
  251. v-model:show={this.popupStatus}
  252. position="bottom"
  253. round
  254. safeAreaInsetBottom={true}
  255. style={{ height: '75%' }}
  256. onClosed={() => {
  257. this.popupLoading = false
  258. }}
  259. >
  260. {/* 优化体验 */}
  261. {this.popupLoading && (
  262. <ChoiceCoupon
  263. usedLength={this.usedLength}
  264. couponCategory={this.couponCategory}
  265. useCoupon={this.useCouponList}
  266. orderAmount={this.orderAmount}
  267. orderGoodsType={this.orderGoodsType}
  268. couponList={this.list}
  269. onClose={() => (this.popupStatus = false)}
  270. onSubmit={(item: any) => this.onSubmit(item)}
  271. />
  272. )}
  273. </Popup>
  274. </>
  275. )
  276. }
  277. })