小程序网络请求方法&&半透明提示框(成功,警告,错误,提示)封装

util.js

function request(url, data, cb) {
  wx.request({
    url: 'https://www.baidu.com' + url,
    data: data,
    method: 'POST',
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    success: function (res) {
      return typeof cb == "function" && cb(res)
    },
    fail: function () {
    return typeof cb == "function" && cb(false)
    }
  })
}
 
function alert(types, msg) {
  switch (types) {
    case 1:
    wx.showToast({
      title: msg,
      image: '../../assets/alert-warn.png'
    });
    break;
  case 2:
    wx.showToast({
      title: msg,
      icon: 'success'
    });
  break;
  case 3:
    wx.showToast({
      title: msg,
      icon: 'loading'
    });
  break;
  case 4:
    wx.showToast({
      title: msg,
      image: '../../assets/alert-error.png'
    });
    break;
  }
}
 
module.exports.request = request
module.exports.alert = alert
 
app.js:
 
var http = require('utils/util.js');
 
func: {
 
  req: http.request
  alert:alert.alert
}
 
index.js(页面使用):
 
const app=getApp();
 
initial:function(){
 
  app.func.req("",{
    //data
  },function(res){
 
     app.alert.alert(1,"正在加载中")
 
  })
}
 
 
 
 
 提示:
 
1.提示,警示图片可以从iconfont上找,下载白色的就可以
 
 
 
 
 
 
 
 
posted @ 2017-12-20 15:24  红叶1994  阅读(3822)  评论(0编辑  收藏  举报