duihuanma.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="icon-title-navBar-box1">
  3. <view @click="handleBack" class="nav-bar-icon"></view>
  4. <text class="nav-bar-title">使用兑换码</text>
  5. </view>
  6. <view>
  7. <view class="tip-content-box">
  8. <icon class="head-img-box" :style="{backgroundImage: 'url(' + myInfoData.icon + ')'}"></icon>
  9. <view>{{ myInfoData.nickName }}</view>
  10. <view>{{ myInfoData.userName }}</view>
  11. <input class="duihuan-input" type="text" focus v-model="duihuamaValue" placeholder="请输入兑换码" />
  12. <view class="tip-btn-box">
  13. <view class="confirm-btn" @click="confirmBtn">兑换码</view>
  14. </view>
  15. </view>
  16. </view>
  17. <!-- 失败 -->
  18. <duihuanError ref="dhErrRef"></duihuanError>
  19. <!-- 成功 -->
  20. <duihuanSuccess ref="dhSucRef"></duihuanSuccess>
  21. </template>
  22. <script setup>
  23. import {
  24. ref,reactive
  25. } from 'vue';
  26. import duihuanError from "./components/duihuanma/duihuanError";
  27. import duihuanSuccess from "./components/duihuanma/duihuanSuccess";
  28. import {
  29. duihuanmaCodeNew,
  30. } from '@/api/my.js'
  31. import cacheManager from '@/utils/cacheManager';
  32. import {toast} from "@/utils/common";
  33. import {myInfo} from "@/api/my";
  34. import {
  35. onLoad,
  36. onShow
  37. } from '@dcloudio/uni-app';
  38. const duihuamaValue = ref(''); // 索引
  39. const dhErrRef = ref(null);
  40. const dhSucRef = ref(null);
  41. const myInfoData = reactive({
  42. userName: '',
  43. nickName: '',
  44. icon: '',
  45. });
  46. onLoad(() => {
  47. getMyInfo();
  48. })
  49. // 获取用户数据
  50. function getMyInfo() {
  51. myInfo({}).then(res => {
  52. myInfoData.userName = res.data.userName;
  53. myInfoData.nickName = res.data.nickName;
  54. if (res.data.nickName) {
  55. myInfoData.nickName = res.data.nickName;
  56. } else {
  57. myInfoData.nickName = '鹅状元';
  58. }
  59. if (res.data.icon) {
  60. myInfoData.icon = res.data.icon;
  61. } else {
  62. getUserImg(res.data.growthType)
  63. }
  64. })
  65. }
  66. // 获取用户头像
  67. function getUserImg(data) {
  68. switch (data) {
  69. case 0:
  70. myInfoData.icon = 'static/images/my/head-img0.png'
  71. break;
  72. case 1:
  73. myInfoData.icon = 'static/images/my/head-img1.png'
  74. break;
  75. case 2:
  76. myInfoData.icon = 'static/images/my/head-img2.png'
  77. break;
  78. case 3:
  79. myInfoData.icon = 'static/images/my/head-img3.png'
  80. break;
  81. default:
  82. myInfoData.icon = 'static/images/my/head-unlogin-img.png'
  83. break;
  84. }
  85. }
  86. function handleBack() {
  87. uni.navigateBack()
  88. }
  89. // 确认
  90. function confirmBtn() {
  91. if (!duihuamaValue.value) {
  92. toast('请输入兑换码')
  93. return;
  94. }
  95. let req = {
  96. code: duihuamaValue.value
  97. }
  98. duihuanmaCodeNew(req).then(res => {
  99. if (res.code == 0) {
  100. // toast('兑换成功')
  101. // 清空缓存
  102. cacheManager.remove("contentInfo");
  103. duihuamaValue.value = '';
  104. dhSucRef.value.handleShow()
  105. } else {
  106. // toast('兑换失败请重试或联系管理员')
  107. dhErrRef.value.handleShow()
  108. }
  109. }).catch(err=> {
  110. toast('兑换失败请重试或联系管理员')
  111. })
  112. }
  113. </script>
  114. <style>
  115. </style>