withdraw.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <cwg-page-wrapper>
  3. <view class="page">
  4. <u-form ref="formRef" :rules="rules" :model="form" class="payment-form">
  5. <cwg-input v-model:value="form.blockchain" fkey="blockchain" type="select" :required="true"
  6. :columns="blockchainList" :label="t('Blockchain.addP2')" rulesKey="blockchain"
  7. @change="handleChange" />
  8. <cwg-input :label="t('WalletApply.p1')" v-model:value="form.address" fkey="address" type="text"
  9. rulesKey="address" :placeholder="t('WalletApply.p2')" :required="true" clearable />
  10. <cwg-input :label="`${t('WalletApply.p3')} ${t('WalletApply.p5', { userBalance: form.userBalance })}`"
  11. rulesKey="amount" v-model:value="form.amount" fkey="amount" type="number"
  12. :placeholder="t('WalletApply.p4')" :required="true" @change="globalExchangeRate" clearable />
  13. <view>
  14. <view class="code-input-wrapper">
  15. <view class="code-input">
  16. <cwg-input v-model:value="form.emailCode" :label="t('newSignup.item9')" fkey="emailCode"
  17. type="text" :required="true" rulesKey="emailCode" :placeholder="t('newSignup.item10')"
  18. @change="handleChange" />
  19. </view>
  20. <view class="get-code-btn1">
  21. <view class="ok-button">
  22. <u-button type="primary" block>{{ getCodeString }}</u-button>
  23. </view>
  24. </view>
  25. <view class="get-code-btn">
  26. <view class="cwg-button">
  27. <u-button type="primary" block @click="handleGetCode">{{ getCodeString }}</u-button>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </u-form>
  33. <view class="fixed-btn">
  34. <view class="cwg-button">
  35. <u-button type="primary" block @click="infoSubmit">{{ t("card.Btn.Submit") }}</u-button>
  36. </view>
  37. </view>
  38. </view>
  39. <cwg-SuccessPrompt v-model:show="showSuccessPrompt" :title="t('pages.wallet.withdraw')"
  40. :desc="t('Custom.Withdraw.Des1')" :btn-click="btnClick" />
  41. </cwg-page-wrapper>
  42. </template>
  43. <script setup lang="ts">
  44. import { ref, onMounted, watch, computed, onBeforeUnmount } from "vue";
  45. import { showToast } from "@/utils/toast";
  46. import { useI18n } from "vue-i18n";
  47. import { ucardApi } from "@/api/ucard";
  48. import useUserStore from "@/stores/use-user-store";
  49. import useRouter from "@/hooks/useRouter";
  50. import { Validators } from "@/utils/validators";
  51. import { onLoad } from '@dcloudio/uni-app'
  52. import { useEmailCountdown } from '@/hooks/useEmailCountdown';
  53. const {
  54. time,
  55. text: getCodeString,
  56. canSend,
  57. start,
  58. restore
  59. } = useEmailCountdown()
  60. onLoad((options) => {
  61. userBalance.value = options.allAmount
  62. })
  63. const userBalance = ref(0);
  64. const router = useRouter();
  65. const { t } = useI18n();
  66. const userStore = useUserStore();
  67. const userInfo = computed(() => userStore.userInfo);
  68. const formRef = ref();
  69. const form = ref<{
  70. emailCode: string;
  71. cardNumber?: string;
  72. password?: string;
  73. country?: string;
  74. email?: string;
  75. }>({
  76. emailCode: "",
  77. });
  78. const text1 = ref("");
  79. const blockchainList = ref([]);
  80. // 表单验证规则
  81. const rules = {
  82. blockchain: [Validators.required(t("Blockchain.addP2"))],
  83. address: [Validators.required(t("WalletApply.p2"))],
  84. amount: [
  85. Validators.required(t("global.validator.v15")),
  86. Validators.custom(validateAmount),
  87. ],
  88. };
  89. function validateAmount(a: any, b?: any, c?: any) {
  90. if (typeof c === "function") {
  91. const value = b;
  92. const callback = c;
  93. const val = String(value ?? "").trim();
  94. const num = Number(val);
  95. const pattern = /^(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?!0+$)\d{1,2})$/;
  96. if (!pattern.test(val)) {
  97. callback(new Error(t("global.validator.v15")));
  98. return;
  99. }
  100. if (isNaN(num) || num <= 0) {
  101. callback(new Error(t("global.validator.v15")));
  102. return;
  103. }
  104. const balance = Number(form.value.userBalance);
  105. if (!isNaN(balance) && num > balance) {
  106. const msg = t("WalletApply.p6", { userBalance: form.value.userBalance });
  107. callback(new Error(msg));
  108. return;
  109. }
  110. return callback();
  111. }
  112. }
  113. function btnClick() {
  114. showSuccessPrompt.value = false;
  115. router.push({ path: "/pages/wallet/index" })
  116. }
  117. // 初始化表单
  118. function initForm() {
  119. const a = { uniqueId: userInfo.value.uniqueId, country: userInfo.value.country, email: userInfo.value.email }
  120. if (userInfo.value) {
  121. form.value = {
  122. ...a,
  123. userBalance: userBalance.value,
  124. emailCode: "",
  125. };
  126. }
  127. text1.value = "";
  128. }
  129. const showSuccessPrompt = ref(false);
  130. // 提交表单
  131. async function infoSubmit() {
  132. try {
  133. await formRef.value?.validate();
  134. let res = await ucardApi.getBlockchainWithdrawApply({
  135. ...form.value,
  136. });
  137. if (res.code == 200) {
  138. showSuccessPrompt.value = true;
  139. } else {
  140. showToast(res.msg);
  141. }
  142. } catch (error) {
  143. console.log(error, 1111);
  144. }
  145. }
  146. //获取区块链
  147. async function getBlockchainDropdown() {
  148. let res = await ucardApi.getBlockchainDropdown({
  149. ...form.value,
  150. type: '2'
  151. });
  152. if (res.code == 200) {
  153. res.data.map((item: any) => {
  154. item.text = item.alias;
  155. item.value = item.blockchain;
  156. });
  157. blockchainList.value = res.data;
  158. } else {
  159. blockchainList.value = []
  160. showToast(res.msg);
  161. }
  162. }
  163. // 发送邮箱验证码
  164. async function sendEmailCode() {
  165. try {
  166. if (!form.value.country) {
  167. showToast(t("vaildate.country.empty"));
  168. return false;
  169. }
  170. if (!form.value.email) {
  171. showToast(t("vaildate.email.empty"));
  172. return false;
  173. }
  174. const res = await ucardApi.getBlockchainWithdrawSendEmailCode({
  175. ...form.value,
  176. });
  177. if (res.code === 200) {
  178. showToast(t("Msg.CodeSuccess"));
  179. start()
  180. return true;
  181. } else {
  182. showToast(t("Msg.CodeFail"));
  183. return false;
  184. }
  185. } catch (error: any) {
  186. console.log(error, 12);
  187. showToast(t("Msg.CodeFail"));
  188. return false;
  189. }
  190. }
  191. // 获取验证码按钮点击
  192. async function handleGetCode() {
  193. if (!canSend.value) return
  194. text1.value = "";
  195. await sendEmailCode();
  196. }
  197. // 表单字段变化
  198. function handleChange(value: any) {
  199. if (value.key === "emailCode") {
  200. form.value.emailCode = value.value;
  201. }
  202. if (value.key === "blockchain") {
  203. changeBlockchain(value.value);
  204. }
  205. }
  206. function changeBlockchain(e) {
  207. blockchainList.value.forEach(item => {
  208. if (item.blockchain === e) {
  209. form.value.receivedCurrency = item.alias;
  210. form.value.exchangeRate = item.exchangeRate;
  211. }
  212. });
  213. if (form.value.amount) {
  214. globalExchangeRate();
  215. }
  216. }
  217. function globalExchangeRate() {
  218. const amount = Number(form.value.amount);
  219. formRef.value.validateField(['amount'], (valid) => {
  220. if (valid.length > 0) {
  221. form.value.receivedAmount = '';
  222. return
  223. }
  224. if (amount && form.value.exchangeRate) {
  225. const receivedAmount = parseFloat((amount * form.value.exchangeRate).toFixed(4));
  226. form.value.receivedAmount = receivedAmount;
  227. } else {
  228. form.value.receivedAmount = '';
  229. }
  230. });
  231. }
  232. onMounted(() => {
  233. console.log(3333);
  234. initForm();
  235. restore()
  236. getBlockchainDropdown();
  237. });
  238. </script>
  239. <style lang="scss" scoped>
  240. @import "@/uni.scss";
  241. .no-button {
  242. width: 100%;
  243. margin: px2rpx(12) 0;
  244. .u-button {
  245. background-color: #ffbdc8 !important;
  246. }
  247. }
  248. .code-input-label {
  249. font-size: var(--font-size-16);
  250. line-height: px2rpx(44);
  251. letter-spacing: px2rpx(1);
  252. color: #474747;
  253. }
  254. .code-input-wrapper {
  255. position: relative;
  256. display: flex;
  257. align-items: center;
  258. }
  259. .code-input {
  260. flex: 1;
  261. }
  262. .get-code-btn1 {
  263. min-width: px2rpx(100);
  264. margin-left: px2rpx(8);
  265. .ok-button {
  266. .u-button {
  267. background-color: #fff;
  268. opacity: 0;
  269. }
  270. }
  271. }
  272. .get-code-btn {
  273. position: absolute;
  274. right: 0;
  275. bottom: px2rpx(12);
  276. min-width: px2rpx(100);
  277. background-color: #fff;
  278. z-index: 1;
  279. margin-left: px2rpx(8);
  280. .cwg-button {
  281. margin: 0;
  282. }
  283. .cwg-button .u-button {
  284. border-radius: px2rpx(8);
  285. height: px2rpx(46) !important;
  286. }
  287. }
  288. .submit-section {
  289. margin: px2rpx(20) 0;
  290. }
  291. .submit-btn {
  292. width: 100%;
  293. }
  294. </style>