经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序之封装http请求
来源:cnblogs  作者:子钦加油  时间:2018/9/26 19:39:39  对本文有异议

下面将封装http请求服务部分的服务以及引用部分

  1. // 本服务用于封装请求
  2. // 返回的是一个promisepromise
  3.  
  4. var sendRrquest = function (url, method, data, header) {
  5. var promise = new Promise(function (resolve, reject) {
  6. wx.request({
  7. url: url,
  8. data: data,
  9. method: method,
  10. header: header,
  11. success: resolve,
  12. fail: reject
  13. })
  14. });
  15. return promise;
  16. };
  17. module.exports.sendRrquest = sendRrquest

在utils文件中创建文件requestService.js文件

下边是在page.js文件中引用部分代码

  1. // 界面一般通过使用Page函数注册一个界面,接收一个Object对象,该对象指定了初始化数据/生命周期函数函数/事件处理函数
  2. // data 存放页面初始化数据数据,类似angular的的$scope
  3. // onLoad 生命周期函数 监听页面加载
  4. // onReady 生命周期函数 监听页面首次渲染完成完成
  5. // onShow 生命周期函数 监听界面显示
  6. // onHide 生命周期函数 监听界面隐藏
  7. // onUnload 生命周期函数 监听页面卸载
  8. // onPullDownRefresh 页面相关事件 监听用户下拉事件
  9. // onReachBottom 页面上拉到达底部触发的事件
  10. // onShareAppmessage 点击左上方分享事件
  11.  
  12. var testService = require('../../utils/testService.js')
  13. var request = require('../../utils/requestService.js')
  14. Page({
  15. data:{
  16. test:'123',
  17. positionlist:[]
  18. },
  19. onLoad:function(){
  20. },
  21. onReady: function () {
  22. var that = this;
  23. testService.test('a');
  24. testService.agerntest('a');
  25. var url = 'https://webapi.tianjihr.com/position/searcher?sort=-refresh_time&offset=10&limit=10';
  26. request.sendRrquest(url, 'GET', {}, {})
  27. .then(function (response) {
  28. that.setData({
  29. positionlist:response.data.list
  30. });
  31. console.log(response);
  32. }, function (error) {
  33. console.log(error);
  34. });
  35. },
  36. onPullDownRefresh: function () {
  37. },
  38. onShareAppMessage: function () {
  39. // 微信分享需要的配置参数
  40. return {
  41. title: '自定义分享标题',
  42. desc: '自定义分享描述',
  43. path: '/page/user?id=123'
  44. }
  45. // console.log(1);
  46. }
  47. });

上边的代码和js代码有不同的代码需要注意

1.异步处理方式改变

原有方式是:

  1. var promise = new Promise();
  2. promise.resolve('成功');
  3. promise.reject('失败');
  4. return promise;

现有的方式:

  1. return new Promise(function (resolve, reject) {
  2. resolve('成功');
  3. reject('失败');
  4. })

2.在promise成功或者失败的回调中不能直接赋值,如:

  1. var that = this;
  2. test()
  3. .then(function(){
  4. that.data.test='';
  5. },function(){
  6. })

需要使用如下方式:

  1. var that = this;
  2. test()
  3. .then(function(){
  4. that.setDatat={
  5. test:123
  6. };
  7. },function(){
  8. })

微信小程序request请求封装

  1. 1 var app = getApp();
  2. 3
  3. 4 function request(url,postData,doSuccess,doFail,doComplete){
  4. 5
  5. 6    var host = getApp().conf.host;
  6. 8
  7. 9     wx.request({
  8. 10
  9. 11       url: host+url,
  10. 13
  11. 14       data:postData,
  12. 15
  13. 16       method: 'POST',
  14. 17
  15. 18       success: function(res){
  16. 19
  17. 20        if(typeof doSuccess == "function"){
  18. 22
  19. 23          doSuccess(res);
  20. 24
  21. 25        }
  22. 26
  23. 27       },
  24. 28
  25. 29       fail: function() {
  26. 31
  27. 32        if(typeof doFail == "function"){
  28. 34
  29. 35          doFail();
  30. 36
  31. 37        }
  32. 38
  33. 39       },
  34. 40
  35. 41       complete: function(){
  36. 43
  37. 44        if(typeof doComplete == "function"){
  38. 46
  39. 47          doComplete();
  40. 48   
  41. 49        }
  42. 51       }
  43. 52
  44. 53    });
  45. 55  }
  46. 57 }60
  47. 61 module.exports.request = request;

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号