methodTool.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import store from '@/store/index.js'
  2. import * as baseUrls from '@/common/request.js'
  3. import api from '@/common/api.js'
  4. // export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/'
  5. export default {
  6. isGoLogin(isBack = true) {
  7. if (!uni.getStorageSync('user_account')) {
  8. uni.navigateTo({
  9. url: '/pages/login/login?isBack=' + isBack
  10. });
  11. return true;
  12. } else {
  13. return false
  14. }
  15. },
  16. isLogin() {
  17. if (uni.getStorageSync('user_account')) {
  18. return true;
  19. } else {
  20. return false
  21. }
  22. },
  23. isLogout() {
  24. // uni.removeStorageSync('user_account')
  25. // uni.removeStorageSync('token')
  26. },
  27. //提示
  28. showToast(title, icon = 'none', time = 2000) {
  29. return setTimeout(() => {
  30. uni.showToast({
  31. title: title,
  32. icon: icon,
  33. duration: time
  34. })
  35. }, 500)
  36. },
  37. //图片路径填补
  38. splitImgHost(url, scale = false, width = 250) {
  39. if (!url) {
  40. return ''
  41. } else if (url.indexOf("http") != -1 || url.indexOf("https") != -1 || url.indexOf("wxfile") != -1) {
  42. } else {
  43. url = baseUrls.BASE_IMG_URL + url
  44. }
  45. if (scale) {
  46. url = url + "?x-oss-process=image/resize,w_" + width
  47. }
  48. return url;
  49. },
  50. exit() {
  51. uni.removeStorageSync('user_account')
  52. uni.removeStorageSync('token')
  53. store.state.userInfo = null
  54. uni.reLaunch({
  55. url: '/pages/index/index'
  56. });
  57. },
  58. /* 时间戳转换成日期
  59. * @param timestamp
  60. * @returns {*}
  61. */
  62. timestampToTime(timestamp, isDay = true,hasChinese) {
  63. var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  64. var Y = date.getFullYear() + (hasChinese ? '' : '-');
  65. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (hasChinese ? '' : '-');
  66. var D = date.getDate() < 10 ? '0' + date.getDate() + (hasChinese ? '' : ' ') : date.getDate() + (hasChinese ? '' :' ');
  67. var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  68. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  69. var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  70. if (isDay) {
  71. if(hasChinese) {
  72. return Y +'年'+ M +'月'+ D + '日';
  73. } else {
  74. return Y + M + D;
  75. }
  76. }
  77. if(hasChinese) {
  78. return Y +'年'+ M +'月'+ D + '日' + h + m + s;
  79. } else {
  80. return Y + M + D + h + m + s;
  81. }
  82. },
  83. //当前时间距离目标时间还有多久
  84. GetRTime(EndTime, isDay = true) {
  85. var EndTime = EndTime //结束时间
  86. var NowTime = new Date(); //当前时间
  87. //后台给我的是10位 精确到秒的 所有下面我就除以了1000,不要小数点后面的
  88. var t = EndTime - (NowTime.getTime() / 1000).toFixed(0);
  89. if (t <= 0) {
  90. return '已结束'
  91. }
  92. //如果后台给的是毫秒 上面不用除以1000 下面的计算时间也都要除以1000 这里我去掉1000了
  93. var d = Math.floor(t / 60 / 60 / 24); //天 var d=Math.floor(t/1000/60/60/24)
  94. var h = Math.floor(t / 60 / 60 % 24); //时 var h=Math.floor(t/1000/60/60%24)
  95. var m = Math.floor(t / 60 % 60); //分 var m=Math.floor(t/1000/60%60)
  96. var s = Math.floor(t % 60); //秒 var s=Math.floor(t/1000%60)
  97. if (parseInt(d) < 10) {
  98. d = "0" + d;
  99. }
  100. if (parseInt(h) < 10) {
  101. h = "0" + h;
  102. }
  103. if (parseInt(m) < 10) {
  104. m = "0" + m;
  105. }
  106. if (parseInt(s) < 10) {
  107. s = "0" + s;
  108. }
  109. if (isDay) {
  110. return d;
  111. }
  112. return d + '天' + h + '小时' + m + '分' + s + '秒'
  113. },
  114. TimeTotimestamp(date) {
  115. var date = date.replace(/-/g, '/');
  116. var newDate = new Date(date)
  117. return (newDate.getTime() / 1000)
  118. },
  119. timest() {
  120. var tmp = Date.parse(new Date()).toString();
  121. tmp = tmp.substr(0, 10);
  122. return tmp;
  123. },
  124. //压缩图片
  125. imageInfos(url) {
  126. var self = this;
  127. return new Promise((resolve, reject) => {
  128. uni.getImageInfo({
  129. src: url,
  130. success: async res => {
  131. let canvasWidth = res.width; //图片原始长宽
  132. let canvasHeight = res.height;
  133. if (canvasWidth > 2000 || canvasHeight > 2000) {
  134. uni.compressImage({
  135. src: url,
  136. quality: 75,
  137. width: '35%',
  138. height: '35%',
  139. success:async rest => {
  140. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  141. resolve(waitUpload);
  142. }
  143. });
  144. } else if(canvasWidth > 1000 || canvasHeight > 1000){
  145. uni.compressImage({
  146. src: url,
  147. quality: 75,
  148. width: '50%',
  149. height: '50%',
  150. success: async rest => {
  151. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  152. resolve(waitUpload);
  153. }
  154. });
  155. } else {
  156. const waitUpload = await self.uploadFile(url, 0);
  157. resolve(waitUpload);
  158. // console.log('无需压缩', url);
  159. // resolve(url);
  160. }
  161. }
  162. });
  163. });
  164. },
  165. //上传图片
  166. uploadFile(options, int) {
  167. return new Promise((resolve, reject) => {
  168. var self = this;
  169. if (options.indexOf('//tmp') === -1 && options.indexOf('//temp') === -1) {
  170. resolve(options)
  171. return
  172. }
  173. var data = {
  174. imageStatus: int
  175. };
  176. api.aliyunpolicy(data).then(res => {
  177. var ossToken = res.data.data.resultContent;
  178. uni.uploadFile({
  179. url: ossToken.host,
  180. name: 'file',
  181. filePath: options,
  182. fileType: 'image',
  183. header: {
  184. AuthorizationToken: 'WX ' + uni.getStorageSync('token')
  185. },
  186. formData: {
  187. key: ossToken.dir,
  188. OSSAccessKeyId: ossToken.accessid,
  189. policy: ossToken.policy,
  190. Signature: ossToken.signature,
  191. callback: ossToken.callback,
  192. success_action_status: 200
  193. },
  194. success: result => {
  195. if (result.statusCode === 200) {
  196. resolve(ossToken.dir);
  197. } else {
  198. uni.showToast({
  199. title: '上传失败',
  200. icon: 'none'
  201. });
  202. return;
  203. }
  204. },
  205. fail: error => {
  206. uni.showToast({
  207. title: '上传接口报错',
  208. icon: 'none'
  209. });
  210. return;
  211. }
  212. });
  213. });
  214. });
  215. },
  216. getYears(strBirthday) {
  217. if (!strBirthday) {
  218. return '-';
  219. }
  220. var returnAge;
  221. var strBirthdayArr = strBirthday.split('-');
  222. var birthYear = strBirthdayArr[0];
  223. var birthMonth = strBirthdayArr[1];
  224. var birthDay = strBirthdayArr[2];
  225. var d = new Date();
  226. var nowYear = d.getFullYear();
  227. var nowMonth = d.getMonth() + 1;
  228. var nowDay = d.getDate();
  229. if (nowYear == birthYear) {
  230. returnAge = 0; //同年 则为0岁
  231. } else {
  232. var ageDiff = nowYear - birthYear; //年之差
  233. if (ageDiff > 0) {
  234. if (nowMonth == birthMonth) {
  235. var dayDiff = nowDay - birthDay; //日之差
  236. if (dayDiff < 0) {
  237. returnAge = ageDiff - 1;
  238. } else {
  239. returnAge = ageDiff;
  240. }
  241. } else {
  242. var monthDiff = nowMonth - birthMonth; //月之差
  243. if (monthDiff < 0) {
  244. returnAge = ageDiff - 1;
  245. } else {
  246. returnAge = ageDiff;
  247. }
  248. }
  249. } else {
  250. returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
  251. }
  252. }
  253. return returnAge; //返回周岁年龄
  254. },
  255. getDate () {
  256. let nowDate = new Date()
  257. let date = {
  258. year: nowDate.getFullYear(),
  259. month: nowDate.getMonth() + 1,
  260. date: nowDate.getDate()
  261. }
  262. return date.year + '-' + date.month + '-' + date.date
  263. },
  264. getZeroTime () {
  265. return Number(new Date(new Date().toLocaleDateString()).getTime()/1000)
  266. },
  267. setClock:function(time){
  268. var that=this, sec= parseInt(time) , clockCount={}, strTimer="";
  269. clockCount=setInterval(function(){
  270. if(sec==0){
  271. $(".js-count-down").html("活动已经结束");
  272. clearInterval(clockCount);
  273. return false;
  274. }
  275. strTimer = that.secondToDate(sec);
  276. $(".js-count-down").html(strTimer);
  277. sec--;
  278. },1000)
  279. },
  280. secondToDate(result){
  281. var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
  282. var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  283. var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  284. if(h==0){
  285. result = m + ":" + s;
  286. }else{
  287. result = h+':'+m + ":" + s
  288. }
  289. return result;
  290. },
  291. /**
  292.      *
  293.      * @param {int} result
  294.      * @returns {string}
  295.      * @remard 单位S转小时分钟秒
  296.      */
  297.     secondToTime(result,Diszing = true) {
  298.         var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  299.         var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  300.         var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  301.         if (h == 0 && Diszing) {
  302.             result = m + ":" + s;
  303.         } else {
  304.             result = h + ':' + m + ":" + s
  305.         }
  306.         return result;
  307.     },
  308. /**
  309. * @param {Object} length 长度
  310. * 获取随机字符串
  311. */
  312. getRandomString(length) {
  313. var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  314. var result = '';
  315. for (var i = length; i > 0; --i)
  316. result += str[Math.floor(Math.random() * str.length)];
  317. return result;
  318. }
  319. }