微信小程序 wx.request wepy 简单封装
2017-10-28 11:18
1866 查看
本文出自:
http://blog.csdn.net/wyk304443164
很简单
使用也很简单
只有一个思路,具体大家可以自由发挥哦~
http://blog.csdn.net/wyk304443164
很简单
import sha1 from './sha1' // sign // 签名 function sign (signObj = {}) { ... // 自行加密 return signObj } // GET请求 function GET (requestHandler, isShowLoading) { return request('GET', sign(requestHandler), isShowLoading) } // POST请求 function POST (requestHandler, isShowLoading) { return request('POST', sign(requestHandler), isShowLoading) } function request (method, requestHandler, isShowLoading = true) { // 加密 let params = requestHandler.params isShowLoading && wx.showLoading && wx.showLoading({title: '加载中...'}) return new Promise((resolve, reject) => { wx.request({ url: requestHandler.url, data: params, method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'Content-Type': method === 'POST' ? 'application/x-www-form-urlencoded' : 'application/json' }, success: function (res) { isShowLoading && wx.hideLoading && wx.hideLoading() // 解密 if (res.data.success) { resolve(res.data.data) } else { reject(res.data.data) // throw new Error('Network request success but data state not success') } }, fail: function () { // 因为hide会让showToast隐藏 isShowLoading && wx.hideLoading && wx.hideLoading() wx.showToast({ title: '网络请求失败', icon: 'success', image: '../style/images/toast_info.png', duration: 1500 }) reject(new Error('Network request failed')) // throw new Error('Network request failed') }, complete: function () { } }) }) } module.exports = { get: GET, post: POST }
使用也很简单
import { API_URL, commom, httpUtils } from '../config' httpUtils.get(`${API_URL.list}?showNum=${10}&page=${1}`) .then((data) => { that.listData = that.listData.concat(data.data) that.$apply() })
只有一个思路,具体大家可以自由发挥哦~
相关文章推荐
- 微信小程序 wepy wx.previewImage 封装
- 微信小程序 wx.request方法的异步封装实例详解
- 微信小程序显示 html wepy + wxParse
- 微信小程序封装请求方法wx.request(OBJECT)
- 微信小程序wx.request接口封装
- 微信小程序对wx.request函数进行封装操作的代码示例
- 微信小程序 wx.request 的封装
- 小程序wx.request请求接口封装
- 微信小程序网络请求wx.request请求
- 微信小程序wx.request程序
- 微信小程序网络请求 wx.request() ,data内参数后台获取不到
- 后端收不到微信小程序wx.request的传值问题 php
- 微信小程序- wx.request请求不到数据
- 微信小程序-wx.request
- 微信小程序之多文件下载的简单封装示例
- 微信小程序-wx.request获取数据
- 微信小程序wx.request请求回来的数据没有及时显示到页面上
- 微信小程序wx.request后台数据交互
- 微信小程序 request接口的封装实例代码
- 微信小程序之多文件下载的简单封装