http.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // get请求
  2. const httpServiceGet= async (path,params)=>{
  3. const {cloud} = getApp();
  4. my.showLoading({
  5. content: '加载中...',
  6. delay: 0,
  7. });
  8. try {
  9. console.log('request begin');
  10. const result = await cloud.application.httpRequest({
  11. //不需要完整域名,只需要接口访问路径即可
  12. // 'path' : '/servantappletservice/auth/registerUser',
  13. 'path' : path,
  14. 'method':'GET',
  15. 'headers':{ },
  16. 'params':params,
  17. // 'body':{ },
  18. //对于一个小程序关联多个云应用的场景,调用非默认云应用,需要指定对应的云应用Id,超时时间单位ms
  19. 'exts':{ "cloudAppId":"21907" ,"timeout":60000} });
  20. console.log('response');
  21. console.log(result);
  22. my.hideLoading({
  23. });
  24. if(result.errno=='0'){
  25. return result.data;
  26. }else{
  27. my.alert({
  28. content: result.errmsg
  29. });
  30. return Promise.reject(result);
  31. }
  32. } catch(e) {
  33. console.log(e);
  34. my.hideLoading({
  35. });
  36. my.alert({
  37. content: 'error'+e.message
  38. // content: e.msg
  39. });
  40. return Promise.reject(e);
  41. }
  42. }
  43. // post请求
  44. const httpServicePost= async (path,params)=>{
  45. const {cloud} = getApp();
  46. my.showLoading({
  47. content: '加载中...',
  48. delay: 0,
  49. });
  50. try {
  51. console.log('request begin');
  52. const result = await cloud.application.httpRequest({
  53. //不需要完整域名,只需要接口访问路径即可
  54. // 'path' : '/servantappletservice/auth/registerUser',
  55. 'path' : path,
  56. 'method':'POST',
  57. 'headers':{ 'Content-type': 'application/json' },
  58. 'params':{},
  59. 'body':params,
  60. //对于一个小程序关联多个云应用的场景,调用非默认云应用,需要指定对应的云应用Id,超时时间单位ms
  61. 'exts':{ "cloudAppId":"21907" ,"timeout":60000} });
  62. console.log('response');
  63. // console.log(JSON.stringify(result));
  64. // my.alert({
  65. // content: JSON.stringify(result)
  66. // });
  67. console.log(result);
  68. my.hideLoading({
  69. });
  70. if(result.errno=='0'){
  71. return result.data;
  72. }else{
  73. my.alert({
  74. content: result.errmsg
  75. });
  76. return Promise.reject(result);
  77. }
  78. } catch(e) {
  79. console.log(e);
  80. my.hideLoading({
  81. });
  82. my.alert({
  83. content: 'error'+e.message
  84. });
  85. return Promise.reject(e);
  86. // throw new Error(e)
  87. }
  88. }
  89. export default{
  90. httpServiceGet,
  91. httpServicePost
  92. }