utils/config.js

var config = {
  APPID:'wxe1af0e00000000f3',
  BASE_URL:'https://xx.xxx.xx/xx/',
}
module.exports = config

utils/util.js 文件

var config = require('config.js')
function Post(url, data, cb,header=null) {

  if(header = 'session'){
    var headerObj = {
      'content-type': 'application/x-www-form-urlencoded',
      'Cookie': 'PHPSESSID=' + wx.getStorageSync('sessionid')
    }
  }else{
    var headerObj = {
      'content-type': 'application/x-www-form-urlencoded'
    }
  }
  wx.request({
    method: 'POST',
    url: config.BASE_URL + url,
    data: data,
    header: headerObj,
    success: (res) => {
      console.log("post 请求:" + config.BASE_URL + url);
      console.log(res.data)
      typeof cb == "function" && cb(res.data, "");
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg)
      console.log("post 请求:" + config.BASE_URL + url);
      console.log(err);
    }
  })
} 
module.exports = {
  httpPost: Post,
}

使用 如 /pages/index.js

const util = require('../../utils/util.js')
test: function() {
	util.httpPost('index.php?s=/sign/gettimeplan',{'userid':805},function(res){
		console.log(res.result[0])
	},'session')
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注