methodTool.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import * as base_url from '@/http/http.js'
  2. export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/'
  3. export default {
  4. isGoLogin(isBack = true) {
  5. if (!uni.getStorageSync('user_account')) {
  6. uni.navigateTo({
  7. url: '/pages4/login/login?isBack=' + isBack
  8. });
  9. return true;
  10. } else {
  11. return false
  12. }
  13. },
  14. isLogin() {
  15. if (uni.getStorageSync('user_account')) {
  16. return true;
  17. } else {
  18. return false
  19. }
  20. },
  21. isLogout() {
  22. // uni.removeStorageSync('user_account')
  23. // uni.removeStorageSync('token')
  24. },
  25. //提示
  26. showToast(title, icon = 'none', time = 2000) {
  27. return setTimeout(() => {
  28. uni.showToast({
  29. title: title,
  30. icon: icon,
  31. duration: time
  32. })
  33. }, 500)
  34. },
  35. //图片路径填补
  36. splitImgHost(url, scale = false, width = 250) {
  37. if (!url) {
  38. return ''
  39. } else if (url.indexOf("http") != -1 || url.indexOf("https") != -1 || url.indexOf("wxfile") != -1) {
  40. } else {
  41. url = base_url.BASE_IMG_URL + url
  42. }
  43. if (scale) {
  44. url = url + "?x-oss-process=image/resize,w_" + width
  45. }
  46. return url;
  47. },
  48. exit() {
  49. uni.removeStorageSync('user_account')
  50. uni.removeStorageSync('token')
  51. uni.reLaunch({
  52. url: '/pages/index/index'
  53. });
  54. },
  55. /* 时间戳转换成日期
  56. * @param timestamp
  57. * @returns {*}
  58. */
  59. timestampToTime(timestamp, isDay = true,hasChinese) {
  60. var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  61. var Y = date.getFullYear() + (hasChinese ? '' : '-');
  62. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (hasChinese ? '' : '-');
  63. var D = date.getDate() < 10 ? '0' + date.getDate() + (hasChinese ? '' : ' ') : date.getDate() + (hasChinese ? '' :' ');
  64. var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  65. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  66. var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  67. if (isDay) {
  68. if(hasChinese) {
  69. return Y +'年'+ M +'月'+ D + '日';
  70. } else {
  71. return Y + M + D;
  72. }
  73. }
  74. if(hasChinese) {
  75. return Y +'年'+ M +'月'+ D + '日' + h + m + s;
  76. } else {
  77. return Y + M + D + h + m + s;
  78. }
  79. },
  80. //当前时间距离目标时间还有多久
  81. GetRTime(EndTime, isDay = true) {
  82. var EndTime = EndTime //结束时间
  83. var NowTime = new Date(); //当前时间
  84. //后台给我的是10位 精确到秒的 所有下面我就除以了1000,不要小数点后面的
  85. var t = EndTime - (NowTime.getTime() / 1000).toFixed(0);
  86. if (t <= 0) {
  87. return '已结束'
  88. }
  89. //如果后台给的是毫秒 上面不用除以1000 下面的计算时间也都要除以1000 这里我去掉1000了
  90. var d = Math.floor(t / 60 / 60 / 24); //天 var d=Math.floor(t/1000/60/60/24)
  91. var h = Math.floor(t / 60 / 60 % 24); //时 var h=Math.floor(t/1000/60/60%24)
  92. var m = Math.floor(t / 60 % 60); //分 var m=Math.floor(t/1000/60%60)
  93. var s = Math.floor(t % 60); //秒 var s=Math.floor(t/1000%60)
  94. if (parseInt(d) < 10) {
  95. d = "0" + d;
  96. }
  97. if (parseInt(h) < 10) {
  98. h = "0" + h;
  99. }
  100. if (parseInt(m) < 10) {
  101. m = "0" + m;
  102. }
  103. if (parseInt(s) < 10) {
  104. s = "0" + s;
  105. }
  106. if (isDay) {
  107. return d;
  108. }
  109. return d + '天' + h + '小时' + m + '分' + s + '秒'
  110. },
  111. TimeTotimestamp(date) {
  112. var date = date.replace(/-/g, '/');
  113. var newDate = new Date(date)
  114. return (newDate.getTime() / 1000)
  115. },
  116. timest() {
  117. var tmp = Date.parse(new Date()).toString();
  118. tmp = tmp.substr(0, 10);
  119. return tmp;
  120. },
  121. getYears(strBirthday) {
  122. if (!strBirthday) {
  123. return '-';
  124. }
  125. var returnAge;
  126. var strBirthdayArr = strBirthday.split('-');
  127. var birthYear = strBirthdayArr[0];
  128. var birthMonth = strBirthdayArr[1];
  129. var birthDay = strBirthdayArr[2];
  130. var d = new Date();
  131. var nowYear = d.getFullYear();
  132. var nowMonth = d.getMonth() + 1;
  133. var nowDay = d.getDate();
  134. if (nowYear == birthYear) {
  135. returnAge = 0; //同年 则为0岁
  136. } else {
  137. var ageDiff = nowYear - birthYear; //年之差
  138. if (ageDiff > 0) {
  139. if (nowMonth == birthMonth) {
  140. var dayDiff = nowDay - birthDay; //日之差
  141. if (dayDiff < 0) {
  142. returnAge = ageDiff - 1;
  143. } else {
  144. returnAge = ageDiff;
  145. }
  146. } else {
  147. var monthDiff = nowMonth - birthMonth; //月之差
  148. if (monthDiff < 0) {
  149. returnAge = ageDiff - 1;
  150. } else {
  151. returnAge = ageDiff;
  152. }
  153. }
  154. } else {
  155. returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
  156. }
  157. }
  158. return returnAge; //返回周岁年龄
  159. },
  160. getDate () {
  161. let nowDate = new Date()
  162. let date = {
  163. year: nowDate.getFullYear(),
  164. month: nowDate.getMonth() + 1,
  165. date: nowDate.getDate()
  166. }
  167. return date.year + '-' + date.month + '-' + date.date
  168. },
  169. getZeroTime () {
  170. return Number(new Date(new Date().toLocaleDateString()).getTime()/1000)
  171. },
  172. setClock:function(time){
  173. var that=this, sec= parseInt(time) , clockCount={}, strTimer="";
  174. clockCount=setInterval(function(){
  175. if(sec==0){
  176. $(".js-count-down").html("活动已经结束");
  177. clearInterval(clockCount);
  178. return false;
  179. }
  180. strTimer = that.secondToDate(sec);
  181. $(".js-count-down").html(strTimer);
  182. sec--;
  183. },1000)
  184. },
  185. secondToDate(result){
  186. var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
  187. var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  188. var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  189. if(h==0){
  190. result = m + ":" + s;
  191. }else{
  192. result = h+':'+m + ":" + s
  193. }
  194. return result;
  195. },
  196. /**
  197.      *
  198.      * @param {int} result
  199.      * @returns {string}
  200.      * @remard 单位S转小时分钟秒
  201.      */
  202.     secondToTime(result,Diszing = true) {
  203.         var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  204.         var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  205.         var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  206.         if (h == 0 && Diszing) {
  207.             result = m + ":" + s;
  208.         } else {
  209.             result = h + ':' + m + ":" + s
  210.         }
  211.         return result;
  212.     },
  213. /**
  214. * @param {Object} length 长度
  215. * 获取随机字符串
  216. */
  217. getRandomString(length) {
  218. var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  219. var result = '';
  220. for (var i = length; i > 0; --i)
  221. result += str[Math.floor(Math.random() * str.length)];
  222. return result;
  223. },
  224. }