convert-index.vue 10 KB

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