sendRequest.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <template>
  2. <view>
  3. <view
  4. class="dialogMask"
  5. v-if="isShow"
  6. :style="{
  7. height: height + 'px',
  8. }"
  9. @click.stop
  10. >
  11. <view
  12. class="dialogContent"
  13. @click.stop
  14. >
  15. <view
  16. class="dialogHead"
  17. @click="hide(2)"
  18. >
  19. <view>
  20. <text class="title">请求构建工具</text>
  21. </view>
  22. <view>
  23. <image
  24. src="@/devTools/page/static/unfold.png"
  25. class="fold"
  26. />
  27. </view>
  28. </view>
  29. <scroll-view
  30. @click.stop
  31. scroll-y
  32. class="scrollList"
  33. :style="{
  34. height: dialogHeight + 'px',
  35. }"
  36. >
  37. <subTitleBar
  38. title="请求地址(url):"
  39. :showArrow="false"
  40. />
  41. <view class="inputRow">
  42. <input
  43. type="text"
  44. placeholder="请输入url地址"
  45. class="input"
  46. v-model="request.url"
  47. maxlength="-1"
  48. />
  49. <text
  50. class="del"
  51. @click="request.url = ''"
  52. >
  53. x
  54. </text>
  55. </view>
  56. <subTitleBar
  57. title="请求方式[method]:"
  58. :showArrow="false"
  59. />
  60. <view class="inputRow">
  61. <picker
  62. @change="request.method = requestMethods[$event.detail.value]"
  63. :value="requestMethods.findIndex((x) => x == request.method)"
  64. :range="requestMethods"
  65. >
  66. <view class="method">
  67. <text class="methodName">{{ request.method }}</text>
  68. <image
  69. class="unfold"
  70. src="@/devTools/page/static/unfold.png"
  71. />
  72. </view>
  73. </picker>
  74. </view>
  75. <subTitleBar
  76. title="请求参数[data]:(json对象)"
  77. :showArrow="false"
  78. />
  79. <view class="inputRow">
  80. <textarea
  81. placeholder="请输入JSON对象"
  82. class="textarea"
  83. v-model="request.data"
  84. maxlength="-1"
  85. />
  86. <text
  87. class="del"
  88. @click="request.data = ''"
  89. >
  90. x
  91. </text>
  92. </view>
  93. <subTitleBar
  94. title="请求头(header):"
  95. :showArrow="false"
  96. />
  97. <view class="inputRow">
  98. <textarea
  99. placeholder="请输入JSON对象"
  100. class="textarea"
  101. v-model="request.header"
  102. maxlength="-1"
  103. />
  104. <text
  105. class="del"
  106. @click="request.header = ''"
  107. >
  108. x
  109. </text>
  110. </view>
  111. <view
  112. :class="[send.status ? 'loading' : '']"
  113. class="sendBtn"
  114. @click="sendRequest"
  115. >
  116. <text
  117. v-if="send.status"
  118. class="sendBtnText"
  119. >
  120. 发送中[{{ send.t }}ms]
  121. <text class="msg">(点击可取消)</text>
  122. </text>
  123. <text
  124. v-else
  125. class="sendBtnText"
  126. >
  127. 发送
  128. </text>
  129. </view>
  130. <template v-if="ajaxHasRes">
  131. <subTitleBar
  132. title="响应结果:"
  133. :showArrow="false"
  134. />
  135. <view class="inputRow">
  136. <objectAnalysis
  137. :data="ajaxRes"
  138. :width="710"
  139. :isOpenFirst="true"
  140. />
  141. </view>
  142. </template>
  143. <view style="height: 100rpx"></view>
  144. </scroll-view>
  145. </view>
  146. </view>
  147. </view>
  148. </template>
  149. <script>
  150. import objectAnalysis from "../listItem/objectAnalysis.vue";
  151. import subTitleBar from "../ui/subTitleBar.vue";
  152. let success, error;
  153. /**
  154. * 转json字符串并格式化
  155. */
  156. function toJsonStr(obj) {
  157. return JSON.stringify(obj, null, 2);
  158. }
  159. export default {
  160. components: {
  161. subTitleBar,
  162. objectAnalysis,
  163. },
  164. data() {
  165. return {
  166. /**
  167. * 是否展示
  168. */
  169. isShow: false,
  170. /**
  171. * 屏幕高度
  172. */
  173. height: uni.getSystemInfoSync().windowHeight,
  174. dialogHeight: Math.ceil(uni.getSystemInfoSync().windowHeight * 0.85),
  175. requestMethods: ["get", "post", "put", "delete", "connect", "head", "options", "trace"],
  176. /**
  177. * 请求构建对象
  178. */
  179. request: {
  180. url: "", //请求地址
  181. header: "", //请求头
  182. method: "get", //请求方式
  183. data: "", //请求参数
  184. },
  185. /**
  186. * 是否有响应结果
  187. */
  188. ajaxHasRes: false,
  189. /**
  190. * 响应结果对象
  191. */
  192. ajaxRes: {},
  193. /**
  194. * 是否状态
  195. */
  196. send: {
  197. status: false, //是否处于发送中状态
  198. t: 0, //等待时间 ms
  199. time: 0, //发送时的时间
  200. },
  201. };
  202. },
  203. mounted() {
  204. let that = this;
  205. setInterval(() => {
  206. if (that.send.status) {
  207. that.send.t = new Date().getTime() - that.send.time;
  208. }
  209. }, 1000 / 24);
  210. },
  211. methods: {
  212. /**
  213. * 展示弹窗
  214. */
  215. show(item, needSend = false) {
  216. let that = this;
  217. if (that.send.status) {
  218. that.send.status = false;
  219. }
  220. if (that.ajaxHasRes) {
  221. that.ajaxHasRes = false;
  222. }
  223. return new Promise((yes, err) => {
  224. success = yes;
  225. error = err;
  226. if (item && item.url && item.method) {
  227. that.request.url = item.url;
  228. if (typeof item.method == "string") {
  229. that.request.method = item.method.toLocaleLowerCase();
  230. } else {
  231. that.request.method = "get";
  232. }
  233. try {
  234. let data = toJsonStr(item.data);
  235. if (Object.keys(data).length == 0) {
  236. data = "";
  237. } else {
  238. that.request.data = data;
  239. }
  240. } catch (error) {
  241. that.request.data = "";
  242. }
  243. try {
  244. that.request.header = toJsonStr(item.header);
  245. } catch (error) {
  246. that.request.header = toJsonStr({
  247. "content-type": "application/x-www-form-urlencoded",
  248. });
  249. }
  250. } else {
  251. that.request.url = "";
  252. that.request.data = "";
  253. that.request.method = "get";
  254. that.ajaxHasRes = false;
  255. that.request.header = toJsonStr({
  256. "content-type": "application/x-www-form-urlencoded",
  257. });
  258. }
  259. that.send.status = false;
  260. that.isShow = true;
  261. if (needSend) {
  262. that.sendRequest();
  263. }
  264. });
  265. },
  266. /**
  267. * 关闭弹窗
  268. */
  269. hide() {
  270. this.isShow = false;
  271. error();
  272. },
  273. /**
  274. * 发送请求
  275. */
  276. sendRequest() {
  277. let that = this;
  278. if (that.send.status) {
  279. return uni.showModal({
  280. title: "提示",
  281. content: "请求还在进行,是否确认取消请求?",
  282. success(res) {
  283. if (res.confirm) {
  284. if (that.send.status) {
  285. that.send.status = false;
  286. that.ajaxHasRes = false;
  287. }
  288. }
  289. },
  290. });
  291. }
  292. let tw = (m) =>
  293. uni.showToast({
  294. icon: "none",
  295. title: m,
  296. });
  297. if (that.request.url == "" || typeof that.request.url != "string") return tw("请输入url");
  298. if (that.request.url.indexOf("http") != 0) return tw("请输入正确的url地址");
  299. if (that.request.url.indexOf("://") == -1) return tw("请输入正确的url地址");
  300. if (that.request.url.length < 10) return tw("请输入正确的url地址");
  301. let data = {};
  302. if (that.request.data != "") {
  303. try {
  304. data = JSON.parse(that.request.data);
  305. } catch (error) {
  306. return tw("请求参数json解析失败!");
  307. }
  308. }
  309. let header = {};
  310. if (that.request.header) {
  311. try {
  312. header = JSON.parse(that.request.header);
  313. } catch (error) {
  314. return tw("请求头json解析失败!");
  315. }
  316. }
  317. header["Devtoolssend"] = 1;
  318. that.send.t = 0;
  319. that.send.time = new Date().getTime();
  320. that.send.status = true;
  321. that.ajaxHasRes = false;
  322. uni.request({
  323. url: that.request.url,
  324. method: that.request.method,
  325. data,
  326. header,
  327. success(res) {
  328. if (!that.send.status || !that.isShow) return;
  329. that.send.status = false;
  330. res["请求用时"] = new Date().getTime() - that.send.time + "ms";
  331. that.$set(that, "ajaxRes", res);
  332. that.ajaxHasRes = true;
  333. uni.showToast({
  334. title: "请求响应成功",
  335. icon: "success",
  336. });
  337. },
  338. fail(msg, request) {
  339. if (!that.send.status || !that.isShow) return;
  340. let res = {
  341. fail: msg,
  342. request,
  343. 请求用时: new Date().getTime() - that.send.time + "ms",
  344. };
  345. that.send.status = false;
  346. that.$set(that, "ajaxRes", res);
  347. that.ajaxHasRes = true;
  348. uni.showToast({
  349. title: "请求响应失败",
  350. icon: "error",
  351. });
  352. },
  353. });
  354. },
  355. },
  356. };
  357. </script>
  358. <style lang="scss" scoped>
  359. .dialogMask {
  360. display: flex;
  361. flex-direction: column-reverse;
  362. background-color: rgba(0, 0, 0, 0.3);
  363. width: 750rpx;
  364. flex: 1;
  365. /* #ifndef APP-PLUS */
  366. height: 100vh;
  367. backdrop-filter: blur(1px);
  368. /* #endif */
  369. position: fixed;
  370. left: 0;
  371. top: 0;
  372. z-index: 999;
  373. .dialogContent {
  374. display: flex;
  375. flex-direction: column;
  376. width: 750rpx;
  377. background-color: #fff;
  378. border-radius: 20rpx 20rpx 0 0;
  379. .dialogHead {
  380. display: flex;
  381. flex-direction: row;
  382. align-items: center;
  383. justify-content: space-between;
  384. height: 80rpx;
  385. border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  386. width: 750rpx;
  387. .title {
  388. margin-left: 20rpx;
  389. font-size: 24rpx;
  390. line-height: 28rpx;
  391. height: 28rpx;
  392. color: #333;
  393. }
  394. .fold {
  395. width: 20rpx;
  396. height: 20rpx;
  397. margin-right: 20rpx;
  398. }
  399. }
  400. .scrollList {
  401. width: 750rpx;
  402. .inputRow {
  403. width: 750rpx;
  404. padding: 0rpx 20rpx;
  405. position: relative;
  406. .input {
  407. width: 710rpx;
  408. border: 1px solid rgba(0, 0, 0, 0.05);
  409. padding-left: 5rpx;
  410. padding-top: 5rpx;
  411. padding-bottom: 5rpx;
  412. padding-right: 50rpx;
  413. font-size: 24rpx;
  414. border-radius: 6rpx;
  415. height: 60rpx;
  416. }
  417. .textarea {
  418. width: 710rpx;
  419. border: 1px solid rgba(0, 0, 0, 0.05);
  420. padding-left: 5rpx;
  421. padding-top: 5rpx;
  422. padding-bottom: 5rpx;
  423. padding-right: 50rpx;
  424. font-size: 24rpx;
  425. border-radius: 6rpx;
  426. height: 140rpx;
  427. }
  428. .del {
  429. position: absolute;
  430. right: 24rpx;
  431. top: 10rpx;
  432. height: 40rpx;
  433. background-color: #fff;
  434. padding: 0 10rpx;
  435. font-size: 24rpx;
  436. color: #999;
  437. line-height: 40rpx;
  438. }
  439. .method {
  440. display: flex;
  441. flex-direction: row;
  442. align-items: center;
  443. .methodName {
  444. font-size: 24rpx;
  445. color: #333;
  446. }
  447. .unfold {
  448. margin-left: 10rpx;
  449. width: 24rpx;
  450. height: 24rpx;
  451. }
  452. }
  453. }
  454. .sendBtn {
  455. width: 710rpx;
  456. margin-left: 20rpx;
  457. margin-top: 30rpx;
  458. margin-bottom: 30rpx;
  459. height: 60rpx;
  460. display: flex;
  461. flex-direction: row;
  462. align-items: center;
  463. justify-content: center;
  464. border-radius: 10rpx;
  465. background-color: rgb(255, 45, 85);
  466. &.loading {
  467. background-color: rgba(255, 45, 85, 0.5);
  468. }
  469. .sendBtnText {
  470. color: #fff;
  471. font-size: 26rpx;
  472. }
  473. .msg {
  474. color: #fff;
  475. font-size: 20rpx;
  476. margin-left: 20rpx;
  477. }
  478. }
  479. }
  480. }
  481. }
  482. </style>