tools.js 10 KB

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