global.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const REQUESTURL = "http://192.168.2.164:8099"; //请求路径
  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. // formatRichText = html => {
  38. // let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
  39. // match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  40. // match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  41. // match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  42. // return match;
  43. // });
  44. // newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
  45. // match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  46. // return match;
  47. // });
  48. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  49. // newContent = newContent.replace(/\<img/gi, '< img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
  50. // return newContent;
  51. // }
  52. // 富文本处理
  53. richTextChange(value) {
  54. /**
  55. * 此代码段处理目的为,匹配富文本代码中的 <img> 标签,并将其图片的宽度修改为适应屏幕
  56. * max-width:100% --- 图片宽度加以限制,避免超出屏幕
  57. * height:auto --- 高度自适应
  58. * display:block --- 此代码,可以去掉图片之间的空白间隔,个人觉得好用
  59. */
  60. value = value.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
  61. // 去掉
  62. value = value.replace(/white-space: nowrap;/gi, '')
  63. return value;
  64. }
  65. // 数字转字符串占位
  66. numberPlaceholder(number, len = 2) {
  67. var string = number.toString();
  68. if (string.length > len) return string;
  69. for (let i = 0; i < len - string.length; i++) {
  70. string = '0' + string;
  71. }
  72. return string;
  73. }
  74. // 防抖
  75. debounce(func, wait = 1000) {
  76. if (typeof func !== 'function') {
  77. throw new TypeError('need a function arguments')
  78. }
  79. let timeid = null;
  80. let result;
  81. return function() {
  82. let context = this;
  83. let args = arguments;
  84. if (timeid) {
  85. clearTimeout(timeid);
  86. }
  87. timeid = setTimeout(function() {
  88. result = func.apply(context, args);
  89. }, wait);
  90. return result;
  91. }
  92. }
  93. // 节流
  94. throttle(func, delay = 1000) {
  95. let that = this;
  96. return function() {
  97. var context = this;
  98. var args = arguments;
  99. var now = Date.now();
  100. if (now - that.prev >= delay) {
  101. console.log("执行")
  102. func.apply(context, args);
  103. that.prev = Date.now();
  104. }
  105. }
  106. }
  107. // 请求
  108. request(options) {
  109. options.url = this.options.REQUESTURL + options.url;
  110. options = REQUESTOPTIONSINTERCEPT(options);
  111. // 根据参数判断loading的形态
  112. if (options.loading) {
  113. uni.showLoading(showLoadingValueTypeOf(options.loading) ? {
  114. title: options.loading
  115. } : {});
  116. }
  117. return new Promise((resolve, reject) => {
  118. uni.request({
  119. ...options,
  120. success(res) {
  121. try {
  122. res = REQUESTSUCCESSINTERCEPT(res)
  123. resolve(res);
  124. } catch (err) {
  125. reject(err)
  126. }
  127. },
  128. fail(err) {
  129. reject(err);
  130. },
  131. complete() {
  132. options.complete && options.complete();
  133. if (options.loading) uni.hideLoading();
  134. }
  135. })
  136. })
  137. }
  138. }
  139. function showLoadingValueTypeOf(value) {
  140. return typeof(value) == 'string';
  141. }
  142. // 系统数据
  143. function getSystemInfo() {
  144. const options = {};
  145. uni.getSystemInfo({
  146. success: res => {
  147. options.StatusBar = res.statusBarHeight;
  148. var capsule = wx.getMenuButtonBoundingClientRect();
  149. if (capsule) {
  150. options.Custom = capsule;
  151. options.CustomBar = capsule.bottom + capsule.top - res.statusBarHeight;
  152. } else {
  153. options.CustomBar = res.statusBarHeight + 50;
  154. }
  155. }
  156. })
  157. return options;
  158. }
  159. export default new global(GLOBALOPTIONS);