index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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: 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. switch( dateMap[date] ){
  204. case 1:
  205. item.todoText = "出";
  206. item.dotColor = "red"
  207. break;
  208. case 2:
  209. item.todoText = "临";
  210. item.dotColor = "green"
  211. break;
  212. default:
  213. item.todoText = "临&出";
  214. }
  215. dates.push( item );
  216. }
  217. calendar.setTodos({
  218. showLabelAlways: true,
  219. dates
  220. })
  221. },
  222. whenChangeMonth(e) {
  223. console.log('whenChangeMonth', e.detail)
  224. let {year, month} = e.detail.next;
  225. let fromDate = object2Date({year, month:month, date:1})
  226. let toDate = object2Date({year, month:month, date:31})
  227. this.setData({fromDate, toDate})
  228. this.loadData()
  229. },
  230. whenChangeWeek(e) {
  231. console.log('whenChangeWeek', e.detail)
  232. },
  233. takeoverTap(e) {
  234. console.log('takeoverTap', e.detail)
  235. },
  236. afterCalendarRender(e) {
  237. console.log('afterCalendarRender', e)
  238. // 获取日历组件上的 calendar 对象
  239. // const calendar = this.selectComponent('#calendar').calendar
  240. // console.log('afterCalendarRender -> calendar', calendar)
  241. },
  242. onSwipe(e) {
  243. console.log('onSwipe', e)
  244. },
  245. showToast(msg) {
  246. if (!msg || typeof msg !== 'string') return
  247. wx.showToast({
  248. title: msg,
  249. icon: 'none',
  250. duration: 1500
  251. })
  252. },
  253. generateRandomDate(type) {
  254. let random = ~~(Math.random() * 10)
  255. switch (type) {
  256. case 'year':
  257. random = 201 * 10 + ~~(Math.random() * 10)
  258. break
  259. case 'month':
  260. random = (~~(Math.random() * 10) % 9) + 1
  261. break
  262. case 'date':
  263. random = (~~(Math.random() * 100) % 27) + 1
  264. break
  265. default:
  266. break
  267. }
  268. return random
  269. },
  270. handleAction(e) {
  271. const { action, disable } = e.currentTarget.dataset
  272. if (disable) {
  273. this.showToast('抱歉,还不支持~😂')
  274. }
  275. this.setData({
  276. rst: []
  277. })
  278. const calendar = this.selectComponent('#calendar').calendar
  279. const { year, month } = calendar.getCurrentYM()
  280. switch (action) {
  281. case 'config':
  282. calendar
  283. .setCalendarConfig({
  284. showLunar: false,
  285. theme: 'elegant',
  286. multi: true
  287. })
  288. .then(conf => {
  289. console.log('设置成功:', conf)
  290. })
  291. break
  292. case 'getConfig':
  293. const config = calendar.getCalendarConfig()
  294. this.showToast('请在控制台查看结果')
  295. console.log('自定义配置: ', config)
  296. break
  297. case 'jump': {
  298. const year = this.generateRandomDate('year')
  299. const month = this.generateRandomDate('month')
  300. const date = this.generateRandomDate('date')
  301. const config = calendar.getCalendarConfig()
  302. if (config.weekMode) {
  303. calendar['weekModeJump']({ year, month, date })
  304. } else {
  305. calendar[action]({ year, month, date })
  306. }
  307. break
  308. }
  309. case 'getSelectedDates': {
  310. const selected = calendar[action]()
  311. if (!selected || !selected.length)
  312. return this.showToast('当前未选择任何日期')
  313. this.showToast('请在控制台查看结果')
  314. console.log('get selected dates: ', selected)
  315. const rst = selected.map(item => JSON.stringify(item))
  316. this.setData({
  317. rst
  318. })
  319. break
  320. }
  321. case 'cancelSelectedDates':
  322. const selected = calendar.getSelectedDates()
  323. calendar[action](selected)
  324. break
  325. case 'setTodos': {
  326. const dates = [
  327. {
  328. year,
  329. month,
  330. date: this.generateRandomDate('date'),
  331. todoText: Math.random() * 10 > 5 ? '领奖日' : ''
  332. }
  333. ]
  334. calendar[action]({
  335. showLabelAlways: true,
  336. dates
  337. })
  338. console.log('set todo: ', dates)
  339. break
  340. }
  341. case 'deleteTodos': {
  342. const todos = [...calendar.getTodos()]
  343. if (todos.length) {
  344. calendar[action]([todos[0]]).then(() => {
  345. const _todos = [...calendar.getTodos()]
  346. setTimeout(() => {
  347. const rst = _todos.map(item => JSON.stringify(item))
  348. this.setData(
  349. {
  350. rst
  351. },
  352. () => {
  353. console.log('delete todo: ', todos[0])
  354. }
  355. )
  356. })
  357. })
  358. } else {
  359. this.showToast('没有待办事项')
  360. }
  361. break
  362. }
  363. case 'clearTodos':
  364. const todos = [...calendar.getTodos()]
  365. if (!todos || !todos.length) {
  366. return this.showToast('没有待办事项')
  367. }
  368. calendar[action]()
  369. break
  370. case 'getTodos': {
  371. const selected = calendar[action]()
  372. if (!selected || !selected.length)
  373. return this.showToast('未设置待办事项')
  374. const rst = selected.map(item => JSON.stringify(item))
  375. rst.map(item => JSON.stringify(item))
  376. console.log("rst", rst)
  377. this.setData({
  378. rst
  379. })
  380. break
  381. }
  382. case 'disableDates':
  383. calendar[action]([
  384. {
  385. year,
  386. month,
  387. date: this.generateRandomDate('date')
  388. }
  389. ])
  390. break
  391. case 'enableArea': {
  392. let sDate = this.generateRandomDate('date')
  393. let eDate = this.generateRandomDate('date')
  394. if (sDate > eDate) {
  395. ;[eDate, sDate] = [sDate, eDate]
  396. }
  397. const area = [`${year}-${month}-${sDate}`, `${year}-${month}-${eDate}`]
  398. calendar[action](area)
  399. this.setData({
  400. rstStr: JSON.stringify(area)
  401. })
  402. break
  403. }
  404. case 'enableDates':
  405. const dates = [
  406. `${year}-${month}-${this.generateRandomDate('date')}`,
  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. ]
  412. calendar[action](dates)
  413. this.setData({
  414. rstStr: JSON.stringify(dates)
  415. })
  416. break
  417. case 'switchView':
  418. if (!this.week) {
  419. calendar[action]('week').then(calendarData => {
  420. console.log('switch success!', calendarData)
  421. })
  422. this.week = true
  423. } else {
  424. calendar[action]().then(calendarData => {
  425. console.log('switch success!', calendarData)
  426. })
  427. this.week = false
  428. }
  429. break
  430. case 'setSelectedDates':
  431. const toSet = [
  432. {
  433. year,
  434. month,
  435. date: this.generateRandomDate('date')
  436. },
  437. {
  438. year,
  439. month,
  440. date: this.generateRandomDate('date')
  441. }
  442. ]
  443. calendar[action](toSet)
  444. break
  445. case 'getCalendarDates':
  446. this.showToast('请在控制台查看结果')
  447. console.log(
  448. calendar.getCalendarDates({
  449. lunar: true
  450. })
  451. )
  452. break
  453. default:
  454. break
  455. }
  456. }
  457. }
  458. Page(conf)