博主信息
博文 38
粉丝 0
评论 3
访问量 51022
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
小程序公共接口请求
意外的博客
原创
2139人浏览过
//在公共文件utils/util.js下面

//地址;精确到控制器,方法在额外传入;
var requestUrl = "http://www.tp.cn/api/Home/";
var AppID = 'wx1678202cdfe457a6';
var AppSecret = 'b227e09e988d29045b4e0a1744f6d011';
//声明一个公共url方法;谁需要谁就调用;
function post(url, data, that, fun) {
//判断传入路径不为空;
if (url == 'undefined') {
return false;
}
//组装好完整的url地址;也就是接口;
var postUrl = requestUrl + url;
wx.request({
url: postUrl, //接口 ; 
data: data,   //参数;
method: "POST", //传入的方法;
dataType: "json", //传入的格式;
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
// that[fun](res.data.result);
//请求成功->得到接口数据(res.data)
that[fun](res.data); //返回a方法里的参数,加上接口里的参数;
// console.log(res.data);   //这个只获取到接口的值;通过回调返回给a方法;
},
fail: function (res) {
console.log('请求失败,请重试');
return {};
}
})
}

//登录接口
function login(url, data, fun, that) {
if (url == 'undefined') {
return false;
}
wx.login({
success: function (res) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: AppID,
secret: AppSecret,
js_code: res.code,
grant_type: 'authorization_code'
},
method: "POST",
dataType: "json",
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
// console.log(res);
data.unionid = 1;
post(url, data, fun, that);
},
fail: function (res) {
console.log('获取用户信息失败!');
return {};
}
})
}
});
}

//将方法post公开,也就是变成公共的;
module.exports.post = post;
//module.exports = {post:post}    和上面相等;
module.exports.login = login;


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学