index.vue 9.4 KB

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