tools.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. let tools = {}
  2. /**
  3. * 大小判断
  4. * @param a
  5. * @param b
  6. * @returns {number}
  7. */
  8. tools.sortNumber = function (a, b) {
  9. return a - b;
  10. }
  11. tools.leftClick = function () {
  12. let pages = getCurrentPages();
  13. if(pages.length>1){
  14. uni.navigateBack({
  15. delta: 1
  16. })
  17. }else {
  18. if(pages[pages.length -1].route.search("pages/finance") != -1){
  19. uni.reLaunch({
  20. url:"/pages/finance/index"
  21. })
  22. }else {
  23. uni.switchTab({
  24. url: '/pages/index/index'
  25. });
  26. }
  27. }
  28. }
  29. tools.formatDateAgo = (dateStr)=> {
  30. const now = new Date();
  31. const target = new Date(dateStr);
  32. const diff = (now - target) / 1000 / 60; // 转换为分钟
  33. if (diff < 1) {
  34. return '刚刚';
  35. } else if (diff < 60) {
  36. return parseInt(diff) + '分钟前';
  37. } else if (diff < 60 * 24) {
  38. return parseInt(diff / 60) + '小时前';
  39. } else if (diff < 60 * 24 * 30) {
  40. return parseInt(diff / 60 / 24) + '天前';
  41. } else if (diff < 60 * 24 * 30 * 12) {
  42. return parseInt(diff / 60 / 24 / 30) + '个月前';
  43. } else {
  44. return parseInt(diff / 60 / 24 / 30 / 12) + '年前';
  45. }
  46. }
  47. tools.getTimeY = function(data) {
  48. return new Date(data).getFullYear();
  49. }
  50. tools.getTimeM = function(data) {
  51. let m = new Date(data).getMonth() + 1;
  52. m = m>10?m:'0'+m
  53. return m
  54. }
  55. tools.setTimeYM = function() {
  56. let YM
  57. let data = new Date()
  58. let y = tools.getTimeY(data)
  59. let m = tools.getTimeM(data)
  60. return YM = `${y}-${m}`
  61. }
  62. /**
  63. * 自定义 获取指定日期到当前日期的 所有年 or 年月
  64. */
  65. tools.getCustomTimeList = (start,end,type)=>{
  66. let timeList = [];
  67. let s = start.split("-");
  68. let e = end.split("-");
  69. let min = new Date()
  70. let max = new Date()
  71. min.setFullYear(s[0], s[1]);
  72. max.setFullYear(e[0], e[1]);
  73. var curr = min;
  74. var str = "";
  75. while (curr <= max) {
  76. var yers = curr.getFullYear();
  77. var month = curr.getMonth();
  78. if(type === 'y-m'){
  79. if (month === 0) {
  80. str = (yers - 1) + "-" + 12;
  81. } else {
  82. str = yers + "-" + (month < 10 ? ("0" + month) : month);
  83. }
  84. timeList.push(str);
  85. curr.setMonth(month + 1);
  86. }else if(type === 'y'){
  87. if (month === 0) {
  88. str = (yers - 1)
  89. } else {
  90. str = yers
  91. }
  92. timeList.push(str);
  93. curr.setMonth(month + 1);
  94. }else{}
  95. }
  96. if(type === 'y-m'){
  97. return timeList.reverse()
  98. }else{
  99. let list = Array.from(new Set(timeList))
  100. return list.reverse()
  101. }
  102. }
  103. // 获取 当前 年-月 配合 getCustomTimeList 使用 获取end
  104. tools.getDateYM = ()=>{
  105. let DATE = new Date()
  106. let Y = DATE.getFullYear()
  107. let M = DATE.getMonth() + 1
  108. return Y + '-' + (M<10?'0'+M:M)
  109. }
  110. /**
  111. * 保留两位小数
  112. * @param num
  113. * @returns {string}
  114. */
  115. tools.twoFloating = function (num) {
  116. // 获取两位小数
  117. let price = "";
  118. price = num * 1;
  119. price = String(price).split(".")[1];
  120. if (price !== undefined && price.length === 1) {
  121. price = `.${price}0`;
  122. } else {
  123. price === undefined ? (price = ".00") : (price = `.${price}`);
  124. }
  125. return price;
  126. }
  127. tools.formatDecimal = function (num, decimal) {
  128. num = num.toString()
  129. let index = num.indexOf('.')
  130. if (index !== -1) {
  131. num = num.substring(0, decimal + index + 1)
  132. } else {
  133. num = num.substring(0)
  134. }
  135. return parseFloat(num).toFixed(decimal)
  136. }
  137. /**
  138. * 错误提示
  139. * @param msg
  140. */
  141. tools.error = function (msg) {
  142. uni.showToast({
  143. 'title': msg,
  144. 'icon': 'error',
  145. 'mask': true,
  146. 'duration': 1500
  147. })
  148. }
  149. /**
  150. * 成功提示
  151. * @param msg
  152. */
  153. tools.success = function (msg) {
  154. uni.showToast({
  155. 'title': msg,
  156. 'icon': 'success',
  157. 'mask': true,
  158. 'duration': 1500
  159. })
  160. }
  161. /**
  162. * 显示Loading
  163. */
  164. tools.showLoading = function () {
  165. uni.showLoading({
  166. title: '加载中...',
  167. mask: true
  168. });
  169. }
  170. /**
  171. * 关闭Loading
  172. */
  173. tools.hideLoading = function () {
  174. uni.hideLoading();
  175. }
  176. /**
  177. * 获取时间戳(毫秒)
  178. * @returns {number}
  179. */
  180. tools.getTime = function () {
  181. return new Date().getTime();
  182. }
  183. tools.updateVersion = function (sysVersion, appUrl) {
  184. let app_version = plus.runtime.version;
  185. if (app_version < sysVersion) {
  186. uni.showLoading({
  187. title: '更新中……'
  188. })
  189. uni.downloadFile({//执行下载
  190. url: appUrl, //下载地址
  191. success: (downloadResult) => {//下载成功
  192. uni.hideLoading();
  193. if (downloadResult.statusCode === 200) {
  194. uni.showModal({
  195. title: '',
  196. content: '更新成功,确定现在重启吗?',
  197. confirmText: '重启',
  198. confirmColor: '#EE8F57',
  199. success: function (res) {
  200. if (res.confirm === true) {
  201. plus.runtime.install(//安装
  202. downloadResult.tempFilePath, {
  203. force: true
  204. },
  205. function (res) {
  206. tools.success('更新成功,重启中')
  207. plus.runtime.restart();
  208. }
  209. );
  210. }
  211. }
  212. });
  213. }
  214. }
  215. });
  216. } else {
  217. // tools.success('你已是最新版本')
  218. }
  219. }
  220. /**
  221. * uniapp html图片显示控制
  222. * @param str
  223. * @returns {*}
  224. */
  225. tools.imgDeal = function (str) {
  226. console.log(str)
  227. if(str===null || str===undefined){
  228. return '';
  229. }else {
  230. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  231. }
  232. }
  233. /**
  234. * 获取平台类型 1:微信,2:支付宝
  235. * @returns {boolean|number}
  236. */
  237. tools.platformType = function () {
  238. let ua = window.navigator.userAgent.toLowerCase();
  239. if (ua.indexOf('micromessenger') != -1) {
  240. return 1;
  241. } else if (ua.indexOf('alipay') != -1) {
  242. return 2;
  243. } else {
  244. return 0;
  245. }
  246. }
  247. /**
  248. * 获取开发平台
  249. * @returns {string}
  250. */
  251. tools.getPlatform = function () {
  252. let platForm = undefined;
  253. // #ifdef H5
  254. platForm = 'H5';
  255. //#endif
  256. // #ifdef APP-PLUS
  257. platForm = 'APP';
  258. //#endif
  259. // #ifdef APP-PLUS-NVUE
  260. platForm = 'APP';
  261. //#endif
  262. // #ifdef APP-NVUE
  263. platForm = 'APP';
  264. //#endif
  265. // #ifdef MP-WEIXIN
  266. platForm = 'MP-WEIXIN'; // 小程序
  267. //#endif
  268. // #ifdef MP-ALIPAY
  269. platForm = 'MP-ALIPAY';
  270. //#endif
  271. // #ifdef MP-BAIDU
  272. platForm = 'MP-BAIDU';
  273. //#endif
  274. // #ifdef MP-TOUTIAO
  275. platForm = 'MP-TOUTIAO';
  276. //#endif
  277. // #ifdef MP-LARK
  278. platForm = 'MP-LARK';
  279. //#endif
  280. // #ifdef MP-QQ
  281. platForm = 'MP-QQ';
  282. //#endif
  283. // #ifdef MP-KUAISHOU
  284. platForm = 'MP-KUAISHOU';
  285. //#endif
  286. // #ifdef QUICKAPP-WEBVIEW
  287. platForm = 'QUICKAPP-WEBVIEW';
  288. //#endif
  289. return platForm;
  290. }
  291. /**
  292. * 记录用户登录信息
  293. * @param data
  294. * @param type
  295. */
  296. tools.setLoginData = function (data) {
  297. uni.setStorageSync('m_id', data.m_id)
  298. uni.setStorageSync('token', data.token)
  299. tools.success('登陆成功')
  300. setTimeout(() => {
  301. uni.switchTab({
  302. url: '/pages/index/index'
  303. });
  304. }, 1500)
  305. }
  306. /**
  307. * 微信JSDK授权
  308. * @param jsApiList
  309. * @returns {Promise<*>}
  310. */
  311. tools.wxInitialize = async function (jsApiList) {
  312. // #ifdef H5
  313. return await getJsConfig({ 'url': window.location.href,'type':3 }).then((ret) => {
  314. if (ret.code === 1) {
  315. wxJs.config({
  316. beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  317. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  318. appId: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  319. timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  320. nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  321. signature: ret.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
  322. jsApiList: jsApiList,// 必填,需要使用的JS接口列表
  323. success: function(res) {
  324. tools.success('成功')
  325. },
  326. fail: function(res) {}
  327. });
  328. return true;
  329. } else {
  330. return false;
  331. }
  332. })
  333. //#endif
  334. }
  335. tools.weiXinPay = function (data) {
  336. // #ifdef H5
  337. return new Promise((resolve, reject) => {
  338. wxJs.chooseWXPay({
  339. appId: data.appId, // 必填,公众号的唯一标识
  340. timestamp: data.timeStamp, // 支付签名时间戳,注意微信 jssdk 中的所有使用 timestamp 字段均为小写。但最新版的支付后台生成签名使用的 timeStamp 字段名需大写其中的 S 字符
  341. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  342. package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  343. signType: data.signType, // 微信支付V3的传入 RSA ,微信支付V2的传入格式与V2统一下单的签名格式保持一致
  344. paySign: data.paySign, // 支付签名
  345. success: function (res) {
  346. resolve(true)
  347. },
  348. fail: function (res) {
  349. resolve(false)
  350. },
  351. });
  352. })
  353. //#endif
  354. }
  355. /**
  356. * 判定当前浏览器是否为微信浏览器
  357. */
  358. tools.isWx = function () {
  359. //#ifdef H5
  360. let ua = window.navigator.userAgent.toLowerCase();
  361. if (ua.indexOf('micromessenger') !== -1) {
  362. return true;
  363. } else {
  364. return false;
  365. }
  366. //#endif
  367. // #ifndef H5
  368. return false;
  369. // #endif
  370. }
  371. tools.wxLogin = function (urlPath) {
  372. // #ifdef H5
  373. // 微信浏览器
  374. let url=window.location.href;
  375. url = url.replace('&code=', '&code_a=1')
  376. url = encodeURIComponent(url);
  377. // let nodeEnv = process.env.NODE_ENV;
  378. // let type = '';
  379. // if(nodeEnv === 'development'){
  380. // type = 2
  381. // }else{
  382. // type = 1
  383. // }
  384. window.location.href = `https://wx.jhcs888.com/#/?redirect_url=${Base64.encode(url)}&type=3`
  385. //#endif
  386. }
  387. tools.setClipboardData=function (text){
  388. uni.setClipboardData({
  389. data: text,
  390. success: function () {
  391. tools.success('复制成功')
  392. }
  393. });
  394. }
  395. tools.download=function (url){
  396. window.open(url)
  397. }
  398. tools.jyStart=function () {
  399. }
  400. export default tools