1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- var app = getApp(); //引入全局app.js,我们可以在globalData中定义一些公用的数据,比如baseUrl、token
- const request = function(url,options){
- let baseUrl = `http://61.164.207.46:8000/yos/rest/index${url?url:''}`
-
- console.log(baseUrl)
- options.data.accesstoken = wx.getStorageSync('token')
- return new Promise((resolve,reject)=>{
- wx.request({
- url:baseUrl,
- method:options.method,
- data:options.method == "GET"?options.data:JSON.stringify(options.data),
- // header这里根据业务情况自行选择需要还是不需要
- header:{
- "Content-Type":'application/json;charset=UTF-8'
- },
- success: (res) => {
- if (res.data.code == -1) {
- wx.showToast({
- title: res.data.msg,
- icon:'none'
- })
- resolve(res.data)
- } else if(res.data.code == 0){
- wx.showToast({
- title: res.data.msg,
- duration:3000,
- icon:'none'
- })
- resolve(res.data)
- } else {
- resolve(res.data)
- }
- },
- fail: (err) => {
- reject(err)
- }
- })
- })
- }
- module.exports = {
- get(url,data){
- return request(url,{
- method:"GET",
- data
- })
- },
- //封装post方法
- post(url,data){
- return request(url,{
- method:"POST",
- data
- })
- }
- }
|