util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. function strlen(value) {
  2. //中文、中文标点、全角字符按1长度,英文、英文符号、数字按0.5长度计算
  3. let cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g;
  4. let mat = value.match(cnReg);
  5. let length = 0;
  6. if (mat) {
  7. return (length = mat.length + (value.length - mat.length) * 0.5);
  8. } else {
  9. return (length = value.length * 0.5);
  10. }
  11. }
  12. /**
  13. *
  14. * 判断是否在微信浏览器 true是
  15. */
  16. function isWeiXinBrowser() {
  17. // #ifdef H5
  18. let ua = window.navigator.userAgent.toLowerCase()
  19. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  20. return true
  21. } else {
  22. return false
  23. }
  24. // #endif
  25. return false
  26. }
  27. /**
  28. * 获取url参数
  29. * @param {*} name
  30. * @param {*}
  31. * @returns
  32. */
  33. function getQueryString(name, url) {
  34. var url = url || window.location.href
  35. var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
  36. var r = url.substr(1).match(reg)
  37. if (r != null) {
  38. return r[2]
  39. }
  40. return null
  41. }
  42. //复制内容
  43. function uniCopy({
  44. content,
  45. success,
  46. error
  47. }) {
  48. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  49. /**
  50. * 小程序端 和 app端的复制逻辑
  51. */
  52. //#ifndef H5
  53. uni.setClipboardData({
  54. data: content,
  55. success: function() {
  56. success("复制成功~")
  57. },
  58. fail: function() {
  59. error("复制失败~")
  60. }
  61. });
  62. //#endif
  63. /**
  64. * H5端的复制逻辑
  65. */
  66. // #ifdef H5
  67. if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
  68. // 不支持
  69. error('浏览器不支持')
  70. }
  71. let textarea = document.createElement("textarea")
  72. textarea.value = content
  73. textarea.readOnly = "readOnly"
  74. document.body.appendChild(textarea)
  75. textarea.select() // 选择对象
  76. textarea.setSelectionRange(0, content.length) //核心
  77. let result = document.execCommand("copy") // 执行浏览器复制命令
  78. if (result) {
  79. success("复制成功~")
  80. } else {
  81. error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
  82. }
  83. textarea.remove()
  84. // #endif
  85. }
  86. //路径转化
  87. function getPath(path) {
  88. if (path.indexOf('?') != -1) {
  89. let arr = path.split('?');
  90. return arr[0];
  91. }
  92. return path;
  93. }
  94. //设置缓存
  95. function setDb(name, value, db_time = 7200) {
  96. let time = (new Date()).getTime();
  97. let data = {
  98. value: value,
  99. time: time,
  100. db_time: db_time
  101. }
  102. uni.setStorageSync(name, data);
  103. }
  104. //获取缓存
  105. function getDb(name) {
  106. try {
  107. let res = uni.getStorageSync(name);
  108. if (!res) {
  109. return '';
  110. }
  111. let time = (new Date()).getTime();
  112. if ((time - res.time) / 1000 >= res.db_time) {
  113. uni.removeStorageSync(name);
  114. return '';
  115. }
  116. return res.value;
  117. } catch (e) {
  118. //TODO handle the exception
  119. return '';
  120. }
  121. }
  122. /**
  123. * 下载图片
  124. */
  125. function image_cache(image_url) {
  126. return new Promise((resolve, reject) => {
  127. let arr = image_url.split('/');
  128. let image_name = arr[arr.length - 1];
  129. var u = getDb('fastadmin' + image_name);
  130. if (u) {
  131. resolve(u);
  132. } else {
  133. // 本地没有缓存 需要下载
  134. uni.downloadFile({
  135. url: image_url,
  136. success: res => {
  137. if (res.statusCode === 200) {
  138. uni.saveFile({
  139. tempFilePath: res.tempFilePath,
  140. success: function(res) {
  141. setDb('fastadmin' + image_name, res.savedFilePath)
  142. resolve(res.savedFilePath);
  143. }
  144. });
  145. } else {
  146. reject('下载失败');
  147. }
  148. },
  149. fail: function() {
  150. reject('下载失败');
  151. }
  152. });
  153. }
  154. });
  155. }
  156. /**
  157. * 重设tabbar
  158. * @param {Object} tablist
  159. */
  160. function setTabbar(tablist) {
  161. tablist.list.forEach((item, index) => {
  162. uni.setTabBarItem({
  163. index: index,
  164. text: item.text,
  165. iconPath: item.image,
  166. selectedIconPath: item.selectedImage,
  167. pagePath: item.path
  168. })
  169. })
  170. uni.setTabBarStyle({
  171. color: tablist.color,
  172. selectedColor: tablist.selectColor,
  173. backgroundColor: tablist.bgColor,
  174. borderStyle: 'black'
  175. })
  176. }
  177. export {
  178. strlen,
  179. isWeiXinBrowser,
  180. getQueryString,
  181. uniCopy,
  182. getPath,
  183. setDb,
  184. getDb,
  185. image_cache,
  186. setTabbar
  187. }