recharge.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <div class="assets">
  3. <div class="head">
  4. <img @click="back" src="@/assets/images/back.png" class="arrow_img" />
  5. <span>{{ $t('lang138') }}</span>
  6. </div>
  7. <div class="pd">
  8. <div class="">
  9. <div class="total_box">
  10. <div>
  11. <span class="text">{{ $t('lang147') }}</span>
  12. </div>
  13. <div class="total">
  14. <span>{{ Math.floor(assets * 100) / 100 || 0 }}</span>
  15. </div>
  16. <div class="bo">
  17. <!-- <img src="@/assets/images/chance.png" class="icon" />
  18. <span>{{ $t('lang121') }}</span> -->
  19. </div>
  20. <div @click="navigation('rechargeRecord')" class="mx">{{ $t('lang122') }}</div>
  21. </div>
  22. <div class="list">
  23. <div class="box">
  24. <div class="box_item">
  25. <span class="left">{{ $t('lang148') }}</span>
  26. <van-field v-model="amount" :placeholder="$t('lang119')" type="number" class="input" input-align="right" />
  27. </div>
  28. <div class="box_item">
  29. <span class="left">{{ $t('lang124') }}</span>
  30. <span>{{ $t('lang126') }}</span>
  31. </div>
  32. <!-- <div class="box_item">
  33. <span class="left">{{ $t('lang125') }}</span>
  34. <van-field v-model="value" :placeholder="$t('lang118')" class="input" input-align="right" />
  35. </div> -->
  36. </div>
  37. </div>
  38. <div class="buy" @click="submit">
  39. <van-button class="buy_btn" type="primary">{{ $t('lang149') }}</van-button>
  40. </div>
  41. <div class="tips">
  42. <div>{{ $t('lang116') }}</div>
  43. <div>{{ tip }}</div>
  44. </div>
  45. </div>
  46. </div>
  47. <van-overlay :show="show">
  48. <div class="wrapper" @click.stop>
  49. <van-loading type="spinner" class="loading" />
  50. </div>
  51. </van-overlay>
  52. </div>
  53. </template>
  54. <script>
  55. import { homeApi } from '@/api/index';
  56. import { dateFormat } from '@/utils/formatTool.js';
  57. import packApi from '@/methods/pack.js';
  58. import { getErc20Contract } from '@/utils/contractHelp';
  59. import usdt from '@/methods/usdt.js';
  60. import { formatAmount } from '@/utils/format.js';
  61. import { Dialog, Notify, Toast } from 'vant';
  62. export default {
  63. data() {
  64. return {
  65. assets: 0,
  66. amount: '',
  67. payToken: undefined, //支付币种
  68. walletBalance: 0,
  69. show: false,
  70. pay_address: '',
  71. tip: '',
  72. };
  73. },
  74. mounted() {
  75. this.getUserInfo();
  76. this.getAddress();
  77. },
  78. methods: {
  79. //返回上一页
  80. back() {
  81. this.$router.back();
  82. },
  83. navigation(name) {
  84. this.$router.push({ name });
  85. },
  86. getAddress() {
  87. homeApi.getAddress().then(res => {
  88. if (res.code == 200) {
  89. console.log(res.data);
  90. this.pay_address = res.data.value;
  91. this.tip = res.data.name;
  92. } else {
  93. }
  94. });
  95. },
  96. getUserInfo() {
  97. homeApi.getUserInfo().then(res => {
  98. if (res.code == 200) {
  99. this.assets = res.data.balance;
  100. this.account = res.data.address;
  101. this.getPayToken(); //获取支付币种
  102. }
  103. });
  104. },
  105. getPayToken() {
  106. packApi.getPayToken(this.account).then(res => {
  107. this.payToken = res;
  108. // this.handleApproveBtnShow(); //获取授权余额
  109. this.getWalletBalance(); //获取钱包余额
  110. });
  111. },
  112. // 查询链上钱包余额
  113. async getWalletBalance() {
  114. const erc20Contract = getErc20Contract(this.payToken);
  115. let bal = await erc20Contract.methods.balanceOf(this.account).call();
  116. this.walletBalance = (formatAmount(bal, 18) * 1).toFixed(2);
  117. },
  118. submit() {
  119. if (!this.amount) {
  120. return this.$toast(this.$t('lang160'));
  121. }
  122. if (this.walletBalance < this.amount) {
  123. return this.$toast(this.$t('lang159'));
  124. }
  125. this.show = true;
  126. usdt
  127. .transfer(this.account, this.pay_address, this.amount, this.payToken)
  128. .then(res => {
  129. // 调回调接口
  130. let params = {
  131. amount: this.amount,
  132. tx_hash: res,
  133. };
  134. homeApi
  135. .updateOrder(params)
  136. .then(res => {
  137. if (res.code == 200) {
  138. this.amount = '';
  139. this.show = false;
  140. Notify({ type: 'success', message: this.$t('lang152') });
  141. } else {
  142. this.show = false;
  143. Toast({ message: `${res.msg}` });
  144. }
  145. })
  146. .catch(err => {
  147. this.show = false;
  148. this.$toast(err.msg);
  149. return;
  150. });
  151. })
  152. .catch(err => {
  153. this.show = false;
  154. this.$toast(err.message);
  155. return;
  156. });
  157. },
  158. },
  159. };
  160. </script>
  161. <style lang="less" scoped>
  162. .head {
  163. position: fixed;
  164. top: 0;
  165. left: 0;
  166. width: 100%;
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. color: rgba(#000, 0.8);
  171. letter-spacing: 1.5px;
  172. font-weight: 550;
  173. background: #fff;
  174. padding: 15px 0;
  175. // z-index: 99;
  176. .arrow_img {
  177. position: absolute;
  178. left: 20px;
  179. width: 10px;
  180. height: 16px;
  181. // transform: translate(0, -50%);
  182. }
  183. }
  184. .f-sb {
  185. display: flex;
  186. align-items: center;
  187. justify-content: space-between;
  188. }
  189. .f-sa {
  190. display: flex;
  191. align-items: center;
  192. justify-content: space-around;
  193. }
  194. .f-sb-n {
  195. display: flex;
  196. align-items: flex-end;
  197. justify-content: space-between;
  198. }
  199. .f-col {
  200. display: flex;
  201. flex-direction: column;
  202. }
  203. .assets {
  204. min-height: 100vh;
  205. padding: 50px 0 0;
  206. // background-color: #fafbfc;
  207. .top {
  208. color: #000;
  209. padding-bottom: 16px;
  210. margin: 0 6px;
  211. }
  212. .pd {
  213. padding: 4px;
  214. margin-top: 4px;
  215. // background-color: #fff;
  216. }
  217. .total_box {
  218. position: relative;
  219. display: flex;
  220. // align-items: center;
  221. flex-direction: column;
  222. justify-content: space-between;
  223. // position: relative;
  224. font-size: 13px;
  225. color: #fff;
  226. height: 140px;
  227. // border-radius: 16px;
  228. padding: 20px 20px;
  229. box-sizing: border-box;
  230. border-radius: 10px;
  231. margin: 10px;
  232. background: linear-gradient(to right, #1ab986, #25d7bb);
  233. .total {
  234. font-size: 28px;
  235. // text-align: center;
  236. padding-top: 10px;
  237. }
  238. .bo {
  239. font-size: 12px;
  240. display: flex;
  241. align-items: center;
  242. color: rgba(#fff, 0.8);
  243. margin: 10px 10px 0 0;
  244. .icon {
  245. width: 20px;
  246. height: 20px;
  247. margin-right: 6px;
  248. border-radius: 50%;
  249. padding: 4px;
  250. background-color: rgba(#000, 0.1);
  251. }
  252. }
  253. .mx {
  254. position: absolute;
  255. right: 20px;
  256. width: fit-content;
  257. font-size: 11px;
  258. color: rgba(#f7f7f7, 0.9);
  259. padding: 2px 12px;
  260. border-radius: 10px;
  261. background-color: rgba(#000, 0.1);
  262. }
  263. .text {
  264. color: rgba(#fff, 0.8);
  265. }
  266. .buy {
  267. position: absolute;
  268. right: 14px;
  269. bottom: 14px;
  270. &_btn {
  271. color: #29b286;
  272. height: 30px;
  273. line-height: 30px;
  274. font-size: 14px;
  275. background-color: #ffffff;
  276. border: none;
  277. padding: 0 16px;
  278. white-space: nowrap;
  279. border-radius: 30px;
  280. }
  281. }
  282. }
  283. .bare {
  284. text-align: center;
  285. font-size: 14px;
  286. padding: 60px 14px;
  287. color: #888;
  288. }
  289. .list {
  290. border-radius: 20px;
  291. padding: 0 20px;
  292. margin-top: 16px;
  293. background-color: #fff;
  294. margin: 16px 6px;
  295. .box {
  296. width: 100%;
  297. color: rgba(51, 51, 51, 1);
  298. font-size: 14px;
  299. padding: 18px 0 18px;
  300. .box_item:last-child {
  301. border-bottom: none;
  302. }
  303. &_item {
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. padding: 16px 0;
  308. border-bottom: 0.5px solid rgba(190, 190, 190, 0.15);
  309. .key {
  310. color: #aaa;
  311. font-size: 12px;
  312. }
  313. .left {
  314. font-size: 13px;
  315. }
  316. .mobile {
  317. font-size: 14px;
  318. color: #000;
  319. font-weight: 550;
  320. }
  321. .input {
  322. width: 180px;
  323. }
  324. }
  325. }
  326. }
  327. .buy {
  328. display: flex;
  329. align-items: center;
  330. justify-content: center;
  331. margin-top: 30px;
  332. &_btn {
  333. width: 80%;
  334. height: 46px;
  335. line-height: 46px;
  336. font-size: 14px;
  337. background-color: #29b286;
  338. border: none;
  339. padding: 0 10px;
  340. white-space: nowrap;
  341. border-radius: 30px;
  342. }
  343. }
  344. .tips {
  345. color: #aaa;
  346. font-size: 12px;
  347. line-height: 180%;
  348. padding: 14px;
  349. }
  350. }
  351. .wrapper {
  352. // display: flex;
  353. // justify-content: center;
  354. .loading {
  355. position: absolute;
  356. left: 46%;
  357. top: 46%;
  358. }
  359. }
  360. </style>