12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { requestUrl, instanceRequset, instanceResponse } from '../config'
- class Index {
- // 全局报错提示
- static err = () => wx.$err = () => wx.showModal({ showCancel: false, title: "报错提示", confirmText: "了解", content: JSON.stringify(data) })
- // 获取系统信息
- static system = that => {
- return new Promise((resolve, reject) => {
- // color ui使用
- try {
- let { statusBarHeight: StatusBar } = wx.getSystemInfoSync(),
- capsule = wx.getMenuButtonBoundingClientRect();
- that.globalData = capsule ? { StatusBar, Custom: capsule, CustomBar: capsule.bottom + capsule.top - StatusBar } : { StatusBar, CustomBar: StatusBar + 50 };
- resolve();
- } catch (err) { reject(err) }
- })
- }
- // 全局请求封装
- static request = () => {
- function URL (argA, argB) {
- let reg1 = /^http:[?=.a-zA-Z0-9_\/\\-]+$/i,
- reg2 = /^https:[?=.a-zA-Z0-9_\/\\-]+$/i;
- if (argB.length == 0 || reg1.test(argA) || reg2.test(argA)) return argA;
- return argB + argA;
- }
- function _request (data) {
- data["url"] = URL(data.url, requestUrl);
- data = instanceRequset(data);
- return new Promise((resolve, reject) => {
- wx.request({
- ...data,
- success: function (res) {
- try { resolve(instanceResponse(res)) } catch (err) { reject(err) }
- },
- fail: function (err) {
- reject(err)
- },
- })
- })
- }
- wx.$request = _request;
- }
- }
- async function init () {
- try {
- await Index.system(this);
- Index.err();
- Index.request();
- this.globalData = { ...this.globalData };
- console.log(this.globalData)
- } catch (err) {
- console.log(err)
- }
- }
- module.exports = init;
|