首页 > 微信 > 详细

微信小程序接口封装

时间:2020-01-09 18:44:48      阅读:91      评论:0      收藏:0      [点我收藏+]

一、创建utils/api.js

 

const host = ‘http://……‘

/**
 * 封装微信的request
 * form: ‘application/x-www-form-urlencoded‘
 */
function request(url, data = {}, method = "GET", contentType = ‘json‘) {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: host+url,
      data: data,
      method: method,
      header: {
        ‘Content-Type‘: contentType.toLowerCase() == ‘json‘ ? "application/json" : "application/x-www-form-urlencoded"
      },
      success: function(res) {
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.errMsg);
        }
      },
      fail: function(err) {
        reject(err)
      }
    })
  });
}

module.exports = {
  request: request
}

二、引入

const api = require(‘../../utils/api.js‘)

三、使用

api.request(‘接口‘, {
              openId: App.globalData.openId,
              id: this.data.id
            }, "POST", ‘form‘).then(res => {
              if (res.code === 0) {
                wx.showToast({
                  title: ‘成功‘,
                  icon: ‘none‘
             })
       }
})

微信小程序接口封装

原文:https://www.cnblogs.com/jing-zhe/p/12172334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!