支付宝小程序通用公共方法封装

const App = getApp(); //获取全局数据
var qts = () => {
  //网络数据请求
  this.request = (url, OBJECT, isload) => {
    //是否开发加载效果
    this.showLoading(isload)
    return new Promise((resolve, reject) => {
      //获取数据
      var defaults = {};
      //合并对象参数
      var datas = Object.assign(defaults, OBJECT);
      my.request({
        url: url,
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: JSON.stringify(datas),
        timeout: 50000,
        dataType: 'json',
        success: (res) => {
          this.hideLoading(isload);
          resolve(res)
        },
        fail: function(res) {
          my.hideLoading();
          if (res.error == 12) {
            this.toast('网络出错');
          } else if (res.error = 13) {
            resolve(res.error)
          } else if (res.error = 14) {
            this.toast('解析失败')
          }
        }
      });
    })
  };
  //提示框
  this.toast = (content, type) => {
    my.showToast({
      type: type == undefined ? "none" : type, //默认空类型
      content: content,
      duration: 2000
    })
  };
  //确认框
  this.confirm = (data) => {
    var obj = data;
    return new Promise((resolve, reject) => {
      my.confirm({
        title: obj.title || '温馨提示',
        content: obj.content,
        confirmButtonText: obj.confirmButtonText || '确定',
        cancelButtonText: obj.cancelButtonText || '取消',
        success: (result) => {
          if (result.confirm) {
            resolve(true);
          } else {
            resolve(false);
          }
        }
      });
    })
  }
  //加载状态
  this.showLoading = (isload, content) => {
    if (isload) {
      my.showLoading({
        content: content === undefined ? '加载中...' : content
      })
    }
  };
  //关闭加载状态
  this.hideLoading = (isload) => {
    if (isload) {
      my.hideLoading()
    }
  };
  //获取手机信息
  this.getSystemInfoPage = () => {
    return new Promise((resolve, reject) => {
      my.getSystemInfo({
        success: (res) => {
          resolve(res)
        }
      })
    })
  };
  //缓存数据
  this.setStorage = (name, datas) => {
    my.setStorage({
      key: name,
      data: datas,
      fail: () => {
        this.toast('缓存失败');
      }
    });
  };
  //获取缓存的数据
  this.getStorage = (name) => {
    return new Promise((resolve, reject) => {
      my.getStorage({
        key: name,
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          this.toast('获取缓存失败');
        }
      });
    })
  };
  //清除缓存
  this.removeStorage = (name) => {
    return new Promise((resolve, reject) => {
      my.removeStorage({
        key: name,
        success: () => {
          resolve('success');
        },
        fail: () => {
          this.toast('清除缓存失败')
        }
      });
    })
  };
  //预览图片
  this.previewImage = (currentIndex, arr) => {
    my.previewImage({
      current: currentIndex,
      urls: arr
    })
  };
  //全局参数调用,不建议多用
  this.globalData = () => {
    return App.globalData
  }
  //创建对象的处理
  if (!(this instanceof qts)) {
    return new qts();
  };
};

//定义输出接口
module.exports = {
  qts: qts
}

//使用方法
var qts = require('/utils/qts.js').qts();
qts.request(url, data).then((res) => {});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值