convert-index.vue 10 KB

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