index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. const conf = {
  2. data: {
  3. calendarConfig: {
  4. showLunar: true
  5. // chooseAreaMode: true,
  6. // firstDayOfWeek: 'Mon',
  7. // disableMode: {
  8. // type: 'after',
  9. // date: '2020-03-9'
  10. // },
  11. // defaultDay: '2020-3-6'
  12. // multi: true
  13. },
  14. actionBtn: [
  15. {
  16. text: '跳转指定日期',
  17. action: 'jump',
  18. color: 'olive'
  19. },
  20. {
  21. text: '获取当前已选',
  22. action: 'getSelectedDay',
  23. color: 'red'
  24. },
  25. {
  26. text: '取消选中日期',
  27. action: 'cancelSelectedDates',
  28. color: 'mauve'
  29. },
  30. {
  31. text: '设置待办事项',
  32. action: 'setTodoLabels',
  33. color: 'cyan'
  34. },
  35. {
  36. text: '删除指定代办',
  37. action: 'deleteTodoLabels',
  38. color: 'pink'
  39. },
  40. {
  41. text: '清空待办事项',
  42. action: 'clearTodoLabels',
  43. color: 'red'
  44. },
  45. {
  46. text: '获取所有代办',
  47. action: 'getTodoLabels',
  48. color: 'purple'
  49. },
  50. {
  51. text: '禁选指定日期',
  52. action: 'disableDay',
  53. color: 'olive'
  54. },
  55. {
  56. text: '指定可选区域',
  57. action: 'enableArea',
  58. color: 'pink'
  59. },
  60. {
  61. text: '指定特定可选',
  62. action: 'enableDays',
  63. color: 'red'
  64. },
  65. {
  66. text: '选中指定日期',
  67. action: 'setSelectedDays',
  68. color: 'cyan'
  69. },
  70. {
  71. text: '周月视图切换',
  72. action: 'switchView',
  73. color: 'orange'
  74. },
  75. {
  76. text: '自定义配置',
  77. action: 'config',
  78. color: 'pink'
  79. },
  80. {
  81. text: '获取自定义配置',
  82. action: 'getConfig',
  83. color: 'olive'
  84. },
  85. {
  86. text: '获取日历面板日期',
  87. action: 'getCalendarDates',
  88. color: 'purple'
  89. }
  90. ]
  91. },
  92. afterTapDay(e) {
  93. console.log('afterTapDay', e.detail)
  94. },
  95. whenChangeMonth(e) {
  96. console.log('whenChangeMonth', e.detail)
  97. },
  98. whenChangeWeek(e) {
  99. console.log('whenChangeWeek', e.detail)
  100. },
  101. onTapDay(e) {
  102. console.log('onTapDay', e.detail)
  103. },
  104. afterCalendarRender(e) {
  105. console.log('afterCalendarRender', e)
  106. // this.calendar.switchView('week').then(() => {
  107. // this.calendar.jump(2020, 3, 1).then(date => {}); // 跳转至某日
  108. // });
  109. },
  110. onSwipe(e) {
  111. console.log('onSwipe', e)
  112. },
  113. showToast(msg) {
  114. if (!msg || typeof msg !== 'string') return
  115. wx.showToast({
  116. title: msg,
  117. icon: 'none',
  118. duration: 1500
  119. })
  120. },
  121. generateRandomDate(type) {
  122. let random = ~~(Math.random() * 10)
  123. switch (type) {
  124. case 'year':
  125. random = 201 * 10 + ~~(Math.random() * 10)
  126. break
  127. case 'month':
  128. random = (~~(Math.random() * 10) % 9) + 1
  129. break
  130. case 'date':
  131. random = (~~(Math.random() * 100) % 27) + 1
  132. break
  133. default:
  134. break
  135. }
  136. return random
  137. },
  138. handleAction(e) {
  139. const { action, disable } = e.currentTarget.dataset
  140. if (disable) {
  141. this.showToast('抱歉,还不支持~😂')
  142. }
  143. this.setData({
  144. rst: []
  145. })
  146. const calendar = this.calendar
  147. const { year, month } = calendar.getCurrentYM()
  148. switch (action) {
  149. case 'config':
  150. calendar
  151. .setCalendarConfig({
  152. showLunar: false,
  153. theme: 'elegant',
  154. multi: true
  155. })
  156. .then(conf => {
  157. console.log('设置成功:', conf)
  158. })
  159. break
  160. case 'getConfig':
  161. const config = calendar.getCalendarConfig()
  162. this.showToast('请在控制台查看结果')
  163. console.log('自定义配置: ', config)
  164. break
  165. case 'jump': {
  166. const year = this.generateRandomDate('year')
  167. const month = this.generateRandomDate('month')
  168. const date = this.generateRandomDate('date')
  169. calendar[action](year, month, date)
  170. break
  171. }
  172. case 'getSelectedDay': {
  173. const selected = calendar[action]()
  174. if (!selected || !selected.length)
  175. return this.showToast('当前未选择任何日期')
  176. this.showToast('请在控制台查看结果')
  177. console.log('get selected days: ', selected)
  178. const rst = selected.map(item => JSON.stringify(item))
  179. this.setData({
  180. rst
  181. })
  182. break
  183. }
  184. case 'cancelSelectedDates':
  185. calendar[action]([
  186. {
  187. year: 2020,
  188. month: 3,
  189. day: 3
  190. }
  191. ])
  192. break
  193. case 'setTodoLabels': {
  194. const days = [
  195. {
  196. year,
  197. month,
  198. day: this.generateRandomDate('date'),
  199. todoText: Math.random() * 10 > 5 ? '领奖日' : ''
  200. }
  201. ]
  202. calendar[action]({
  203. showLabelAlways: true,
  204. days
  205. })
  206. console.log('set todo labels: ', days)
  207. break
  208. }
  209. case 'deleteTodoLabels': {
  210. const todos = [...calendar.getTodoLabels()]
  211. if (todos && todos.length) {
  212. todos.length = 1
  213. calendar[action](todos)
  214. const _todos = [...calendar.getTodoLabels()]
  215. setTimeout(() => {
  216. const rst = _todos.map(item => JSON.stringify(item))
  217. this.setData(
  218. {
  219. rst
  220. },
  221. () => {
  222. console.log('set todo labels: ', todos)
  223. }
  224. )
  225. })
  226. } else {
  227. this.showToast('没有待办事项')
  228. }
  229. break
  230. }
  231. case 'clearTodoLabels':
  232. const todos = [...calendar.getTodoLabels()]
  233. if (!todos || !todos.length) {
  234. return this.showToast('没有待办事项')
  235. }
  236. calendar[action]()
  237. break
  238. case 'getTodoLabels': {
  239. const selected = calendar[action]()
  240. if (!selected || !selected.length)
  241. return this.showToast('未设置待办事项')
  242. const rst = selected.map(item => JSON.stringify(item))
  243. rst.map(item => JSON.stringify(item))
  244. this.setData({
  245. rst
  246. })
  247. break
  248. }
  249. case 'disableDay':
  250. calendar[action]([
  251. {
  252. year,
  253. month,
  254. day: this.generateRandomDate('date')
  255. }
  256. ])
  257. break
  258. case 'enableArea': {
  259. let sDate = this.generateRandomDate('date')
  260. let eDate = this.generateRandomDate('date')
  261. if (sDate > eDate) {
  262. ;[eDate, sDate] = [sDate, eDate]
  263. }
  264. const area = [`${year}-${month}-${sDate}`, `${year}-${month}-${eDate}`]
  265. calendar[action](area)
  266. this.setData({
  267. rstStr: JSON.stringify(area)
  268. })
  269. break
  270. }
  271. case 'enableDays':
  272. const days = [
  273. `${year}-${month}-${this.generateRandomDate('date')}`,
  274. `${year}-${month}-${this.generateRandomDate('date')}`,
  275. `${year}-${month}-${this.generateRandomDate('date')}`,
  276. `${year}-${month}-${this.generateRandomDate('date')}`,
  277. `${year}-${month}-${this.generateRandomDate('date')}`
  278. ]
  279. calendar[action](days)
  280. this.setData({
  281. rstStr: JSON.stringify(days)
  282. })
  283. break
  284. case 'switchView':
  285. if (!this.week) {
  286. calendar[action]('week')
  287. this.week = true
  288. } else {
  289. calendar[action]()
  290. this.week = false
  291. }
  292. break
  293. case 'setSelectedDays':
  294. const toSet = [
  295. {
  296. year,
  297. month,
  298. day: this.generateRandomDate('date')
  299. },
  300. {
  301. year,
  302. month,
  303. day: this.generateRandomDate('date')
  304. }
  305. ]
  306. calendar[action](toSet)
  307. break
  308. case 'getCalendarDates':
  309. this.showToast('请在控制台查看结果')
  310. console.log(calendar.getCalendarDates())
  311. break
  312. default:
  313. break
  314. }
  315. }
  316. }
  317. Page(conf)