gift-coupon.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <template>
  2. <view class="page">
  3. <view class="tab-bar">
  4. <view
  5. v-for="(tab, idx) in tabs"
  6. :key="idx"
  7. class="tab-item"
  8. :class="activeTab === idx ? 'active' : ''"
  9. @click="switchTab(idx)"
  10. >
  11. <text>{{ tab.name }}</text>
  12. </view>
  13. </view>
  14. <!-- 赠送 -->
  15. <scroll-view v-if="activeTab === 0" scroll-y class="content" refresher-enabled
  16. :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
  17. <view class="tip">把你的会员券转赠给推荐的人(直推 / 隔级),对方可直接用它购买会员。</view>
  18. <view class="section">
  19. <view class="section-title">1. 选择接收人(你的推荐下级)</view>
  20. <view v-if="recipients.length === 0" class="empty-tip">暂无可赠送的推荐下级(仅支持直推/隔级推荐人)</view>
  21. <view v-for="r in recipients" :key="r.userId" class="recipient-item"
  22. :class="selectedRecipient && selectedRecipient.userId === r.userId ? 'selected' : ''"
  23. @click="pickRecipient(r)">
  24. <image v-if="r.avatar" class="avatar" :src="r.avatar" mode="aspectFill"></image>
  25. <view v-else class="avatar avatar-default">{{ r.nickname | firstChar }}</view>
  26. <view class="recipient-info">
  27. <text class="nick">{{ r.nickname }}</text>
  28. <text class="level-tag">{{ r.giftLevel === 2 ? '隔级' : '直推' }}</text>
  29. </view>
  30. <view v-if="selectedRecipient && selectedRecipient.userId === r.userId" class="check">✓</view>
  31. </view>
  32. </view>
  33. <view class="section">
  34. <view class="section-title">2. 选择要赠送的会员券</view>
  35. <view v-if="coupons.length === 0" class="empty-tip">你还没有可赠送的会员券</view>
  36. <view v-for="c in coupons" :key="c.id" class="coupon-item"
  37. :class="selectedCoupon && selectedCoupon.id === c.id ? 'selected' : ''"
  38. @click="pickCoupon(c)">
  39. <view class="coupon-left">
  40. <text class="coupon-value">{{ formatValue(c) }}</text>
  41. <text class="coupon-type">{{ typeName(c) }}</text>
  42. </view>
  43. <view class="coupon-body">
  44. <text class="coupon-name">{{ c.name }}</text>
  45. <text class="coupon-cond">{{ conditionText(c.minSpend) }}</text>
  46. <text class="coupon-exp">有效期至 {{ formatDate(c.validUntil) }}</text>
  47. </view>
  48. <view v-if="selectedCoupon && selectedCoupon.id === c.id" class="check">✓</view>
  49. </view>
  50. </view>
  51. <view class="section" v-if="coupons.length > 0">
  52. <view class="section-title">3. 留言(选填)</view>
  53. <input class="remark-input" v-model="remark" placeholder="写句祝福吧~" maxlength="30" />
  54. </view>
  55. <view class="action-bar">
  56. <button class="gift-btn" :disabled="!canGift || sending" @click="doGift">
  57. {{ sending ? '赠送中...' : '赠送' }}
  58. </button>
  59. </view>
  60. </scroll-view>
  61. <!-- 赠送记录 -->
  62. <scroll-view v-else scroll-y class="content" refresher-enabled
  63. :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
  64. <view v-if="sentList.length === 0" class="empty-state">
  65. <text class="empty-icon">🎁</text>
  66. <text class="empty-text">还没有赠送记录</text>
  67. </view>
  68. <view v-for="item in sentList" :key="item.transferId" class="sent-item">
  69. <view class="sent-row">
  70. <text class="sent-name">{{ item.couponName }}</text>
  71. <text class="sent-status" :class="'st-' + item.status">{{ statusLabel(item.status) }}</text>
  72. </view>
  73. <view class="sent-row sub">
  74. <text>赠给:{{ item.receiverNickname }}</text>
  75. <text class="sent-level">{{ item.giftLevelLabel }}</text>
  76. </view>
  77. <view v-if="item.remark" class="sent-remark">留言:{{ item.remark }}</view>
  78. <button v-if="item.status === 'GIFTED'" class="revoke-btn" @click="doRevoke(item)">撤回</button>
  79. </view>
  80. </scroll-view>
  81. </view>
  82. </template>
  83. <script>
  84. import {
  85. getGiftRecipients,
  86. getGiftableCoupons,
  87. giftCoupon,
  88. revokeGiftCoupon,
  89. getSentGifts
  90. } from '../../utils/api.js'
  91. export default {
  92. data() {
  93. return {
  94. tabs: [{ name: '赠送会员券' }, { name: '赠送记录' }],
  95. activeTab: 0,
  96. recipients: [],
  97. coupons: [],
  98. selectedRecipient: null,
  99. selectedCoupon: null,
  100. remark: '',
  101. sending: false,
  102. sentList: [],
  103. refreshing: false
  104. }
  105. },
  106. computed: {
  107. canGift: function() {
  108. return !!this.selectedRecipient && !!this.selectedCoupon && !this.sending
  109. }
  110. },
  111. onShow: function() {
  112. this.loadAll()
  113. },
  114. filters: {
  115. firstChar: function(name) {
  116. if (!name) return '友'
  117. return name.substring(0, 1)
  118. }
  119. },
  120. methods: {
  121. loadAll: function() {
  122. var self = this
  123. self.refreshing = true
  124. Promise.all([getGiftRecipients(), getGiftableCoupons()]).then(function(res) {
  125. self.recipients = (res[0] && res[0].data) || []
  126. self.coupons = (res[1] && res[1].data) || []
  127. }).catch(function(e) {
  128. console.error('加载可赠送数据失败', e)
  129. }).then(function() {
  130. self.refreshing = false
  131. })
  132. self.loadSent()
  133. },
  134. loadSent: function() {
  135. var self = this
  136. getSentGifts().then(function(res) {
  137. self.sentList = (res && res.data) || []
  138. }).catch(function(e) {
  139. console.error('加载赠送记录失败', e)
  140. })
  141. },
  142. onRefresh: function() {
  143. this.loadAll()
  144. },
  145. switchTab: function(idx) {
  146. this.activeTab = idx
  147. },
  148. pickRecipient: function(r) {
  149. this.selectedRecipient = r
  150. },
  151. pickCoupon: function(c) {
  152. this.selectedCoupon = c
  153. },
  154. doGift: function() {
  155. var self = this
  156. if (!self.canGift) return
  157. self.sending = true
  158. giftCoupon(self.selectedRecipient.userId, self.selectedCoupon.id, self.remark).then(function(res) {
  159. if (res && res.code === 200) {
  160. uni.showToast({ title: '赠送成功', icon: 'success' })
  161. self.selectedRecipient = null
  162. self.selectedCoupon = null
  163. self.remark = ''
  164. self.loadAll()
  165. } else {
  166. uni.showToast({ title: (res && res.message) || '赠送失败', icon: 'none' })
  167. }
  168. }).catch(function(e) {
  169. uni.showToast({ title: '赠送失败,请重试', icon: 'none' })
  170. console.error('赠送失败', e)
  171. }).then(function() {
  172. self.sending = false
  173. })
  174. },
  175. doRevoke: function(item) {
  176. var self = this
  177. uni.showModal({
  178. title: '撤回转赠',
  179. content: '确定撤回赠给「' + item.receiverNickname + '」的券吗?',
  180. success: function(m) {
  181. if (!m.confirm) return
  182. revokeGiftCoupon(item.transferId).then(function(res) {
  183. if (res && res.code === 200) {
  184. uni.showToast({ title: '已撤回', icon: 'success' })
  185. self.loadSent()
  186. } else {
  187. uni.showToast({ title: (res && res.message) || '撤回失败', icon: 'none' })
  188. }
  189. }).catch(function(e) {
  190. uni.showToast({ title: '撤回失败', icon: 'none' })
  191. console.error(e)
  192. })
  193. }
  194. })
  195. },
  196. formatValue: function(c) {
  197. if (c.type === 'DISCOUNT') {
  198. return (c.discountRate ? (c.discountRate / 100).toFixed(1) : '9.0') + '折'
  199. }
  200. return '¥' + (c.value ? (c.value / 100).toFixed(2) : '0.00')
  201. },
  202. typeName: function(c) {
  203. if (c.type === 'FIXED') return '满减券'
  204. if (c.type === 'DISCOUNT') return '折扣券'
  205. return '代金券'
  206. },
  207. conditionText: function(minSpend) {
  208. if (!minSpend || minSpend <= 0) return '无门槛'
  209. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  210. },
  211. formatDate: function(d) {
  212. if (!d) return '长期有效'
  213. var dt = new Date(d)
  214. if (isNaN(dt.getTime())) return String(d)
  215. var m = dt.getMonth() + 1
  216. var day = dt.getDate()
  217. return dt.getFullYear() + '-' + (m < 10 ? '0' + m : m) + '-' + (day < 10 ? '0' + day : day)
  218. },
  219. statusLabel: function(status) {
  220. if (status === 'GIFTED') return '待使用'
  221. if (status === 'USED') return '已使用'
  222. if (status === 'REVOKED') return '已撤回'
  223. return status
  224. }
  225. }
  226. }
  227. </script>
  228. <style scoped>
  229. .page {
  230. display: flex;
  231. flex-direction: column;
  232. height: 100vh;
  233. background: #f5f6fa;
  234. }
  235. .tab-bar {
  236. display: flex;
  237. background: #fff;
  238. border-bottom: 1px solid #eee;
  239. }
  240. .tab-item {
  241. flex: 1;
  242. text-align: center;
  243. padding: 24rpx 0;
  244. font-size: 30rpx;
  245. color: #888;
  246. }
  247. .tab-item.active {
  248. color: #ff6b35;
  249. font-weight: bold;
  250. border-bottom: 4rpx solid #ff6b35;
  251. }
  252. .content {
  253. flex: 1;
  254. padding: 24rpx;
  255. }
  256. .tip {
  257. background: #fff7f2;
  258. color: #ff6b35;
  259. font-size: 24rpx;
  260. padding: 18rpx 22rpx;
  261. border-radius: 12rpx;
  262. margin-bottom: 24rpx;
  263. }
  264. .section {
  265. background: #fff;
  266. border-radius: 16rpx;
  267. padding: 24rpx;
  268. margin-bottom: 24rpx;
  269. }
  270. .section-title {
  271. font-size: 30rpx;
  272. font-weight: bold;
  273. margin-bottom: 20rpx;
  274. color: #333;
  275. }
  276. .empty-tip {
  277. font-size: 26rpx;
  278. color: #aaa;
  279. text-align: center;
  280. padding: 30rpx 0;
  281. }
  282. .recipient-item,
  283. .coupon-item {
  284. display: flex;
  285. align-items: center;
  286. padding: 20rpx;
  287. border: 2rpx solid #eee;
  288. border-radius: 12rpx;
  289. margin-bottom: 16rpx;
  290. position: relative;
  291. }
  292. .recipient-item.selected,
  293. .coupon-item.selected {
  294. border-color: #ff6b35;
  295. background: #fff7f2;
  296. }
  297. .avatar {
  298. width: 72rpx;
  299. height: 72rpx;
  300. border-radius: 50%;
  301. margin-right: 18rpx;
  302. }
  303. .avatar-default {
  304. background: #ff6b35;
  305. color: #fff;
  306. font-size: 30rpx;
  307. text-align: center;
  308. line-height: 72rpx;
  309. }
  310. .recipient-info {
  311. display: flex;
  312. flex-direction: column;
  313. flex: 1;
  314. }
  315. .nick {
  316. font-size: 28rpx;
  317. color: #333;
  318. }
  319. .level-tag {
  320. font-size: 22rpx;
  321. color: #ff6b35;
  322. margin-top: 6rpx;
  323. }
  324. .coupon-left {
  325. width: 140rpx;
  326. display: flex;
  327. flex-direction: column;
  328. align-items: center;
  329. justify-content: center;
  330. border-right: 2rpx dashed #eee;
  331. padding-right: 16rpx;
  332. }
  333. .coupon-value {
  334. font-size: 36rpx;
  335. color: #ff6b35;
  336. font-weight: bold;
  337. }
  338. .coupon-type {
  339. font-size: 22rpx;
  340. color: #999;
  341. margin-top: 6rpx;
  342. }
  343. .coupon-body {
  344. flex: 1;
  345. padding-left: 20rpx;
  346. display: flex;
  347. flex-direction: column;
  348. }
  349. .coupon-name {
  350. font-size: 28rpx;
  351. color: #333;
  352. }
  353. .coupon-cond,
  354. .coupon-exp {
  355. font-size: 22rpx;
  356. color: #999;
  357. margin-top: 6rpx;
  358. }
  359. .check {
  360. color: #ff6b35;
  361. font-size: 36rpx;
  362. font-weight: bold;
  363. margin-left: 12rpx;
  364. }
  365. .remark-input {
  366. background: #f7f7f7;
  367. border-radius: 10rpx;
  368. padding: 18rpx 20rpx;
  369. font-size: 28rpx;
  370. }
  371. .action-bar {
  372. padding: 20rpx 0 40rpx;
  373. }
  374. .gift-btn {
  375. background: #ff6b35;
  376. color: #fff;
  377. font-size: 32rpx;
  378. border-radius: 44rpx;
  379. }
  380. .gift-btn[disabled] {
  381. background: #ffc4ad;
  382. }
  383. .empty-state {
  384. text-align: center;
  385. padding: 120rpx 0;
  386. }
  387. .empty-icon {
  388. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  389. display: block;
  390. }
  391. .empty-text {
  392. font-size: 28rpx;
  393. color: #aaa;
  394. margin-top: 20rpx;
  395. }
  396. .sent-item {
  397. background: #fff;
  398. border-radius: 16rpx;
  399. padding: 24rpx;
  400. margin-bottom: 20rpx;
  401. }
  402. .sent-row {
  403. display: flex;
  404. justify-content: space-between;
  405. align-items: center;
  406. }
  407. .sent-row.sub {
  408. margin-top: 10rpx;
  409. font-size: 24rpx;
  410. color: #999;
  411. }
  412. .sent-name {
  413. font-size: 30rpx;
  414. color: #333;
  415. font-weight: bold;
  416. }
  417. .sent-level {
  418. font-size: 22rpx;
  419. color: #ff6b35;
  420. }
  421. .sent-status {
  422. font-size: 24rpx;
  423. }
  424. .st-GIFTED { color: #ff6b35; }
  425. .st-USED { color: #999; }
  426. .st-REVOKED { color: #bbb; }
  427. .sent-remark {
  428. font-size: 24rpx;
  429. color: #888;
  430. margin-top: 10rpx;
  431. }
  432. .revoke-btn {
  433. margin-top: 18rpx;
  434. background: #fff;
  435. color: #ff6b35;
  436. border: 2rpx solid #ff6b35;
  437. font-size: 26rpx;
  438. border-radius: 30rpx;
  439. padding: 4rpx 0;
  440. }
  441. </style>