util.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { get } from "./db.js";
  2. // 检验手机号格式
  3. export const checkPhone = (mobile) => {
  4. // return /^1[345678]\d{9}$/.test(mobile);
  5. if (mobile.length == 0) {
  6. return false;
  7. }
  8. if (mobile.length != 11) {
  9. return false;
  10. }
  11. // var myreg = /^0?(13[0-9]|15[0-9]|17[013678]|18[0-9]|14[57]|19[0-9]|18[0-9])[0-9]{8}$/;
  12. var myreg = /^1(3|4|5|6|7|8|9)\d{9}$/;
  13. if (!myreg.test(mobile)) {
  14. return false;
  15. }
  16. return true;
  17. };
  18. // 密码验证格式
  19. export const checkPwd = (pwd) => {
  20. if (pwd.length == 0) {
  21. return false;
  22. }
  23. var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
  24. var re = new RegExp(reg);
  25. if (re.test(pwd)) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. };
  31. //不足位数前面补0
  32. export const PrefixInteger = (num, length) => {
  33. return (Array(length).join("0") + num).slice(-length);
  34. };
  35. //深拷贝
  36. export const deepCopy = (newobj, obj) => {
  37. if (typeof obj != "object") {
  38. return obj;
  39. }
  40. for (var attr in obj) {
  41. var a = {};
  42. if (newobj[attr]) {
  43. a = newobj[attr];
  44. }
  45. newobj[attr] = deepCopy(a, obj[attr]);
  46. }
  47. return newobj;
  48. };
  49. //轻提示
  50. export const toast = (msg = "", callback = function () {}) => {
  51. uni.showToast({
  52. title: msg,
  53. icon: "none",
  54. duration: 1000,
  55. success() {
  56. setTimeout(function () {
  57. callback();
  58. }, 1500);
  59. },
  60. });
  61. };
  62. /* 弹窗 */
  63. export const modal = (title = "", content, callback = function () {}) => {
  64. uni.showModal({
  65. title: title,
  66. content: content,
  67. confirmColor: "#376cbb",
  68. success: function (res) {
  69. if (res.confirm) {
  70. callback();
  71. } else if (res.cancel) {
  72. }
  73. },
  74. });
  75. };
  76. /* 弹窗 */
  77. export const modalShow = (title = "", content, confirm, callback = function () {}) => {
  78. uni.showModal({
  79. title: title,
  80. content: content,
  81. showCancel: false,
  82. confirmText: confirm == "" ? "确定" : "知道了",
  83. confirmColor: "#376cbb",
  84. success: function (res) {
  85. if (res.confirm) {
  86. callback();
  87. } else if (res.cancel) {
  88. }
  89. },
  90. });
  91. };
  92. /* 处理时间格式 */
  93. export const dateFormatter = (str, bol = false, bol2 = false) => {
  94. //默认返回yyyy-MM-dd HH-mm-ss
  95. var dateStr = String(str);
  96. if (!(dateStr.indexOf("-") > -1 && dateStr.indexOf("T") > -1)) {
  97. dateStr = dateStr.replace(/\-/g, "/");
  98. }
  99. var d = new Date(dateStr);
  100. var year = d.getFullYear();
  101. var month = d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
  102. var day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
  103. var hour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
  104. var minute = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
  105. var second = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
  106. if (bol) {
  107. if (bol2) {
  108. return `${month}月${day}日 ${hour}时${minute}分${second}秒`;
  109. }
  110. return [year, month, day].join("-") + " " + [hour, minute, second].join(":");
  111. } else {
  112. if (bol2) {
  113. return `${month}月${day}日`;
  114. }
  115. return [year, month, day].join("-");
  116. }
  117. };
  118. //加载显示
  119. export const loading = (msg = "加载中") => {
  120. uni.showToast({
  121. title: msg,
  122. icon: "loading",
  123. });
  124. };
  125. //加载隐藏
  126. export const loadHide = () => {
  127. uni.hideToast();
  128. };
  129. /**
  130. * 函数防抖 (只执行最后一次点击)
  131. * @param fn
  132. * @param delay
  133. * @returns {Function}
  134. * @constructor
  135. */
  136. export const Debounce = (fn, t = 300) => {
  137. let timer;
  138. return function () {
  139. let args = arguments;
  140. if (timer) {
  141. clearTimeout(timer);
  142. }
  143. timer = setTimeout(() => {
  144. timer = null;
  145. fn.apply(this, args);
  146. }, t);
  147. };
  148. };
  149. /**
  150. * 函数节流
  151. * @param fn
  152. * @param interval
  153. * @returns {Function}
  154. * @constructor
  155. */
  156. export const Throttle = (fn, t = 500) => {
  157. let last;
  158. let timer;
  159. return function () {
  160. let args = arguments;
  161. let now = +new Date();
  162. if (last && now - last < t) {
  163. clearTimeout(timer);
  164. timer = setTimeout(() => {
  165. last = now;
  166. fn.apply(this, args);
  167. }, t);
  168. } else {
  169. last = now;
  170. fn.apply(this, args);
  171. }
  172. };
  173. };
  174. // 上传图片封装
  175. export const upload = {
  176. /* 单张上传 */
  177. Single: function (Funurl, fn, isCamera) {
  178. // 获取用户token和用户信息
  179. let token = get("access_token");
  180. let authHeader = token || "";
  181. uni.chooseImage({
  182. count: 1, ///最多可以选择一张图片
  183. sizeType: ["original", "compressed"], //原图或压缩图
  184. sourceType: isCamera ? ["album"] : ["album", "camera"], //图片来源
  185. success: function (res) {
  186. const tempFilePaths = res.tempFilePaths; //相当于src路径
  187. uni.showToast({
  188. title: "正在上传...",
  189. icon: "loading",
  190. mask: true,
  191. duration: 10000,
  192. });
  193. uni.uploadFile({
  194. header: {
  195. "Content-Type": "multipart/form-data",
  196. Authorization: authHeader,
  197. },
  198. name: "file",
  199. url: Funurl,
  200. filePath: tempFilePaths[0],
  201. success(res) {
  202. fn(res);
  203. uni.hideToast();
  204. uni.showToast({
  205. title: "上传成功",
  206. icon: "none",
  207. mark: true,
  208. });
  209. },
  210. fail(err) {
  211. uni.hideToast();
  212. uni.showModal({
  213. title: "错误提示",
  214. content: "上传图片失败" + err,
  215. showCancel: false,
  216. success: function (res) {},
  217. });
  218. },
  219. });
  220. },
  221. });
  222. },
  223. //上传多张图片
  224. Much: function (Funurl, fn, count) {
  225. // 获取用户token和用户信息
  226. let token = get("access_token");
  227. let authHeader = token || "";
  228. var list = new Array();
  229. uni.chooseImage({
  230. count: count, //最多可以选择3张图
  231. sizeType: ["original", "compressed"],
  232. sourceType: ["album", "camera"],
  233. success: function (res) {
  234. const tempFilePaths = res.tempFilePaths;
  235. let uploadImgCount = 0;
  236. uni.showToast({
  237. title: "正在上传...",
  238. icon: "loading",
  239. mask: true,
  240. duration: 10000,
  241. });
  242. for (var i = 0; i < tempFilePaths.length; i++) {
  243. uni.uploadFile({
  244. url: Funurl,
  245. name: "file",
  246. filePath: tempFilePaths[i], //第几张图片
  247. header: {
  248. "Content-Type": "multipart/form-data",
  249. Authorization: authHeader,
  250. },
  251. success(res) {
  252. uploadImgCount++;
  253. fn(res);
  254. //如果是最后一张,则隐藏等待中
  255. if (uploadImgCount === tempFilePaths.length) {
  256. uni.hideToast();
  257. // uni.showToast({
  258. // title: '上传图片成功',
  259. // icon: 'none',
  260. // mark: true,
  261. // })
  262. } else {
  263. that.upload(imgs);
  264. }
  265. },
  266. fail(res) {
  267. uni.hideToast();
  268. uni.showModal({
  269. title: "错误提示",
  270. content: "上传图片失败",
  271. showCancel: false,
  272. success: function (res) {},
  273. });
  274. },
  275. });
  276. }
  277. },
  278. });
  279. },
  280. };
  281. // 判断是否大于当前时间
  282. export const isTimeGreaterThanCurrent = (timeToCheck) => {
  283. const time = new Date(timeToCheck);
  284. const currentTime = new Date();
  285. return time.getTime() < currentTime.getTime();
  286. };
  287. // 判断离结束时间 是否大于指定时间
  288. export const isWithinOneHour = (activityTime, endTime) => {
  289. const str = activityTime.replace(/-/g, "/");
  290. const date = new Date(str);
  291. const times = date.getTime();
  292. const num = new Date().getTime();
  293. return times - num <= endTime;
  294. };