微信小程序初体验--封装http请求
2023-09-27 14:29:06 时间
最近看了一下微信小程序,大致翻了一下,发现跟angular很相似的,但是比angular简单的很多具体可参考官方文档
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/page.html?t=2017112
http://www.cnblogs.com/happen-/p/6278327.html
下面将封装http请求服务部分的服务以及引用部分
// 本服务用于封装请求 // 返回的是一个promisepromise var sendRrquest = function (url, method, data, header) { var promise = new Promise(function (resolve, reject) { wx.request({ url: url, data: data, method: method, header: header, success: resolve, fail: reject }) }); return promise; }; module.exports.sendRrquest = sendRrquest
在utils文件中创建文件requestService.js文件
下边是在page.js文件中引用部分代码
// 界面一般通过使用Page函数注册一个界面,接收一个Object对象,该对象指定了初始化数据/生命周期函数函数/事件处理函数 // data 存放页面初始化数据数据,类似angular的的$scope // onLoad 生命周期函数 监听页面加载 // onReady 生命周期函数 监听页面首次渲染完成完成 // onShow 生命周期函数 监听界面显示 // onHide 生命周期函数 监听界面隐藏 // onUnload 生命周期函数 监听页面卸载 // onPullDownRefresh 页面相关事件 监听用户下拉事件 // onReachBottom 页面上拉到达底部触发的事件 // onShareAppmessage 点击左上方分享事件 var testService = require('../../utils/testService.js') var request = require('../../utils/requestService.js') Page({ data:{ test:'123', positionlist:[] }, onLoad:function(){ }, onReady: function () { var that = this; testService.test('a'); testService.agerntest('a'); var url = 'https://webapi.tianjihr.com/position/searcher?sort=-refresh_time&offset=10&limit=10'; request.sendRrquest(url, 'GET', {}, {}) .then(function (response) { that.setData({ positionlist:response.data.list }); console.log(response); }, function (error) { console.log(error); }); }, onPullDownRefresh: function () { }, onShareAppMessage: function () { // 微信分享需要的配置参数 return { title: '自定义分享标题', desc: '自定义分享描述', path: '/page/user?id=123' } // console.log(1); } });
上边的代码和js代码有不同的代码需要注意
1.异步处理方式改变
原有方式是:
var promise = new Promise(); promise.resolve('成功'); promise.reject('失败'); return promise;
现有的方式:
return new Promise(function (resolve, reject) { resolve('成功'); reject('失败'); })
2.在promise成功或者失败的回调中不能直接赋值,如:
var that = this; test() .then(function(){ that.data.test=''; },function(){ })
需要使用如下方式:
var that = this; test() .then(function(){ that.setDatat={ test:123 }; },function(){ })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
var
app = getApp();
function
request(url,postData,doSuccess,doFail,doComplete){
var
host = getApp().conf.host;
wx.request({
url: host+url,
data:postData,
method:
'POST'
,
success:
function
(res){
if
(
typeof
doSuccess ==
"function"
){
doSuccess(res);
}
},
fail:
function
() {
if
(
typeof
doFail ==
"function"
){
doFail();
}
},
complete:
function
() {
if
(
typeof
doComplete ==
"function"
){
doComplete();
}
}
});
}
}
module.exports.request = request;
|
相关文章
- 自然语言处理-应用场景-聊天机器人(一):fastText实现意图识别【QA/问答机器人、TASK/任务机器人、CHAT/闲聊机器人】--> 代码封装、对外提供接口
- php封装json通信接口详解及实例
- JDBC辅助类封装 及应用
- 【Jetpack】一次Android权限请求库的封装记录
- JS微信分享不好写?来封装一下
- DBhelper封装jdbc并解决特殊字符的问题
- 封装Redis缓存操作类,避免缓存雪崩、缓存击穿、缓存穿透
- 【微信公众平台开发】封装获取天气预报功能
- Entity Framework底层操作封装V2版本号(3)
- 微信小程序扫码封装
- vue 全选组件封装
- 封装方法个人总结
- 90.网络安全渗透测试—[常规漏洞挖掘与利用篇6]—[文件包含-PHP封装伪协议详解&实战示例]
- 微信小程序网络请求封装
- iOS-在AFN基础上进行网络请求的封装
- 自己封装的poi操作Excel工具类