helps.ets 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { InternalAxiosRequestConfig, AxiosResponse, AxiosError } from "@ohos/axios"
  2. import promptAction from '@ohos.promptAction'
  3. // 是否在控制台打印某项log
  4. const pConfig = false
  5. const pHeaders = false
  6. const pUrl = true
  7. const pParams = true
  8. const pBody = true
  9. const pResponse = true
  10. const pData = true
  11. const pError = true
  12. // @CustomDialog
  13. // struct CustomDialogExample {
  14. // controller: CustomDialogController
  15. // build() {
  16. // Column() {
  17. // Text('我是内容')
  18. // .fontSize(20)
  19. // .margin({ top: 10, bottom: 10 })
  20. // }
  21. // }
  22. // }
  23. const printRequest = (config: InternalAxiosRequestConfig) => {
  24. if (pConfig) {
  25. console.debug("printRequest config", JSON.stringify(config))
  26. }
  27. if (pHeaders) {
  28. console.debug("printRequest headers", JSON.stringify(config.headers))
  29. }
  30. if (pUrl) {
  31. console.debug("printRequest url", `Method:${config.method} ${config.baseURL}${config.url}`)
  32. }
  33. if (pBody) {
  34. console.debug("printRequest 请求参数data", JSON.stringify(config.data))
  35. }
  36. if (pParams) {
  37. console.debug("printRequest 请求参数params", JSON.stringify(config.params))
  38. }
  39. }
  40. const printResponse = (response: AxiosResponse) => {
  41. if (pResponse) {
  42. console.debug('printResponse response: ', JSON.stringify(response))
  43. }
  44. if (pData) {
  45. console.debug("printResponse data", JSON.stringify(response.data))
  46. }
  47. }
  48. const printError = (error: AxiosError) => {
  49. if (pError) {
  50. console.debug("printError error", error.message)
  51. }
  52. promptAction.showToast({
  53. message: error.message ?? "请求出错",
  54. duration: 1500,
  55. bottom: 100
  56. })
  57. }
  58. // 处理返回数据
  59. const handleRes = (response: AxiosResponse): [boolean, string] => {
  60. let isSuccess = true
  61. let msg = ""
  62. if (response.status === 200) { //判断返回状态是否为200
  63. if (String(response.data?.code) === "200") { //判断数据的code是否为200,有可能是500的服务器错误
  64. isSuccess = true
  65. msg = `请求数据成功`
  66. }
  67. else {
  68. isSuccess = false
  69. // msg = `${response.data?.code}: ${response.data?.msg ?? "服务器错误"}`
  70. msg = `${response.data?.msg ?? "服务器错误"}`
  71. }
  72. }
  73. else {
  74. isSuccess = false
  75. msg = `状态码非200 status: ${response.status}}`
  76. }
  77. if (!isSuccess) {
  78. promptAction.showToast({
  79. message: msg,
  80. duration: 2000,
  81. bottom: 100
  82. })
  83. // const dialogController: CustomDialogController = new CustomDialogController({
  84. // builder: CustomDialogExample({}),
  85. // })
  86. // dialogController.open()
  87. }
  88. return [isSuccess, msg]
  89. }
  90. export { printRequest, printResponse, printError, handleRes }