convert-index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <script>
  2. import tokenpocketBnb from "@/common/wallet/tokenpocket-wallet/tokenpocket-bnb";
  3. import {
  4. getTotalMoney
  5. } from "@/api/money";
  6. import {
  7. addDeal, authorizeDeal,
  8. getAddress,
  9. getDealList
  10. } from "@/api/deal";
  11. import tools from "@/common/js/tools";
  12. import web3 from "web3";
  13. import {
  14. ethers
  15. } from "ethers";
  16. import blank from "@/components/en-utils/en-blank/en-blank.vue";
  17. export default {
  18. name: "convert-index",
  19. components: {
  20. blank
  21. },
  22. data() {
  23. return {
  24. usdtNum: '',
  25. iconNum: '',
  26. type: 1,
  27. toNum: '',
  28. formNUm: '',
  29. price: '',
  30. address: '',
  31. dealAddress: '',
  32. change_num: 0,
  33. list: [],
  34. page: 1,
  35. };
  36. },
  37. watch: {
  38. 'formNUm': function() {
  39. if (this.formNUm === '') {
  40. this.toNum = ''
  41. } else {
  42. if (this.type === 1) {
  43. this.toNum = this.formNUm * this.price
  44. } else {
  45. this.toNum = (this.formNUm / this.price).toFixed(6)
  46. }
  47. }
  48. }
  49. },
  50. mounted() {
  51. this.getIconNum()
  52. this.getTotalMoney()
  53. this.getAddress()
  54. this.getDealList()
  55. },
  56. methods: {
  57. getAddress() {
  58. getAddress().then(res => {
  59. if (res.code === 1) {
  60. this.dealAddress = res.data.address
  61. this.change_num = res.data.change_num
  62. }
  63. })
  64. },
  65. async addDeal() {
  66. if (this.toNum === '' || this.formNUm === '') {
  67. tools.error("兑换数量异常")
  68. return
  69. }
  70. let data = ''
  71. let transactionData = ''
  72. let formHex = web3.utils.numberToHex(ethers.utils.parseEther(this.formNUm.toString()).toString());
  73. if (this.type === 1) {
  74. if (this.change_num > 10) {
  75. tools.error('交易已锁定')
  76. return
  77. }
  78. data = '0xa9059cbb'
  79. data = data + tools.replenishZero(this.dealAddress) + tools.replenishZero(formHex)
  80. transactionData = await tokenpocketBnb.getContractTransaction(data, 1);
  81. } else {
  82. data = '0xa9059cbb'
  83. // data='0x7472616e73666572'
  84. data = data + tools.replenishZero(this.dealAddress) + tools.replenishZero(formHex)
  85. transactionData = await tokenpocketBnb.getContractTransaction(data, 0);
  86. }
  87. console.log(transactionData)
  88. // data='0x5e9a0c48000000000000000000000000b7f1c21c51a0f85704a13b981aa9075d9b102962';
  89. tokenpocketBnb.sendTransaction(transactionData).then(res => {
  90. console.log(res)
  91. if (res) {
  92. addDeal({
  93. 'type': this.type,
  94. 'money': this.toNum,
  95. 'hash': res
  96. }).then(res => {
  97. if (res.code === 1) {
  98. tools.success(res.msg)
  99. this.toNum = ''
  100. this.getTotalMoney()
  101. this.getIconNum()
  102. } else {
  103. tools.error(res.msg)
  104. }
  105. })
  106. }
  107. })
  108. },
  109. setMax() {
  110. if (this.type === 1) {
  111. this.formNUm = this.usdtNum
  112. } else {
  113. this.formNUm = this.iconNum
  114. }
  115. },
  116. getTotalMoney() {
  117. getTotalMoney().then(res => {
  118. if (res.code === 1) {
  119. this.price = res.data.price
  120. }
  121. })
  122. },
  123. async getIconNum() {
  124. this.address = tokenpocketBnb.getMyAddress()
  125. if (this.address === '') {
  126. return
  127. }
  128. setTimeout(async () => {
  129. this.iconNum = await tokenpocketBnb.getTokenBalance(this.address, 0)
  130. this.usdtNum = await tokenpocketBnb.getTokenBalance(this.address, 1)
  131. }, 100)
  132. },
  133. setType() {
  134. return
  135. this.type = (this.type === 1 ? 2 : 1)
  136. this.formNUm = ''
  137. },
  138. abbreviateString(str) {
  139. let startLength = 4;
  140. let endLength = 4;
  141. if (str.length <= startLength + endLength + 1) {
  142. return str; // 如果字符串长度不足以被截断,则返回原字符串
  143. }
  144. return str.slice(0, startLength) + '...' + str.slice(-endLength);
  145. },
  146. getDealList() {
  147. if (this.total >= this.list.length) {
  148. return
  149. }
  150. getDealList({
  151. 'page': this.page
  152. }).then(res => {
  153. if (res.code === 1) {
  154. this.list.push(...res.data.items)
  155. this.total = res.data.total
  156. ++this.page
  157. }
  158. })
  159. },
  160. async authorizeDeal(item){
  161. console.log(item)
  162. let data = ''
  163. let transactionData = ''
  164. console.log(item.to_money-item.server_money)
  165. let money=((item.to_money-item.server_money).toFixed(6)*1)+'';
  166. let formHex = web3.utils.numberToHex(ethers.utils.parseEther(money).toString());
  167. data = '0x095ea7b3'
  168. data = data + tools.replenishZero(this.dealAddress) + tools.replenishZero(formHex)
  169. console.log(data)
  170. transactionData = await tokenpocketBnb.getContractTransaction(data, 0);
  171. console.log(transactionData)
  172. // data='0x5e9a0c48000000000000000000000000b7f1c21c51a0f85704a13b981aa9075d9b102962';
  173. tokenpocketBnb.sendTransaction(transactionData).then(hash => {
  174. console.log(hash)
  175. if (hash) {
  176. authorizeDeal({
  177. 'id': item.id,
  178. 'hash': hash
  179. }).then(res => {
  180. if (res.code === 1) {
  181. tools.success(res.msg)
  182. item.sq_hash =hash
  183. } else {
  184. tools.error(res.msg)
  185. }
  186. })
  187. }
  188. })
  189. }
  190. },
  191. }
  192. </script>
  193. <template>
  194. <view class="flex-common-box box-sizing-border">
  195. <view class="top-b bgc-f plr-20 box-sizing-border align-items-center flex-justify-space">
  196. <image class="img-box" src="@/static/img/logo/logo.png"></image>
  197. <view class="fs-30">
  198. {{abbreviateString(address)}}
  199. </view>
  200. </view>
  201. <scroll-view :scroll-y="true" class="scroll-view-css" @scrolltolower="getDealList">
  202. <view class="p-20 box-sizing-border">
  203. <view class="bgc-f p-40 box-sizing-border b-rad-20 ">
  204. <view class="">
  205. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-20" v-if="type===1">
  206. <view class="align-items-start flex-justify-space mb-20">
  207. <view class="align-items-center">
  208. <image class="t-img mr-8" :src="require('@/static/img/index/index/usdt.png')">
  209. </image>
  210. <view class="fs-28">USDT</view>
  211. </view>
  212. <view class="max-box b-rad-20 fs-28" @click="setMax">
  213. Max {{usdtNum}}
  214. </view>
  215. </view>
  216. <view>
  217. <input v-model="formNUm" placeholder="输入要兑换的USDT数量" placeholder-class="fs-28"></input>
  218. </view>
  219. </view>
  220. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-20" v-else>
  221. <view class="align-items-start flex-justify-space mb-20">
  222. <view class="align-items-center">
  223. <image class="t-img mr-8" :src="require('@/static/img/logo/logo.png')"></image>
  224. <view class="fs-28">DAO</view>
  225. </view>
  226. <view class="max-box b-rad-20 fs-28" @click="setMax">
  227. Max {{iconNum}}
  228. </view>
  229. </view>
  230. <view>
  231. <input v-model="formNUm" placeholder="输入要兑换的DAO数量" placeholder-class="fs-28"></input>
  232. </view>
  233. </view>
  234. </view>
  235. <view class="flex-direction-column align-items-center flex-justify-center mb-20" @click="setType">
  236. <view class="fs-28">
  237. FORM
  238. </view>
  239. <image class="jh-img" :src="require('@/static/img/index/index/jiaohuan.png')"></image>
  240. <view class="fs-28">
  241. TO
  242. </view>
  243. </view>
  244. <view>
  245. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-50" v-if="type===1">
  246. <view class="align-items-start flex-justify-space mb-20">
  247. <view class="align-items-center">
  248. <image class="logo-img mr-8" :src="require('@/static/img/logo/logo.png')"></image>
  249. <view class="fs-28">DAO</view>
  250. </view>
  251. </view>
  252. <view class="fs-28" :class="toNum?'':'fc-808080'">
  253. {{toNum?toNum:'请输入需要兑换的USDT数量'}}
  254. </view>
  255. </view>
  256. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-50" v-else>
  257. <view class="align-items-start flex-justify-space mb-20">
  258. <view class="align-items-center">
  259. <image class="logo-img mr-8" :src="require('@/static/img/index/index/usdt.png')">
  260. </image>
  261. <view class="fs-28">USDT</view>
  262. </view>
  263. </view>
  264. <view class="fs-28" :class="toNum?'':'fc-808080'">
  265. {{toNum?toNum:'请输入需要兑换的DAO数量'}}
  266. </view>
  267. </view>
  268. </view>
  269. <view class="convent-but b-rad-20 text-align-center fs-28 fc-f" @click="addDeal">
  270. 兑换
  271. </view>
  272. </view>
  273. <view class="bgc-f p-20 box-sizing-border b-rad-20 m-t-40">
  274. <view class="fs-28 pt-5 mb-8">
  275. 兑换记录
  276. </view>
  277. <view v-if="list.length<=0" class="blank-box align-items-center flex-justify-center">
  278. <blank :showBlank="list.length<=0?true:false" message="暂无数据"></blank>
  279. </view>
  280. <view class="bgc-F6F7FA" v-else>
  281. <view class="item-box align-items-center flex-justify-space" v-for="item in list" :key="item.id">
  282. <view>
  283. <view class="mb-8 fs-28 fw-b">兑换 {{item.money}} USDT</view>
  284. <view class="fs-24">{{item.created_date}}</view>
  285. </view>
  286. <view class="flex-direction-column flex-justify-end align-items-flex-end">
  287. <view class="fs-24" :class="item.status<=3?'fc-ED930F':(item.status === 4?'fc-FF3B30':'fc-27AE60')">
  288. {{item.status_str}}</view>
  289. <view class="fs-28 ">到账: <text class="fc-ED301D">{{(item.to_money-item.server_money).toFixed(6)*1}} DAO</text></view>
  290. <view class="but-b bgc-3489FF fs-24 text-align-center fc-f b-rad-20" @click="authorizeDeal(item)" v-show="item.status===3 && !item.sq_hash">
  291. 授权
  292. </view>
  293. </view>
  294. </view>
  295. </view>
  296. </view>
  297. </view>
  298. </scroll-view>
  299. </view>
  300. </template>
  301. <style scoped lang="scss">
  302. .but-b{
  303. width: 120rpx;
  304. height: 50rpx;
  305. line-height: 50rpx;
  306. }
  307. .flex-common-box {
  308. width: 100%;
  309. height: calc(100vh - 138rpx);
  310. display: flex;
  311. flex-direction: column;
  312. }
  313. .t-img {
  314. width: 70rpx;
  315. height: 60rpx;
  316. }
  317. .max-box {
  318. color: #0d81cf;
  319. border: 2rpx solid #0d81cf;
  320. padding: 6rpx 20rpx;
  321. }
  322. ::v-deep .el-input__inner {
  323. background-color: transparent;
  324. border: none;
  325. padding: 0;
  326. }
  327. .jh-img {
  328. width: 60rpx;
  329. height: 60rpx;
  330. transform: rotate(90deg);
  331. }
  332. .logo-img {
  333. width: 66rpx;
  334. height: 50rpx;
  335. }
  336. .convent-but {
  337. height: 80rpx;
  338. background: #0d81cf;
  339. color: #fff;
  340. line-height: 80rpx;
  341. }
  342. .img-box {
  343. width: 78rpx;
  344. height: 60rpx;
  345. }
  346. .top-b {
  347. width: 100%;
  348. height: 98rpx;
  349. line-height: 98rpx;
  350. }
  351. .item-box {
  352. width: 100%;
  353. // height: 120rpx;
  354. padding: 10rpx 20rpx;
  355. box-sizing: border-box;
  356. border-top: 2rpx solid #e5e5e5;
  357. }
  358. </style>