• 微信小程序的接口调用封装


    首先呢是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)
    
    }
    
    })
    
    }
    
    };

    将固定前缀等参数定义在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();
    
    }
    
    })
  • 相关阅读:
    字符编码 进制转换
    Android工具HierarchyViewer 代码导读(1) 功能实现演示
    jQuery中的bind(), live(), on(), delegate()
    [转]ActionScript3.0中XML处理方法
    Pane和Panel的区别
    [转]在命令行中编译运行Java Applet
    [转]关于五险一金,你知道多少?
    [转]ActionScript3.0对象深复制
    [转]用Flashbug调试Flash
    [转]用EditPlus搭建简易的Java开发环境
  • 原文地址:https://www.cnblogs.com/mzj143/p/13190313.html
Copyright © 2020-2023  润新知