common.plugins.js 9.2 KB

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