index.js 12 KB

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