common.plugins.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. export default {
  2. /**
  3. * 参数处理
  4. * @param params 参数
  5. */
  6. tansParams(params) {
  7. let result = "";
  8. for (const propName of Object.keys(params)) {
  9. const value = params[propName];
  10. var part = encodeURIComponent(propName) + "=";
  11. if (value !== null && value !== "" && typeof value !== "undefined") {
  12. if (typeof value === "object") {
  13. for (const key of Object.keys(value)) {
  14. if (value[key] !== null && value[key] !== "" && typeof value[key] !== "undefined") {
  15. let params = propName + "[" + key + "]";
  16. var subPart = encodeURIComponent(params) + "=";
  17. result += subPart + encodeURIComponent(value[key]) + "&";
  18. }
  19. }
  20. } else {
  21. result += part + encodeURIComponent(value) + "&";
  22. }
  23. }
  24. }
  25. return result;
  26. },
  27. /**
  28. * @一键拨号
  29. */
  30. makePhoneCall(phone) {
  31. uni.makePhoneCall({
  32. phoneNumber: phone,
  33. success: function () {
  34. console.log('success');
  35. },
  36. fail: function () {
  37. }
  38. });
  39. },
  40. /**
  41. * @复制粘贴板
  42. * @param {传入值} content
  43. * @returns
  44. */
  45. uniCopy({ content, success, error }) {
  46. if (!content) return error('复制的内容不能为空 !')
  47. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  48. /**
  49. * 小程序端 和 app端的复制逻辑
  50. */
  51. //#ifdef APP-PLUS || MP-WEIXIN
  52. uni.setClipboardData({
  53. data: content,
  54. success: function () {
  55. success("复制成功~")
  56. console.log('success');
  57. },
  58. fail: function () {
  59. success("复制失败~")
  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. * @param {时间处理(今日,昨日)}
  88. * @param {传入值} time
  89. * @returns
  90. */
  91. jktTimes(time) {
  92. if (time == undefined) return "";
  93. var today = new Date().getDate();//当前时间-日
  94. var day = new Date(time).getDate();//传入时间-日
  95. var newday = today - day
  96. if (newday == 0) {
  97. var newTime = time.split(" ")[1]
  98. var newTime2 = newTime.split(":")
  99. return "今天" + newTime2[0] + ":" + newTime2[1];
  100. } else if (newday == 1) {
  101. var newTime = time.split(" ")[1]
  102. var newTime2 = newTime.split(":")
  103. return "昨天" + newTime2[0] + ":" + newTime2[1];
  104. } else {
  105. var newTime = time.split(" ")[0]
  106. return newTime
  107. }
  108. },
  109. /**
  110. * @指定获取开始时间结束时间
  111. */
  112. getDays(value) {
  113. let oneDay = 24 * 60 * 60 * 1000;
  114. let endTime = new Date(Date.now());
  115. endTime = this.formatterDate(endTime, "yyyy-MM-dd");
  116. let startTime = new Date(Date.now() - value * oneDay);
  117. startTime = this.formatterDate(startTime, "yyyy-MM-dd");
  118. const days = [startTime, endTime,]
  119. return days;
  120. },
  121. /**
  122. * @处理公共日期格式
  123. */
  124. formatterDate(date, fmt) {
  125. let nowDate = {
  126. yyyy: date.getFullYear(), // 年
  127. MM: date.getMonth() + 1, // 月份
  128. dd: date.getDate(), //日
  129. hh: date.getHours(),
  130. mm: date.getMinutes(),
  131. ss: date.getSeconds(),
  132. };
  133. if (/(y+)/.test(fmt)) {
  134. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  135. }
  136. for (var k in nowDate) {
  137. if (new RegExp("(" + k + ")").test(fmt)) {
  138. fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? nowDate[k] : ("00" + nowDate[k]).substr(("" + nowDate[k]).length));
  139. }
  140. }
  141. return fmt;
  142. },
  143. };