common.plugins.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import modal from "./modal.plugins";
  2. export default {
  3. /**
  4. * 参数处理
  5. * @param params 参数
  6. */
  7. tansParams(params) {
  8. let result = "";
  9. for (const propName of Object.keys(params)) {
  10. const value = params[propName];
  11. var part = encodeURIComponent(propName) + "=";
  12. if (value !== null && value !== "" && typeof value !== "undefined") {
  13. if (typeof value === "object") {
  14. for (const key of Object.keys(value)) {
  15. if (value[key] !== null && value[key] !== "" && typeof value[key] !== "undefined") {
  16. let params = propName + "[" + key + "]";
  17. var subPart = encodeURIComponent(params) + "=";
  18. result += subPart + encodeURIComponent(value[key]) + "&";
  19. }
  20. }
  21. } else {
  22. result += part + encodeURIComponent(value) + "&";
  23. }
  24. }
  25. }
  26. return result;
  27. },
  28. /**
  29. * 数据映射
  30. * @param reKey 需要返回的key
  31. * @param isKey 需要比对的key
  32. * @param value 对比值
  33. * @param data 数据集
  34. */
  35. mapping(reKey, isKey, value, data) {
  36. if (typeof value == "string" && value.indexOf(",") > -1) {
  37. //value为字符串的id集合
  38. let arr = value.split(",");
  39. let returnValue = "";
  40. arr.forEach((e, index) => {
  41. data.forEach((f) => {
  42. if (e == f[isKey]) {
  43. returnValue = returnValue ? `${returnValue},${f[reKey]}` : f[reKey];
  44. }
  45. })
  46. })
  47. return returnValue;
  48. } else {
  49. if (!data) return;
  50. for (let i = 0; i < data.length; i++) {
  51. if (value == data[i][isKey]) {
  52. return data[i][reKey];
  53. }
  54. }
  55. }
  56. },
  57. /**
  58. * @一键拨号
  59. */
  60. makePhoneCall(phone) {
  61. uni.makePhoneCall({
  62. phoneNumber: phone,
  63. success: function () {
  64. console.log('success');
  65. },
  66. fail: function () {
  67. }
  68. });
  69. },
  70. /**
  71. * 树结构过滤
  72. * @param {*} treeData
  73. * @param {*} ids
  74. * @returns
  75. */
  76. findTreeNodes(treeData, ids) {
  77. const result = [];
  78. const findNodes_ = (nodes, idArray) => {
  79. nodes.forEach(node => {
  80. if (idArray.includes(node.id)) {
  81. result.push(node);
  82. }
  83. if (node.children && node.children.length > 0) {
  84. findNodes_(node.children, idArray);
  85. }
  86. });
  87. };
  88. findNodes_(treeData, ids);
  89. return result;
  90. },
  91. /**
  92. * @复制粘贴板
  93. * @param {传入值} content
  94. * @returns
  95. */
  96. uniCopy({ content, success, error }) {
  97. if (!content) return error('复制的内容不能为空 !')
  98. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  99. /**
  100. * 小程序端 和 app端的复制逻辑
  101. */
  102. //#ifdef APP-PLUS || MP-WEIXIN
  103. uni.setClipboardData({
  104. data: content,
  105. success: function () {
  106. success("复制成功~")
  107. console.log('success');
  108. },
  109. fail: function () {
  110. success("复制失败~")
  111. }
  112. });
  113. //#endif
  114. /**
  115. * H5端的复制逻辑
  116. */
  117. // #ifdef H5
  118. if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
  119. // 不支持
  120. error('浏览器不支持')
  121. }
  122. let textarea = document.createElement("textarea")
  123. textarea.value = content
  124. textarea.readOnly = "readOnly"
  125. document.body.appendChild(textarea)
  126. textarea.select() // 选择对象
  127. textarea.setSelectionRange(0, content.length) //核心
  128. let result = document.execCommand("copy") // 执行浏览器复制命令
  129. if (result) {
  130. success("复制成功~")
  131. } else {
  132. error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
  133. }
  134. textarea.remove()
  135. // #endif
  136. },
  137. /**
  138. * @根据时间分类数据
  139. * @param {数据集} data
  140. * @param {需要处理的时间key} timeKey
  141. * @returns
  142. */
  143. groupedItems(data, timeKey) {
  144. if (data <= 0) {
  145. return false
  146. }
  147. const grouped = {};
  148. data.forEach((item) => {
  149. const date = item[timeKey].split("T")[0];
  150. grouped[date] = grouped[date] || [];
  151. grouped[date].push(item);
  152. });
  153. return grouped;
  154. },
  155. /**
  156. * @公共获取URL中的参数
  157. */
  158. getUrlList() {
  159. // 截取url中的list
  160. var url = window.location.href;
  161. var theRequest = new Object();
  162. if (url.indexOf("?") != -1) {
  163. var str = url.split("?")[1];
  164. var strs = str.split("&");
  165. for (var i = 0; i < strs.length; i++) {
  166. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  167. }
  168. }
  169. return theRequest;
  170. },
  171. /**
  172. * @数组对象排序
  173. * @return
  174. * @param {数据} data
  175. * @param {0 从小到大 1 从大到小} sort
  176. */
  177. sortEvent(data, sort) {
  178. let arr = [];
  179. // 将需要排序的 key, 进行排列
  180. let sortKeys = Object.keys(JSON.parse(JSON.stringify(data))).sort((a, b) => {
  181. return sort == 0 ? JSON.parse(JSON.stringify(data))[a].sort - JSON.parse(JSON.stringify(data))[b].sort : JSON.parse(JSON.stringify(data))[b].sort - JSON.parse(JSON.stringify(data))[a].sort;
  182. });
  183. // 循环排列好的 key, 重新组成一个新的数组
  184. for (var sortIndex in sortKeys) {
  185. arr.push(JSON.parse(JSON.stringify(data))[sortKeys[sortIndex]]);
  186. }
  187. return arr;
  188. },
  189. /**
  190. * @数组对象去重
  191. * @methods data 需要去重的数据
  192. * @methods objectName 需要去重的对象名称
  193. */
  194. uniq(data, objectName) {
  195. if (!objectName) {
  196. var newArr = [...new Set(data)]
  197. return newArr;
  198. } else {
  199. let obj = {};
  200. let peon = data.reduce((cur, next) => {
  201. obj[next[objectName]] ? "" : obj[next[objectName]] = true && cur.push(next);
  202. return cur;
  203. }, []) //设置cur默认类型为数组,并且初始值为空的数组
  204. return peon;
  205. }
  206. },
  207. /**
  208. * @判断当前是否有网络
  209. */
  210. isNetwork() {
  211. let status = true
  212. // 获取网络状态
  213. uni.getNetworkType({
  214. success: function (res) {
  215. if (res.networkType === "none") {
  216. modal.msg("网络异常,请稍后重试!");
  217. status = false
  218. } else {
  219. status = true
  220. }
  221. },
  222. });
  223. return status
  224. },
  225. /**
  226. * @判断用户拒绝权限是否超过48小时
  227. */
  228. isExpirationTime() {
  229. let sotrTime = uni.getStorageSync("expirationTime");
  230. if (sotrTime) {
  231. if (sotrTime + 3600 * 24 * 2 <= Date.parse(new Date()) / 1000) {
  232. return true
  233. } else {
  234. return false
  235. }
  236. } else {
  237. return true
  238. }
  239. },
  240. /**
  241. * @判断是否为微信公众号
  242. */
  243. isWechatMp() {
  244. var ua = navigator?.userAgent.toLowerCase();
  245. if (ua?.match(/MicroMessenger/i) == 'micromessenger') {
  246. return true;
  247. } else {
  248. // 不在微信内置浏览器中
  249. return false;
  250. }
  251. },
  252. /**
  253. * @是否显示容器
  254. * @判断是否为微信公众号
  255. */
  256. isVisible() {
  257. let visible = true;
  258. //#ifdef H5
  259. visible = !this.isWechatMp();
  260. //#endif
  261. //#ifdef APP-PLUS || MP-WEIXIN
  262. visible = true;
  263. //#endif
  264. return visible;
  265. },
  266. /**
  267. * 图片点击放大
  268. * @param {*类型} type (1:string:图片地址 2:array(数组对象))
  269. * @param {*数据} data
  270. */
  271. imgEnlarge(type, data) {
  272. let param = {}
  273. if (type == 1) {
  274. param = {
  275. urls: [data],
  276. current: data
  277. }
  278. }
  279. if (type == 2) {
  280. let arr = []
  281. for (let i = 0; i < data.length; i++) {
  282. arr.push(data[i].url)
  283. }
  284. param = {
  285. urls: arr,
  286. current: arr[0]
  287. }
  288. }
  289. if (data && data.length > 0) {
  290. uni.previewImage(param)
  291. }
  292. },
  293. };