index.js 12 KB

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