methodTool.js 8.8 KB

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