人民医院前端

jsObjectText.vue 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="js-obj">
  3. <text @click="tryExpandObject" :class="
  4. 'js-obj-text '+
  5. type + (type == 'string'?(' ' + messageType):'')+
  6. (expandable ? ' expandable': '')+
  7. (expanded?' expanded':'')"
  8. :style="{whiteSpace:isKeyObj?'nowrap':'',wordBreak:'break-all'}"
  9. >{{expandable?(expanded?'▼ ':'▶ '):''}}{{shortObjString}}</text>
  10. <view v-if="expandObjs && expanded"
  11. class="js-child-host"
  12. :style="{
  13. marginLeft: `-${identWidth}rpx`,
  14. }">
  15. <view class="js-child" v-for="(ixx,kxx) in expandObjs" :key="kxx" >
  16. <text @longpress="onCopy(ixx.key, ixx.obj)" class="js-child-key">{{ixx.key}}: </text>
  17. <jsObjectText
  18. :obj="ixx.obj"
  19. :isKeyObj="true"
  20. :identWidth="ixx.key.length*12"
  21. />
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import { setClipboardText } from '../../common/clipboard.js'
  28. export default {
  29. name: 'jsObjectText',
  30. props: {
  31. obj: {
  32. default: null,
  33. },
  34. isKeyObj: {
  35. type: Boolean,
  36. default: false,
  37. },
  38. messageType: {
  39. type: String,
  40. default: '',
  41. },
  42. identWidth: {
  43. type: Number,
  44. default: 0,
  45. },
  46. },
  47. watch: {
  48. obj() {
  49. this.loadObjectInfo();
  50. },
  51. },
  52. methods: {
  53. onCopy(key, obj) {
  54. setClipboardText(JSON.stringify(obj));
  55. uni.showToast({
  56. icon: 'none',
  57. title: `已复制字段 ${key} !`,
  58. })
  59. },
  60. tryExpandObject(e) {
  61. if(this.type == 'object') {
  62. if(this.expandObjs == null) {
  63. this.expandObjs = [];
  64. if(this.obj instanceof Array) {
  65. for (let i = 0; i < this.obj.length; i++) {
  66. this.expandObjs.push({
  67. key: i,
  68. obj: this.obj[i]
  69. })
  70. }
  71. }
  72. else {
  73. for (let key in this.obj) {
  74. this.expandObjs.push({
  75. key: key,
  76. obj: this.obj[key]
  77. })
  78. }
  79. }
  80. this.expanded = true;
  81. } else {
  82. this.expanded = !this.expanded;
  83. }
  84. e.stopPropagation();
  85. }
  86. },
  87. loadObjectInfo() {
  88. const maxPropLen = 30;
  89. const maxPropStrLen = 10;
  90. let type = typeof this.obj;
  91. let finalString = '';
  92. let finalExpandable = '';
  93. if(type == 'object') {
  94. if(this.obj == null) {
  95. finalString = 'null';
  96. type = 'null';
  97. finalExpandable = false;
  98. } else {
  99. if(Object.prototype.toString.call(this.obj) == '[object Array]') {
  100. let str = '[ ';
  101. for (let i = 0; i < this.obj.length; i++) {
  102. let item = this.obj[i];
  103. if(typeof item === 'object')
  104. str += '{...}'
  105. else if(typeof item === 'string') {
  106. if(item.length < maxPropStrLen)
  107. str += `"${item}"`
  108. else
  109. str += `"${item.substr(0, maxPropStrLen)}..."`
  110. } else
  111. str += '' + item;
  112. if(str.length > maxPropLen) {
  113. if(i < this.obj.length - 1)
  114. str += ', ...'
  115. break;
  116. } else if(i != this.obj.length - 1) {
  117. str += ','
  118. }
  119. }
  120. finalString = str + ' ]';
  121. }
  122. else {
  123. let str = '{ ', i = 0, keys = Object.keys(this.obj);
  124. for (let i = 0; i < keys.length; i++) {
  125. const key = keys[i];
  126. const item = this.obj[key];
  127. if(typeof item === 'object')
  128. str += `${key}: {...}`
  129. else if(typeof item === 'string') {
  130. if(item.length < maxPropStrLen)
  131. str += `${key}: '${item}'`
  132. else
  133. str += `${key}: '${item.substr(0, maxPropStrLen)}...'`
  134. } else
  135. str += `${key}: ${item}`
  136. if(str.length > maxPropLen) {
  137. if(i < keys.length - 1)
  138. str += ', ...'
  139. break;
  140. } else if(i != keys.length - 1) {
  141. str += ','
  142. }
  143. }
  144. finalString = str + ' }';
  145. }
  146. this.expandObjs = null;
  147. finalExpandable = true;
  148. }
  149. } else if(type == 'number' || type == 'boolean') {
  150. finalString = this.obj;
  151. finalExpandable = false;
  152. } else if(type == 'function') {
  153. finalString = "f " + this.obj.toString().match(/function\s*([^(]*)\(/)[1] + "()";
  154. finalExpandable = false;
  155. } else if(type == 'undefined') {
  156. finalString = 'undefined';
  157. finalExpandable = false;
  158. } else if(type == 'string') {
  159. finalString = this.isKeyObj ? `"${this.obj}"` : this.obj;
  160. finalExpandable = false;
  161. } else {
  162. finalString = this.obj;
  163. finalExpandable = false;
  164. }
  165. this.$nextTick(() => {
  166. this.type = type;
  167. this.shortObjString = finalString;
  168. this.expandable = finalExpandable;
  169. })
  170. },
  171. },
  172. data() {
  173. return {
  174. type: '',
  175. shortObjString: '',
  176. expandObjs: null,
  177. expandable: false,
  178. expanded: false,
  179. }
  180. },
  181. mounted() {
  182. this.loadObjectInfo();
  183. },
  184. }
  185. </script>
  186. <style lang="scss">
  187. .js-obj {
  188. display: flex;
  189. flex-direction: column;
  190. }
  191. .js-child-host {
  192. display: flex;
  193. flex-direction: column;
  194. padding-left: 10rpx;
  195. }
  196. .js-child {
  197. display: flex;
  198. flex-direction: row;
  199. }
  200. .js-child-key {
  201. font-size: 30rpx;
  202. margin-right: 5rpx;
  203. color: #d10dc1;
  204. }
  205. .js-obj-text {
  206. font-size: 30rpx;
  207. flex: 1;
  208. &.expandable {
  209. background-color: #f0f0f0;
  210. padding: 2rpx 6rpx;
  211. border-radius: 4rpx;
  212. color: #b80aad;
  213. }
  214. &.expanded {
  215. background-color: #fff;
  216. }
  217. &.object {
  218. color: #555;
  219. }
  220. &.undefined, &.boolean, &.null {
  221. color: #d10dc1;
  222. }
  223. &.number {
  224. color: #006eff;
  225. }
  226. &.string {
  227. color: #cb0000;
  228. }
  229. &.function {
  230. font-style: italic;
  231. }
  232. &.error {
  233. color: #f06806;
  234. }
  235. &.info {
  236. color: #0abbf0;
  237. }
  238. &.log {
  239. color: #555;
  240. }
  241. &.warn {
  242. color: #f0c723;
  243. }
  244. }
  245. </style>