myCoupon.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="content">
  3. <z-paging-swiper>
  4. <u-navbar
  5. slot="top"
  6. title-color="#fff"
  7. :custom-back="customBack"
  8. :border-bottom="false"
  9. back-icon-color="#CCE8FF"
  10. :background="{ background: '#008CFF' }"
  11. title="我的优惠券"
  12. >
  13. <view class="navbar-font" slot="right" @click="jumpPage('/pages/center/coupon/couponRules/couponRules')"> 规则 </view>
  14. </u-navbar>
  15. <z-tabs ref="tabs" slot="top" :list="tabList" :current="current" barWidth="90rpx" @change="tabsChange"> </z-tabs>
  16. <swiper class="swiper" :current="current" @transition="swiperTransition" @animationfinish="swiperAnimationfinish">
  17. <swiper-item class="swiper-item" v-for="(item, index) in tabList" :key="index">
  18. <!-- <view class="search-part" v-if="current === 0">
  19. <u-input class="search-part-input" v-model="form.exchangeCode" placeholder="请输入兑换码" />
  20. <u-button class="search-part-btn" type="primary" shape="circle" size="medium" @click.stop="handleExchange">兑换</u-button>
  21. </view> -->
  22. <coupon-swiper-list-item :tabIndex="index" :currentIndex="current" ref="swiperListItem" />
  23. </swiper-item>
  24. </swiper>
  25. <!-- 绑定车牌 -->
  26. <u-popup v-model="bindVehiclePop.showChangeVehicleNo" :mask-close-able="false" mode="center" border-radius="20" width="700rpx">
  27. <view class="popup-vehicleNo">
  28. <view class="popup-vehicleNo-title">绑定车牌</view>
  29. <view class="popup-vehicleNo-center"></view>
  30. <view class="popup-vehicleNo-content">
  31. <view class="new-plate-number">
  32. <view class="message-input-wrap" @click="messageInputClick">
  33. <u-message-input :maxlength="8" width="60" font-size="40" :disabled-keyboard="true" v-model="form.vehicleNo" />
  34. </view>
  35. </view>
  36. </view>
  37. <view class="popup-vehicleNo-select">
  38. <u-collapse ref="refValue" :head-style="{ fontSize: '28rpx' }">
  39. <u-collapse-item title="点击选择车牌" align="center">
  40. <scroll-view class="popup-vehicleNo-select-scroll" scroll-y="true">
  41. <u-cell-group v-if="bindVehiclePop.vehicleList.length">
  42. <u-cell-item :title="item.value" v-for="(item, index) in bindVehiclePop.vehicleList" :key="index" :arrow="false">
  43. <u-radio-group v-model="form.vehicleNo" @change="radioGroupChange">
  44. <u-radio :name="item.value" :key="index"></u-radio>
  45. </u-radio-group>
  46. </u-cell-item>
  47. </u-cell-group>
  48. <template v-else>
  49. <view class="popup-vehicleNo-select-empty">暂无绑定车牌</view>
  50. </template>
  51. </scroll-view>
  52. </u-collapse-item>
  53. </u-collapse>
  54. </view>
  55. <view class="vehicleNo-btn">
  56. <u-button type="primary" :disabled="!form.vehicleNo" :loading="bindVehiclePop.loading" @click="handleBindVehicle">确认</u-button>
  57. <u-button type="primary" plain @click="cencelBindVehicle">取消</u-button>
  58. </view>
  59. </view>
  60. </u-popup>
  61. <!-- 选择颜色 -->
  62. <u-action-sheet :list="bindVehiclePop.colorList" @click="confirmColor" v-model="bindVehiclePop.colorShow" />
  63. <!-- 车牌号键盘 -->
  64. <u-keyboard
  65. ref="uKeyboard"
  66. mode="car"
  67. @change="keyboardChange"
  68. @confirm="keyboardConfirm"
  69. @backspace="backspace"
  70. v-model="bindVehiclePop.keyboardshow"
  71. />
  72. </z-paging-swiper>
  73. <u-toast ref="uToast" />
  74. </view>
  75. </template>
  76. <script>
  77. export default {
  78. data() {
  79. return {
  80. tabList: [
  81. {
  82. name: '未使用',
  83. value: 0,
  84. list: []
  85. },
  86. {
  87. name: '已使用',
  88. value: 1,
  89. list: []
  90. },
  91. {
  92. name: '已失效',
  93. value: 2,
  94. list: []
  95. }
  96. ],
  97. current: 0,
  98. form: {
  99. exchangeCode: '',
  100. source: 1,
  101. vehicleNo: '',
  102. merchantCode: '',
  103. qrcodeNo: '',
  104. couponId: ''
  105. },
  106. // 绑定车牌
  107. bindVehiclePop: {
  108. show: false,
  109. vehicleList: [],
  110. showChangeVehicleNo: false,
  111. colorShow: false,
  112. loading: false,
  113. colorList: [
  114. {
  115. text: '蓝色',
  116. colorCode: 0
  117. },
  118. {
  119. text: '黄色',
  120. colorCode: 1
  121. },
  122. {
  123. text: '黑色',
  124. colorCode: 2
  125. },
  126. {
  127. text: '白色',
  128. colorCode: 3
  129. },
  130. {
  131. text: '绿色',
  132. colorCode: 4
  133. },
  134. {
  135. text: '其他',
  136. colorCode: 99
  137. }
  138. ],
  139. keyboardshow: false
  140. }
  141. };
  142. },
  143. onLoad(options) {
  144. const { merchantCode, qrcodeNo, couponId } = options;
  145. if (merchantCode && qrcodeNo && couponId) {
  146. this.form.merchantCode = merchantCode;
  147. this.form.couponId = couponId;
  148. this.form.qrcodeNo = qrcodeNo;
  149. this.bindVehiclePop.showChangeVehicleNo = true;
  150. this.getCarsList();
  151. }
  152. },
  153. methods: {
  154. // tabs通知swiper切换
  155. tabsChange(index) {
  156. this.current = index;
  157. },
  158. // swiper滑动中
  159. swiperTransition(e) {
  160. this.$refs.tabs.setDx(e.detail.dx);
  161. },
  162. //swiper滑动结束
  163. swiperAnimationfinish(e) {
  164. this.current = e.detail.current;
  165. this.$refs.tabs.unlockDx();
  166. },
  167. /**
  168. * 兑换
  169. * @date 2022-12-23
  170. * @returns {any}
  171. */
  172. handleExchange() {
  173. if (this.form.exchangeCode) {
  174. this.bindVehiclePop.showChangeVehicleNo = true;
  175. this.getCarsList();
  176. } else {
  177. this.$refs.uToast.show({
  178. title: '请输入兑换码',
  179. type: 'warning'
  180. });
  181. }
  182. },
  183. /**
  184. * @description: 获取我的车辆列表
  185. * @return {*}
  186. */
  187. async getCarsList() {
  188. const { data } = await this.$u.api.getMycars({ pageNum: 1, pageSize: 100 });
  189. const vehicleList = [];
  190. data.rows.forEach((item) => {
  191. const { vehicleNo } = item;
  192. vehicleList.push({ label: vehicleNo, value: vehicleNo });
  193. });
  194. this.bindVehiclePop.vehicleList = vehicleList;
  195. await this.$nextTick();
  196. this.$refs.refValue.init();
  197. },
  198. /**
  199. * 兑换优惠券
  200. * @date 2022-12-23
  201. * @returns {any}
  202. */
  203. async exchangeCoupon() {
  204. const { vehicleNo } = this.form;
  205. const reg =
  206. /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$/;
  207. if (reg.test(vehicleNo) && vehicleNo.indexOf('I') === -1) {
  208. try {
  209. this.bindVehiclePop.loading = true;
  210. const { code } = await this.$u.api.exchangeCouponApi({ ...this.form });
  211. if (code === 200) {
  212. this.$refs.uToast.show({
  213. title: '兑换成功!',
  214. type: 'success'
  215. });
  216. this.$refs['swiperListItem'][this.current].reloadData();
  217. setTimeout(() => {
  218. location.href = location.origin + location.pathname;
  219. }, 500);
  220. }
  221. this.bindVehiclePop.loading = false;
  222. } catch (error) {
  223. this.bindVehiclePop.loading = false;
  224. }
  225. } else {
  226. this.$refs.uToast.show({
  227. title: '车牌输入错误,请重新输入!',
  228. type: 'error'
  229. });
  230. }
  231. },
  232. /**
  233. * 点击输入框弹出车牌键盘
  234. * @date 2023-02-22
  235. * @returns {any}
  236. */
  237. messageInputClick() {
  238. this.bindVehiclePop.keyboardshow = true;
  239. this.form.vehicleNo = '';
  240. },
  241. /**
  242. * 颜色下拉确认
  243. * @date 2023-02-22
  244. * @param {any} e
  245. * @returns {any}
  246. */
  247. confirmColor(e) {
  248. this.bindVehiclePop.colorShow = false;
  249. },
  250. /**
  251. * 按键被点击(点击退格键不会触发此事件)
  252. * @date 2023-02-22
  253. * @param {any} val
  254. * @returns {any}
  255. */
  256. keyboardChange(val) {
  257. // 将每次按键的值拼接到value变量中,注意+=写法
  258. this.form.vehicleNo += val;
  259. },
  260. /**
  261. * 退格键被点击
  262. * @date 2023-02-22
  263. * @returns {any}
  264. */
  265. backspace() {
  266. // 删除value的最后一个字符
  267. if (this.form.vehicleNo.length) this.form.vehicleNo = this.form.vehicleNo.substr(0, this.form.vehicleNo.length - 1);
  268. },
  269. /**
  270. * 键盘输入完成后确认
  271. * @date 2023-02-22
  272. * @returns {any}
  273. */
  274. keyboardConfirm() {
  275. this.bindVehiclePop.colorShow = true;
  276. },
  277. /**
  278. * 单选点击
  279. * @date 2023-02-22
  280. * @param {any} e
  281. * @returns {any}
  282. */
  283. radioGroupChange(e) {
  284. this.form.vehicleNo = e;
  285. },
  286. /**
  287. * 关闭绑定弹框
  288. * @date 2023-02-22
  289. * @returns {any}
  290. */
  291. cencelBindVehicle() {
  292. this.form.vehicleNo = '';
  293. this.bindVehiclePop.showChangeVehicleNo = false;
  294. },
  295. /**
  296. * 提交绑定
  297. * @date 2023-02-22
  298. * @returns {any}
  299. */
  300. handleBindVehicle() {
  301. if (this.form.vehicleNo) {
  302. this.exchangeCoupon();
  303. } else {
  304. this.$refs.uToast.show({
  305. title: '请输入或选择车牌号!',
  306. type: 'warning'
  307. });
  308. }
  309. },
  310. /**
  311. * 调整页面
  312. * @date 2023-02-22
  313. * @param {any} url
  314. * @returns {any}
  315. */
  316. jumpPage(url) {
  317. this.$u.route({
  318. url
  319. });
  320. },
  321. /**
  322. * tabbar 返回
  323. * @date 2023-02-22
  324. * @returns {any}
  325. */
  326. customBack() {
  327. this.$u.route({
  328. type: 'switchTab',
  329. url: 'pages/center/index'
  330. });
  331. }
  332. }
  333. };
  334. </script>
  335. <style lang="scss" scoped>
  336. @import './myCoupon.scss';
  337. </style>