methodTool.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. },
  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) {
  41. } else {
  42. url = baseUrls.BASE_IMG_URL + url
  43. }
  44. if (scale) {
  45. url = url + "?x-oss-process=image/resize,w_" + width
  46. }
  47. return url;
  48. },
  49. exit() {
  50. uni.removeStorageSync('user_account')
  51. uni.removeStorageSync('token')
  52. store.state.userInfo = null
  53. uni.reLaunch({
  54. url: '/pages/index/index'
  55. });
  56. },
  57. /* 时间戳转换成日期
  58. * @param timestamp
  59. * @returns {*}
  60. */
  61. timestampToTime(timestamp, isDay = true) {
  62. var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  63. var Y = date.getFullYear() + '-';
  64. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  65. var D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  66. var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  67. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  68. var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  69. if (isDay) {
  70. return Y + M + D;
  71. }
  72. return Y + M + D + h + m + s;
  73. },
  74. //当前时间距离目标时间还有多久
  75. GetRTime(EndTime, isDay = true) {
  76. var EndTime = EndTime //结束时间
  77. var NowTime = new Date(); //当前时间
  78. //后台给我的是10位 精确到秒的 所有下面我就除以了1000,不要小数点后面的
  79. var t = EndTime - (NowTime.getTime() / 1000).toFixed(0);
  80. if (t <= 0) {
  81. return '已结束'
  82. }
  83. //如果后台给的是毫秒 上面不用除以1000 下面的计算时间也都要除以1000 这里我去掉1000了
  84. var d = Math.floor(t / 60 / 60 / 24); //天 var d=Math.floor(t/1000/60/60/24)
  85. var h = Math.floor(t / 60 / 60 % 24); //时 var h=Math.floor(t/1000/60/60%24)
  86. var m = Math.floor(t / 60 % 60); //分 var m=Math.floor(t/1000/60%60)
  87. var s = Math.floor(t % 60); //秒 var s=Math.floor(t/1000%60)
  88. if (parseInt(d) < 10) {
  89. d = "0" + d;
  90. }
  91. if (parseInt(h) < 10) {
  92. h = "0" + h;
  93. }
  94. if (parseInt(m) < 10) {
  95. m = "0" + m;
  96. }
  97. if (parseInt(s) < 10) {
  98. s = "0" + s;
  99. }
  100. if (isDay) {
  101. return d;
  102. }
  103. return d + '天' + h + '小时' + m + '分' + s + '秒'
  104. },
  105. TimeTotimestamp(date) {
  106. var date = date.replace(/-/g, '/');
  107. var newDate = new Date(date)
  108. return (newDate.getTime() / 1000)
  109. },
  110. timest() {
  111. var tmp = Date.parse(new Date()).toString();
  112. tmp = tmp.substr(0, 10);
  113. return tmp;
  114. },
  115. //压缩图片
  116. imageInfos(url) {
  117. var self = this;
  118. return new Promise((resolve, reject) => {
  119. uni.getImageInfo({
  120. src: url,
  121. success: async res => {
  122. let canvasWidth = res.width; //图片原始长宽
  123. let canvasHeight = res.height;
  124. if (canvasWidth > 1000 || canvasHeight > 1000) {
  125. uni.compressImage({
  126. src: url,
  127. quality: 75,
  128. width: '50%',
  129. height: '50%',
  130. success: rest => {
  131. resolve(rest.tempFilePath);
  132. }
  133. });
  134. } else {
  135. console.log('无需压缩', url);
  136. resolve(url);
  137. }
  138. }
  139. });
  140. });
  141. },
  142. //上传图片
  143. uploadFile(options, int) {
  144. return new Promise((resolve, reject) => {
  145. var self = this;
  146. if (options.indexOf('//tmp/') === -1) {
  147. resolve(options)
  148. return
  149. }
  150. var data = {
  151. imageStatus: int
  152. };
  153. api.aliyunpolicy(data).then(res => {
  154. var ossToken = res.data.data.resultContent;
  155. uni.uploadFile({
  156. url: ossToken.host,
  157. name: 'file',
  158. filePath: options,
  159. fileType: 'image',
  160. header: {
  161. AuthorizationToken: 'WX ' + uni.getStorageSync('token')
  162. },
  163. formData: {
  164. key: ossToken.dir,
  165. OSSAccessKeyId: ossToken.accessid,
  166. policy: ossToken.policy,
  167. Signature: ossToken.signature,
  168. callback: ossToken.callback,
  169. success_action_status: 200
  170. },
  171. success: result => {
  172. if (result.statusCode === 200) {
  173. resolve(ossToken.dir);
  174. } else {
  175. uni.showToast({
  176. title: '上传失败',
  177. icon: 'none'
  178. });
  179. return;
  180. }
  181. },
  182. fail: error => {
  183. uni.showToast({
  184. title: '上传接口报错',
  185. icon: 'none'
  186. });
  187. return;
  188. }
  189. });
  190. });
  191. });
  192. },
  193. getYears(strBirthday) {
  194. if (!strBirthday) {
  195. return '-';
  196. }
  197. var returnAge;
  198. var strBirthdayArr = strBirthday.split('-');
  199. var birthYear = strBirthdayArr[0];
  200. var birthMonth = strBirthdayArr[1];
  201. var birthDay = strBirthdayArr[2];
  202. var d = new Date();
  203. var nowYear = d.getFullYear();
  204. var nowMonth = d.getMonth() + 1;
  205. var nowDay = d.getDate();
  206. if (nowYear == birthYear) {
  207. returnAge = 0; //同年 则为0岁
  208. } else {
  209. var ageDiff = nowYear - birthYear; //年之差
  210. if (ageDiff > 0) {
  211. if (nowMonth == birthMonth) {
  212. var dayDiff = nowDay - birthDay; //日之差
  213. if (dayDiff < 0) {
  214. returnAge = ageDiff - 1;
  215. } else {
  216. returnAge = ageDiff;
  217. }
  218. } else {
  219. var monthDiff = nowMonth - birthMonth; //月之差
  220. if (monthDiff < 0) {
  221. returnAge = ageDiff - 1;
  222. } else {
  223. returnAge = ageDiff;
  224. }
  225. }
  226. } else {
  227. returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
  228. }
  229. }
  230. return returnAge; //返回周岁年龄
  231. },
  232. getDate () {
  233. let nowDate = new Date()
  234. let date = {
  235. year: nowDate.getFullYear(),
  236. month: nowDate.getMonth() + 1,
  237. date: nowDate.getDate()
  238. }
  239. return date.year + '-' + date.month + '-' + date.date
  240. },
  241. getZeroTime () {
  242. return Number(new Date(new Date().toLocaleDateString()).getTime()/1000)
  243. },
  244. setClock:function(time){
  245. var that=this, sec= parseInt(time) , clockCount={}, strTimer="";
  246. clockCount=setInterval(function(){
  247. if(sec==0){
  248. $(".js-count-down").html("活动已经结束");
  249. clearInterval(clockCount);
  250. return false;
  251. }
  252. strTimer = that.secondToDate(sec);
  253. $(".js-count-down").html(strTimer);
  254. sec--;
  255. },1000)
  256. },
  257. secondToDate(result){
  258. var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
  259. var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  260. var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  261. if(h==0){
  262. result = m + ":" + s;
  263. }else{
  264. result = h+':'+m + ":" + s
  265. }
  266. return result;
  267. }
  268. }