index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-modal-mask pub-scroll-box" v-if="visible">
  4. <div class="send-wrap">
  5. <i class="el-icon-close" @click="visible = false"></i>
  6. <i class="el-icon-question" @click="helpShow = true"></i>
  7. <h3 class="title">发红包</h3>
  8. <p class="redpacket-tips" :class="{'hidden': !tips}">{{tips}}</p>
  9. <div class="main-box">
  10. <div class="input-item">
  11. <span class="text">
  12. <em>拼</em>总金额
  13. </span>
  14. <div class="packet-box">
  15. <input type="number" v-model.number="money" placeholder="0.00">
  16. <span class="unit">
  17. {{symbol}}
  18. <i v-if="group.eosInfo" class="down-arrow"></i>
  19. <div class="code-menu" v-if="group.eosInfo">
  20. <div class="code-item" @click="isGameToken = !isGameToken">
  21. {{!isGameToken ? group.eosInfo.token : 'EOS'}}
  22. </div>
  23. </div>
  24. </span>
  25. </div>
  26. </div>
  27. <p class="input-tips">
  28. 当前为拼手气红包
  29. </p>
  30. <div class="input-item pack-num-input">
  31. <span class="text">红包个数</span>
  32. <div class="packet-box">
  33. <input type="number" v-model.number="packetNum" placeholder="请填写个数">
  34. <span class="unit">个</span>
  35. </div>
  36. </div>
  37. <p class="group-user-num">
  38. 本群共{{group.userCounts}}人
  39. </p>
  40. <textarea class="words" v-model="word" placeholder="恭喜发财,大吉大利"></textarea>
  41. <div class="sum">
  42. {{amountSum}} <span>{{isGameToken ? group.eosInfo.token : 'EOS'}}</span>
  43. </div>
  44. <button class="send-btn" @click="sendPacket" :class="{'is-disable': !amountSum || !packetNum, 'loading': isLoading}">
  45. <i v-if="isLoading" class="el-icon-loading"></i> 塞钱进红包
  46. </button>
  47. </div>
  48. <p class="bot">未领取的红包,将于24小时后发起退款</p>
  49. </div>
  50. <div class="help-wrap" v-show="helpShow">
  51. <i class="el-icon-close" @click="helpShow = false"></i>
  52. <h3 class="title">帮助</h3>
  53. <div class="content">
  54. <div class="item">
  55. 1. 发出的红包均为拼手气红包,抢得的
  56. 红包金额随机;一次最多发100个红包;
  57. </div>
  58. <div class="item">
  59. 2. 发红包时可选择币种;抢到的红包金额
  60. 直接转账到对应币种的绑定账户;若未绑
  61. 定账户,则所得币种余额在绑定账户后即
  62. 刻转账;
  63. </div>
  64. <div class="item">
  65. 3. MeeChat会从每次发出红包的金额中收
  66. 取2%的手续费,用于支付上链成本
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </transition>
  72. </template>
  73. <script>
  74. import { mapState } from 'vuex'
  75. import { Message } from 'element-ui'
  76. import NP from 'number-precision'
  77. import EosHelper from '@/util/eosHelper.js'
  78. export default {
  79. name: 'packetSend',
  80. data () {
  81. return {
  82. helpShow: false,
  83. money: 1,
  84. packetNum: 1,
  85. word: '恭喜发财,大吉大利',
  86. tips: '',
  87. isLoading: false,
  88. isGameToken: false // 是否为游戏代币
  89. }
  90. },
  91. computed: {
  92. ...mapState([
  93. 'account',
  94. 'group',
  95. 'curSession'
  96. ]),
  97. isPrivate () {
  98. return this.$store.getters.isPrivate
  99. },
  100. amountSum () {
  101. return this.money
  102. },
  103. symbol () {
  104. return this.isGameToken ? this.group.eosInfo.token : 'EOS'
  105. },
  106. minSum () {
  107. return this.isGameToken ? NP.divide(this.group.eosInfo.min_amount, 10000) : 0.1
  108. },
  109. maxSum () {
  110. return this.isGameToken ? NP.divide(this.group.eosInfo.max_amount, 10000) : 200
  111. }
  112. },
  113. watch: {
  114. money (val) {
  115. if (val > this.maxSum) {
  116. this.money = this.maxSum
  117. this.showTip(`总金额 不能多于 ${this.maxSum} ${this.symbol}`)
  118. }
  119. if (val && val / this.packetNum < 0.01) {
  120. this.money = NP.times(this.packetNum, 0.01)
  121. this.showTip(`单个红包最小为 0.01 ${this.symbol}`)
  122. }
  123. },
  124. packetNum (to, from) {
  125. if (this.money && NP.divide(this.money, to) < 0.01) {
  126. this.packetNum = from
  127. this.showTip(`单个红包最小为 0.01 ${this.symbol}`)
  128. }
  129. if (to > 100) {
  130. this.packetNum = 100
  131. this.showTip('红包个数最大为 100 个')
  132. }
  133. }
  134. },
  135. methods: {
  136. showTip (msg, timeout = 3000) {
  137. this.tips = msg
  138. setTimeout(() => {
  139. this.tips = ''
  140. }, timeout)
  141. },
  142. async sendPacket () {
  143. if (this.money < this.minSum) {
  144. this.showTip(`总金额 不能少于 ${this.minSum} ${this.symbol}`)
  145. return false
  146. }
  147. this.isLoading = true
  148. let eosAmount = this.amountSum.toFixed(4) + ` ${this.symbol}`
  149. let toAccount = 'meechatadmin'
  150. let memo = {
  151. type: 'redpack',
  152. num: this.packetNum,
  153. memo: this.word,
  154. sid: this.isPrivate ? this.curSession : this.group.groupId
  155. }
  156. let tokenCode = this.isGameToken ? this.group.eosInfo.token_code : 'eosio.token'
  157. let symbol = this.symbol
  158. let [balance] = await EosHelper.getCurrencyBalance(tokenCode, this.account.name, symbol)
  159. if (balance) {
  160. let userBalance = this.isGameToken ? balance.replace(new RegExp('\\s' + symbol), '') : balance.replace(/\sEOS/, '')
  161. if (this.amountSum > Number(userBalance)) {
  162. Message({
  163. message: '账户余额不足',
  164. type: 'error'
  165. })
  166. this.isLoading = false
  167. return
  168. }
  169. if (this.isGameToken) {
  170. let tokenCode = this.group.eosInfo.token_code
  171. EosHelper.doSymbolTransfer(this.account.name, 'meechatadmin', eosAmount, JSON.stringify(memo), this.account.authority, tokenCode)
  172. .then(res => {
  173. this.visible = false
  174. }).catch(msg => {
  175. if (!msg.type) {
  176. let json = JSON.parse(msg)
  177. let details = json.error.details
  178. Message({
  179. message: details[0].message,
  180. type: 'error'
  181. })
  182. }
  183. }).finally(() => {
  184. this.isLoading = false
  185. })
  186. } else {
  187. EosHelper.transfer(this.account.name, toAccount, eosAmount, JSON.stringify(memo), this.account.authority)
  188. .then((res) => {
  189. this.visible = false
  190. }).catch(msg => {
  191. if (!msg.type) {
  192. let json = JSON.parse(msg)
  193. let details = json.error.details
  194. Message({
  195. message: details[0].message,
  196. type: 'error'
  197. })
  198. }
  199. }).finally(() => {
  200. this.isLoading = false
  201. })
  202. }
  203. } else {
  204. Message({
  205. message: '网络太差~ 请重试',
  206. type: 'error'
  207. })
  208. this.isLoading = false
  209. }
  210. }
  211. },
  212. mounted () {
  213. }
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. @import './style.scss';
  218. </style>