小程序網絡請求方法&&半透明提示框(成功,警告,錯誤,提示)封裝


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