vuexList.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="storageList">
  3. <objectAnalysis
  4. v-if="isLoaded"
  5. :data="storageData"
  6. :isOpenFirst="true"
  7. :width="710"
  8. :isDiyMenu="true"
  9. @diyMenu="diyMenu"
  10. ref="objectAnalysis"
  11. />
  12. <view v-else class="dataLoading">
  13. <text class="status">加载中</text>
  14. </view>
  15. <text v-if="isLoaded" class="tipsText"> 长按非对象类型的数据可编辑 </text>
  16. </view>
  17. </template>
  18. <script>
  19. import objectAnalysis from './objectAnalysis.vue'
  20. export default {
  21. components: {
  22. objectAnalysis
  23. },
  24. props: {
  25. /**
  26. * 全局变量类型
  27. */
  28. stateType: {
  29. type: String,
  30. default: 'vuex'
  31. }
  32. },
  33. data() {
  34. return {
  35. /**
  36. * 是否完成加载
  37. */
  38. isLoaded: false,
  39. /**
  40. * 缓存里的数据
  41. */
  42. storageData: {}
  43. }
  44. },
  45. methods: {
  46. /**
  47. * 加载数据
  48. */
  49. getData() {
  50. let that = this
  51. let data = {}
  52. if (that.stateType == 'vuex') {
  53. try {
  54. data = JSON.parse(JSON.stringify(that.$store.state))
  55. } catch (error) {}
  56. } else if (that.stateType == 'pinia') {
  57. try {
  58. if (uni.Pinia) {
  59. data = JSON.parse(JSON.stringify(uni.Pinia.getActivePinia().state.value))
  60. } else if (that.$pinia) {
  61. data = JSON.parse(JSON.stringify(that.$pinia.state.value))
  62. }
  63. } catch (error) {}
  64. } else if (that.stateType == 'globalData') {
  65. try {
  66. data = JSON.parse(JSON.stringify(getApp().globalData))
  67. } catch (error) {}
  68. }
  69. that.isLoaded = false
  70. setTimeout(() => {
  71. that.storageData = data
  72. that.isLoaded = true
  73. }, 500)
  74. },
  75. /**
  76. * 自定义长按事件
  77. */
  78. diyMenu({ item, index }) {
  79. let that = this
  80. let menu = [
  81. {
  82. text: `复制键(key)`,
  83. click() {
  84. uni.setClipboardData({
  85. data: that.toString(item.k)
  86. })
  87. }
  88. },
  89. {
  90. text: `复制值(value)`,
  91. click() {
  92. uni.setClipboardData({
  93. data: that.toString(item.v)
  94. })
  95. }
  96. }
  97. ]
  98. if (typeof item.v != 'object') {
  99. menu.push({
  100. text: '编辑值',
  101. click() {
  102. let keyList = that.$refs.objectAnalysis.getFaKeyList(item.i)
  103. let title = ''
  104. if (keyList.length == 0) {
  105. title = 'key'
  106. } else {
  107. keyList.map((x) => {
  108. title = (title == '' ? '' : title + '.') + x
  109. })
  110. }
  111. let isBool = typeof item.v == 'boolean'
  112. if (isBool) {
  113. item.v = item.v ? 'true' : 'false'
  114. }
  115. if (item.v === undefined || item.v === null) {
  116. item.v = ''
  117. }
  118. uni.$emit('devTools_showEditDialog', {
  119. title,
  120. value: item.v
  121. })
  122. uni.$on('devTools_editDialogClose', () => {
  123. uni.$off('devTools_editDialogSaveSuccess')
  124. uni.$off('devTools_editDialogClose')
  125. })
  126. uni.$on('devTools_editDialogSaveSuccess', (val) => {
  127. uni.$off('devTools_editDialogSaveSuccess')
  128. uni.$off('devTools_editDialogClose')
  129. if (isBool && (val == 'true' || val == 'false')) {
  130. val = val == 'true'
  131. }
  132. let data
  133. if (that.stateType == 'vuex') {
  134. data = that.$store.state
  135. } else if (that.stateType == 'pinia') {
  136. if (uni.Pinia) {
  137. data = uni.Pinia.getActivePinia().state.value
  138. } else if (that.$pinia) {
  139. data = that.$pinia.state.value
  140. }
  141. } else if (that.stateType == 'globalData') {
  142. data = getApp().globalData
  143. }
  144. let lastKey = keyList.pop()
  145. keyList.map((x) => {
  146. data = data[x]
  147. })
  148. that.$set(data, lastKey, val)
  149. that.getData()
  150. })
  151. }
  152. })
  153. }
  154. uni.showActionSheet({
  155. itemList: menu.map((x) => x.text),
  156. success({ tapIndex }) {
  157. menu[tapIndex].click()
  158. }
  159. })
  160. },
  161. getFaKeyList() {}
  162. }
  163. }
  164. </script>
  165. <style lang="scss" scoped>
  166. .storageList {
  167. padding: 20rpx;
  168. width: 750rpx;
  169. }
  170. .dataLoading {
  171. width: 750rpx;
  172. height: 400rpx;
  173. display: flex;
  174. flex-direction: row;
  175. align-items: center;
  176. justify-content: center;
  177. .status {
  178. font-size: 20rpx;
  179. color: #888;
  180. line-height: 20rpx;
  181. }
  182. }
  183. .tipsText {
  184. font-size: 20rpx;
  185. color: #8799a3;
  186. margin-top: 40rpx;
  187. }
  188. </style>