index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-modal-mask pub-scroll-box packet-send-wrap" v-if="visible">
  4. <back-bar :title="$t('redPacket.title')" class="redpacket-backbar" @onBack="visible = false"></back-bar>
  5. <div class="send-wrap" :class="[{'is-private':isPrivate}]">
  6. <i class="el-icon-close" @click="visible = false"></i>
  7. <i class="el-icon-question" @click="helpShow = true"></i>
  8. <h3 class="title">{{ $t('redPacket.title') }}</h3>
  9. <p class="redpacket-tips" :class="{'hidden': !tips}">{{tips}}</p>
  10. <div class="main-box">
  11. <div class="input-item">
  12. <span class="text">
  13. <!--<em v-if="!isPrivate">{{ $t('redPacket.random') }}</em>-->
  14. {{ $t('redPacket.totalAmount') }}
  15. </span>
  16. <div class="packet-box" >
  17. <input type="number" v-model.number="money" placeholder="0.00">
  18. <div class="unit">
  19. <div :class="['cur-unit',{'has-arrow':group.eosInfo}]">{{symbol}}</div>
  20. <div class="code-menu" v-if="group.eosInfo">
  21. <div class="code-item" @click="isGameToken = !isGameToken">
  22. {{!isGameToken ? group.eosInfo.token : 'EOS'}}
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <p class="input-tips" v-if="!isPrivate">
  29. {{ $t('redPacket.tip1') }}
  30. </p>
  31. <div class="input-item pack-num-input" v-if="!isPrivate">
  32. <span class="text">{{ $t('redPacket.num') }}</span>
  33. <div class="packet-box">
  34. <input type="number" v-model.number="packetNum" :placeholder="$t('redPacket.placeholder1')">
  35. <div class="unit">{{ $t('redPacket.unit') }}</div>
  36. </div>
  37. </div>
  38. <p class="group-user-num" v-if="!isPrivate">
  39. {{ $t('redPacket.tip2', {num: group.userCounts}) }}
  40. </p>
  41. <textarea class="words" v-model="word" :placeholder="$t('redPacket.memo')"></textarea>
  42. <div class="sum">
  43. {{amountSum}} <span>{{isGameToken ? group.eosInfo.token : 'EOS'}}</span>
  44. </div>
  45. <button class="send-btn" @click="sendPacket" :class="{'is-disable': !amountSum || !packetNum, 'loading': isLoading}">
  46. <i v-if="isLoading" class="el-icon-loading"></i> {{ $t('redPacket.sendBtn') }}
  47. </button>
  48. </div>
  49. <p class="bot">{{ $t('redPacket.tip3') }}</p>
  50. </div>
  51. <div class="help-wrap" v-show="helpShow">
  52. <i class="el-icon-close" @click="helpShow = false"></i>
  53. <h3 class="title">{{ $t('redPacket.helpTitle') }}</h3>
  54. <div class="content">
  55. <div class="item">
  56. {{ $t('redPacket.helpRule1') }}
  57. </div>
  58. <div class="item">
  59. {{ $t('redPacket.helpRule2') }}
  60. </div>
  61. <div class="item">
  62. {{ $t('redPacket.helpRule3') }}
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </transition>
  68. </template>
  69. <script>
  70. import { mapState, mapGetters } from 'vuex'
  71. import { Message } from 'element-ui'
  72. import NP from 'number-precision'
  73. import EosHelper from '@/util/eosHelper.js'
  74. import { getMeechatType } from '@/util/util'
  75. import backBar from '@/components/backBar'
  76. export default {
  77. name: 'packetSend',
  78. data () {
  79. return {
  80. visible: true,
  81. helpShow: false,
  82. money: 1,
  83. packetNum: 1,
  84. word: this.$t('redPacket.memo'),
  85. tips: '',
  86. isLoading: false,
  87. isGameToken: false, // 是否为游戏代币
  88. meechatType: getMeechatType()// meechat版本
  89. }
  90. },
  91. components: {
  92. backBar
  93. },
  94. computed: {
  95. ...mapState([
  96. 'account',
  97. 'group',
  98. 'curSession',
  99. 'scatter'
  100. ]),
  101. ...mapGetters(['isPrivate']),
  102. amountSum () {
  103. return this.money
  104. },
  105. symbol () {
  106. return this.isGameToken ? this.group.eosInfo.token : 'EOS'
  107. },
  108. minSum () {
  109. return this.isGameToken ? NP.divide(this.group.eosInfo.min_amount, 10000) : 0.1
  110. },
  111. maxSum () {
  112. return this.isGameToken ? NP.divide(this.group.eosInfo.max_amount, 10000) : 200
  113. }
  114. },
  115. watch: {
  116. money (val) {
  117. if (val > this.maxSum) {
  118. this.money = this.maxSum
  119. this.showTip(`${this.$t('redPacket.maxMoneyTip')} ${this.maxSum} ${this.symbol}`)
  120. }
  121. if (val && val / this.packetNum < 0.01) {
  122. this.money = NP.times(this.packetNum, 0.01)
  123. this.showTip(`${this.$t('redPacket.maxMoneyTip')} 0.01 ${this.symbol}`)
  124. }
  125. },
  126. packetNum (to, from) {
  127. if (this.money && NP.divide(this.money, to) < 0.01) {
  128. this.packetNum = from
  129. this.showTip(`${this.$t('redPacket.singleMinMoneyTip')} 0.01 ${this.symbol}`)
  130. }
  131. if (to > 100) {
  132. this.packetNum = 100
  133. this.showTip(`${this.$t('redPacket.maxNumberTip')} 100 个`)
  134. }
  135. }
  136. },
  137. methods: {
  138. showTip (msg, timeout = 3000) {
  139. this.tips = msg
  140. setTimeout(() => {
  141. this.tips = ''
  142. }, timeout)
  143. },
  144. async sendPacket () {
  145. if (this.money < this.minSum) {
  146. this.showTip(`${this.$t('redPacket.minMoneyTip')} ${this.minSum} ${this.symbol}`)
  147. return false
  148. }
  149. this.isLoading = true
  150. let eosAmount = this.amountSum.toFixed(4) + ` ${this.symbol}`
  151. let toAccount = 'meechatadmin'
  152. let memo = {
  153. type: 'redpack',
  154. num: this.packetNum,
  155. memo: this.word,
  156. sid: this.isPrivate ? this.curSession : this.group.groupId
  157. }
  158. let tokenCode = this.isGameToken ? this.group.eosInfo.token_code : 'eosio.token'
  159. let symbol = this.symbol
  160. let [balance] = await EosHelper.getCurrencyBalance(tokenCode, this.account.name, symbol)
  161. if (balance) {
  162. let userBalance = this.isGameToken ? balance.replace(new RegExp('\\s' + symbol), '') : balance.replace(/\sEOS/, '')
  163. if (this.amountSum > Number(userBalance)) {
  164. Message({
  165. message: this.$t('public.noMoney'),
  166. type: 'error'
  167. })
  168. this.isLoading = false
  169. return
  170. }
  171. if (this.isGameToken) {
  172. let tokenCode = this.group.eosInfo.token_code
  173. EosHelper.doSymbolTransfer(this.account.name, 'meechatadmin', eosAmount, JSON.stringify(memo), this.account.authority, tokenCode)
  174. .then(res => {
  175. this.visible = false
  176. }).catch(msg => {
  177. if (!msg.type) {
  178. let json = JSON.parse(msg)
  179. let details = json.error.details
  180. Message({
  181. message: details[0].message,
  182. type: 'error'
  183. })
  184. }
  185. }).finally(() => {
  186. this.isLoading = false
  187. })
  188. } else {
  189. EosHelper.transfer(this.account.name, toAccount, eosAmount, JSON.stringify(memo), this.account.authority)
  190. .then((res) => {
  191. this.visible = false
  192. }).catch(msg => {
  193. if (!msg.type) {
  194. let json = JSON.parse(msg)
  195. let details = json.error.details
  196. Message({
  197. message: details[0].message,
  198. type: 'error'
  199. })
  200. }
  201. }).finally(() => {
  202. this.isLoading = false
  203. })
  204. }
  205. } else {
  206. Message({
  207. message: this.$t('public.badNetwork'),
  208. type: 'error'
  209. })
  210. this.isLoading = false
  211. }
  212. }
  213. },
  214. mounted () {
  215. }
  216. }
  217. </script>
  218. <style lang="scss">
  219. @import './style.scss';
  220. </style>