methodTool.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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: '/pages4/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. setUuid(id) {
  51. uni.setStorageSync('uuid',id)
  52. },
  53. getUuid() {
  54. return uni.getStorageSync('uuid')
  55. },
  56. exit() {
  57. uni.removeStorageSync('user_account')
  58. uni.removeStorageSync('token')
  59. store.state.userInfo = null
  60. uni.reLaunch({
  61. url: '/pages/index/index'
  62. });
  63. },
  64. /* 时间戳转换成日期
  65. * @param timestamp
  66. * @returns {*}
  67. */
  68. timestampToTime(timestamp, isDay = true,hasChinese) {
  69. var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  70. var Y = date.getFullYear() + (hasChinese ? '' : '-');
  71. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (hasChinese ? '' : '-');
  72. var D = date.getDate() < 10 ? '0' + date.getDate() + (hasChinese ? '' : ' ') : date.getDate() + (hasChinese ? '' :' ');
  73. var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  74. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  75. var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  76. if (isDay) {
  77. if(hasChinese) {
  78. return Y +'年'+ M +'月'+ D + '日';
  79. } else {
  80. return Y + M + D;
  81. }
  82. }
  83. if(hasChinese) {
  84. return Y +'年'+ M +'月'+ D + '日' + h + m + s;
  85. } else {
  86. return Y + M + D + h + m + s;
  87. }
  88. },
  89. // 时间戳转换日期
  90. formDate(time, formate = 'yyyy-mm-dd hh:mm:ss') {
  91. console.log('fsdfgd==============')
  92. var padDate = function (va) {
  93. va = va < 10 ? '0' + va : va
  94. return va
  95. }
  96. if (time) {
  97. var value = new Date(time * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  98. // var value = new Date(parseFloat(time))
  99. var year = value.getFullYear()
  100. var month = padDate(value.getMonth() + 1)
  101. var day = padDate(value.getDate())
  102. var hour = padDate(value.getHours())
  103. var minutes = padDate(value.getMinutes())
  104. var seconds = padDate(value.getSeconds())
  105. let res = ''
  106. switch (formate) {
  107. case 'mm-dd': {
  108. res = month + '-' + day
  109. break
  110. }
  111. case 'yyyy-mm-dd': {
  112. res = year + '-' + month + '-' + day
  113. break
  114. }
  115. case 'yyyy-mm': {
  116. res = year + '-' + month
  117. break
  118. }
  119. case 'mm月dd日': {
  120. res = month + '月' + day + '日'
  121. break
  122. }
  123. case 'yyyy年mm月dd日': {
  124. res = year + '年' + month + '月' + day + '日'
  125. break
  126. }
  127. case 'yyyy年mm月': {
  128. res = year + '年' + month + '月'
  129. break
  130. }
  131. case 'yyyy-mm-dd hh:mm': {
  132. res = year + '-' + month + '-' + day + ' ' + hour + ':' + minutes
  133. break
  134. }
  135. case 'yyyy.mm.dd':
  136. res = year + '.' + month + '.' + day
  137. break
  138. case 'yyyy-mm-dd hh:mm:ss':
  139. default: {
  140. res = year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
  141. break
  142. }
  143. }
  144. return res
  145. }
  146. return '--'
  147. },
  148. //当前时间距离目标时间还有多久
  149. GetRTime(EndTime, isDay = true) {
  150. var EndTime = EndTime //结束时间
  151. var NowTime = new Date(); //当前时间
  152. //后台给我的是10位 精确到秒的 所有下面我就除以了1000,不要小数点后面的
  153. var t = EndTime - (NowTime.getTime() / 1000).toFixed(0);
  154. if (t <= 0) {
  155. return '已结束'
  156. }
  157. //如果后台给的是毫秒 上面不用除以1000 下面的计算时间也都要除以1000 这里我去掉1000了
  158. var d = Math.floor(t / 60 / 60 / 24); //天 var d=Math.floor(t/1000/60/60/24)
  159. var h = Math.floor(t / 60 / 60 % 24); //时 var h=Math.floor(t/1000/60/60%24)
  160. var m = Math.floor(t / 60 % 60); //分 var m=Math.floor(t/1000/60%60)
  161. var s = Math.floor(t % 60); //秒 var s=Math.floor(t/1000%60)
  162. if (parseInt(d) < 10) {
  163. d = "0" + d;
  164. }
  165. if (parseInt(h) < 10) {
  166. h = "0" + h;
  167. }
  168. if (parseInt(m) < 10) {
  169. m = "0" + m;
  170. }
  171. if (parseInt(s) < 10) {
  172. s = "0" + s;
  173. }
  174. if (isDay) {
  175. return d;
  176. }
  177. return d + '天' + h + '小时' + m + '分' + s + '秒'
  178. },
  179. TimeTotimestamp(date) {
  180. var date = date.replace(/-/g, '/');
  181. var newDate = new Date(date)
  182. return (newDate.getTime() / 1000)
  183. },
  184. timest() {
  185. var tmp = Date.parse(new Date()).toString();
  186. tmp = tmp.substr(0, 10);
  187. return tmp;
  188. },
  189. //压缩图片
  190. imageInfos(url) {
  191. var self = this;
  192. return new Promise((resolve, reject) => {
  193. uni.getImageInfo({
  194. src: url,
  195. success: async res => {
  196. let canvasWidth = res.width; //图片原始长宽
  197. let canvasHeight = res.height;
  198. if (canvasWidth > 2000 || canvasHeight > 2000) {
  199. uni.compressImage({
  200. src: url,
  201. quality: 75,
  202. width: '35%',
  203. height: '35%',
  204. success:async rest => {
  205. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  206. console.log(waitUpload,'waitUpload')
  207. resolve(waitUpload);
  208. }
  209. });
  210. } else if(canvasWidth > 1000 || canvasHeight > 1000){
  211. uni.compressImage({
  212. src: url,
  213. quality: 75,
  214. width: '50%',
  215. height: '50%',
  216. success: async rest => {
  217. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  218. console.log(waitUpload,'waitUpload')
  219. resolve(waitUpload);
  220. }
  221. });
  222. } else {
  223. const waitUpload = await self.uploadFile(url, 0);
  224. console.log(waitUpload,'waitUpload')
  225. resolve(waitUpload);
  226. // console.log('无需压缩', url);
  227. // resolve(url);
  228. }
  229. }
  230. });
  231. });
  232. },
  233. //上传图片
  234. uploadFile(options, int) {
  235. return new Promise((resolve, reject) => {
  236. var self = this;
  237. if (options.indexOf('//tmp') === -1 && options.indexOf('//temp') === -1) {
  238. resolve(options)
  239. return
  240. }
  241. var data = {
  242. imageStatus: int
  243. };
  244. api.aliyunpolicy(data).then(res => {
  245. var ossToken = res.data.data.resultContent;
  246. uni.uploadFile({
  247. url: ossToken.host,
  248. name: 'file',
  249. filePath: options,
  250. fileType: 'image',
  251. header: {
  252. AuthorizationToken: 'WX ' + uni.getStorageSync('token')
  253. },
  254. formData: {
  255. key: ossToken.dir,
  256. OSSAccessKeyId: ossToken.accessid,
  257. policy: ossToken.policy,
  258. Signature: ossToken.signature,
  259. // callback: ossToken.callback,
  260. success_action_status: 200
  261. },
  262. success: result => {
  263. if (result.statusCode === 200) {
  264. resolve(ossToken.dir);
  265. } else {
  266. uni.showToast({
  267. title: '上传失败',
  268. icon: 'none'
  269. });
  270. return;
  271. }
  272. },
  273. fail: error => {
  274. uni.showToast({
  275. title: '上传接口报错',
  276. icon: 'none'
  277. });
  278. return;
  279. }
  280. });
  281. });
  282. });
  283. },
  284. getYears(strBirthday) {
  285. if (!strBirthday) {
  286. return '-';
  287. }
  288. var returnAge;
  289. var strBirthdayArr = strBirthday.split('-');
  290. var birthYear = strBirthdayArr[0];
  291. var birthMonth = strBirthdayArr[1];
  292. var birthDay = strBirthdayArr[2];
  293. var d = new Date();
  294. var nowYear = d.getFullYear();
  295. var nowMonth = d.getMonth() + 1;
  296. var nowDay = d.getDate();
  297. if (nowYear == birthYear) {
  298. returnAge = 0; //同年 则为0岁
  299. } else {
  300. var ageDiff = nowYear - birthYear; //年之差
  301. if (ageDiff > 0) {
  302. if (nowMonth == birthMonth) {
  303. var dayDiff = nowDay - birthDay; //日之差
  304. if (dayDiff < 0) {
  305. returnAge = ageDiff - 1;
  306. } else {
  307. returnAge = ageDiff;
  308. }
  309. } else {
  310. var monthDiff = nowMonth - birthMonth; //月之差
  311. if (monthDiff < 0) {
  312. returnAge = ageDiff - 1;
  313. } else {
  314. returnAge = ageDiff;
  315. }
  316. }
  317. } else {
  318. returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
  319. }
  320. }
  321. return returnAge; //返回周岁年龄
  322. },
  323. getDate () {
  324. let nowDate = new Date()
  325. let date = {
  326. year: nowDate.getFullYear(),
  327. month: nowDate.getMonth() + 1,
  328. date: nowDate.getDate()
  329. }
  330. return date.year + '-' + date.month + '-' + date.date
  331. },
  332. getZeroTime () {
  333. return Number(new Date(new Date().toLocaleDateString()).getTime()/1000)
  334. },
  335. setClock:function(time){
  336. var that=this, sec= parseInt(time) , clockCount={}, strTimer="";
  337. clockCount=setInterval(function(){
  338. if(sec==0){
  339. $(".js-count-down").html("活动已经结束");
  340. clearInterval(clockCount);
  341. return false;
  342. }
  343. strTimer = that.secondToDate(sec);
  344. $(".js-count-down").html(strTimer);
  345. sec--;
  346. },1000)
  347. },
  348. secondToDate(result){
  349. var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
  350. var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  351. var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  352. if(h==0){
  353. result = m + ":" + s;
  354. }else{
  355. result = h+':'+m + ":" + s
  356. }
  357. return result;
  358. },
  359. /**
  360.      *
  361.      * @param {int} result
  362.      * @returns {string}
  363.      * @remard 单位S转小时分钟秒
  364.      */
  365.     secondToTime(result,Diszing = true) {
  366.         var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  367.         var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  368.         var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  369.         if (h == 0 && Diszing) {
  370.             result = m + ":" + s;
  371.         } else {
  372.             result = h + ':' + m + ":" + s
  373.         }
  374.         return result;
  375.     },
  376. /**
  377. * @param {Object} length 长度
  378. * 获取随机字符串
  379. */
  380. getRandomString(length) {
  381. var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  382. var result = '';
  383. for (var i = length; i > 0; --i)
  384. result += str[Math.floor(Math.random() * str.length)];
  385. return result;
  386. },
  387. }