networkItem.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <view
  3. class="networkItem"
  4. :class="['type-' + item.type]"
  5. @longpress.stop="networkLongpress"
  6. >
  7. <view class="content">
  8. <view class="head">
  9. <view
  10. class="method"
  11. :class="'type-' + item.method"
  12. >
  13. <text
  14. class="methodText"
  15. :class="'type-' + item.method"
  16. >
  17. {{ item.method }}
  18. </text>
  19. </view>
  20. <view class="path">
  21. <text class="pageText">{{ getPath(item.url) }}</text>
  22. </view>
  23. </view>
  24. <objectAnalysis
  25. :data="getItem(item)"
  26. :width="710"
  27. :canLongpress="false"
  28. @onLongpress="networkLongpress"
  29. />
  30. <view class="msgBar">
  31. <text class="data">
  32. {{ item.date }}
  33. </text>
  34. <text
  35. class="time"
  36. :style="{
  37. width: '100rpx',
  38. color: getTimeColor,
  39. }"
  40. >
  41. {{ item.useTime }}s
  42. </text>
  43. <text
  44. class="status"
  45. :class="'s-' + item.type"
  46. style="width: 120rpx"
  47. >
  48. {{ getTypeName(item.type) }}
  49. </text>
  50. <text
  51. v-if="item.type == 1"
  52. class="time"
  53. :style="{
  54. color: getSizeColor,
  55. }"
  56. >
  57. {{ getByteSize }}
  58. </text>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import objectAnalysis from "./objectAnalysis.vue";
  65. export default {
  66. components: {
  67. objectAnalysis,
  68. },
  69. props: {
  70. /**
  71. * console单行数据
  72. */
  73. item: {
  74. type: Object,
  75. default() {
  76. return {
  77. id: 0, //请求id
  78. type: 0, // 0发起请求中 1请求成功 2请求失败
  79. date: "", //请求日期
  80. sendTime: 0, //发送请求的时间
  81. responseTime: 0, //响应时间
  82. useTime: 0, //请求总耗时
  83. url: "", //请求地址
  84. header: "", //请求头
  85. method: "get", //请求方式
  86. data: "", //请求参数
  87. responseBody: "", //响应主体
  88. responseHeader: "", //响应头
  89. responseStatus: "", //响应编码
  90. responseMsg: "", //响应报错信息
  91. responseBodySize: 0, //请求主体大小
  92. };
  93. },
  94. },
  95. },
  96. computed: {
  97. /**
  98. * 获取请求时间的颜色
  99. */
  100. getTimeColor() {
  101. if (this.item.useTime == 0) {
  102. return "#eeeeee";
  103. } else if (this.item.useTime > 3) {
  104. return "#fa3534";
  105. } else if (this.item.useTime > 1) {
  106. return "#ff9900";
  107. } else {
  108. return "#909399";
  109. }
  110. },
  111. /**
  112. * 获取字节大小,b转kb mb
  113. */
  114. getByteSize() {
  115. let size = Number(this.item.responseBodySize);
  116. if (null == size || size == "") return "0.00 KB";
  117. var unitArr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  118. var index = 0;
  119. var srcsize = parseFloat(size);
  120. index = Math.floor(Math.log(srcsize) / Math.log(1024));
  121. size = srcsize / Math.pow(1024, index);
  122. size = size.toFixed(2); //保留的小数位数
  123. if (Number(this.item.responseBodySize) < 1024) {
  124. return (size / 1000).toFixed(2) + "KB";
  125. }
  126. return size + unitArr[index];
  127. },
  128. /**
  129. * 获取响应大小的颜色
  130. */
  131. getSizeColor() {
  132. let size = this.item.responseBodySize;
  133. if (size == 0) {
  134. return "#fa3534";
  135. } else if (size > 256 * 1024) {
  136. return "#ff9900";
  137. } else if (size > 1024 * 1024) {
  138. return "#fa3534";
  139. } else {
  140. return "#909399";
  141. }
  142. },
  143. },
  144. methods: {
  145. /**
  146. * 通过url获取路径
  147. */
  148. getPath(url) {
  149. if (!url) return "无";
  150. function getPathFromUrl(url) {
  151. const pathStart = url.indexOf("//") + 2;
  152. const pathEnd = url.indexOf("?", pathStart) >= 0 ? url.indexOf("?", pathStart) : url.length;
  153. return url.substring(url.indexOf("/", pathStart), pathEnd);
  154. }
  155. return getPathFromUrl(url);
  156. },
  157. /**
  158. * 获取请求类型名称
  159. */
  160. getTypeName(type) {
  161. return ["请求中...", "请求完成", "请求失败"][Number(type)];
  162. },
  163. /**
  164. * 精简item
  165. */
  166. getItem(item) {
  167. return {
  168. data: item.data,
  169. responseMsg: item.responseMsg,
  170. responseStatus: item.responseStatus,
  171. method: item.method,
  172. url: item.url,
  173. header: item.header,
  174. responseBody: item.responseBody,
  175. responseHeader: item.responseHeader,
  176. responseBodySize: this.getByteSize,
  177. };
  178. },
  179. /**
  180. * 长按事件
  181. */
  182. networkLongpress() {
  183. let that = this;
  184. let menu = [
  185. {
  186. text: `重发此请求`,
  187. click() {
  188. that.$emit("goSendRequest", that.item);
  189. },
  190. },
  191. {
  192. text: `复制请求日志信息`,
  193. click() {
  194. uni.setClipboardData({
  195. data: JSON.stringify(that.item),
  196. });
  197. },
  198. },
  199. {
  200. text: `在请求构建工具中打开`,
  201. click() {
  202. that.$emit("goOpenRequest", that.item);
  203. },
  204. },
  205. {
  206. text: `删除此记录`,
  207. click() {
  208. uni.$emit("devTools_delNetworkItemById", that.item.id);
  209. uni.showToast({
  210. title: "删除成功!",
  211. icon: "success",
  212. });
  213. },
  214. },
  215. ];
  216. uni.showActionSheet({
  217. itemList: menu.map((x) => x.text),
  218. success({ tapIndex }) {
  219. menu[tapIndex].click();
  220. },
  221. });
  222. },
  223. },
  224. };
  225. </script>
  226. <style lang="scss" scoped>
  227. .networkItem:active {
  228. background-color: rgba(0, 0, 0, 0.03);
  229. }
  230. .networkItem {
  231. display: flex;
  232. flex-direction: row;
  233. align-items: flex-start;
  234. justify-content: space-between;
  235. width: 750rpx;
  236. padding: 10rpx 20rpx;
  237. border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  238. &.type-0 {
  239. background-color: rgb(255, 251, 229);
  240. }
  241. &.type-2 {
  242. background-color: rgb(255, 240, 240);
  243. }
  244. .content {
  245. width: 710rpx;
  246. display: flex;
  247. flex-direction: column;
  248. .head {
  249. display: flex;
  250. flex-direction: row;
  251. align-items: center;
  252. margin-bottom: 5rpx;
  253. .method {
  254. background-color: #e2e2e2;
  255. color: #333;
  256. border-radius: 4rpx;
  257. padding: 4rpx 6rpx;
  258. border-radius: 10rpx;
  259. &.type-get {
  260. background-color: rgba(168, 25, 197, 0.1);
  261. }
  262. &.type-post {
  263. background-color: rgba(255, 217, 0, 0.1);
  264. }
  265. .methodText {
  266. color: #333;
  267. font-size: 22rpx;
  268. line-height: 22rpx;
  269. /* #ifndef APP-PLUS */
  270. max-width: 650rpx;
  271. /* #endif */
  272. &.type-get {
  273. // color: #fff;
  274. }
  275. &.type-post {
  276. // color: #fff;
  277. }
  278. }
  279. }
  280. .path {
  281. width: 620rpx;
  282. /* #ifdef APP-PLUS */
  283. width: 620rpx;
  284. /* #endif */
  285. /* #ifndef APP-PLUS */
  286. max-width: 620rpx;
  287. /* #endif */
  288. lines: 1;
  289. margin-left: 6rpx;
  290. overflow: hidden;
  291. /* #ifdef H5 */
  292. // 限制行数
  293. display: -webkit-box;
  294. -webkit-box-orient: vertical;
  295. -webkit-line-clamp: 1;
  296. /* #endif */
  297. .pageText {
  298. font-size: 24rpx;
  299. color: #333;
  300. }
  301. }
  302. }
  303. .context {
  304. font-size: 20rpx;
  305. color: #333;
  306. line-height: 24rpx;
  307. }
  308. .msgBar {
  309. display: flex;
  310. flex-direction: row;
  311. margin-top: 5rpx;
  312. .data {
  313. font-size: 16rpx;
  314. color: #888;
  315. /* #ifndef APP-PLUS */
  316. min-width: 90rpx;
  317. /* #endif */
  318. }
  319. .time {
  320. font-size: 16rpx;
  321. color: #333;
  322. margin-left: 20rpx;
  323. }
  324. .status {
  325. margin-left: 20rpx;
  326. font-size: 16rpx;
  327. &.s-0 {
  328. color: #fa3534;
  329. }
  330. &.s-1 {
  331. color: #909399;
  332. }
  333. &.s-2 {
  334. color: #ff9900;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. </style>