methodTool.js 12 KB

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