global.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const REQUESTURL = "http://192.168.2.164:8888"; //请求路径
  2. // 短文字提示框
  3. const LOADING = function(msg) {
  4. uni.showLoading({
  5. title: msg, // 除支付宝最多显示7个汉字长度
  6. icon: "none"
  7. })
  8. };
  9. // 请求参数拦截器
  10. const REQUESTOPTIONSINTERCEPT = function(options) {
  11. return options;
  12. };
  13. // 服务器应答数据拦截器
  14. const REQUESTSUCCESSINTERCEPT = function(res) {
  15. res = res.data;
  16. if (res.code != 0) throw res.msg;
  17. return res;
  18. };
  19. // 传递给global的参数
  20. const GLOBALOPTIONS = {
  21. REQUESTURL
  22. };
  23. // 将暴露在整个项目的全局自定义类
  24. class global {
  25. constructor(options) {
  26. this.options = options;
  27. this.loading = LOADING;
  28. this.systemInfo = getSystemInfo();
  29. this.prev = Date.now(); //节流使用
  30. }
  31. // 获取二维码参数 options.scene
  32. qrcodeValue(scene){
  33. scene=decodeURIComponent(scene).split("&");
  34. scene = scene.map(item => (item.split("=")));
  35. return Object.fromEntries(scene);
  36. }
  37. // 富文本处理
  38. richTextChange(value){
  39. /**
  40. * 此代码段处理目的为,匹配富文本代码中的 <img> 标签,并将其图片的宽度修改为适应屏幕
  41. * max-width:100% --- 图片宽度加以限制,避免超出屏幕
  42. * height:auto --- 高度自适应
  43. * display:block --- 此代码,可以去掉图片之间的空白间隔,个人觉得好用
  44. */
  45. value = value.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
  46. return value;
  47. }
  48. // 数字转字符串占位
  49. numberPlaceholder(number, len = 2) {
  50. var string = number.toString();
  51. if (string.length > len) return string;
  52. for (let i = 0; i < len - string.length; i++) {
  53. string = '0' + string;
  54. }
  55. return string;
  56. }
  57. // 防抖
  58. debounce(func, wait = 1000) {
  59. if (typeof func !== 'function') {
  60. throw new TypeError('need a function arguments')
  61. }
  62. let timeid = null;
  63. let result;
  64. return function() {
  65. let context = this;
  66. let args = arguments;
  67. if (timeid) {
  68. clearTimeout(timeid);
  69. }
  70. timeid = setTimeout(function() {
  71. result = func.apply(context, args);
  72. }, wait);
  73. return result;
  74. }
  75. }
  76. // 节流
  77. throttle(func, delay = 1000) {
  78. let that = this;
  79. return function() {
  80. var context = this;
  81. var args = arguments;
  82. var now = Date.now();
  83. if (now - that.prev >= delay) {
  84. console.log("执行")
  85. func.apply(context, args);
  86. that.prev = Date.now();
  87. }
  88. }
  89. }
  90. // 请求
  91. request(options) {
  92. options.url = this.options.REQUESTURL + options.url;
  93. options = REQUESTOPTIONSINTERCEPT(options);
  94. // 根据参数判断loading的形态
  95. if (options.loading) {
  96. uni.showLoading(showLoadingValueTypeOf(options.loading) ? {
  97. title: options.loading
  98. } : {});
  99. }
  100. return new Promise((resolve, reject) => {
  101. uni.request({
  102. ...options,
  103. success(res) {
  104. try {
  105. res = REQUESTSUCCESSINTERCEPT(res)
  106. resolve(res);
  107. } catch (err) {
  108. reject(err)
  109. }
  110. },
  111. fail(err) {
  112. reject(err);
  113. },
  114. complete() {
  115. options.complete && options.complete();
  116. if (options.loading) uni.hideLoading();
  117. }
  118. })
  119. })
  120. }
  121. }
  122. function showLoadingValueTypeOf(value) {
  123. return typeof(value) == 'string';
  124. }
  125. // 系统数据
  126. function getSystemInfo() {
  127. const options = {};
  128. uni.getSystemInfo({
  129. success: res => {
  130. options.StatusBar = res.statusBarHeight;
  131. var capsule = wx.getMenuButtonBoundingClientRect();
  132. if (capsule) {
  133. options.Custom = capsule;
  134. options.CustomBar = capsule.bottom + capsule.top - res.statusBarHeight;
  135. } else {
  136. options.CustomBar = res.statusBarHeight + 50;
  137. }
  138. }
  139. })
  140. return options;
  141. }
  142. export default new global(GLOBALOPTIONS);