tools.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // import wxJs from "jweixin-module"
  2. // import jWeixin from "wecomjsdk"
  3. // import {
  4. // getWxJsSign
  5. // } from "@/api/weChat";
  6. let tools = {}
  7. /**
  8. * 大小判断
  9. * @param a
  10. * @param b
  11. * @returns {number}
  12. */
  13. tools.sortNumber = function(a, b) {
  14. return a - b;
  15. }
  16. tools.formatDateAgo = (dateStr)=>{
  17. const now = new Date();
  18. const target = new Date(dateStr);
  19. const diff = (now - target) / 1000 / 60; // 转换为分钟
  20. if (diff < 1) {
  21. return '刚刚';
  22. } else if (diff < 60) {
  23. return parseInt(diff) + '分钟前';
  24. } else if (diff < 60 * 24) {
  25. return parseInt(diff / 60) + '小时前';
  26. } else if (diff < 60 * 24 * 30) {
  27. return parseInt(diff / 60 / 24) + '天前';
  28. } else if (diff < 60 * 24 * 30 * 12) {
  29. return parseInt(diff / 60 / 24 / 30) + '个月前';
  30. } else {
  31. return parseInt(diff / 60 / 24 / 30 / 12) + '年前';
  32. }
  33. }
  34. tools.leftClick = function() {
  35. let pages = getCurrentPages();
  36. if (pages.length > 1) {
  37. uni.navigateBack({
  38. delta: 1
  39. })
  40. } else {
  41. uni.reLaunch({
  42. url: '/pages/index/index'
  43. });
  44. }
  45. }
  46. //邮箱/手机***过滤
  47. tools.starPhone = function(value, type) {
  48. const start = value.slice(0, 3)
  49. const end = value.slice(-type)
  50. return `${start}****${end}`
  51. }
  52. // 验证密码格式
  53. //只能由字母、数字组成,6-12位
  54. tools.passwordInput = function(val) {
  55. const emailReg =
  56. /^(?=.*\d)(?=.*[A-z])[\da-zA-Z]{6,12}$/;
  57. if (!emailReg.test(val)) {
  58. // this.error("密码格式错误")
  59. return;
  60. } else {
  61. return true;
  62. }
  63. }
  64. //验证手机格式
  65. tools.phoneInput = function(val) {
  66. const emailReg =
  67. /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
  68. if (!emailReg.test(val)) {
  69. // this.error("手机格式错误")
  70. return;
  71. } else {
  72. return true;
  73. }
  74. }
  75. /**
  76. * 保留两位小数
  77. * @param num
  78. * @returns {string}
  79. */
  80. tools.twoFloating = function(num) {
  81. // 获取两位小数
  82. let price = "";
  83. price = num * 1;
  84. price = String(price).split(".")[1];
  85. if (price !== undefined && price.length === 1) {
  86. price = `.${price}0`;
  87. } else {
  88. price === undefined ? (price = ".00") : (price = `.${price}`);
  89. }
  90. return price;
  91. }
  92. tools.formatDecimal = function(num, decimal) {
  93. num = num.toString()
  94. let index = num.indexOf('.')
  95. if (index !== -1) {
  96. num = num.substring(0, decimal + index + 1)
  97. } else {
  98. num = num.substring(0)
  99. }
  100. return parseFloat(num).toFixed(decimal)
  101. }
  102. /**
  103. * 错误提示
  104. * @param msg
  105. */
  106. tools.error = function(msg) {
  107. uni.showToast({
  108. 'title': msg,
  109. 'icon': 'error',
  110. 'mask': true,
  111. 'duration': 1500
  112. })
  113. }
  114. /**
  115. * 成功提示
  116. * @param msg
  117. */
  118. tools.success = function(msg) {
  119. uni.showToast({
  120. 'title': msg,
  121. 'icon': 'success',
  122. 'mask': true,
  123. 'duration': 1500
  124. })
  125. }
  126. /**
  127. * 显示Loading
  128. */
  129. tools.showLoading = function() {
  130. uni.showLoading({
  131. title: '加载中...',
  132. mask: true
  133. });
  134. }
  135. /**
  136. * 关闭Loading
  137. */
  138. tools.hideLoading = function() {
  139. uni.hideLoading();
  140. }
  141. /**
  142. * 获取时间戳(毫秒)
  143. * @returns {number}
  144. */
  145. tools.getTime = function() {
  146. return new Date().getTime();
  147. }
  148. tools.updateVersion = function(sysVersion, appUrl) {
  149. let app_version = plus.runtime.version;
  150. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  151. console.log(app_version < sysVersion)
  152. if (app_version < sysVersion) {
  153. uni.showLoading({
  154. title: '更新中……'
  155. })
  156. uni.downloadFile({ //执行下载
  157. url: appUrl, //下载地址
  158. success: (downloadResult) => { //下载成功
  159. uni.hideLoading();
  160. if (downloadResult.statusCode === 200) {
  161. uni.showModal({
  162. title: '',
  163. content: '更新成功,确定现在重启吗?',
  164. confirmText: '重启',
  165. confirmColor: '#EE8F57',
  166. success: function(res) {
  167. if (res.confirm === true) {
  168. plus.runtime.install( //安装
  169. downloadResult.tempFilePath, {
  170. force: true
  171. },
  172. function(res) {
  173. tools.success('更新成功,重启中')
  174. plus.runtime.restart();
  175. }
  176. );
  177. }
  178. }
  179. });
  180. }
  181. }
  182. });
  183. } else {
  184. // tools.success('你已是最新版本')
  185. }
  186. }
  187. /**
  188. * uniapp html图片显示控制
  189. * @param str
  190. * @returns {*}
  191. */
  192. tools.imgDeal = function(str) {
  193. console.log(str)
  194. if (str === null || str === undefined) {
  195. return '';
  196. } else {
  197. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  198. }
  199. }
  200. /**
  201. * 获取平台类型 1:微信,2:支付宝
  202. * @returns {boolean|number}
  203. */
  204. tools.platformType = function() {
  205. let ua = window.navigator.userAgent.toLowerCase();
  206. if (ua.indexOf('micromessenger') != -1) {
  207. return 1;
  208. } else if (ua.indexOf('alipay') != -1) {
  209. return 2;
  210. } else {
  211. return 0;
  212. }
  213. }
  214. /**
  215. * 获取开发平台
  216. * @returns {string}
  217. */
  218. tools.getPlatform = function() {
  219. let platForm = undefined;
  220. // #ifdef H5
  221. platForm = 'H5';
  222. //#endif
  223. // #ifdef APP-PLUS
  224. platForm = 'APP';
  225. //#endif
  226. // #ifdef APP-PLUS-NVUE
  227. platForm = 'APP';
  228. //#endif
  229. // #ifdef APP-NVUE
  230. platForm = 'APP';
  231. //#endif
  232. // #ifdef MP-WEIXIN
  233. platForm = 'MP-WEIXIN'; // 小程序
  234. //#endif
  235. // #ifdef MP-ALIPAY
  236. platForm = 'MP-ALIPAY';
  237. //#endif
  238. // #ifdef MP-BAIDU
  239. platForm = 'MP-BAIDU';
  240. //#endif
  241. // #ifdef MP-TOUTIAO
  242. platForm = 'MP-TOUTIAO';
  243. //#endif
  244. // #ifdef MP-LARK
  245. platForm = 'MP-LARK';
  246. //#endif
  247. // #ifdef MP-QQ
  248. platForm = 'MP-QQ';
  249. //#endif
  250. // #ifdef MP-KUAISHOU
  251. platForm = 'MP-KUAISHOU';
  252. //#endif
  253. // #ifdef QUICKAPP-WEBVIEW
  254. platForm = 'QUICKAPP-WEBVIEW';
  255. //#endif
  256. return platForm;
  257. }
  258. /**
  259. * 记录用户登录信息
  260. * @param data
  261. * @param type
  262. */
  263. tools.setLoginData = function(data) {
  264. uni.setStorageSync('m_id', data.m_id)
  265. uni.setStorageSync('token', data.token)
  266. tools.success('登陆成功')
  267. setTimeout(() => {
  268. uni.reLaunch({
  269. url: '/pages/index/index'
  270. });
  271. }, 1500)
  272. }
  273. /**
  274. * 微信JSDK授权
  275. * @param jsApiList
  276. * @returns {Promise<*>}
  277. */
  278. // tools.wxInitialize = async function(jsApiList) {
  279. // return await getWxJsSign({
  280. // 'url': window.location.href,
  281. // 'type': 1
  282. // }).then((ret) => {
  283. // if (ret.code === 1) {
  284. // wxJs.config({
  285. // beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  286. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  287. // appId: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  288. // timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  289. // nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  290. // signature: ret.data.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  291. // jsApiList: jsApiList, // 必填,需要使用的JS接口列表
  292. // success: function(res) {
  293. // console.log(res)
  294. // console.log('config:---------------OK')
  295. // tools.success('成功')
  296. // },
  297. // fail: function(res) {
  298. // console.log('config-error')
  299. // console.log(res)
  300. // }
  301. // });
  302. // return true;
  303. // } else {
  304. // return false;
  305. // }
  306. // })
  307. // }
  308. /**
  309. * 企业微信信息授权
  310. * @param jsApiList
  311. * @returns {Promise<unknown>}
  312. */
  313. // tools.wxAgentInitialize = async function(jsApiList) {
  314. // return await getWxJsSign({
  315. // 'url': window.location.href,
  316. // 'type': 2
  317. // }).then((ret) => {
  318. // if (ret.code === 1) {
  319. // jWeixin.agentConfig({
  320. // corpid: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  321. // agentid: ret.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  322. // timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
  323. // nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
  324. // signature: ret.data.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  325. // jsApiList: jsApiList, // 必填,需要使用的JS接口列表
  326. // success: function(res) {
  327. // console.log('agentConfig:---------------OK')
  328. // console.log(res)
  329. // },
  330. // fail: function(res) {
  331. // console.log('agentConfig-error')
  332. // console.log(res)
  333. // if (res.errMsg.indexOf('function not exist') > -1) {
  334. // alert('版本过低请升级')
  335. // }
  336. // }
  337. // });
  338. // return true;
  339. // } else {
  340. // return false;
  341. // }
  342. // })
  343. // }
  344. tools.download=function (url){
  345. window.open(url)
  346. }
  347. export default tools