小程序模板网

微信小程序设置http请求的步骤详解----结合后台接口文档

分享者:kingrome2017,来自原文地址

 

				
  1. var root = "https://testapi.bidata.com.cn/platform/v3";
  2. var env = "dev";
  3.  
  4.  
  5. var _getURL = function(url) {
  6. if(env == "mock") {
  7. return url + ".json";
  8. } else {
  9. return root + url;
  10. }
  11. };
  12.  
  13. var _request = function(url,method,data,onSuccess,onError) {
  14. data = data || {};
  15. var behavior = data._behavior;
  16. if(behavior) {
  17. delete data._behavior;
  18. } else {
  19. behavior = "";
  20. }
  21. wx.request({
  22. "url" : _getURL(url),
  23. "method" : method,
  24. "data" : data,
  25. "success" : function(result) {
  26. var code = result.data.CODE || result.data.code;
  27. if(code == 200) {
  28. onSuccess(result);
  29. } else if(onError && typeof onError == 'function'){
  30. onError(result);
  31. } else {
  32. // if(result.data.code == 401) {
  33. // App.fireEvent("evt_invalid_token",result);
  34. // }
  35. // wx.showModal({
  36. // "title" : "提示",
  37. // "content" : result.data.msg,
  38. // "showCancel":false
  39. // });
  40.  
  41. }
  42. },
  43. "error" : onError || function(e) {
  44. console.error(e);
  45. },
  46. "header" : {
  47. // "X-Token" : wx.getStorageSync("token"),
  48. // "X-Behavior" : behavior,
  49. "X-Uid":"LMKCVXSU",
  50. "X-Nonce":"45494415",
  51. "X-Timestamp":"1493689633656",
  52. "X-Signature":'96f0ca5db1564eabd17c844d9927738fb9fed3a6'
  53.  
  54. }
  55. }
  56. )};
  57.  
  58. module.exports={
  59. $get : function(url,data,onSuccess,onError) {
  60. _request(url,"GET",data,onSuccess,onError);
  61. },
  62. $put : function(url,data,onSuccess,onError) {
  63. _request(url,"PUT",data,onSuccess,onError);
  64. },
  65. $post : function(url,data,onSuccess,onError) {
  66. _request(url,"POST",data,onSuccess,onError);
  67. },
  68. $delete : function(url,data,onSuccess,onError) {
  69. _request(url,"DELETE",data,onSuccess,onError);
  70. },
  71. root:root
  72. }


易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/23670.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询