BackpackDialog.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <custom-dialog
  3. :visible.sync="dialogVisible"
  4. title=""
  5. content-width="720rpx"
  6. closeImg="/static/island/UI/btn_close.png"
  7. closeImgTop="120rpx"
  8. background-color="transparent"
  9. @close="onClose"
  10. >
  11. <view class="backpack-content">
  12. <view class="backpack-name">背包</view>
  13. <view class="backpack-items">
  14. <view
  15. class="backpack-grid"
  16. v-for="index in 80"
  17. :key="index"
  18. @click="onGridClick(index-1)"
  19. >
  20. <view
  21. class="item-grid"
  22. :class="{'selected': selectedIndex === index-1 && items[index-1]}"
  23. >
  24. <template v-if="items[index-1]">
  25. <text class="count-text">{{items[index-1].count}}</text>
  26. <image class="item-icon" :src="items[index-1].icon" mode="aspectFit"></image>
  27. <text class="item-name">{{items[index-1].name}}</text>
  28. </template>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="backpack-bottom">
  33. <view class="bottom-info">
  34. <text>{{selectedItem ? selectedItem.description : '请选择道具查看详情'}}</text>
  35. </view>
  36. <view class="bottom-actions">
  37. <view class="coin-info">
  38. <image class="currency-icon" src="/static/island/UI/wd_icon_xingyuan.png" mode="aspectFit"></image>
  39. <text>{{selectedItem ? selectedItem.price : 0}}</text>
  40. </view>
  41. <view class="sell-btn" :class="{'disabled': !selectedItem}" @click="onSellClick">售卖</view>
  42. </view>
  43. </view>
  44. </view>
  45. </custom-dialog>
  46. </template>
  47. <script>
  48. import CustomDialog from '../CustomDialog/CustomDialog.vue'
  49. export default {
  50. name: 'BackpackDialog',
  51. components: {
  52. CustomDialog
  53. },
  54. props: {
  55. visible: {
  56. type: Boolean,
  57. default: false
  58. }
  59. },
  60. data() {
  61. return {
  62. dialogVisible: false,
  63. selectedIndex: -1,
  64. items: [],
  65. loading: false
  66. }
  67. },
  68. computed: {
  69. selectedItem() {
  70. return this.selectedIndex >= 0 && this.selectedIndex < this.items.length
  71. ? this.items[this.selectedIndex]
  72. : null
  73. }
  74. },
  75. watch: {
  76. visible(val) {
  77. this.dialogVisible = val
  78. if(val) {
  79. this.fetchBagList()
  80. }
  81. },
  82. dialogVisible(val) {
  83. this.$emit('update:visible', val)
  84. }
  85. },
  86. methods: {
  87. fetchBagList() {
  88. this.loading = true
  89. uni.request({
  90. url: this.$apiHost + '/Game/get_bag_list',
  91. method: 'GET',
  92. data: {
  93. uuid: getApp().globalData.uuid,
  94. },
  95. header: {
  96. 'Content-Type': 'application/x-www-form-urlencoded',
  97. 'sign': getApp().globalData.headerSign,
  98. },
  99. success: (res) => {
  100. if (res.data.code === 0) {
  101. this.items = res.data.data.bagList.map(item => ({
  102. id: item.id,
  103. tid: item.tid,
  104. type: item.type,
  105. count: item.num,
  106. name: item.name,
  107. icon: item.image,
  108. description: `这是一个${item.name},数量:${item.num}`,
  109. price: item.price // 这里可以根据实际需求设置价格
  110. }))
  111. // 默认选中第一个有道具的格子
  112. this.selectedIndex = this.items.length > 0 ? 0 : -1
  113. } else {
  114. uni.showToast({
  115. title: res.data.msg || '获取背包数据失败',
  116. icon: 'none'
  117. })
  118. }
  119. },
  120. fail: (err) => {
  121. console.error('获取背包数据失败:', err)
  122. uni.showToast({
  123. title: '获取背包数据失败',
  124. icon: 'none'
  125. })
  126. },
  127. complete: () => {
  128. this.loading = false
  129. }
  130. })
  131. },
  132. onClose() {
  133. this.dialogVisible = false
  134. },
  135. onGridClick(index) {
  136. if(this.items[index]) {
  137. this.selectedIndex = index
  138. this.$emit('select', this.items[index])
  139. }
  140. },
  141. onSellClick() {
  142. if(!this.selectedItem) return
  143. const item = this.selectedItem
  144. const totalPrice = item.count * item.price
  145. uni.showModal({
  146. title: '确认出售',
  147. content: `出售${item.count}个${item.name},会获得铃钱:${totalPrice},\n确定出售吗?`,
  148. confirmText: '确定',
  149. cancelText: '取消',
  150. success: (res) => {
  151. if(res.confirm) {
  152. uni.request({
  153. url: this.$apiHost + '/Game/sell_bag_item',
  154. method: 'POST',
  155. data: {
  156. uuid: getApp().globalData.uuid,
  157. bag_id: item.id
  158. },
  159. header: {
  160. 'Content-Type': 'application/x-www-form-urlencoded',
  161. 'sign': getApp().globalData.headerSign,
  162. },
  163. success: (res) => {
  164. if (res.data.code === 0) {
  165. uni.showToast({
  166. title: '出售成功',
  167. icon: 'success'
  168. })
  169. // 刷新背包列表
  170. this.fetchBagList()
  171. // 通知父组件更新铃钱
  172. this.$emit('money-change', res.data.data.money)
  173. } else {
  174. uni.showToast({
  175. title: res.data.msg || '出售失败',
  176. icon: 'none'
  177. })
  178. }
  179. },
  180. fail: (err) => {
  181. console.error('出售失败:', err)
  182. uni.showToast({
  183. title: '出售失败',
  184. icon: 'none'
  185. })
  186. }
  187. })
  188. }
  189. }
  190. })
  191. }
  192. }
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. @import './BackpackDialog.scss';
  197. </style>