fetchApis.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * 此处填写文件描述
  3. * @Author: huangjq
  4. * @Date: 2019-12-27
  5. * @Project jz
  6. */
  7. const fs = require("fs")
  8. const request = require('request');
  9. //easy-mock的url
  10. const serverUrl = "http://192.168.10.14:7300"
  11. //项目ID
  12. const projectId = "5ca5ea72ab5bcfad10cf1607";
  13. //easy-mock的具有该项目权限的用户名密码
  14. const username = "huangjq"
  15. const password = "123456"
  16. //登录请求token
  17. request({
  18. url: `${serverUrl}/api/u/login`,
  19. method: "POST",
  20. json: true,
  21. headers: {
  22. "content-type": "application/json",
  23. },
  24. body: {name: username, password: password}
  25. }, function (error, response, body) {
  26. if (!error && response.statusCode === 200) {
  27. //请求项目信息
  28. request({
  29. url: `${serverUrl}/api/mock?project_id=${projectId}&page_size=2000&page_index=1&keywords=`,
  30. headers: {
  31. 'Authorization': `Bearer ${body.data.token}`,
  32. }
  33. }, function (error, response, body) {
  34. if (!error && response.statusCode === 200) {
  35. console.log("请求成功")
  36. let filePath = "./mock/serverApi.json"
  37. fs.writeFileSync(filePath, body)
  38. console.log('文件写入结束', filePath)
  39. } else {
  40. console.error("请求失败,请检查用户名或密码是否有误", error)
  41. }
  42. });
  43. }
  44. });