index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import todo from '../../component/v2/plugins/todo'
  2. import selectable from '../../component/v2/plugins/selectable'
  3. import solarLunar from '../../component/v2/plugins/solarLunar/index'
  4. import timeRange from '../../component/v2/plugins/time-range'
  5. import week from '../../component/v2/plugins/week'
  6. import holidays from '../../component/v2/plugins/holidays/index'
  7. import plugin from '../../component/v2/plugins/index'
  8. import util, {formatDate, object2Date} from '../../utils/util.js'
  9. const app = getApp()
  10. plugin
  11. .use(todo)
  12. .use(solarLunar)
  13. .use(selectable)
  14. .use(week)
  15. .use(timeRange)
  16. .use(holidays)
  17. const conf = {
  18. data: {
  19. outList:[],
  20. visitList:[],
  21. tableData: [],
  22. curDate: '',
  23. fromDate:'',
  24. toDate:'',
  25. calendarConfig: {
  26. theme: 'elegant',
  27. showHolidays: false,
  28. emphasisWeek: true,
  29. chooseAreaMode: false,
  30. autoChoosedWhenJump: true,
  31. highlightToday: true,
  32. defaultDate: formatDate(new Date())
  33. // autoChoosedWhenJump: true
  34. },
  35. actionBtn: [
  36. {
  37. text: '跳转指定日期',
  38. action: 'jump',
  39. color: 'olive'
  40. },
  41. {
  42. text: '获取当前已选',
  43. action: 'getSelectedDates',
  44. color: 'red'
  45. },
  46. {
  47. text: '取消选中日期',
  48. action: 'cancelSelectedDates',
  49. color: 'mauve'
  50. },
  51. {
  52. text: '设置待办事项',
  53. action: 'setTodos',
  54. color: 'cyan'
  55. },
  56. {
  57. text: '删除指定代办',
  58. action: 'deleteTodos',
  59. color: 'pink'
  60. },
  61. {
  62. text: '清空待办事项',
  63. action: 'clearTodos',
  64. color: 'red'
  65. },
  66. {
  67. text: '获取所有代办',
  68. action: 'getTodos',
  69. color: 'purple'
  70. },
  71. {
  72. text: '禁选指定日期',
  73. action: 'disableDates',
  74. color: 'olive'
  75. },
  76. {
  77. text: '指定可选区域',
  78. action: 'enableArea',
  79. color: 'pink'
  80. },
  81. {
  82. text: '指定特定可选',
  83. action: 'enableDates',
  84. color: 'red'
  85. },
  86. {
  87. text: '选中指定日期',
  88. action: 'setSelectedDates',
  89. color: 'cyan'
  90. },
  91. {
  92. text: '周月视图切换',
  93. action: 'switchView',
  94. color: 'orange'
  95. },
  96. {
  97. text: '获取自定义配置',
  98. action: 'getConfig',
  99. color: 'olive'
  100. },
  101. {
  102. text: '获取日历面板日期',
  103. action: 'getCalendarDates',
  104. color: 'purple'
  105. }
  106. ]
  107. },
  108. initData(){
  109. let curDate = formatDate(new Date());
  110. let fromDate = curDate.substr(0,8)+ "01"
  111. let toDate = curDate.substr(0,8)+ "31"
  112. this.setData( {curDate, fromDate, toDate})
  113. },
  114. afterTapDate(e) {
  115. let curDate = object2Date( e.detail );
  116. this.setData({curDate});
  117. this.filterData()
  118. },
  119. onLoad: function(){
  120. this.initData();
  121. app.checkLogin(res=>{
  122. if( !res.userId ){
  123. wx.navigateTo({
  124. url: '/pages/index/index',
  125. })
  126. }else{
  127. this.loadData()
  128. }
  129. })
  130. },
  131. filterData(){
  132. let {curDate, outList, visitList} = this.data;
  133. let allList = outList.map( item=>{
  134. item.isOut= true
  135. return item;
  136. }).concat( visitList );
  137. let tableData = []
  138. for( let i in allList){
  139. if( allList[i].fromDate <= curDate && allList[i].toDate >= curDate){
  140. tableData.push( allList[i] );
  141. }
  142. }
  143. console.log("filterData", curDate, tableData)
  144. this.setData({tableData})
  145. },
  146. gotoDetail( e ){
  147. let {id,out} = e.currentTarget.dataset;
  148. if( out ){
  149. wx.navigateTo({
  150. url: `/pages/detail/out/index?id=${id}`,
  151. })
  152. }else{
  153. wx.navigateTo({
  154. url: `/pages/detail/visit/index?id=${id}`,
  155. })
  156. }
  157. },
  158. loadData(){
  159. let {fromDate, toDate} = this.data;
  160. app.formPost("User.loadData", {fromDate, toDate}).then( res=>{
  161. if( res.code == 200){
  162. let outList = res.data.outList||[];
  163. let visitList = res.data.visitList||[];
  164. this.setData({outList, visitList});
  165. this.filterData();
  166. this.loadMarks();
  167. }
  168. })
  169. },
  170. loadMarks(){
  171. const calendar = this.selectComponent('#calendar').calendar
  172. let {outList, visitList} = this.data;
  173. var dateMap = {};
  174. // 外出
  175. for(let i in outList ){
  176. let item = outList[i];
  177. let {fromDate,toDate} = item;
  178. if( toDate < fromDate) continue;
  179. while( fromDate <= toDate){
  180. dateMap[fromDate] = 1;
  181. fromDate = util.nextDate(fromDate);
  182. }
  183. }
  184. //
  185. for(let i in visitList ){
  186. let item = visitList[i];
  187. let {fromDate,toDate} = item;
  188. if( toDate < fromDate) continue;
  189. let tempDate = fromDate
  190. while( tempDate <= toDate){
  191. let curNum = dateMap[tempDate]||0;
  192. if( curNum == 1 ){
  193. dateMap[tempDate] = 3;
  194. } else if( !curNum ){
  195. dateMap[tempDate] = 2
  196. }
  197. tempDate = util.nextDate( tempDate );
  198. }
  199. }
  200. var dates = [];
  201. for( let date in dateMap ){
  202. let d = new Date(date);
  203. let item = {year: d.getFullYear(), month: d.getMonth()+1, date: d.getDate()};
  204. switch( dateMap[date] ){
  205. case 1:
  206. // item.todoText = "出";
  207. // item.dotColor = "red"
  208. item.todoText = [['出', '#FBBF24']];
  209. break;
  210. case 2:
  211. // item.todoText = "临";
  212. // item.dotColor = "green"
  213. item.todoText = [['临', '#34D399']];
  214. break;
  215. default:
  216. // item.todoText = "临&出";
  217. item.todoText = [['临', '#34D399'], ['出', '#FBBF24']];
  218. }
  219. dates.push( item );
  220. }
  221. calendar.setTodos({
  222. showLabelAlways: true,
  223. dates
  224. })
  225. },
  226. whenChangeMonth(e) {
  227. console.log('whenChangeMonth', e.detail)
  228. let {year, month} = e.detail.next;
  229. let fromDate = object2Date({year, month:month, date:1})
  230. let toDate = object2Date({year, month:month, date:31})
  231. this.setData({fromDate, toDate})
  232. this.loadData()
  233. },
  234. whenChangeWeek(e) {
  235. console.log('whenChangeWeek', e.detail)
  236. },
  237. takeoverTap(e) {
  238. console.log('takeoverTap', e.detail)
  239. },
  240. afterCalendarRender(e) {
  241. console.log('afterCalendarRender', e)
  242. // 获取日历组件上的 calendar 对象
  243. // const calendar = this.selectComponent('#calendar').calendar
  244. // console.log('afterCalendarRender -> calendar', calendar)
  245. },
  246. onSwipe(e) {
  247. console.log('onSwipe', e)
  248. },
  249. showToast(msg) {
  250. if (!msg || typeof msg !== 'string') return
  251. wx.showToast({
  252. title: msg,
  253. icon: 'none',
  254. duration: 1500
  255. })
  256. },
  257. generateRandomDate(type) {
  258. let random = ~~(Math.random() * 10)
  259. switch (type) {
  260. case 'year':
  261. random = 201 * 10 + ~~(Math.random() * 10)
  262. break
  263. case 'month':
  264. random = (~~(Math.random() * 10) % 9) + 1
  265. break
  266. case 'date':
  267. random = (~~(Math.random() * 100) % 27) + 1
  268. break
  269. default:
  270. break
  271. }
  272. return random
  273. },
  274. handleAction(e) {
  275. const { action, disable } = e.currentTarget.dataset
  276. if (disable) {
  277. this.showToast('抱歉,还不支持~😂')
  278. }
  279. this.setData({
  280. rst: []
  281. })
  282. const calendar = this.selectComponent('#calendar').calendar
  283. const { year, month } = calendar.getCurrentYM()
  284. switch (action) {
  285. case 'config':
  286. calendar
  287. .setCalendarConfig({
  288. showLunar: false,
  289. theme: 'elegant',
  290. multi: true
  291. })
  292. .then(conf => {
  293. console.log('设置成功:', conf)
  294. })
  295. break
  296. case 'getConfig':
  297. const config = calendar.getCalendarConfig()
  298. this.showToast('请在控制台查看结果')
  299. console.log('自定义配置: ', config)
  300. break
  301. case 'jump': {
  302. const year = this.generateRandomDate('year')
  303. const month = this.generateRandomDate('month')
  304. const date = this.generateRandomDate('date')
  305. const config = calendar.getCalendarConfig()
  306. if (config.weekMode) {
  307. calendar['weekModeJump']({ year, month, date })
  308. } else {
  309. calendar[action]({ year, month, date })
  310. }
  311. break
  312. }
  313. case 'getSelectedDates': {
  314. const selected = calendar[action]()
  315. if (!selected || !selected.length)
  316. return this.showToast('当前未选择任何日期')
  317. this.showToast('请在控制台查看结果')
  318. console.log('get selected dates: ', selected)
  319. const rst = selected.map(item => JSON.stringify(item))
  320. this.setData({
  321. rst
  322. })
  323. break
  324. }
  325. case 'cancelSelectedDates':
  326. const selected = calendar.getSelectedDates()
  327. calendar[action](selected)
  328. break
  329. case 'setTodos': {
  330. const dates = [
  331. {
  332. year,
  333. month,
  334. date: this.generateRandomDate('date'),
  335. todoText: Math.random() * 10 > 5 ? '领奖日' : ''
  336. }
  337. ]
  338. calendar[action]({
  339. showLabelAlways: true,
  340. dates
  341. })
  342. console.log('set todo: ', dates)
  343. break
  344. }
  345. case 'deleteTodos': {
  346. const todos = [...calendar.getTodos()]
  347. if (todos.length) {
  348. calendar[action]([todos[0]]).then(() => {
  349. const _todos = [...calendar.getTodos()]
  350. setTimeout(() => {
  351. const rst = _todos.map(item => JSON.stringify(item))
  352. this.setData(
  353. {
  354. rst
  355. },
  356. () => {
  357. console.log('delete todo: ', todos[0])
  358. }
  359. )
  360. })
  361. })
  362. } else {
  363. this.showToast('没有待办事项')
  364. }
  365. break
  366. }
  367. case 'clearTodos':
  368. const todos = [...calendar.getTodos()]
  369. if (!todos || !todos.length) {
  370. return this.showToast('没有待办事项')
  371. }
  372. calendar[action]()
  373. break
  374. case 'getTodos': {
  375. const selected = calendar[action]()
  376. if (!selected || !selected.length)
  377. return this.showToast('未设置待办事项')
  378. const rst = selected.map(item => JSON.stringify(item))
  379. rst.map(item => JSON.stringify(item))
  380. console.log("rst", rst)
  381. this.setData({
  382. rst
  383. })
  384. break
  385. }
  386. case 'disableDates':
  387. calendar[action]([
  388. {
  389. year,
  390. month,
  391. date: this.generateRandomDate('date')
  392. }
  393. ])
  394. break
  395. case 'enableArea': {
  396. let sDate = this.generateRandomDate('date')
  397. let eDate = this.generateRandomDate('date')
  398. if (sDate > eDate) {
  399. ;[eDate, sDate] = [sDate, eDate]
  400. }
  401. const area = [`${year}-${month}-${sDate}`, `${year}-${month}-${eDate}`]
  402. calendar[action](area)
  403. this.setData({
  404. rstStr: JSON.stringify(area)
  405. })
  406. break
  407. }
  408. case 'enableDates':
  409. const dates = [
  410. `${year}-${month}-${this.generateRandomDate('date')}`,
  411. `${year}-${month}-${this.generateRandomDate('date')}`,
  412. `${year}-${month}-${this.generateRandomDate('date')}`,
  413. `${year}-${month}-${this.generateRandomDate('date')}`,
  414. `${year}-${month}-${this.generateRandomDate('date')}`
  415. ]
  416. calendar[action](dates)
  417. this.setData({
  418. rstStr: JSON.stringify(dates)
  419. })
  420. break
  421. case 'switchView':
  422. if (!this.week) {
  423. calendar[action]('week').then(calendarData => {
  424. console.log('switch success!', calendarData)
  425. })
  426. this.week = true
  427. } else {
  428. calendar[action]().then(calendarData => {
  429. console.log('switch success!', calendarData)
  430. })
  431. this.week = false
  432. }
  433. break
  434. case 'setSelectedDates':
  435. const toSet = [
  436. {
  437. year,
  438. month,
  439. date: this.generateRandomDate('date')
  440. },
  441. {
  442. year,
  443. month,
  444. date: this.generateRandomDate('date')
  445. }
  446. ]
  447. calendar[action](toSet)
  448. break
  449. case 'getCalendarDates':
  450. this.showToast('请在控制台查看结果')
  451. console.log(
  452. calendar.getCalendarDates({
  453. lunar: true
  454. })
  455. )
  456. break
  457. default:
  458. break
  459. }
  460. }
  461. }
  462. Page(conf)