packetSend.vue 7.4 KB

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