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


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上找,下载白色的就可以
 
 
 
 
 
 
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM