consumptionTransfers.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <s-layout class="set-wrap" :title="t('wallet.points_transfer')" :bgStyle="{ color: '#FFF' }">
  3. <uni-forms :model="state.model" :rules="state.rules" validateTrigger="bind" labelPosition="left" border
  4. class="form-box" labelWidth='200' ref="FormRef">
  5. <view class="bg-white ss-p-x-30">
  6. <uni-forms-item name="recipientUserName" :label="t('wallet.recipient')" :required="true"
  7. :error-message="state.usernameErrorMsg">
  8. <uni-easyinput v-model="state.model.recipientUserName" type="recipientUserName"
  9. :placeholder="t('wallet.enter_recipient_username')" :inputBorder="false" :clearable="false" @blur="verifyUsername">
  10. <template v-slot:right>
  11. <view v-if="state.verifyUsername" class="icon">
  12. <image style :src="sheep.$url.static('/static/images/shibai.png')" />
  13. </view>
  14. <view v-else class="icon">
  15. <image :src="sheep.$url.static('/static/images/chenggong.png')" />
  16. </view>
  17. </template>
  18. </uni-easyinput>
  19. </uni-forms-item>
  20. <uni-forms-item name="recipientUserPhone" :label="t('wallet.recipient_phone')" :required="true"
  21. :error-message="state.mobileErrorMsg">
  22. <uni-easyinput v-model="state.model.recipientUserPhone" type="number"
  23. :placeholder="t('wallet.enter_recipient_phone')" :inputBorder="false" :clearable="false"
  24. @blur="verifyPhone" >
  25. <template v-slot:right>
  26. <view v-if="state.verifyMobile" class="icon">
  27. <image style :src="sheep.$url.static('/static/images/shibai.png')" />
  28. </view>
  29. <view v-else class="icon">
  30. <image :src="sheep.$url.static('/static/images/chenggong.png')" />
  31. </view>
  32. </template>
  33. </uni-easyinput>
  34. </uni-forms-item>
  35. <uni-forms-item name="mobile" :label="t('account.phone_number')" class="mobile loginUniFormItem ss-p-t-10"
  36. >
  37. <uni-easyinput v-model="state.model.smsCodeUseReqDTO.mobile" type="smsCodeUseReqDTO"
  38. :placeholder="t('account.enter_phone_number')" :inputBorder="false" :clearable="false" :disabled="true">
  39. <template v-slot:right>
  40. <button class="ss-reset-button code-btn code-btn-start" :disabled="verifyUsername == true || state.verifyMobile == true"
  41. :class="{ 'disabled': verifyUsername == true || state.verifyMobile == true }"
  42. @tap="getSmsCode('consumptionTransfers', state.model.smsCodeUseReqDTO.mobile)">
  43. {{ getSmsTimer('consumptionTransfers') }}
  44. </button>
  45. </template>
  46. </uni-easyinput>
  47. </uni-forms-item>
  48. <uni-forms-item name="smsCodeUseReqDTO.code" :label="t('account.verification_code')" :required="true">
  49. <uni-easyinput v-model="state.model.smsCodeUseReqDTO.code" type="number"
  50. :placeholder="t('account.enter_verification_code')" :inputBorder="false" :clearable="false" >
  51. </uni-easyinput>
  52. </uni-forms-item>
  53. <uni-forms-item name="consumptionPoints" :label="t('wallet.transfer_amount')" :required="true">
  54. <uni-easyinput v-model="state.model.consumptionPoints" type="number"
  55. :placeholder="t('wallet.enter_transfer_amount')" :inputBorder="false" :clearable="false" @input="validateInput" />
  56. </uni-forms-item>
  57. </view>
  58. </uni-forms>
  59. <view class="ss-flex ss-row-center ss-col-center ss-m-t-30">
  60. {{t('wallet.current_transferable_amount')}}<text class="text-red">{{canUseConsumptionPoints}}</text>
  61. <!-- <button class="ss-m-l-10 all-btn " @click="useAllPonints">{{t('common.all')}}</button> -->
  62. </view>
  63. <su-fixed bottom placeholder bg="none">
  64. <view class="footer-box ss-p-20 ss-flex">
  65. <button class="ss-rest-button btn" @tap="onSubmit">{{t('common.confirm')}}</button>
  66. </view>
  67. </su-fixed>
  68. </s-layout>
  69. </template>
  70. <script setup>
  71. import {
  72. computed,
  73. reactive,
  74. onBeforeMount,
  75. ref,
  76. unref,
  77. watch,
  78. nextTick
  79. } from 'vue';
  80. import sheep from '@/sheep';
  81. import {
  82. clone
  83. } from 'lodash';
  84. import {
  85. onLoad
  86. } from '@dcloudio/uni-app';
  87. import {
  88. points2point
  89. } from '@/sheep/hooks/useGoods';
  90. import {
  91. email
  92. } from '@/sheep/validate/form';
  93. import AuthUtil from '@/sheep/api/member/auth';
  94. import ConsumptionApi from '@/sheep/api/distri/consumption';
  95. import {
  96. showAuthModal,
  97. closeAuthModal,
  98. getSmsCode,
  99. getSmsTimer,
  100. showWalletModal,
  101. colseWalletModal
  102. } from '@/sheep/hooks/useModal';
  103. import { t } from '@/locale'
  104. const userInfo = computed(() => sheep.$store('user').userInfo);
  105. const userWallet = computed(() => sheep.$store('user').userWallet);
  106. const state = reactive({
  107. verifyUsername: true,
  108. verifyMobile:true,
  109. usernameErrorMsg: '',
  110. mobileErrorMsg: '',
  111. model: {
  112. recipientUserName: undefined,
  113. recipientUserId: undefined,
  114. recipientUserPhone: undefined,
  115. consumptionPoints: undefined,
  116. smsCodeUseReqDTO: {
  117. mobile: undefined,
  118. scene: 11,
  119. code: undefined
  120. }
  121. },
  122. rules: {
  123. recipientUserName: {
  124. rules: [{
  125. required: true,
  126. errorMessage: t('wallet.recipient_cannot_be_empty'),
  127. }, ],
  128. },
  129. recipientUserPhone: {
  130. rules: [{
  131. required: true,
  132. errorMessage: t('wallet.recipient_phone_cannot_be_empty'),
  133. }, ],
  134. },
  135. "smsCodeUseReqDTO.code": {
  136. rules: [{
  137. required: true,
  138. errorMessage: t('wallet.verification_code_cannot_be_empty'),
  139. }, ],
  140. },
  141. consumptionPoints: {
  142. rules: [{
  143. required: true,
  144. errorMessage: t('wallet.transfer_amount_cannot_be_empty'),
  145. },
  146. {
  147. validateFunction: function (rule, value, data, callback) {
  148. if (value<=0) {
  149. callback(t('wallet.transfer_amount_less_than_zero'));
  150. }
  151. return true;
  152. },
  153. },
  154. ],
  155. },
  156. }
  157. });
  158. const canUseConsumptionPoints = computed(() => points2point(userWallet.value.integralDO.consumptionPoints));
  159. let lastUsername = ref('')
  160. async function verifyUsername(e) {
  161. const username = e.detail.value;
  162. if(username == lastUsername.value) return;
  163. if (username == '') {
  164. state.verifyUsername = true
  165. return false;
  166. }else if (username === userInfo.value.username) {
  167. state.usernameErrorMsg = t('wallet.recipient_cannot_be_self')
  168. return false;
  169. }
  170. lastUsername.value = username
  171. // 提交数据
  172. const {
  173. data
  174. } = await AuthUtil.verifyUsername(username);
  175. if (data) {
  176. state.usernameErrorMsg = t('wallet.user_does_not_exist')
  177. } else {
  178. state.usernameErrorMsg = ''
  179. }
  180. state.verifyUsername = data
  181. }
  182. async function verifyPhone(e) {
  183. const phone = e.detail.value;
  184. if (phone == '' || !state.model.recipientUserName) {
  185. // 为空或者没输入用户名,不调校验
  186. state.verifyMobile = true
  187. return false;
  188. }
  189. // 提交数据
  190. const {
  191. data
  192. } = await AuthUtil.verifyPhone(state.model.recipientUserName, phone);
  193. // false就是已经有这个用户名,可以登录,true是没有,不可以登录
  194. if (!data || data.mobile !== phone ) {
  195. state.mobileErrorMsg = t('wallet.username_phone_mismatch')
  196. state.verifyMobile = true
  197. } else {
  198. state.mobileErrorMsg = ''
  199. state.model.recipientUserId = data.id
  200. state.verifyMobile = false
  201. }
  202. }
  203. async function validateInput(value) {
  204. // 确保输入是整数
  205. const intValue = parseInt(value);
  206. const strPoints = value.toString()
  207. const [integerPart, decimalPart] = strPoints.split('.')
  208. if (decimalPart) {
  209. const points = parseFloat(`${integerPart}.${decimalPart.slice(0, 2)}`);
  210. // 使用 nextTick 确保 DOM 更新
  211. nextTick(() => {
  212. state.model.consumptionPoints = integerPart;
  213. });
  214. }
  215. if (intValue > parseInt(canUseConsumptionPoints.value)) {
  216. nextTick(() => {
  217. state.model.consumptionPoints = parseInt(canUseConsumptionPoints.value);
  218. });
  219. } else {
  220. nextTick(() => {
  221. state.model.consumptionPoints = intValue;
  222. });
  223. }
  224. }
  225. // 提交审核
  226. const FormRef = ref(null);
  227. const onSubmit = async () => {
  228. if(state.model.recipientUserName === userInfo.value.username){
  229. uni.showToast({
  230. title: t('wallet.cannot_transfer_to_self'),
  231. icon: 'none',
  232. duration: 2000
  233. })
  234. return;
  235. }else if (state.verifyUsername ) {
  236. return;
  237. }else if(state.verifyMobile ){
  238. return;
  239. }
  240. // 参数校验
  241. const validate = await unref(FormRef)
  242. .validate()
  243. .catch((error) => {
  244. console.log('error: ', error);
  245. });
  246. if (!validate) {
  247. return;
  248. }
  249. const {
  250. data,
  251. code
  252. } = await ConsumptionApi.createConsumptionLog(state.model);
  253. if (code === 0) {
  254. showWalletModal({msg:t('wallet.transfer_successful')})
  255. uni.$emit('consumptionTransfersComplete');
  256. sheep.$router.redirect('/pages/user/wallet/score')
  257. }
  258. }
  259. const isLogin = computed(() => sheep.$store('user').isLogin);
  260. // 监听到在这个页面登陆,并刷新页面
  261. watch(
  262. () => isLogin.value,
  263. (newVal) => {
  264. if (newVal) {
  265. window.location.reload()
  266. }
  267. }, {
  268. deep: true, // 深度监听
  269. },
  270. );
  271. onLoad(async (options) => {
  272. if (!isLogin.value) {
  273. showAuthModal();
  274. }
  275. state.model.smsCodeUseReqDTO.mobile = userInfo.value.mobile
  276. });
  277. </script>
  278. <style lang="scss" scoped>
  279. .code-btn-start {
  280. width: 158rpx;
  281. height: 56rpx;
  282. line-height: normal;
  283. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  284. border-radius: 28rpx;
  285. font-size: 26rpx;
  286. font-weight: 500;
  287. color: #fff;
  288. }
  289. .disabled {
  290. border: 1px solid #f7f7f7;
  291. background: gainsboro !important;
  292. }
  293. .icon {
  294. display: flex;
  295. align-items: center;
  296. margin-right: 7rpx
  297. }
  298. .icon image {
  299. width: 35rpx;
  300. height: 35rpx;
  301. }
  302. :deep() {
  303. .file-picker__progress {
  304. height: 0 !important;
  305. }
  306. .uni-list-item__content-title {
  307. font-size: 28rpx !important;
  308. color: #333333 !important;
  309. line-height: normal !important;
  310. }
  311. .uni-icons {
  312. font-size: 40rpx !important;
  313. }
  314. .is-disabled {
  315. color: #333333;
  316. }
  317. }
  318. :deep(.disabled) {
  319. opacity: 1;
  320. }
  321. .gender-name {
  322. font-size: 28rpx;
  323. font-weight: 500;
  324. line-height: normal;
  325. color: #333333;
  326. }
  327. .title-box {
  328. font-size: 28rpx;
  329. font-weight: 500;
  330. color: #666666;
  331. line-height: 100rpx;
  332. }
  333. .btn {
  334. width: 710rpx;
  335. height: 80rpx;
  336. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  337. border-radius: 40rpx;
  338. font-size: 30rpx;
  339. font-weight: 500;
  340. color: $white;
  341. }
  342. .btn-two {
  343. width: 310rpx;
  344. height: 80rpx;
  345. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  346. border-radius: 40rpx;
  347. font-size: 30rpx;
  348. font-weight: 500;
  349. color: $white;
  350. }
  351. .radio-dark {
  352. filter: grayscale(100%);
  353. filter: gray;
  354. opacity: 0.4;
  355. }
  356. .content-img {
  357. border-radius: 50%;
  358. }
  359. .header-box-content {
  360. position: relative;
  361. width: 160rpx;
  362. height: 160rpx;
  363. overflow: hidden;
  364. border-radius: 50%;
  365. }
  366. .avatar-action {
  367. position: absolute;
  368. left: 50%;
  369. transform: translateX(-50%);
  370. bottom: 0;
  371. z-index: 1;
  372. width: 160rpx;
  373. height: 46rpx;
  374. background: rgba(#000000, 0.3);
  375. .avatar-action-btn {
  376. width: 160rpx;
  377. height: 46rpx;
  378. font-weight: 500;
  379. font-size: 24rpx;
  380. color: #ffffff;
  381. }
  382. }
  383. // 绑定项
  384. .account-list {
  385. background-color: $white;
  386. height: 100rpx;
  387. padding: 0 20rpx;
  388. .list-img {
  389. width: 40rpx;
  390. height: 40rpx;
  391. margin-right: 10rpx;
  392. }
  393. .list-name {
  394. font-size: 28rpx;
  395. color: #333333;
  396. }
  397. .info {
  398. .avatar {
  399. width: 38rpx;
  400. height: 38rpx;
  401. border-radius: 50%;
  402. overflow: hidden;
  403. }
  404. .name {
  405. font-size: 28rpx;
  406. font-weight: 400;
  407. color: $dark-9;
  408. }
  409. }
  410. .bind-box {
  411. width: 100rpx;
  412. height: 50rpx;
  413. line-height: normal;
  414. display: flex;
  415. justify-content: center;
  416. align-items: center;
  417. font-size: 24rpx;
  418. .bind-btn {
  419. width: 100%;
  420. height: 100%;
  421. border-radius: 25rpx;
  422. background: #f4f4f4;
  423. color: #999999;
  424. }
  425. .relieve-btn {
  426. width: 100%;
  427. height: 100%;
  428. border-radius: 25rpx;
  429. background: var(--ui-BG-Main-opacity-1);
  430. color: var(--ui-BG-Main);
  431. }
  432. }
  433. }
  434. .list-border {
  435. font-size: 28rpx;
  436. font-weight: 400;
  437. color: #333333;
  438. border-bottom: 2rpx solid #eeeeee;
  439. }
  440. image {
  441. width: 100%;
  442. height: 100%;
  443. }
  444. </style>