index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="pub-modal-mask pub-scroll-box" v-if="visible">
  4. <div class="packet-bg" v-if="!openInfo" v-loading='isLoading'>
  5. <i class="el-icon-close" @click="visible = false"></i>
  6. <div class="user-info" :class="{'small': info.ext.redpack_status == 1}">
  7. <img class="avatar" v-if="info.avatar" :src="info.avatar" alt="">
  8. <div v-else class="avatar no-avatar" :class="'avatar_bg' + userId % 9">{{info.name.slice(0,2).toUpperCase()}}</div>
  9. <span class="name">{{info.name}}</span>
  10. <div class="text" v-if="info.ext.redpack_status == 0">{{info.content.title}}</div>
  11. </div>
  12. <!-- redpack_status: 0 => 进行中, 1 => 已抢完, 2 => 已过期 -->
  13. <div class="open-btn" :class="{'loading': isLoading}" v-if="info.ext.redpack_status == 0" @click="openPacket">{{$t('redPacket.open')}}</div>
  14. <div class="redpack-tips" v-else-if="info.ext.redpack_status == 1">
  15. {{$t('redPacket.noPacketTips')}}
  16. </div>
  17. <div class="redpack-tips" v-else-if="info.ext.redpack_status == 2">
  18. {{$t('redPacket.expiredPacketTips')}}
  19. </div>
  20. <div class="detail" v-if="userId == info.userId || info.ext.redpack_status == 1" @click="showDetail">{{$t('redPacket.seePacketDetail')}}</div>
  21. </div>
  22. <div class="detail-bg" :class="{'is-quantity':quantity}" v-else @touchstart="touchstartForH5" @touchend="touchendForH5">
  23. <i class="el-icon-close" @click="visible = false"></i>
  24. <nav class="nav-bar">
  25. <i class="el-icon-arrow-left" @click="visible = false"></i>
  26. <!-- <a class="packet-record">红包记录</a> -->
  27. </nav>
  28. <img class="avatar" v-if="info.avatar" :src="info.avatar" alt="">
  29. <div v-else class="avatar no-avatar" :class="'avatar_bg' + userId % 9">{{info.name.slice(0,2).toUpperCase()}}</div>
  30. <span class="name">{{info.name}}</span>
  31. <div class="text">{{info.content.title}}</div>
  32. <div class="money" v-if="quantity">
  33. {{formatNum(quantity)}} <em>{{openInfo.tokenType}}</em>
  34. </div>
  35. <p class="state" v-if="quantity">{{$t('redPacket.transferTo')}}{{openInfo.type.toUpperCase()}}{{$t('redPacket.account')}}</p>
  36. <p class="state-num">
  37. {{ $t('redPacket.tip4', { total: openInfo.num_total, num: openInfo.num_total - openInfo.num_left, unit: $t('redPacket.unit') }) }}
  38. </p>
  39. <ul class="packet-list pub-scroll-box">
  40. <li v-for="(item, key) in openInfo.logs" :key="key">
  41. <img class="avatar" v-if="item.cover_photo" :src="item.cover_photo" alt="">
  42. <div v-else class="avatar no-avatar" :class="'avatar_bg' + item.user_id % 9">
  43. {{item.nick_name.slice(0,2).toUpperCase()}}
  44. </div>
  45. <div class="content">
  46. <div class="top">
  47. <span class="user-name">{{item.nick_name}}</span>
  48. <span class="amount">{{formatNum(item.quantity_int)}} {{openInfo.tokenType}}</span>
  49. </div>
  50. <div class="bottom">
  51. <span class="time">{{formatTime(item.update_time_int)}}</span>
  52. <span class="best" v-if="item.best"></span>
  53. </div>
  54. </div>
  55. </li>
  56. </ul>
  57. <p class="tips">{{$t('redPacket.tip3')}}</p>
  58. </div>
  59. </div>
  60. </transition>
  61. </template>
  62. <script>
  63. import API from '@/api'
  64. import NP from 'number-precision'
  65. import { mapState } from 'vuex'
  66. import { Message } from 'element-ui'
  67. import { getMeechatType } from '@/util/util'
  68. import dayjs from 'dayjs'
  69. export default {
  70. name: 'packetGet',
  71. data () {
  72. return {
  73. openInfo: null,
  74. isLoading: false,
  75. isHasDetail: false,
  76. meechatType: getMeechatType(),
  77. startClientX: 0
  78. }
  79. },
  80. computed: {
  81. ...mapState([
  82. 'userId',
  83. 'account'
  84. ]),
  85. trxId () {
  86. return this.info.content.trxId
  87. },
  88. quantity () {
  89. if (this.openInfo && this.openInfo.logs.length) {
  90. let arr = this.openInfo.logs
  91. let item = arr.find(v => {
  92. return v.user_id == this.userId
  93. })
  94. if (item) {
  95. return item.quantity_int
  96. } else {
  97. return 0
  98. }
  99. } else {
  100. return 0
  101. }
  102. },
  103. isPrivate () {
  104. return this.$store.getters.isPrivate
  105. }
  106. },
  107. methods: {
  108. openPacket () {
  109. this.isLoading = true
  110. API.redpack.grabPacket({
  111. trx_id: this.trxId
  112. }).then(({ data }) => {
  113. // 抢光
  114. if (data.code === -1001) {
  115. this.$store.commit('unpdatePacketItem', {
  116. type: 'redpack_status',
  117. trxId: this.trxId,
  118. data: 1
  119. })
  120. } else {
  121. this.openInfo = data.data
  122. this.$store.commit('unpdatePacketItem', {
  123. type: 'grabbed',
  124. trxId: this.trxId,
  125. data: 1
  126. })
  127. }
  128. }).finally(() => {
  129. this.isLoading = false
  130. })
  131. },
  132. showDetail () {
  133. API.redpack.grabDetail({
  134. trx_id: this.trxId
  135. }).then(({ data }) => {
  136. this.openInfo = data.data
  137. if (this.meechatType == 'h5') this.initSwipeForH5()
  138. })
  139. },
  140. formatNum (num) {
  141. return NP.divide(Number(num), 10000)
  142. },
  143. formatTime (timestamp) {
  144. return dayjs(timestamp).format('HH:mm')
  145. },
  146. preHandleOpenInfo (openInfo) {
  147. if (openInfo && openInfo.logs.length === openInfo.num_total) {
  148. let best = openInfo.logs[0]
  149. openInfo.logs.forEach(item => {
  150. if (item.quantity_int > best.quantity_int) {
  151. best = item
  152. }
  153. })
  154. best.best = true
  155. }
  156. },
  157. touchstartForH5 (ev) {
  158. if (this.meechatType != 'h5') return
  159. this.startClientX = ev.changedTouches[0].clientX
  160. },
  161. touchendForH5 (ev) {
  162. if (this.meechatType != 'h5') return
  163. let clientX = ev.changedTouches[0].clientX
  164. if (Math.abs(clientX - this.startClientX) > 60) this.visible = false
  165. }
  166. },
  167. created () {
  168. if (this.userId) {
  169. API.redpack.grabDetail({
  170. trx_id: this.trxId
  171. }).then(({ data }) => {
  172. // 已经领取
  173. if ((this.info.userId == this.userId && this.isPrivate) || this.info.ext.grabbed) {
  174. this.openInfo = data.data
  175. } else if (data.data.num_left == 0) {
  176. // 没领取
  177. this.$store.commit('unpdatePacketItem', {
  178. type: 'redpack_status',
  179. trxId: this.trxId,
  180. data: data.data.redpack_status
  181. })
  182. }
  183. this.visible = true
  184. })
  185. } else {
  186. Message({
  187. message: this.$t('public.loginTip'),
  188. type: 'warning'
  189. })
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss" scoped>
  195. @import './style.scss';
  196. </style>