123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- var _debug = true;
- var host = _debug ? 'https://test.toktok.beer/' : 'https://app.toktok.beer/';
- //http://192.168.2.65:7500/
- //https://test.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
- }
|