methodTool.js 9.3 KB

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