withdraw.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 ls from "@/utils/store2";
  53. onLoad((options) => {
  54. userBalance.value = options.allAmount
  55. })
  56. const userBalance = ref(0);
  57. const router = useRouter();
  58. const { t } = useI18n();
  59. const userStore = useUserStore();
  60. const userInfo = computed(() => userStore.userInfo);
  61. const formRef = ref();
  62. const form = ref<{
  63. emailCode: string;
  64. cardNo?: string;
  65. password?: string;
  66. country?: string;
  67. email?: string;
  68. }>({
  69. emailCode: "",
  70. });
  71. const text1 = ref("");
  72. const blockchainList = ref([]);
  73. const timer = ref(59);
  74. const getCodeString = ref("");
  75. const interval = ref<NodeJS.Timeout | null>(null);
  76. // 表单验证规则
  77. const rules = {
  78. blockchain: [Validators.required(t("Blockchain.addP2"))],
  79. address: [Validators.required(t("WalletApply.p2"))],
  80. amount: [
  81. Validators.required(t("global.validator.v15")),
  82. Validators.custom(validateAmount),
  83. ],
  84. };
  85. function validateAmount(a: any, b?: any, c?: any) {
  86. if (typeof c === "function") {
  87. const value = b;
  88. const callback = c;
  89. const val = String(value ?? "").trim();
  90. const num = Number(val);
  91. const pattern = /^(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?!0+$)\d{1,2})$/;
  92. if (!pattern.test(val)) {
  93. callback(new Error(t("global.validator.v15")));
  94. return;
  95. }
  96. if (isNaN(num) || num <= 0) {
  97. callback(new Error(t("global.validator.v15")));
  98. return;
  99. }
  100. const balance = Number(form.value.userBalance);
  101. if (!isNaN(balance) && num > balance) {
  102. const msg = t("WalletApply.p6", { userBalance: form.value.userBalance });
  103. callback(new Error(msg));
  104. return;
  105. }
  106. return callback();
  107. }
  108. }
  109. function btnClick() {
  110. showSuccessPrompt.value = false;
  111. router.push({ path: "/pages/wallet/index" })
  112. }
  113. // 初始化表单
  114. function initForm() {
  115. const a = { uniqueId: userInfo.value.uniqueId, country: userInfo.value.country, email: userInfo.value.email }
  116. if (userInfo.value) {
  117. form.value = {
  118. ...a,
  119. userBalance: userBalance.value,
  120. emailCode: "",
  121. };
  122. }
  123. text1.value = "";
  124. timer.value = 59;
  125. getCodeString.value = t("newSignup.item11");
  126. if (interval.value) {
  127. clearInterval(interval.value);
  128. interval.value = null;
  129. }
  130. }
  131. // 初始化定时器
  132. function initTimer() {
  133. const savedTimer = Number(ls.get("cvvTimer")) || 59;
  134. if (savedTimer === 59) {
  135. getCodeString.value = t("newSignup.item11");
  136. } else {
  137. timer.value = savedTimer;
  138. startTimer();
  139. }
  140. }
  141. // 开始倒计时
  142. function startTimer() {
  143. if (interval.value) {
  144. return;
  145. }
  146. getCodeString.value = `${t("signup.form.waitCode1")}${timer.value}${t(
  147. "signup.form.waitCode2"
  148. )}`;
  149. interval.value = setInterval(() => {
  150. timer.value--;
  151. localStorage.setItem("cvvTimer", timer.value.toString());
  152. if (timer.value > 0) {
  153. getCodeString.value = `${t("signup.form.waitCode1")}${timer.value}${t(
  154. "signup.form.waitCode2"
  155. )}`;
  156. } else {
  157. getCodeString.value = t("newSignup.item11");
  158. if (interval.value) {
  159. clearInterval(interval.value);
  160. interval.value = null;
  161. }
  162. timer.value = 59;
  163. localStorage.setItem("cvvTimer", "59");
  164. }
  165. }, 1000);
  166. }
  167. const showSuccessPrompt = ref(false);
  168. // 提交表单
  169. async function infoSubmit() {
  170. try {
  171. await formRef.value?.validate();
  172. let res = await ucardApi.getBlockchainWithdrawApply({
  173. ...form.value,
  174. });
  175. if (res.code == 200) {
  176. showSuccessPrompt.value = true;
  177. } else {
  178. showToast(res.msg);
  179. }
  180. } catch (error) {
  181. console.log(error, 1111);
  182. }
  183. }
  184. //获取区块链
  185. async function getBlockchainDropdown() {
  186. let res = await ucardApi.getBlockchainDropdown({
  187. ...form.value,
  188. type: '2'
  189. });
  190. if (res.code == 200) {
  191. res.data.map((item: any) => {
  192. item.text = item.alias;
  193. item.value = item.blockchain;
  194. });
  195. blockchainList.value = res.data;
  196. } else {
  197. blockchainList.value = []
  198. showToast(res.msg);
  199. }
  200. }
  201. // 发送邮箱验证码
  202. async function sendEmailCode() {
  203. try {
  204. if (!form.value.country) {
  205. showToast(t("vaildate.country.empty"));
  206. return false;
  207. }
  208. if (!form.value.email) {
  209. showToast(t("vaildate.email.empty"));
  210. return false;
  211. }
  212. const res = await ucardApi.getBlockchainWithdrawSendEmailCode({
  213. ...form.value,
  214. });
  215. if (res.code === 200) {
  216. showToast(t("Msg.CodeSuccess"));
  217. startTimer();
  218. return true;
  219. } else {
  220. showToast(t("Msg.CodeFail"));
  221. return false;
  222. }
  223. } catch (error: any) {
  224. console.log(error, 12);
  225. showToast(t("Msg.CodeFail"));
  226. return false;
  227. }
  228. }
  229. // 获取验证码按钮点击
  230. async function handleGetCode() {
  231. if (timer.value > 0 && timer.value < 59) {
  232. return;
  233. }
  234. text1.value = "";
  235. await sendEmailCode();
  236. }
  237. // 表单字段变化
  238. function handleChange(value: any) {
  239. if (value.key === "emailCode") {
  240. form.value.emailCode = value.value;
  241. }
  242. if (value.key === "blockchain") {
  243. changeBlockchain(value.value);
  244. }
  245. }
  246. function changeBlockchain(e) {
  247. blockchainList.value.forEach(item => {
  248. if (item.blockchain === e) {
  249. form.value.receivedCurrency = item.alias;
  250. form.value.exchangeRate = item.exchangeRate;
  251. }
  252. });
  253. if (form.value.amount) {
  254. globalExchangeRate();
  255. }
  256. }
  257. function globalExchangeRate() {
  258. const amount = Number(form.value.amount);
  259. formRef.value.validateField(['amount'], (valid) => {
  260. if (valid.length > 0) {
  261. form.value.receivedAmount = '';
  262. return
  263. }
  264. if (amount && form.value.exchangeRate) {
  265. const receivedAmount = parseFloat((amount * form.value.exchangeRate).toFixed(4));
  266. form.value.receivedAmount = receivedAmount;
  267. } else {
  268. form.value.receivedAmount = '';
  269. }
  270. });
  271. }
  272. onMounted(() => {
  273. initForm();
  274. initTimer();
  275. getBlockchainDropdown();
  276. });
  277. // 组件卸载时清理
  278. onBeforeUnmount(() => {
  279. if (interval.value) {
  280. clearInterval(interval.value);
  281. interval.value = null;
  282. }
  283. });
  284. </script>
  285. <style lang="scss" scoped>
  286. @import "@/uni.scss";
  287. .no-button {
  288. width: 100%;
  289. margin: px2rpx(12) 0;
  290. .u-button {
  291. background-color: #ffbdc8 !important;
  292. }
  293. }
  294. .code-input-label {
  295. font-size: var(--font-size-16);
  296. line-height: px2rpx(44);
  297. letter-spacing: px2rpx(1);
  298. color: #474747;
  299. }
  300. .code-input-wrapper {
  301. position: relative;
  302. display: flex;
  303. align-items: center;
  304. }
  305. .code-input {
  306. flex: 1;
  307. }
  308. .get-code-btn1 {
  309. min-width: px2rpx(100);
  310. margin-left: px2rpx(8);
  311. .ok-button {
  312. .u-button {
  313. background-color: #fff;
  314. opacity: 0;
  315. }
  316. }
  317. }
  318. .get-code-btn {
  319. position: absolute;
  320. right: 0;
  321. bottom: px2rpx(12);
  322. min-width: px2rpx(100);
  323. background-color: #fff;
  324. z-index: 1;
  325. margin-left: px2rpx(8);
  326. .cwg-button {
  327. margin: 0;
  328. }
  329. .cwg-button .u-button {
  330. border-radius: px2rpx(8);
  331. height: px2rpx(46) !important;
  332. }
  333. }
  334. .submit-section {
  335. margin: px2rpx(20) 0;
  336. }
  337. .submit-btn {
  338. width: 100%;
  339. }
  340. </style>