init.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { requestUrl, instanceRequset, instanceResponse } from '../config'
  2. class Index {
  3. // 全局报错提示
  4. static err = () => wx.$err = () => wx.showModal({ showCancel: false, title: "报错提示", confirmText: "了解", content: JSON.stringify(data) })
  5. // 获取系统信息
  6. static system = that => {
  7. return new Promise((resolve, reject) => {
  8. // color ui使用
  9. try {
  10. let { statusBarHeight: StatusBar } = wx.getSystemInfoSync(),
  11. capsule = wx.getMenuButtonBoundingClientRect();
  12. that.globalData = capsule ? { StatusBar, Custom: capsule, CustomBar: capsule.bottom + capsule.top - StatusBar } : { StatusBar, CustomBar: StatusBar + 50 };
  13. resolve();
  14. } catch (err) { reject(err) }
  15. })
  16. }
  17. // 全局请求封装
  18. static request = () => {
  19. function URL (argA, argB) {
  20. let reg1 = /^http:[?=.a-zA-Z0-9_\/\\-]+$/i,
  21. reg2 = /^https:[?=.a-zA-Z0-9_\/\\-]+$/i;
  22. if (argB.length == 0 || reg1.test(argA) || reg2.test(argA)) return argA;
  23. return argB + argA;
  24. }
  25. function _request (data) {
  26. data["url"] = URL(data.url, requestUrl);
  27. data = instanceRequset(data);
  28. return new Promise((resolve, reject) => {
  29. wx.request({
  30. ...data,
  31. success: function (res) {
  32. try { resolve(instanceResponse(res)) } catch (err) { reject(err) }
  33. },
  34. fail: function (err) {
  35. reject(err)
  36. },
  37. })
  38. })
  39. }
  40. wx.$request = _request;
  41. }
  42. }
  43. async function init () {
  44. try {
  45. await Index.system(this);
  46. Index.err();
  47. Index.request();
  48. this.globalData = { ...this.globalData };
  49. console.log(this.globalData)
  50. } catch (err) {
  51. console.log(err)
  52. }
  53. }
  54. module.exports = init;