1.封装

api.js

// 配置API接口地址
const baseUrl = 'http://www.kuitao8.com/'; // 引入 弹窗组件
var modal = weex.requireModule('modal');
// 引入 请求数据组件
var stream = weex.requireModule('stream');
// 身份验证
import jwtdecode from 'jwt-simple'; // 自定义判断元素类型JS
function toType (obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
} // 参数过滤函数
function filterNull (o) {
for (var key in o) {
if (o[key] === null) {
delete o[key]
}
if (toType(o[key]) === 'string') {
o[key] = o[key].trim()
} else if (toType(o[key]) === 'object') {
o[key] = filterNull(o[key])
} else if (toType(o[key]) === 'array') {
o[key] = filterNull(o[key])
}
}
return o
} // 工具方法
function toParams(obj) {
var param = ""
for(const name in obj) {
if(typeof obj[name] != 'function') {
param += "&" + name + "=" + encodeURI(obj[name])
}
}
return param.substring(1)
}; /**
* 接口处理函数
*/
function apiStream (method, url, params, success, failure) {
// 过滤参数
if (params) {
params = filterNull(params)
} /*** stream ***/
if(method === 'GET'){
// GET 方法
stream.fetch({
method: 'GET',
type: 'text',
url: baseUrl + url + toParams(params)
}, function(res) {
if (res.ok) {
// 解密
let currentData = jwtdecode.decode(res.data, 'michahzdee2016', 'HS256');
success(currentData);
}else {
modal.toast({
message: '请求失败,请检查网络!',
duration: 2
})
}
})
}else if(method === 'POST'){
// POST 方法
stream.fetch({
method: 'POST',
type: 'text',
url: baseUrl + url,
headers: {'Content-Type':'application/x-www-form-urlencoded'},
body: toParams(params)
}, function(res) {
if (res.ok) {
// 解密
let currentData = jwtdecode.decode(res.data, 'michahzdee2016', 'HS256');
success(currentData);
}else {
modal.toast({
message: '请求失败,请检查网络!',
duration: 2
})
}
},function(progress) {
//
})
}
}; // 返回在vue模板中的调用接口
export default {
get: function (url, params, success, failure) {
return apiStream('GET', url, params, success, failure)
},
post: function (url, params, success, failure) {
return apiStream('POST', url, params, success, failure)
}
}

  

2.entry.js  全局注册

import api from './api'
// 将API方法绑定到全局
Vue.prototype.$api = api

3.页面调用

let params = {
catid:10,
pagesize:20
}
/*请求数据*/
this.$api.get('webservice/Api/List?',params,function(data) {
console.log(JSON.stringify(data));
})

.

最新文章

  1. This is US 我们的生活
  2. 实现滑动可固定header以及页面刷新
  3. [moka同学收藏]网页上的“返回上一页”的几种实现代码
  4. k8s volume
  5. 安装eclipse的hadoop开发环境
  6. asp.net 未能写入输出文件--“拒绝访问”的解决办法
  7. Configure a welcome page in Struts
  8. (转载)PHP 判断常量,变量和函数是否存在
  9. uva 10026 Problem C: Edit Step Ladders
  10. storm 使用过程中遇到的问题
  11. 判断数字 字母 isDigit(), isalpha()
  12. 如何通过Visual Studio来管理我们的数据库项目
  13. 此地址使用了一个通常用于网络浏览以外目的的端口。出于安全原因,Firefox 取消了该请求。
  14. Base64格式上传文件至阿里云(java)
  15. UVA-10375 唯一分解定理
  16. ElasicSearch(2) Linux运行
  17. Spring@Autowired注解与自动装配(转发)
  18. Linux下计划任务以及crontab权限问题
  19. [luogu P4230]连环病原体
  20. 【LeetCode】199. Binary Tree Right Side View

热门文章

  1. 16,re模块的常用方法
  2. 读CSS DIV网页样式与布局心得体会
  3. NYOJ 1023 还是回文
  4. 九度oj 题目1262:Sequence Construction puzzles(I)_构造全递增序列
  5. 【Luogu】P2158仪仗队(欧拉函数)
  6. [BZOJ1592] [Usaco2008 Feb]Making the Grade 路面修整(DP)
  7. Linux 如何实现 VLAN
  8. 洛谷P3393 逃离僵尸岛
  9. 16.1114 模拟考试T1
  10. HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】