utils.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. export default {
  2. // 日期格式化
  3. formatDate(datestr, fmt = 'yyyy-MM-dd') {
  4. let date = new Date(datestr)
  5. let o = {
  6. 'M+': date.getMonth() + 1, // 月份
  7. 'd+': date.getDate(), // 日
  8. 'h+': date.getHours(), // 小时
  9. 'm+': date.getMinutes(), // 分
  10. 's+': date.getSeconds(), // 秒
  11. 'S': date.getMilliseconds() // 毫秒
  12. }
  13. if (/(y+)/.test(fmt)) {
  14. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  15. }
  16. for (var k in o) {
  17. if (new RegExp('(' + k + ')').test(fmt)) {
  18. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  19. }
  20. }
  21. return fmt
  22. },
  23. // 判断期间范围是否在12:00-12:30,20:00-20:30
  24. duringTime(){
  25. let nowDate = new Date();
  26. let hour = nowDate.getHours();
  27. let minutes = nowDate.getMinutes();
  28. let seconds = nowDate.getSeconds();
  29. if((hour == 12 && minutes < 30) || (hour == 20 && minutes < 30)){
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. },
  35. // 时间格式化
  36. formatDateTime(datestr, fmt = 'yyyy-MM-dd hh:mm:ss') {
  37. let date = new Date(datestr)
  38. let o = {
  39. 'M+': date.getMonth() + 1, // 月份
  40. 'd+': date.getDate(), // 日
  41. 'h+': date.getHours(), // 小时
  42. 'm+': date.getMinutes(), // 分
  43. 's+': date.getSeconds(), // 秒
  44. 'S': date.getMilliseconds() // 毫秒
  45. }
  46. if (/(y+)/.test(fmt)) {
  47. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  48. }
  49. for (var k in o) {
  50. if (new RegExp('(' + k + ')').test(fmt)) {
  51. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  52. }
  53. }
  54. return fmt
  55. },
  56. // 时间戳转时间倒计时
  57. formatTime(datestr){
  58. let day = Math.floor(datestr/1000/60/60/24);
  59. let hour = Math.floor(datestr/1000/60/60%24);
  60. let min = Math.floor(datestr/1000/60%60);
  61. let sec = Math.floor(datestr/1000%60);
  62. if (hour < 10) {
  63. hour = "0" + hour;
  64. }
  65. if (min < 10) {
  66. min = "0" + min;
  67. }
  68. if (sec < 10) {
  69. sec = "0" + sec;
  70. }
  71. let str = '';
  72. if(day > 0){
  73. str = day+'天'+hour + ":" + min + ":" + sec;
  74. }else{
  75. str = hour + ":" + min + ":" + sec;
  76. }
  77. return str;
  78. },
  79. // 计算工作年限
  80. getAge(datestr){
  81. if(datestr){
  82. let nowDate = new Date().toLocaleDateString();
  83. let jobDate = new Date(datestr * 1000).toLocaleDateString();
  84. let nowYear = Number(nowDate.split('/')[0]);
  85. let nowMonth = Number(nowDate.split('/')[1]);
  86. let nowDay = Number(nowDate.split('/')[2]);
  87. let jobYear = Number(jobDate.split('/')[0]);
  88. let jobMonth = Number(jobDate.split('/')[1]);
  89. let jobDay = Number(jobDate.split('/')[2]);
  90. let jobAge = nowYear - jobYear - 1;
  91. if(jobMonth < nowMonth || (jobMonth == nowMonth && jobDay <= nowDay)){
  92. jobAge++;
  93. }
  94. return jobAge;
  95. }else{
  96. return '-';
  97. }
  98. },
  99. /*
  100. * 根据Value格式化为带有换行、空格格式的HTML代码
  101. * @param strValue {String} 需要转换的值
  102. * @return {String}转换后的HTML代码
  103. * @example
  104. * getFormatCode("测\r\n\s试") => “测<br/> 试”
  105. */
  106. getFormatCode(strValue) {
  107. return strValue.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, '&nbsp;');
  108. },
  109. // 格式化数字 ‘xx.00’
  110. formatNum(num, len = 2) {
  111. if (isNaN(num) || !num) {
  112. return '0.00';
  113. } else {
  114. return parseFloat(num).toFixed(len)
  115. }
  116. },
  117. // 格式化手机号
  118. formatPhone(cellValue) {
  119. if (Number(cellValue) && String(cellValue).length === 11) {
  120. var mobile = String(cellValue)
  121. var reg = /^(\d{3})\d{4}(\d{4})$/
  122. return mobile.replace(reg, '$1****$2')
  123. } else {
  124. return cellValue
  125. }
  126. },
  127. // 保存到localstorage
  128. setStorage(key, value) {
  129. if (value.expire) {
  130. value.expire = new Date().getTime() + value.expire * 1000;
  131. }
  132. uni.setStorage({
  133. key: key,
  134. data: JSON.stringify(value),
  135. success: function () {}
  136. });
  137. },
  138. // 获取localStorage里面到name值
  139. getStorage(name) {
  140. try {
  141. const value = JSON.parse(uni.getStorageSync(name));
  142. let nowDate = new Date();
  143. let expireDate = new Date(value.expire);
  144. if (value) {
  145. if (value.expire) {
  146. if (nowDate < expireDate) {
  147. return value.token
  148. } else {
  149. return null;
  150. }
  151. } else {
  152. return value
  153. }
  154. } else {
  155. return null;
  156. }
  157. } catch (e) {
  158. return null;
  159. }
  160. },
  161. //随机key
  162. getRandom(len) {
  163. len = len || 32;
  164. var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  165. var maxPos = $chars.length;
  166. var pwd = '';
  167. for (var i = 0; i < len; i++) {
  168. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  169. }
  170. return pwd;
  171. },
  172. // 将返回的时间戳与当前时间戳进行比较,转换成几秒前、几分钟前、几小时前、几天前的形式
  173. getDateDiff(dateTime) {
  174. var publishTime = dateTime, //秒
  175. d_seconds,
  176. d_minutes,
  177. d_hours,
  178. d_days,
  179. timeNow = parseInt(new Date().getTime() / 1000), //秒
  180. d,
  181. date = new Date(publishTime * 1000),
  182. Y = date.getFullYear(),
  183. M = date.getMonth() + 1,
  184. D = date.getDate(),
  185. H = date.getHours(),
  186. m = date.getMinutes(),
  187. s = date.getSeconds();
  188. //小于10的在前面补0
  189. if (M < 10) {
  190. M = '0' + M;
  191. }
  192. if (D < 10) {
  193. D = '0' + D;
  194. }
  195. if (H < 10) {
  196. H = '0' + H;
  197. }
  198. if (m < 10) {
  199. m = '0' + m;
  200. }
  201. if (s < 10) {
  202. s = '0' + s;
  203. }
  204. d = timeNow - publishTime;
  205. d_days = parseInt(d / 86400);
  206. d_hours = parseInt(d / 3600);
  207. d_minutes = parseInt(d / 60);
  208. d_seconds = parseInt(d);
  209. if (d_days > 0 && d_days < 3) {
  210. return d_days + '天前';
  211. } else if (d_days <= 0 && d_hours > 0) {
  212. return d_hours + '小时前';
  213. } else if (d_hours <= 0 && d_minutes > 0) {
  214. return d_minutes + '分钟前';
  215. } else if (d_seconds < 60) {
  216. if (d_seconds <= 0) {
  217. return '刚刚发表';
  218. } else {
  219. return d_seconds + '秒前';
  220. }
  221. } else if (d_days >= 3 && d_days < 30) {
  222. return M + '-' + D + ' ' + H + ':' + m;
  223. } else if (d_days >= 30) {
  224. return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
  225. }
  226. },
  227. // 去登录
  228. toLogin(){
  229. uni.showModal({
  230. title: '提示',
  231. content: '登录才可以领取奖励,是否去登录',
  232. success: function (res) {
  233. if (res.confirm) {
  234. uni.navigateTo({
  235. url: "/pages/login/login"
  236. })
  237. } else if (res.cancel) {
  238. }
  239. }
  240. })
  241. },
  242. //判断微信浏览器
  243. iswx() {
  244. let isWeixin = false;
  245. // #ifdef H5
  246. var ua = navigator.userAgent.toLowerCase();
  247. isWeixin = ua.indexOf('micromessenger') != -1;
  248. // #endif
  249. return isWeixin;
  250. },
  251. // 微信登录跳转
  252. toWechatlogin(params) {
  253. this.setStorage('back_url', params);
  254. if (this.iswx()) {
  255. let url = encodeURI('https://www.glt365.com/weapp/bind');
  256. window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3d3fef277795c663&redirect_uri='+url+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
  257. } else {
  258. uni.navigateTo({
  259. url: '/pages/login/login'
  260. })
  261. }
  262. },
  263. // 跳转页面 type: switchTab
  264. toUrl(url, type){
  265. var reg = /(http|https):\/\/([\w.]+\/?)\S*/ig
  266. if(url.match(reg)){
  267. uni.navigateTo({
  268. url: '/pages/user/webView?url='+url
  269. })
  270. } else {
  271. if(type == 'switchTab'){
  272. uni.switchTab({
  273. url: url
  274. })
  275. }else{
  276. uni.navigateTo({
  277. url: url
  278. })
  279. }
  280. }
  281. },
  282. //
  283. formatLocation(res) {
  284. var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
  285. var REGION_PROVINCE = [];
  286. var addressBean = {
  287. REGION_PROVINCE: null,
  288. REGION_COUNTRY: null,
  289. REGION_CITY: null,
  290. ADDRESS: null
  291. };
  292. function regexAddressBean(address, addressBean) {
  293. regex = /^(.*?[市]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
  294. var addxress = regex.exec(address);
  295. addressBean.REGION_CITY = addxress[1];
  296. addressBean.REGION_COUNTRY = addxress[2];
  297. addressBean.ADDRESS = addxress[3] + "(" + res.name + ")";
  298. // console.log(addxress);
  299. }
  300. if (!(REGION_PROVINCE = regex.exec(res.address))) {
  301. regex = /^(.*?(省|自治区))(.*?)$/;
  302. REGION_PROVINCE = regex.exec(res.address);
  303. addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
  304. regexAddressBean(REGION_PROVINCE[3], addressBean);
  305. } else {
  306. addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
  307. regexAddressBean(res.address, addressBean);
  308. }
  309. return addressBean
  310. },
  311. // 计算经纬度距离
  312. distance(location1, location2) {
  313. let lat1 = location1.lat;
  314. let lng1= location1.lon;
  315. let lat2 = location2.lat;
  316. let lng2= location2.lon;
  317. var radLat1 = lat1 * Math.PI / 180.0;
  318. var radLat2 = lat2 * Math.PI / 180.0;
  319. var a = radLat1 - radLat2;
  320. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  321. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  322. s = s * 6378.137;
  323. s = Math.round(s * 10000) / 10000;
  324. return this.formatNum(s) // 单位千米
  325. }
  326. }