tools.js 12 KB

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