index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import Week from "./func/week";
  2. import { Logger, Slide, GetDate, initialTasks } from "./func/utils";
  3. import initCalendar, {
  4. jump,
  5. getCurrentYM,
  6. whenChangeDate,
  7. renderCalendar,
  8. whenMulitSelect,
  9. whenSingleSelect,
  10. whenChooseArea,
  11. getCalendarDates,
  12. } from "./main.js";
  13. import {
  14. getPatientOnlineRecord,
  15. addPatientOnlineRecord,
  16. } from "../../pages/home/request";
  17. import tickleBehavior, {
  18. getTickleContext,
  19. } from "../../core/behavior/tickle.behavior";
  20. const slide = new Slide();
  21. const logger = new Logger();
  22. const getDate = new GetDate();
  23. // 获取上个月
  24. function getPrevMonth(year, month) {
  25. if (month === 1) {
  26. return { year: year - 1, month: 12 };
  27. }
  28. return { year, month: month - 1 };
  29. }
  30. // 获取下个月
  31. function getNextMonth(year, month) {
  32. if (month === 12) {
  33. return { year: year + 1, month: 1 };
  34. }
  35. return { year, month: month + 1 };
  36. }
  37. function pad(num) {
  38. return num < 10 ? "0" + num : "" + num;
  39. }
  40. Component({
  41. options: {
  42. styleIsolation: "apply-shared",
  43. multipleSlots: true, // 在组件定义时的选项中启用多slot支持
  44. },
  45. properties: {
  46. calendarConfig: {
  47. type: Object,
  48. value: {},
  49. },
  50. },
  51. behaviors: [tickleBehavior],
  52. data: {
  53. noClockIn: [],
  54. clockIn: [],
  55. id: "",
  56. handleMap: {
  57. prev_year: "chooseYear",
  58. prev_month: "chooseMonth",
  59. next_month: "chooseMonth",
  60. next_year: "chooseYear",
  61. },
  62. currentMonth: null,
  63. currentYear: null,
  64. firstDate: null,
  65. lastDate: null,
  66. dateArrType: 1,
  67. days: [],
  68. dayData: '',
  69. },
  70. observers: {
  71. "calendar.days": function (newVal) {
  72. // console.log(newVal, "newVal", this.data.calendar);
  73. if (newVal.length > 0) {
  74. this.setData({
  75. currentMonth: newVal[0].month,
  76. currentYear: newVal[0].year,
  77. });
  78. let firstDate = null;
  79. const empytGrids = this.data.calendar.empytGrids || [];
  80. const days = this.data.calendar.days || [];
  81. let prevData = null;
  82. if (empytGrids.length > 0) {
  83. // 有上月补齐格子,第一个就是上月的
  84. prevData = {
  85. year: getPrevMonth(days[0].year, days[0].month).year,
  86. month: getPrevMonth(days[0].year, days[0].month).month,
  87. day: empytGrids[0].day,
  88. };
  89. } else if (days.length > 0) {
  90. // 没有上月补齐格子,第一个就是本月1号
  91. prevData = {
  92. year: days[0].year,
  93. month: days[0].month,
  94. day: days[0].day,
  95. };
  96. }
  97. let lastDate = null;
  98. let nextData = null;
  99. const lastEmptyGrids = this.data.calendar.lastEmptyGrids || [];
  100. if (lastEmptyGrids.length > 0) {
  101. nextData = {
  102. year: getNextMonth(days[0].year, days[0].month).year,
  103. month: getNextMonth(days[0].year, days[0].month).month,
  104. day: lastEmptyGrids[lastEmptyGrids.length - 1].day,
  105. };
  106. } else if (days.length > 0) {
  107. nextData = {
  108. year: days[days.length - 1].year,
  109. month: days[days.length - 1].month,
  110. day: days[days.length - 1].day,
  111. };
  112. }
  113. lastDate =
  114. nextData.year + "-" + pad(nextData.month) + "-" + pad(nextData.day);
  115. firstDate =
  116. prevData.year + "-" + pad(prevData.month) + "-" + pad(prevData.day);
  117. this.setData({
  118. firstDate,
  119. lastDate,
  120. });
  121. this.getCaRecord(this.data.id, firstDate, lastDate);
  122. }
  123. },
  124. },
  125. lifetimes: {
  126. attached: async function () {
  127. this.initComp();
  128. this.setData({ id: wx.getStorageSync("recordId") || "" });
  129. },
  130. detached: function () {
  131. initialTasks.flag = "finished";
  132. initialTasks.tasks.length = 0;
  133. },
  134. },
  135. methods: {
  136. // 补卡
  137. async onPatchCard(e: any) {
  138. try {
  139. console.log(e, '点击补卡');
  140. const cardId = e.currentTarget.dataset.id;
  141. const { firstDate, lastDate } = this.data;
  142. console.log(firstDate, lastDate, "获取日期", cardId);
  143. await addPatientOnlineRecord(cardId)
  144. wx.showToast({
  145. title: "补卡成功",
  146. icon: "success",
  147. duration: 1500,
  148. });
  149. await this.getCaRecord(this.data.id, this.data.firstDate, this.data.lastDate);
  150. } catch (error: any) {
  151. getTickleContext.call(this).showWarnMessage(error.errMsg);
  152. }
  153. },
  154. async getCaRecord(id, startDate, endDate) {
  155. try {
  156. const res = await getPatientOnlineRecord(id, startDate, endDate);
  157. const { currentYear, currentMonth } = this.data;
  158. if (res && res.length > 0) {
  159. // 1. 先筛选出本月的数据
  160. const monthData = (res || []).filter(
  161. (item) =>
  162. item.arrangeYear == currentYear && item.arrangeMon == currentMonth
  163. );
  164. // 2. 遍历本月 days,赋值
  165. (this.data.calendar.days || []).forEach((dayItem) => {
  166. // 只处理本月的格子
  167. if (dayItem.year == currentYear && dayItem.month == currentMonth) {
  168. const match = monthData.find((d) => d.arrangeDay == dayItem.day);
  169. if (match) {
  170. dayItem.clockIn = [...match.clockIn];
  171. dayItem.noClockIn = [...match.noClockIn];
  172. dayItem.type = match.type;
  173. }
  174. }
  175. });
  176. const matchData = monthData.filter((d) => d.arrangeDay ==this.data.dayData );
  177. console.log(matchData, 'matchData');
  178. if (matchData.length > 0) {
  179. this.setData({
  180. noClockIn: matchData[0].noClockIn,
  181. clockIn: matchData[0].clockIn,
  182. });
  183. }
  184. this.setData({
  185. days: this.data.calendar.days,
  186. });
  187. console.log(this.data.dayData, 'dayData');
  188. if (this.data.dateArrType == 1) {
  189. const currentDay = this.data.days.find(
  190. (item) => item.lunar.isToday
  191. );
  192. if (currentDay) {
  193. this.setData({
  194. noClockIn: currentDay.noClockIn,
  195. clockIn: currentDay.clockIn,
  196. });
  197. }
  198. }
  199. }
  200. } catch (error) {
  201. getTickleContext.call(this).showWarnMessage(error.errMsg);
  202. }
  203. },
  204. initComp() {
  205. const calendarConfig = this.setDefaultDisableDate();
  206. this.setConfig(calendarConfig);
  207. },
  208. setDefaultDisableDate() {
  209. const calendarConfig = this.properties.calendarConfig || {};
  210. if (calendarConfig.disableMode && !calendarConfig.disableMode.date) {
  211. calendarConfig.disableMode.date = getDate.toTimeStr(
  212. getDate.todayDate()
  213. );
  214. }
  215. return calendarConfig;
  216. },
  217. setConfig(config) {
  218. if (config.markToday && typeof config.markToday === "string") {
  219. config.highlightToday = true;
  220. }
  221. config.theme = config.theme || "default";
  222. this.weekMode = config.weekMode;
  223. this.setData(
  224. {
  225. calendarConfig: config,
  226. },
  227. () => {
  228. initCalendar(this, config);
  229. }
  230. );
  231. },
  232. chooseDate(e) {
  233. const { type } = e.currentTarget.dataset;
  234. if (!type) return;
  235. const methodName = this.data.handleMap[type];
  236. this[methodName](type);
  237. },
  238. chooseYear(type) {
  239. const { curYear, curMonth } = this.data.calendar;
  240. if (!curYear || !curMonth) return logger.warn("异常:未获取到当前年月");
  241. if (this.weekMode) {
  242. return console.warn("周视图下不支持点击切换年月");
  243. }
  244. let newYear = +curYear;
  245. let newMonth = +curMonth;
  246. if (type === "prev_year") {
  247. newYear -= 1;
  248. } else if (type === "next_year") {
  249. newYear += 1;
  250. }
  251. this.render(curYear, curMonth, newYear, newMonth);
  252. },
  253. chooseMonth(type) {
  254. const { curYear, curMonth } = this.data.calendar;
  255. if (!curYear || !curMonth) return logger.warn("异常:未获取到当前年月");
  256. if (this.weekMode) return console.warn("周视图下不支持点击切换年月");
  257. let newYear = +curYear;
  258. let newMonth = +curMonth;
  259. if (type === "prev_month") {
  260. newMonth = newMonth - 1;
  261. if (newMonth < 1) {
  262. newYear -= 1;
  263. newMonth = 12;
  264. }
  265. } else if (type === "next_month") {
  266. newMonth += 1;
  267. if (newMonth > 12) {
  268. newYear += 1;
  269. newMonth = 1;
  270. }
  271. }
  272. this.render(curYear, curMonth, newYear, newMonth);
  273. },
  274. render(curYear, curMonth, newYear, newMonth) {
  275. whenChangeDate.call(this, {
  276. curYear,
  277. curMonth,
  278. newYear,
  279. newMonth,
  280. });
  281. this.setData({
  282. "calendar.curYear": newYear,
  283. "calendar.curMonth": newMonth,
  284. });
  285. renderCalendar.call(this, newYear, newMonth);
  286. },
  287. /**
  288. * 日期点击事件
  289. * @param {!object} e 事件对象
  290. */
  291. tapDayItem(e) {
  292. this.setData({
  293. dateArrType: 2,
  294. });
  295. const { idx, date = {} } = e.currentTarget.dataset;
  296. const selectedDate = `${date.year} ${date.month}.${date.day}`;
  297. this.triggerEvent("onSelect", {
  298. selectedDate,
  299. });
  300. const { day, disable, clockIn, noClockIn, type } = date;
  301. this.setData({
  302. noClockIn: noClockIn,
  303. clockIn: clockIn,
  304. dayData: day,
  305. });
  306. console.log(
  307. this.data.dayData,
  308. "点击本月的日期",
  309. day,
  310. "clockIn",
  311. clockIn,
  312. "noClockIn",
  313. noClockIn,
  314. "type",
  315. type,
  316. );
  317. if (disable || !day) return;
  318. const config = this.data.calendarConfig || this.config || {};
  319. const { multi, chooseAreaMode } = config;
  320. if (multi) {
  321. whenMulitSelect.call(this, idx);
  322. } else if (chooseAreaMode) {
  323. whenChooseArea.call(this, idx);
  324. } else {
  325. whenSingleSelect.call(this, idx);
  326. }
  327. this.setData({
  328. "calendar.noDefault": false,
  329. });
  330. },
  331. doubleClickToToday() {
  332. if (this.config.multi || this.weekMode) return;
  333. if (this.count === undefined) {
  334. this.count = 1;
  335. } else {
  336. this.count += 1;
  337. }
  338. if (this.lastClick) {
  339. const difference = new Date().getTime() - this.lastClick;
  340. if (difference < 500 && this.count >= 2) {
  341. jump.call(this);
  342. }
  343. this.count = undefined;
  344. this.lastClick = undefined;
  345. } else {
  346. this.lastClick = new Date().getTime();
  347. }
  348. },
  349. /**
  350. * 日历滑动开始
  351. * @param {object} e
  352. */
  353. calendarTouchstart(e) {
  354. const t = e.touches[0];
  355. const startX = t.clientX;
  356. const startY = t.clientY;
  357. this.slideLock = true; // 滑动事件加锁
  358. this.setData({
  359. "gesture.startX": startX,
  360. "gesture.startY": startY,
  361. });
  362. },
  363. /**
  364. * 日历滑动中
  365. * @param {object} e
  366. */
  367. calendarTouchmove(e) {
  368. const { gesture } = this.data;
  369. const { preventSwipe } = this.properties.calendarConfig;
  370. if (!this.slideLock || preventSwipe) return;
  371. if (slide.isLeft(gesture, e.touches[0])) {
  372. this.handleSwipe("left");
  373. this.slideLock = false;
  374. }
  375. if (slide.isRight(gesture, e.touches[0])) {
  376. this.handleSwipe("right");
  377. this.slideLock = false;
  378. }
  379. },
  380. calendarTouchend(e) {
  381. this.setData({
  382. "calendar.leftSwipe": 0,
  383. "calendar.rightSwipe": 0,
  384. });
  385. },
  386. handleSwipe(direction) {
  387. let swipeKey = "calendar.leftSwipe";
  388. let swipeCalendarType = "next_month";
  389. let weekChangeType = "next_week";
  390. if (direction === "right") {
  391. swipeKey = "calendar.rightSwipe";
  392. swipeCalendarType = "prev_month";
  393. weekChangeType = "prev_week";
  394. }
  395. this.setData({
  396. [swipeKey]: 1,
  397. });
  398. this.currentYM = getCurrentYM();
  399. if (this.weekMode) {
  400. this.slideLock = false;
  401. this.currentDates = getCalendarDates();
  402. if (weekChangeType === "prev_week") {
  403. Week(this).calculatePrevWeekDays();
  404. } else if (weekChangeType === "next_week") {
  405. Week(this).calculateNextWeekDays();
  406. }
  407. this.onSwipeCalendar(weekChangeType);
  408. this.onWeekChange(weekChangeType);
  409. return;
  410. }
  411. this.chooseMonth(swipeCalendarType);
  412. this.onSwipeCalendar(swipeCalendarType);
  413. },
  414. onSwipeCalendar(direction) {
  415. this.triggerEvent("onSwipe", {
  416. directionType: direction,
  417. currentYM: this.currentYM,
  418. });
  419. },
  420. onWeekChange(direction) {
  421. this.triggerEvent("whenChangeWeek", {
  422. current: {
  423. currentYM: this.currentYM,
  424. dates: [...this.currentDates],
  425. },
  426. next: {
  427. currentYM: getCurrentYM(),
  428. dates: getCalendarDates(),
  429. },
  430. directionType: direction,
  431. });
  432. this.currentDates = null;
  433. this.currentYM = null;
  434. },
  435. },
  436. });