luanqing-date-picker.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <uni-popup type="bottom" ref="popupDialogObj">
  3. <view v-if="!isSimple">
  4. <view class="title_bar">
  5. <text class="cancel_text" :style="'color:'+themeColor" style="min-width: 100rpx;" @click="close">取消</text>
  6. <text class="title_text" v-if="isMultiple">{{title}}{{checkedList && checkedList.length > 0 ? '(已选'+checkedList.length+'天)':'(可选多天)'}}</text>
  7. <text class="title_text" v-else>{{title}}</text>
  8. <text class="cancel_text" :style="'color:'+themeColor" style="min-width: 100rpx;" v-if="isMultiple" @click="comfirmModel">确定</text>
  9. <text class="cancel_text" style="min-width: 100rpx;" v-else></text>
  10. </view>
  11. <view class="uni_flex_row_align_left year_month_bar">
  12. <text class="year_text">{{year}}年</text>
  13. <uni-icons type="back" @click="lastMonth" size="21"></uni-icons>
  14. <text class="title_date">{{month < 10 ? '0'+month:month}}月</text>
  15. <uni-icons v-if="(year < curYear) || (month < curMonth && year === curYear)" type="forward" @click="nextMonth" size="21"></uni-icons>
  16. </view>
  17. <luanqing-calendar
  18. ref="calendarObj"
  19. :mode="isMultiple ? 'multiple':'single'"
  20. :isAbleSelectFutureDate="isAbleSelectFutureDate"
  21. :selected="isMultiple ? checkedList || [] : []"
  22. :insert="true"
  23. :lunar="true"
  24. :themeColor="themeColor"
  25. :clearDate="false"
  26. :showMonth="showMonthOnCenter"
  27. :isShowHeader="false"
  28. @change="checkedEvent"
  29. />
  30. </view>
  31. <view v-else>
  32. <simple-picker-date
  33. ref="simplePickerObj"
  34. @onCancel="onCancel"
  35. @onConfirm="onConfirm"
  36. :defaultValue="defaultDate"
  37. :themeColor="themeColor"
  38. :startDate="startDate"
  39. :title="title"
  40. :endDate="endDate">
  41. </simple-picker-date>
  42. </view>
  43. </uni-popup>
  44. </template>
  45. <script>
  46. import luanqingCalendar from './luanqing-calendar.vue';
  47. import simplePickerDate from './simple-pickerdate.vue';
  48. function getNowDate() {
  49. let date = new Date();
  50. let year = date.getFullYear();
  51. let month = date.getMonth() + 1;
  52. let day = date.getDate();
  53. if (month.toString().length == 1) {
  54. month = "0" + month;
  55. }
  56. if (day.toString().length == 1) {
  57. day = "0" + day;
  58. }
  59. return year + "-" + month + "-" + day;
  60. }
  61. export default {
  62. components:{ luanqingCalendar,simplePickerDate },
  63. // 日期选择完成
  64. emits:['finishSelectDate'],
  65. props: {
  66. // 是否默认选中今日
  67. checkedToady:{
  68. type:Boolean,
  69. default:false
  70. },
  71. // 默认选中的日期列表(可用于传入之前选择的值)
  72. defaultCheckedList:{
  73. type: Array,
  74. default:()=>{
  75. return []
  76. }
  77. },
  78. // 是否简单选择日期(滚动小弹窗单选年月日)
  79. isSimple:{
  80. type: Boolean,
  81. default: false
  82. },
  83. // 是否多选,默认多选(弹出日期选择)
  84. isMultiple:{
  85. type: Boolean,
  86. default: true
  87. },
  88. // 是否可以选择未来日期
  89. isAbleSelectFutureDate:{
  90. type: Boolean,
  91. default: true
  92. },
  93. // 是否在日历中间显示大大的月份数字
  94. showMonthOnCenter:{
  95. type: Boolean,
  96. default: true
  97. },
  98. // 主题风格颜色
  99. themeColor:{
  100. type: String,
  101. default: '#E00300'
  102. },
  103. // 弹窗的标题文字
  104. title:{
  105. type: String,
  106. default: '选择日期'
  107. },
  108. startDate: {
  109. type: String,
  110. default () {
  111. return "1971-01-01"
  112. }
  113. },
  114. endDate: {
  115. type: String,
  116. default () {
  117. return "2099-12-31"
  118. }
  119. },
  120. defaultDate: {
  121. type: String,
  122. default () {
  123. return getNowDate()
  124. }
  125. }
  126. },
  127. data() {
  128. return {
  129. checkedList:[],
  130. year:2022,
  131. month:7,
  132. curYear:2022,
  133. curMonth:6
  134. }
  135. },
  136. created() {
  137. let date = new Date();
  138. this.month = date.getMonth() + 1;
  139. this.year = date.getFullYear();
  140. this.date = date.getDate();
  141. this.curYear = this.year;
  142. this.curMonth = this.month;
  143. if(!this.$props.isMultiple){
  144. if(this.$props.checkedToady){
  145. let dateStr = ''+this.year+'-'+(this.month < 10 ? '0'+this.month:this.month)+'-'+(this.date < 10 ? '0'+this.date:this.date);
  146. this.checkedList = [{date:dateStr}];
  147. }
  148. if(this.$props.defaultCheckedList){
  149. this.$props.defaultCheckedList.forEach((item,index)=>{
  150. this.checkedList.push({date:item});
  151. })
  152. }
  153. }
  154. },
  155. methods: {
  156. onCancel(){
  157. this.close();
  158. },
  159. onConfirm(date){
  160. this.close();
  161. this.$emit("finishSelectDate",[ date.dateValue]);
  162. },
  163. updateCheckedList(list){
  164. this.checkedList = list || [];
  165. },
  166. comfirmModel(){
  167. this.$emit("finishSelectDate",this.checkedList || []);
  168. this.close();
  169. },
  170. getCheckedList(){
  171. return this.checkedList || [];
  172. },
  173. lastMonth(){
  174. if(this.month === 1){
  175. this.year -= 1;
  176. this.month = 12;
  177. }else{
  178. this.month -= 1;
  179. }
  180. this.$refs.calendarObj.pre();
  181. },
  182. nextMonth(){
  183. if(this.month === 12){
  184. this.year += 1;
  185. this.month = 1;
  186. }else{
  187. this.month += 1;
  188. }
  189. this.$refs.calendarObj.next();
  190. },
  191. checkedEvent(weeks){
  192. let list = [];
  193. // 是否重复
  194. let isDuplicate = false;
  195. this.checkedList.forEach(item=>{
  196. if(item.date === weeks.fulldate){
  197. isDuplicate = true;
  198. }else{
  199. list.push(item);
  200. }
  201. })
  202. if(!isDuplicate){
  203. list.push({date:weeks.fulldate})
  204. }
  205. this.checkedList = list;
  206. if(!this.$props.isMultiple){
  207. let temp = [...(this.checkedList || [])];
  208. this.$emit("finishSelectDate",temp);
  209. this.checkedList = [];
  210. this.close();
  211. }
  212. },
  213. open() {
  214. // #ifndef MP
  215. let date = new Date();
  216. this.month = date.getMonth() + 1;
  217. this.year = date.getFullYear();
  218. this.date = date.getDate();
  219. this.curYear = this.year;
  220. this.curMonth = this.month;
  221. // #endif
  222. this.$refs.popupDialogObj.open();
  223. },
  224. close() {
  225. this.$refs.popupDialogObj.close();
  226. },
  227. }
  228. }
  229. </script>
  230. <style>
  231. @import url('luanqing-date-picker.css');
  232. </style>