index.js 11 KB

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