tools.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import permision from "@/js_sdk/wa-permission/permission.js"
  2. let tools = {}
  3. /**
  4. * 大小判断
  5. * @param a
  6. * @param b
  7. * @returns {number}
  8. */
  9. tools.sortNumber = function (a, b) {
  10. return a - b;
  11. }
  12. /**
  13. * 保留两位小数
  14. * @param num
  15. * @returns {string}
  16. */
  17. tools.twoFloating = function (num) {
  18. // 获取两位小数
  19. let price = "";
  20. price = num * 1;
  21. price = String(price).split(".")[1];
  22. if (price !== undefined && price.length === 1) {
  23. price = `.${price}0`;
  24. } else {
  25. price === undefined ? (price = ".00") : (price = `.${price}`);
  26. }
  27. return price;
  28. }
  29. tools.formatDecimal = function (num, decimal) {
  30. num = num.toString()
  31. let index = num.indexOf('.')
  32. if (index !== -1) {
  33. num = num.substring(0, decimal + index + 1)
  34. } else {
  35. num = num.substring(0)
  36. }
  37. return parseFloat(num).toFixed(decimal)
  38. }
  39. /**
  40. * 错误提示
  41. * @param msg
  42. */
  43. tools.error = function (msg,icon='error') {
  44. uni.showToast({
  45. 'title': msg,
  46. 'icon': icon,
  47. 'mask': true,
  48. 'duration': 1500
  49. })
  50. }
  51. /**
  52. * 成功提示
  53. * @param msg
  54. */
  55. tools.success = function (msg,icon='success') {
  56. uni.showToast({
  57. 'title': msg,
  58. 'icon': icon,
  59. 'mask': true,
  60. 'duration': 1500
  61. })
  62. }
  63. /**
  64. * 显示Loading
  65. */
  66. tools.showLoading = function () {
  67. uni.showLoading({
  68. title: '加载中...',
  69. mask: true
  70. });
  71. }
  72. /**
  73. * 关闭Loading
  74. */
  75. tools.hideLoading = function () {
  76. uni.hideLoading();
  77. }
  78. /**
  79. * 获取时间戳(毫秒)
  80. * @returns {number}
  81. */
  82. tools.getTime = function () {
  83. return new Date().getTime();
  84. }
  85. tools.getCountDays=function (date){
  86. let curDate = new Date(date);
  87. // 获取当前月份
  88. curDate.setDate(32);
  89. // 返回当前月份的天数
  90. return 32-curDate.getDate();
  91. }
  92. /**
  93. * 获取当前年
  94. * @returns {number}
  95. */
  96. tools.getYear=function (){
  97. return new Date().getFullYear()
  98. }
  99. tools.updateVersion = function (sysVersion, appUrl) {
  100. let app_version = plus.runtime.version;
  101. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  102. console.log(app_version < sysVersion)
  103. if (app_version < sysVersion) {
  104. uni.showLoading({
  105. title: '更新中……'
  106. })
  107. uni.downloadFile({//执行下载
  108. url: appUrl, //下载地址
  109. success: (downloadResult) => {//下载成功
  110. uni.hideLoading();
  111. if (downloadResult.statusCode === 200) {
  112. uni.showModal({
  113. title: '',
  114. content: '更新成功,确定现在重启吗?',
  115. confirmText: '重启',
  116. confirmColor: '#EE8F57',
  117. success: function (res) {
  118. if (res.confirm === true) {
  119. plus.runtime.install(//安装
  120. downloadResult.tempFilePath, {
  121. force: true
  122. },
  123. function (res) {
  124. tools.success('更新成功,重启中')
  125. plus.runtime.restart();
  126. }
  127. );
  128. }
  129. }
  130. });
  131. }
  132. }
  133. });
  134. } else {
  135. // tools.success('你已是最新版本')
  136. }
  137. }
  138. /**
  139. * uniapp html图片显示控制
  140. * @param str
  141. * @returns {*}
  142. */
  143. tools.imgDeal = function (str) {
  144. console.log(str)
  145. if(str===null || str===undefined){
  146. return '';
  147. }else {
  148. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  149. }
  150. }
  151. /**
  152. * 获取平台类型 1:微信,2:支付宝
  153. * @returns {boolean|number}
  154. */
  155. tools.platformType = function () {
  156. let ua = window.navigator.userAgent.toLowerCase();
  157. if (ua.indexOf('micromessenger') != -1) {
  158. return 1;
  159. } else if (ua.indexOf('alipay') != -1) {
  160. return 2;
  161. } else {
  162. return 0;
  163. }
  164. }
  165. /**
  166. * 手机震动
  167. */
  168. tools.vibrate=function (){
  169. console.log('vibrate')
  170. // #ifndef H5
  171. let platform=uni.getSystemInfoSync().platform
  172. // #ifdef APP-PLUS
  173. if (platform === "ios") {
  174. let UIImpactFeedbackGenerator = plus.ios.importClass('UIImpactFeedbackGenerator');
  175. let impact = new UIImpactFeedbackGenerator();
  176. impact.prepare();
  177. impact.init(1);
  178. impact.impactOccurred();
  179. }
  180. if (platform === "android") {
  181. uni.vibrateShort();
  182. }
  183. // #endif
  184. //#endif
  185. }
  186. /**
  187. * 获取权限
  188. */
  189. tools.getAndroidPermission = async function(permissionName) {
  190. let ret = await permision.requestAndroidPermission(permissionName);
  191. console.log(ret)
  192. return ret;
  193. }
  194. /**
  195. * ios权限验证
  196. * @param permissionName
  197. * @returns {Promise<any>}
  198. */
  199. tools.getIosPermission = async function(permissionName) {
  200. let ret = await permision.judgeIosPermission(permissionName);
  201. console.log(ret)
  202. return ret;
  203. }
  204. /**
  205. * 获取开发平台
  206. * @returns {string}
  207. */
  208. tools.getPlatform = function () {
  209. let platForm = undefined;
  210. // #ifdef H5
  211. platForm = 'H5';
  212. //#endif
  213. // #ifdef APP-PLUS
  214. platForm = 'APP';
  215. //#endif
  216. // #ifdef APP-PLUS-NVUE
  217. platForm = 'APP';
  218. //#endif
  219. // #ifdef APP-NVUE
  220. platForm = 'APP';
  221. //#endif
  222. // #ifdef MP-WEIXIN
  223. platForm = 'MP-WEIXIN'; // 小程序
  224. //#endif
  225. // #ifdef MP-ALIPAY
  226. platForm = 'MP-ALIPAY';
  227. //#endif
  228. // #ifdef MP-BAIDU
  229. platForm = 'MP-BAIDU';
  230. //#endif
  231. // #ifdef MP-TOUTIAO
  232. platForm = 'MP-TOUTIAO';
  233. //#endif
  234. // #ifdef MP-LARK
  235. platForm = 'MP-LARK';
  236. //#endif
  237. // #ifdef MP-QQ
  238. platForm = 'MP-QQ';
  239. //#endif
  240. // #ifdef MP-KUAISHOU
  241. platForm = 'MP-KUAISHOU';
  242. //#endif
  243. // #ifdef QUICKAPP-WEBVIEW
  244. platForm = 'QUICKAPP-WEBVIEW';
  245. //#endif
  246. return platForm;
  247. }
  248. tools.leftClick = function () {
  249. let pages = getCurrentPages();
  250. if (pages.length > 1) {
  251. uni.navigateBack({
  252. delta: 1
  253. })
  254. } else {
  255. uni.reLaunch({
  256. url: '/pages/index/index'
  257. });
  258. }
  259. }
  260. tools.getDate = function () {
  261. let myDate = new Date();
  262. // let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
  263. // let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  264. // let myToday = myDate.getDate(); //获取当前日(1-31)
  265. // let myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
  266. // let myHour = myDate.getHours(); //获取当前小时数(0-23)
  267. // let myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
  268. // let mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
  269. return myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate()
  270. }
  271. tools.getRandFileName = function (filePath)
  272. {
  273. let extIndex = filePath.lastIndexOf('.');
  274. let extName = extIndex === -1 ? '' : filePath.substr(extIndex);
  275. return parseInt('' + Date.now() + Math.floor(Math.random() * 900 + 100), 10).toString(36) + extName;
  276. }
  277. /**
  278. * 自定义 获取指定日期到当前日期的 所有年 or 年月
  279. */
  280. tools.getCustomTimeList = (start,end,type)=>{
  281. let timeList = [];
  282. let s = start.split("-");
  283. let e = end.split("-");
  284. let min = new Date()
  285. let max = new Date()
  286. min.setFullYear(s[0], s[1]);
  287. max.setFullYear(e[0], e[1]);
  288. var curr = min;
  289. var str = "";
  290. while (curr <= max) {
  291. var yers = curr.getFullYear();
  292. var month = curr.getMonth();
  293. if(type === 'y-m'){
  294. if (month === 0) {
  295. str = (yers - 1) + "-" + 12;
  296. } else {
  297. str = yers + "-" + (month < 10 ? ("0" + month) : month);
  298. }
  299. timeList.push(str);
  300. curr.setMonth(month + 1);
  301. }else if(type === 'y'){
  302. if (month === 0) {
  303. str = (yers - 1)
  304. } else {
  305. str = yers
  306. }
  307. timeList.push(str);
  308. curr.setMonth(month + 1);
  309. }else{}
  310. }
  311. if(type === 'y-m'){
  312. return timeList.reverse()
  313. }else{
  314. let list = Array.from(new Set(timeList))
  315. return list.reverse()
  316. }
  317. }
  318. // 获取 当前 年-月 配合 getCustomTimeList 使用 获取end
  319. tools.getDateYM = ()=>{
  320. let DATE = new Date()
  321. let Y = DATE.getFullYear()
  322. let M = DATE.getMonth() + 1
  323. return Y + '-' + (M<10?'0'+M:M)
  324. }
  325. /**
  326. * 记录用户登录信息
  327. * @param data
  328. * @param type
  329. */
  330. tools.setLoginData = function (data) {
  331. uni.setStorageSync('token', data.access_token)
  332. uni.setStorageSync('tokenType', data.token_type)
  333. uni.setStorageSync('refreshToken', data.access_token)
  334. uni.setStorageSync('mobile', data.username)
  335. tools.success('登陆成功')
  336. // setTimeout(() => {
  337. // if (data.status * 1 === 0) {
  338. // //审核状态跳转至待审核页面
  339. // uni.reLaunch({
  340. // url: '/pages/login/module/await-audit'
  341. // });
  342. // } else {
  343. // uni.reLaunch({
  344. // url: '/pages/index/home'
  345. // });
  346. // }
  347. // }, 1500)
  348. }
  349. tools.addQueryString=function (params) {
  350. let str = '';
  351. for (let Key in params) {
  352. str += Key + '=' + params[Key] + '&';
  353. }
  354. return '?' + str;
  355. //return '?' + str.substr(0, str.length -1); 严谨一些
  356. }
  357. tools.setCosToken=function (data){
  358. uni.setStorageSync('cosToken',data)
  359. }
  360. tools.delCosToken=function (data){
  361. uni.removeStorageSync('cosToken')
  362. }
  363. tools.getCosToken=function (){
  364. let cosToken= uni.getStorageSync('cosToken')
  365. if(!cosToken){
  366. return undefined
  367. }
  368. let time=new Date().getTime()
  369. if(cosToken.expiredTime && (cosToken.expiredTime*1000)-time >100000){
  370. return cosToken
  371. }else {
  372. return undefined
  373. }
  374. }
  375. export default tools