1
0

index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * 功能:工具类
  3. * 作者:小莫唐尼
  4. * 邮箱:studio@925i.cn
  5. * 时间:2022年07月21日 17:30:14
  6. * 版本:v0.1.0
  7. * 修改记录:
  8. * 修改内容:
  9. * 修改人员:
  10. * 修改时间:
  11. */
  12. import HaloTokenConfig from '@/config/uhalo.config.js'
  13. import {getAppConfigs} from '@/config/index.js'
  14. const utils = {
  15. /**
  16. * 检查是否为http/https链接
  17. */
  18. checkIsUrl: function (s) {
  19. var reg = /^(http(s)?:\/\/)/i;
  20. return reg.test(s)
  21. },
  22. // 检查链接
  23. checkUrl: function (url) {
  24. if (!url) return '';
  25. if (!this.checkIsUrl(url)) return HaloTokenConfig.BASE_API + url;
  26. return url
  27. },
  28. // 检查封面图
  29. checkThumbnailUrl: function (thumbnail, mustRealUrl = false) {
  30. if (!thumbnail && mustRealUrl) {
  31. return this.checkUrl(getAppConfigs().imagesConfig.defaultStaticThumbnailUrl);
  32. }
  33. let _url = this.checkUrl(getAppConfigs().imagesConfig.defaultThumbnailUrl);
  34. if (_url) {
  35. if (_url.indexOf('?') === -1) {
  36. _url += `?next-v=${new Date().getTime()}`
  37. } else {
  38. _url += `&next-v=${new Date().getTime()}`
  39. }
  40. }
  41. if (!thumbnail) return _url;
  42. if (!this.checkIsUrl(thumbnail)) return HaloTokenConfig.BASE_API + thumbnail;
  43. return thumbnail
  44. },
  45. // 检查图片
  46. checkImageUrl: function (image) {
  47. let _url
  48. this.checkUrl(getAppConfigs().imagesConfig.defaultImageUrl);
  49. if (_url) {
  50. if (_url.indexOf('?') === -1) {
  51. _url += `?next-v=${new Date().getTime()}`
  52. } else {
  53. _url += `&next-v=${new Date().getTime()}`
  54. }
  55. }
  56. if (!image) return _url;
  57. if (!this.checkIsUrl(image)) return HaloTokenConfig.BASE_API + image;
  58. return image
  59. },
  60. // 检查头像
  61. checkAvatarUrl: function (avatar) {
  62. if (!avatar) {
  63. let _url = this.checkUrl(getAppConfigs().imagesConfig.defaultAvatarUrl);
  64. if (_url) {
  65. if (_url.indexOf('?') === -1) {
  66. _url += `?next-v=${new Date().getTime()}`
  67. } else {
  68. _url += `&next-v=${new Date().getTime()}`
  69. }
  70. }
  71. return _url;
  72. }
  73. if (!this.checkIsUrl(avatar)) return HaloTokenConfig.BASE_API + avatar;
  74. return avatar
  75. },
  76. // 检查文件类型
  77. fnCheckIsFileType(type, attachment) {
  78. if (!attachment) return false;
  79. if (!attachment.mediaType) return false;
  80. if (type === 'video') return attachment.mediaType.indexOf('video/') !== -1;
  81. else if (type === 'image') return attachment.mediaType.indexOf('image/') !== -1;
  82. else return false;
  83. },
  84. groupData: function (arr, fun) {
  85. const groups = {}
  86. arr.forEach((el) => {
  87. const group = fun(el)
  88. groups[group] = groups[group] || []
  89. groups[group].push(el)
  90. })
  91. return Object.keys(groups).map((group) => {
  92. // 更改data的数据结构 可以改变新数据的结构 data即为newData的数组中每个数据的结构
  93. let data = {
  94. groupName: group,
  95. data: groups[group]
  96. }
  97. return data
  98. })
  99. },
  100. /**
  101. * 数据分组
  102. * @param {Object} oldData 数据源
  103. * @param {Object} prop 分组属性
  104. */
  105. arrayGroupBy: function (oldData, prop) {
  106. const newData = this.groupData(oldData, (item) => {
  107. return item[prop]
  108. })
  109. return newData
  110. },
  111. /**
  112. * 深克隆
  113. * @param {Object} obj 数据源
  114. */
  115. deepClone(obj) {
  116. var objClone = Array.isArray(obj) ? [] : {};
  117. if (obj && typeof obj === "object") {
  118. for (const key in obj) {
  119. if (obj.hasOwnProperty(key)) {
  120. if (obj[key] && typeof obj[key] === "object") {
  121. objClone[key] = this.deepClone(obj[key]);
  122. } else {
  123. objClone[key] = obj[key];
  124. }
  125. }
  126. }
  127. }
  128. return objClone;
  129. },
  130. /**
  131. * 复制内容到剪贴板
  132. * @param {Object} obj 数据源
  133. */
  134. copyText(content, tips = '复制成功') {
  135. uni.setClipboardData({
  136. data: content,
  137. showToast: false,
  138. success: () => {
  139. if (tips) {
  140. uni.showToast({
  141. icon: 'none',
  142. title: tips
  143. })
  144. }
  145. }
  146. })
  147. },
  148. previewImage(list) {
  149. list = list.map(item => {
  150. return this.checkUrl(item)
  151. })
  152. uni.previewImage({
  153. urls: list,
  154. loop: true
  155. })
  156. },
  157. /**
  158. * 检查是否为json字符串
  159. * @param {Object} jsonStr 数据源
  160. */
  161. checkJsonAndParse(jsonStr) {
  162. try {
  163. const jsonResult = JSON.parse(jsonStr);
  164. return {
  165. ok: true,
  166. jsonData: jsonResult,
  167. }
  168. } catch (e) {
  169. return {
  170. ok: false,
  171. jsonData: {},
  172. }
  173. }
  174. },
  175. isObject(obj) {
  176. return obj && typeof obj === 'object' && !Array.isArray(obj);
  177. },
  178. deepMerge(target, source) {
  179. let output = Object.assign({}, target);
  180. if (this.isObject(target) && this.isObject(source)) {
  181. Object.keys(source).forEach(key => {
  182. const targetValue = target[key];
  183. const sourceValue = source[key];
  184. if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
  185. output[key] = targetValue.concat(sourceValue);
  186. } else if (this.isObject(targetValue) && this.isObject(sourceValue)) {
  187. output[key] = this.deepMerge(targetValue, sourceValue);
  188. } else {
  189. output[key] = sourceValue;
  190. }
  191. });
  192. }
  193. return output;
  194. }
  195. };
  196. export default utils;