index.vue 8.8 KB

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