index.js 11 KB

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