当前位置:   article > 正文

微信小程序之调用接口方法封装_微信小程序method="put"用法

微信小程序method="put"用法

首相是http.js文件代码:

const app = getApp();

var http = {

get: function(url, data, callback) {

data.access_token = wx.getStorageSync('access_token');

wx.request({

url: app.domain + url,

method: 'get',

header: {

'companyId':21,

'appid': app.appid,

'appSecret': app.appSecret,

'access_token': wx.getStorageSync("access_token"),

'Cookie': 'access_token='+wx.getStorageSync("access_token")

},

data: data,

success: res => {

callback(null, res.data);

},

fail: err => {

callback(err)

}

})

},

post: function(url, data, callback, formStyle) {

data.access_token = wx.getStorageSync('access_token');

 

wx.request({

url: app.domain + url,

method: 'post',

header: {

'companyId': 21,

'appid': app.appid,

'appSecret': app.appSecret,

'access_token': wx.getStorageSync("access_token"),

'Cookie': 'access_token=' + wx.getStorageSync("access_token"),

'content-type': typeof formStyle == "undefined" ? 'application/json' : formStyle

},

data: data,

success: res => {

callback(null, res.data);

},

fail: err => {

callback(err)

}

})

},

put: function(url, data, callback, formStyle) {

data.access_token = wx.getStorageSync('access_token');

 

wx.request({

url: app.domain + url + "?access_token=" + data.access_token,

method: 'put',

header: {

'companyId': 21,

'appid': app.appid,

'appSecret': app.appSecret,

'access_token': wx.getStorageSync("access_token"),

'Cookie': 'access_token=' + wx.getStorageSync("access_token"),

'content-type': typeof formStyle == "undefined" ? 'application/json' : formStyle

},

data: data,

success: res => {

callback(null, res.data);

},

fail: err => {

callback(err)

}

})

},

delete: function(url, data, callback, formStyle) {

data.access_token = wx.getStorageSync('access_token');

wx.request({

url: app.domain + url,

method: 'delete',

header: {

'companyId': 21,

'appid': app.appid,

'appSecret': app.appSecret,

'access_token': wx.getStorageSync("access_token"),

'Cookie': 'access_token=' + wx.getStorageSync("access_token"),

'content-type': typeof formStyle == "undefined" ? 'application/json' : formStyle

},

data: data,

success: res => {

callback(null, res.data);

},

fail: err => {

callback(err)

}

})

}

};

module.exports = http;

 

 

 

2.将固定前缀等参数定义在app.js中

domain: 'http://a.test.cn/',

signKey: 'ec3c9d73689c4e24ae7e4783dda56a60',

 

 

调用举例:

var staffId = this.data.staffData.staffId;

var fileId = this.data.fileData.fileId;

var accessUrl = this.data.avatar;

http.post("/updateAvatar", {

staffId: staffId,

accessUrl: accessUrl,

fileId: fileId

}, function (err, res) {

if (res.errCode == 0) {

const toast = Toast.success('修改成功');

Toast.clear();

}

})

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/583751
推荐阅读
  

闽ICP备14008679号