var _debug = false; var host = _debug ? 'https://test.toktok.beer/' : 'https://app.toktok.beer/'; /** * POST请求 * URL:接口 * postData:参数,json类型 * doSuccess:成功的回调函数 * doFail:失败的回调函数 */ function postData(url, parmas, success, fail) { let token = wx.getStorageSync('Auth-Token') || ''; let header = { 'content-type': 'application/x-www-form-urlencoded', 'Auth-User-Agent': 'WechatApplet#WeChat#unknown#WeChat#100', 'Auth-Token': token } for (var key in parmas) { if(parmas[key] == null) { delete parmas[key]; } } wx.request({ //项目的真正接口,通过字符串拼接方式实现 url: host + url, header: header, data: parmas, method: 'POST', success(res) { _debug ? console.log(res) : ''; if(res.data.code==200) { success(res) }else if(res.data.code==401 || res.data.code==402) { wx.redirectTo({ url: '../login/login' }) }else{ wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } }, fail(res) { console.log(res) } }) } //GET请求,不需传参,直接URL调用, function getData(url, parmas, success) { let token = wx.getStorageSync('Auth-Token') || ''; let header = { 'content-type': 'application/x-www-form-urlencoded', 'Auth-User-Agent': 'WechatApplet#WeChat#unknown#WeChat#100', 'Auth-Token': token } for (var key in parmas) { if(parmas[key] == null) { delete parmas[key]; } } wx.request({ url: host + url, header: header, data: parmas, method: 'GET', success(res) { _debug ? console.log(res) : ''; if(res.data.code==200) { success(res) }else if(res.data.code==401 || res.data.code==402) { wx.redirectTo({ url: '../login/login' }) }else{ wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } }, fail(res) { console.log(res) } }) } function getUrlKey (url, name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null } /** * module.exports用来导出代码 * js文件中通过var call = require("../util/request.js") 加载 * 在引入引入文件的时候" "里面的内容通过../../../这种类型,小程序的编译器会自动提示,因为你可能 * 项目目录不止一级,不同的js文件对应的工具类的位置不一样 */ module.exports = { _debug: _debug, host: host, postData: postData, getData: getData, getUrlKey: getUrlKey }