methodTool.js 11 KB

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