utils.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import clone from './clone.js'
  2. /**
  3. * @desc 函数防抖
  4. * @param func 函数
  5. * @param wait 延迟执行毫秒数
  6. * @param immediate true 表立即执行,false 表非立即执行
  7. */
  8. export let debounce = (func, wait, immediate) => {
  9. let timeout;
  10. return function() {
  11. let context = this;
  12. let args = arguments;
  13. if (timeout) clearTimeout(timeout);
  14. if (immediate) {
  15. let callNow = !timeout;
  16. timeout = setTimeout(() => {
  17. timeout = null;
  18. }, wait);
  19. if (callNow) func.apply(context, args);
  20. } else {
  21. timeout = setTimeout(() => {
  22. func.apply(context, args);
  23. }, wait);
  24. }
  25. };
  26. };
  27. /**
  28. * @desc 函数节流
  29. * @param func 函数
  30. * @param delay 毫秒数
  31. */
  32. export let throttle = (func, delay) => {
  33. let timeout;
  34. return function() {
  35. let args = arguments;
  36. if (!timeout) {
  37. timeout = setTimeout(() => {
  38. func.apply(this, args);
  39. timeout = null;
  40. }, delay);
  41. }
  42. };
  43. };
  44. /**
  45. * 函数节流
  46. * @param fn
  47. * @param interval
  48. * @returns {Function}
  49. * @constructor
  50. */
  51. let startThrottleTimer = true; // 是否第一次执行 第一次点击时立即执行一次,后续不再立即执行
  52. let isThrottleTimer = false; // 第一次是否执行
  53. let ThrottleTimer = true; // 节流函数开关
  54. // 节流函数
  55. export let Throttle = function(fn, delay) {
  56. var arg = Array.from(arguments).splice(2);
  57. if (ThrottleTimer) {
  58. if (startThrottleTimer) {
  59. fn.apply(this, arg);
  60. startThrottleTimer = false;
  61. }
  62. ThrottleTimer = false;
  63. setTimeout(() => {
  64. ThrottleTimer = true;
  65. if (isThrottleTimer) {
  66. fn.apply(this, arg);
  67. }
  68. startThrottleTimer = true;
  69. isThrottleTimer = !startThrottleTimer;
  70. }, delay);
  71. }
  72. };
  73. // 校验手机号
  74. export function phoneValidate(phone) {
  75. const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  76. return reg.test(phone)
  77. }
  78. // 校验+86手机号 \ 座机电话
  79. export function is86Phone(phone) {
  80. const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  81. if (reg.test(phone)) {
  82. return true
  83. }
  84. const str2 = phone.substring(0, 2)
  85. if (str2 == '86') {
  86. const str3 = phone.substring(2, phone.length - 1)
  87. if (reg.test(str3)) {
  88. return true
  89. }
  90. }
  91. const regMob = /^((0\d{2,3})-)?(\d{7,8})$/
  92. if (regMob.test(phone)) {
  93. return true
  94. }
  95. return false
  96. }
  97. // 复制文本
  98. export function copy(data, msg) {
  99. uni.setClipboardData({
  100. data: data,
  101. success: () => {
  102. uni.showToast({
  103. title: msg || '复制成功',
  104. icon: 'none',
  105. mask: true
  106. })
  107. }
  108. })
  109. }
  110. // 拨打电话
  111. export function callPhone(phone) {
  112. uni.makePhoneCall({
  113. phoneNumber: phone
  114. })
  115. }
  116. // 拆分字符串--汉字 | 数字
  117. export function splitStr(str) {
  118. if (!str) {
  119. return
  120. }
  121. let show_text = str
  122. const arr = Array.from(new Set(show_text.match(/\d+(.\d+)?/g)))
  123. let replace_text = arr.map(item => {
  124. if (item.length >= 8 && item.length <= 11) {
  125. return item
  126. }
  127. })
  128. for (var i = 0; i < replace_text.length; i++) {
  129. var replaceString = '**' + replace_text[i] + "**"
  130. show_text = show_text.replace(RegExp(replace_text[i]), replaceString)
  131. }
  132. return show_text.split('**')
  133. }
  134. // 预览pdf
  135. export function exportPDF(pdfUrl) {
  136. // #ifdef H5
  137. window.open(
  138. pdfUrl
  139. )
  140. // #endif
  141. // 微信下载文件需要在微信公众平台>开发>开发管理>服务器域名>downloadFile合法域名>配置白名单域名
  142. // #ifdef MP-WEIXIN
  143. uni.downloadFile({
  144. url: pdfUrl,
  145. success: res => {
  146. console.log(res)
  147. if (res.statusCode === 200) {
  148. // 预览pdf文件
  149. uni.openDocument({
  150. filePath: res.tempFilePath,
  151. showMenu: true, // 右上角菜单,可以进行分享保存pdf
  152. success: function(file) {
  153. console.log("file-success", file)
  154. }
  155. })
  156. }
  157. }
  158. })
  159. // #endif
  160. // #ifdef APP-PLUS
  161. uni.downloadFile({
  162. url: pdfUrl,
  163. success: res => {
  164. console.log(res)
  165. if (res.statusCode === 200) {
  166. // 保存pdf文件至手机,一般安卓端存储路径为:手机存储/dcim/camera文件夹下
  167. uni.showLoading({
  168. title: '请稍等...'
  169. })
  170. setTimeout(function() {
  171. uni.hideLoading()
  172. // 预览pdf文件
  173. uni.openDocument({
  174. filePath: res.tempFilePath,
  175. showMenu: true,
  176. success: function(file) {
  177. console.log("file-success", file)
  178. }
  179. })
  180. }, 1000)
  181. }
  182. }
  183. })
  184. // #endif
  185. }
  186. /**
  187. * 抹除api请求的对象的null和undefined
  188. */
  189. export const wipeNulish = (obj) => {
  190. obj = Object(obj) // 不是对象类型就转成对象类型
  191. obj = clone(obj, true) // 不改变原来的值
  192. Object.keys(obj)
  193. .forEach((key) => {
  194. // 删除null和undefined
  195. obj[key] == null && delete obj[key]
  196. if (
  197. typeof obj[key] === 'object' &&
  198. Object.prototype.toString.call(obj) === '[object Object]'
  199. ) {
  200. // 不对Array或Date或RegExp这样的对象处理
  201. // 递归删除
  202. wipeNulish(obj[key])
  203. }
  204. })
  205. return obj
  206. }
  207. // 调用历史栈页面方法
  208. export function pageInit(obj = {}) {
  209. if (!obj.route || !obj.funName) {
  210. return
  211. }
  212. return new Promise(resolve => {
  213. let params = {
  214. route: obj.route,
  215. funName: obj.funName
  216. }
  217. const pages = getCurrentPages()
  218. const listPage = pages.find(item => item.route === params.route)
  219. if (listPage) {
  220. console.log(listPage)
  221. const fun = listPage.$vm[params.funName]
  222. fun && fun()
  223. setTimeout(_ => {
  224. resolve()
  225. }, 300)
  226. } else {
  227. resolve()
  228. }
  229. })
  230. }
  231. // 获取url参数--普通
  232. export const getUrlParamsNormal = (param) => {
  233. let query = ''
  234. let hash = window.location.hash
  235. const index = hash.indexOf('#')
  236. query = hash.substring(index, hash.length)
  237. const indexQuery = hash.indexOf('?')
  238. query = query.substring(indexQuery + 1, hash.length)
  239. let vars = query.split("&")
  240. for (let i = 0; i < vars.length; i++) {
  241. let pair = vars[i].split("=")
  242. if (pair[0] == param) {
  243. return pair[1]
  244. }
  245. }
  246. return (false)
  247. }
  248. // 检测微信是否安装
  249. export function checkApp() {
  250. if (plus.runtime.isApplicationExist({
  251. pname: 'com.tencent.mm',
  252. action: 'weixin://'
  253. })) {
  254. return true
  255. console.log("微信应用已安装");
  256. } else {
  257. return false
  258. console.log("微信应用未安装");
  259. }
  260. }
  261. // 数字保留两位小数或保留整数
  262. export function numFixtwo(num) {
  263. let number = Number(num)
  264. if (parseInt(number.toString()) == parseFloat(number.toString())) {
  265. return number
  266. } else {
  267. return number.toFixed(2)
  268. }
  269. }
  270. // 手机号3-4-4结构
  271. export function formatPhone(phone) {
  272. let phoneCopy = ''
  273. if (phone.length === 3 || phone.length === 8) {
  274. phone += " "
  275. }
  276. if (phone.length === 11) { // 处理复制的号码
  277. const mobileReg = /(?=(\d{4})+$)/g
  278. phoneCopy = phone.replace(mobileReg, " ")
  279. }
  280. return phoneCopy ? phoneCopy : phone
  281. }
  282. // 精确计算加法
  283. export function acc(arg1, arg2) {
  284. let r1 = deal(arg1)
  285. let r2 = deal(arg2)
  286. let m = Math.pow(10, Math.max(r1, r2))
  287. return (arg1 * m + arg2 * m) / m
  288. }
  289. function deal(arg) {
  290. var t = 0;
  291. try {
  292. t = arg.toString().split(".")[1].length
  293. } catch (e) { }
  294. return t
  295. }
  296. // 精确计算减法
  297. export function rce(arg1, arg2) {
  298. let r1 = deal(arg1)
  299. let r2 = deal(arg2)
  300. let m = Math.pow(10, Math.max(r1, r2))
  301. return (arg1 * m - arg2 * m) / m
  302. }
  303. // function deal(arg) {
  304. // var t = 0;
  305. // try {
  306. // t = arg.toString().split(".")[1].length
  307. // } catch (e) { }
  308. // return t
  309. // }
  310. // 手机号码分割过滤
  311. export const filterPhone = (phone, formart = " ") => {
  312. let phoneCopy = phone
  313. if (phone.length === 3 || phone.length === 8) {
  314. phoneCopy += formart
  315. }
  316. if (phone.length === 11) { // 处理复制的号码
  317. const mobileReg = /(?=(\d{4})+$)/g
  318. phoneCopy = phone.replace(mobileReg, formart)
  319. }
  320. return phoneCopy
  321. }
  322. // 解除手机分割-只支持空格拆分
  323. export const openFilterPhone = (phone) => {
  324. return phone.replace(/\s/g, "")
  325. }