util.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import dayjs from "dayjs";
  2. /*
  3. * @params key 名字
  4. * @params value 值
  5. * @params path 路径
  6. * @params expireDays 存取的时间
  7. */
  8. export function setCookie(key, value, path, expireDays) {
  9. if (!key) return;
  10. let extDate = new Date();
  11. path = "path=" + (path || "") + "/;";
  12. extDate.setDate(extDate.getDate() + (expireDays || 0));
  13. expireDays = extDate.toGMTString();
  14. document.cookie = key.trim() + "=" + value + ";" + path + expireDays;
  15. }
  16. export function delCookie(key) {
  17. document.cookie = key + "=; path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  18. }
  19. /**
  20. * 获取cookie值
  21. * @param name
  22. * @returns {null}
  23. */
  24. export const getCookie = function (name) {
  25. let arr,
  26. reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  27. if (arr == document.cookie.match(reg)) {
  28. return unescape(arr[2]);
  29. } else {
  30. return null;
  31. }
  32. };
  33. export function setLocalStrage(key, value) {
  34. if (!key || !window.localStorage) {
  35. return;
  36. } else {
  37. window.localStorage.setItem(key.trim(), value);
  38. }
  39. }
  40. export function delLocalSt(key) {
  41. if (!key || !window.localStorage) {
  42. return;
  43. } else {
  44. window.localStorage.removeItem(key);
  45. }
  46. }
  47. export function getLocalStrage(key) {
  48. if (!key || !window.localStorage) {
  49. return;
  50. } else {
  51. window.localStorage.getItem(key);
  52. }
  53. }
  54. export const getRandomKey = () => {
  55. let key = "" + new Date().getTime() + Math.floor(Math.random() * 1000000);
  56. return key;
  57. };
  58. export function browser() {
  59. var u = navigator.userAgent;
  60. // app = navigator.appVersion;
  61. return {
  62. trident: u.indexOf("Trident") > -1, //IE内核
  63. presto: u.indexOf("Presto") > -1, //opera内核
  64. webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
  65. gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核
  66. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  67. ios: !!u.match(/Mac OS X/), //ios终端
  68. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  69. android: u.indexOf("DAYAAPPA") > -1 || u.indexOf("Adr") > -1, //android终端
  70. iPhone: u.indexOf("DAYAAPPI") > -1, //是否为iPhone或者QQHD浏览器
  71. isApp: u.indexOf("DAYAAPPI") > -1 || u.indexOf("DAYAAPPA") > -1 || u.indexOf("Adr") > -1,
  72. iPad: u.indexOf("iPad") > -1, //是否iPad
  73. webApp: u.indexOf("Safari") == -1, //是否web应该程序,没有头部与底部
  74. weixin: u.indexOf("MicroMessenger") > -1, //是否微信 (2015-01-22新增)
  75. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  76. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i),
  77. };
  78. }
  79. // 获取时分秒
  80. export function utilDateInfo(date) {
  81. if (typeof date == "string") {
  82. date = date.replace(/-/gi, "/");
  83. }
  84. let d = new Date(date);
  85. let hour = d.getHours() >= 10 ? d.getHours() : "0" + d.getHours();
  86. let minute = d.getMinutes() >= 10 ? d.getMinutes() : "0" + d.getMinutes();
  87. return hour + ":" + minute + ":00";
  88. }
  89. // 获取年月日
  90. export function utilDate(date, type) {
  91. if (typeof date == "string") {
  92. date = date.replace(/-/gi, "/");
  93. }
  94. let d = new Date(date);
  95. let year = d.getFullYear() >= 10 ? d.getFullYear() : "0" + d.getFullYear();
  96. let month = d.getMonth() + 1 >= 10 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1);
  97. let day = d.getDate() >= 10 ? d.getDate() : "0" + d.getDate();
  98. if (type == "ios") {
  99. return year + "/" + month + "/" + day;
  100. }
  101. return year + "-" + month + "-" + day;
  102. }
  103. /**
  104. * 小于10的数变成 (0x)
  105. * @param {数值} num
  106. */
  107. export function getSTD(num) {
  108. return Number(num) >= 10 ? num : "0" + num;
  109. }
  110. /**
  111. * 获取格式化的年月日
  112. * @param {日期} time
  113. * @param {不是IOS里使用(多用于接口参数)} noIos
  114. */
  115. export function getYMD(time, noIos) {
  116. let tempDate = time || new Date();
  117. if (typeof tempDate == "string") {
  118. tempDate = new Date(time.replace(/-/gi, "/"));
  119. }
  120. let t = noIos ? "-" : "/";
  121. let month = getSTD(tempDate.getMonth() + 1);
  122. let day = getSTD(tempDate.getDate());
  123. return tempDate.getFullYear() + t + month + t + day;
  124. }
  125. /**
  126. *
  127. * @param {周几的索引值} index
  128. */
  129. export function getWeekString(index) {
  130. let weekText = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  131. return weekText[index];
  132. }
  133. /**
  134. * 手机号验证
  135. * @param {手机号} phoneNumber
  136. */
  137. export function checkPhone(phoneNumber) {
  138. let result = true;
  139. if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phoneNumber)) {
  140. result = false;
  141. }
  142. return result;
  143. }
  144. // 学生地址
  145. export function validStudentUrl() {
  146. let url = window.location.hostname;
  147. let returnUrl = "";
  148. if (/test/.test(url)) {
  149. // test环境
  150. returnUrl = "https://test.gym.lexiaoya.cn/mdaya";
  151. } else if (/dev/.test(url)) {
  152. // dev 环境
  153. returnUrl = "https://dev.gym.lexiaoya.cn/mdaya";
  154. } else {
  155. //线上
  156. returnUrl = "https://gym.lexiaoya.cn/mdaya";
  157. }
  158. return returnUrl;
  159. }
  160. // 老师地址
  161. export function validTeacherUrl() {
  162. let url = window.location.hostname;
  163. let returnUrl = "";
  164. if (/test/.test(url)) {
  165. // test环境
  166. returnUrl = "https://test.gym.lexiaoya.cn/mteacher";
  167. } else if (/dev/.test(url)) {
  168. // dev 环境
  169. returnUrl = "https://dev.gym.lexiaoya.cn/mteacher";
  170. } else {
  171. //线上
  172. returnUrl = "https://gym.lexiaoya.cn/mteacher";
  173. }
  174. return returnUrl;
  175. }
  176. export function vaildMusicScoreUrl() {
  177. const url = window.location.hostname
  178. let returnUrl = ""
  179. if (/dev/.test(url) || /192.168/.test(url)) {
  180. returnUrl = "https://test.gym.lexiaoya.cn"
  181. } else if (/test/.test(url)) {
  182. // test 环境
  183. returnUrl = "https://test.gym.lexiaoya.cn"
  184. } else {
  185. returnUrl = "https://gym.lexiaoya.cn"
  186. }
  187. return returnUrl
  188. }
  189. /**
  190. * @desc 函数防抖
  191. * @param func 目标函数
  192. * @param wait 延迟执行毫秒数
  193. */
  194. export function debounce(func, wait) {
  195. let timeout = null;
  196. return function () {
  197. let context = this;
  198. let args = arguments;
  199. if (timeout) clearTimeout(timeout);
  200. timeout = setTimeout(() => {
  201. func.apply(context, args);
  202. }, wait);
  203. };
  204. }
  205. /**
  206. * @desc 函数节流
  207. * @param func 函数
  208. * @param wait 延迟执行毫秒数
  209. */
  210. export function throttle(func, wait) {
  211. let timeout = null;
  212. return function () {
  213. let context = this;
  214. let args = arguments;
  215. if (!timeout) {
  216. timeout = setTimeout(() => {
  217. timeout = null;
  218. func.apply(context, args);
  219. }, wait);
  220. }
  221. };
  222. }
  223. /**
  224. * 转换金额为保留2位小数
  225. * @param x
  226. * @returns 强制保留2位小数的金额
  227. */
  228. export function changeTwoDecimal(amt) {
  229. let f_x = parseFloat(amt);
  230. if (isNaN(f_x)) {
  231. return "0.00";
  232. }
  233. f_x = Math.round(f_x * 100) / 100;
  234. let s_x = f_x.toString();
  235. let pos_decimal = s_x.indexOf(".");
  236. if (pos_decimal < 0) {
  237. pos_decimal = s_x.length;
  238. s_x += ".";
  239. }
  240. while (s_x.length <= pos_decimal + 2) {
  241. s_x += "0";
  242. }
  243. return s_x;
  244. }
  245. export function addTimerFormMinute(classDate, startTime, time) {
  246. let endTime = dayjs(classDate + " " + startTime).add(time, "minute");
  247. if (!dayjs(classDate + " " + "23:59:59").isBefore(endTime)) {
  248. return endTime.format("HH:mm:ss");
  249. } else {
  250. return "";
  251. }
  252. }
  253. export function diffTimerFormMinute(classDate, startTime, endTime) {
  254. return Math.abs(dayjs(classDate + " " + startTime).diff(classDate + " " + endTime, "Minute"));
  255. }
  256. export function timerFormMinute(classDate, startTime, time) {
  257. let endTime = dayjs(classDate + " " + startTime).add(time, "minute");
  258. if (!dayjs(classDate + " " + "21:00").isBefore(endTime)) {
  259. return endTime.format("HH:mm");
  260. } else {
  261. return "";
  262. }
  263. }