graceDate.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template name="graceDate">
  2. <view class="grace-date" v-if="show" :style="{top:top, zIndex:zIndex}" @tap.stop="" @touchmove.stop="">
  3. <view class="grace-date-header">
  4. <text class="grace-date-header-btn grace-icons" @click="prevYear">&#xe600;&#xe600;</text>
  5. <text class="grace-date-header-btn grace-icons" @click="prevMonth">&#xe600;</text>
  6. <text class="grace-date-header-date grace-icons">{{cYear}} 年 {{cMonth}} 月</text>
  7. <text class="grace-date-header-btn grace-icons" @click="nextMonth">&#xe601;</text>
  8. <text class="grace-date-header-btn grace-icons" @click="nextYear">&#xe601;&#xe601;</text>
  9. </view>
  10. <view class="grace-date-week">
  11. <text class="grace-date-weeks" v-for="(item, index) in weeks" :key="index">{{item}}</text>
  12. </view>
  13. <view class="grace-date-days">
  14. <view :class="['grace-date-ditems', currentDayIn == cYear+'-'+cMonthStr+'-'+ item.date ? 'grace-d-current' : '']"
  15. :style="{background: currentDayIn == cYear+'-'+cMonthStr+'-'+ item.date ? activeBgColor : bgColor, borderRadius:borderRadius}"
  16. @click="chooseDate(cYear+'-'+cMonthStr+'-'+item.date, item.date)" v-for="(item, index) in days" :key="index">
  17. <text class="grace-date-day" :class="[currentDayIn == (cYear+'-'+cMonthStr+'-'+item.date) ? 'grace-d-current-txt' : '']">{{item.date}}</text>
  18. <text class="grace-date-nl" v-if="isLunar" :class="[currentDayIn == (cYear+'-'+cMonthStr+'-'+item.date) ? 'grace-d-current-txt' : '']">{{item.nl}}</text>
  19. </view>
  20. </view>
  21. <view class="grace-nowrap grace-flex-center" style="margin-top:50rpx;" v-if="isTime">
  22. <picker class="grace-date-time" mode="time" @change="timechange" :value="currentTimeIn">
  23. <text>时间 : {{currentTimeIn}}</text>
  24. </picker>
  25. </view>
  26. <view class="grace-date-btns" v-if="isTime">
  27. <text class="grace-date-btns-text" @click="close" style="color:#888888;">关闭</text>
  28. <text class="grace-date-btns-text" @click="submit">确定</text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. var CalendarData = new Array(100);
  34. var madd = new Array(12);
  35. var tgString = "甲乙丙丁戊己庚辛壬癸";
  36. var dzString = "子丑寅卯辰巳午未申酉戌亥";
  37. var numString = "一二三四五六七八九十";
  38. var monString = "正二三四五六七八九十冬腊";
  39. var weekString = "日一二三四五六";
  40. var sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
  41. var cYear, cMonth, cDay, TheDate;
  42. CalendarData = new Array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95);
  43. madd[0] = 0;
  44. madd[1] = 31;
  45. madd[2] = 59;
  46. madd[3] = 90;
  47. madd[4] = 120;
  48. madd[5] = 151;
  49. madd[6] = 181;
  50. madd[7] = 212;
  51. madd[8] = 243;
  52. madd[9] = 273;
  53. madd[10] = 304;
  54. madd[11] = 334;
  55. function GetBit(m, n){return (m >> n) & 1;}
  56. //农历转换
  57. function e2c() {
  58. TheDate = (arguments.length != 3) ? new Date() : new Date(arguments[0], arguments[1], arguments[2]);
  59. var total, m, n, k;
  60. var isEnd = false;
  61. var tmp = TheDate.getYear();
  62. if (tmp < 1900) {tmp += 1900;}
  63. total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 38;
  64. if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) {total++;}
  65. for (m = 0; ; m++) {
  66. k = (CalendarData[m] < 0xfff) ? 11 : 12;
  67. for (n = k; n >= 0; n--) {
  68. if (total <= 29 + GetBit(CalendarData[m], n)) {isEnd = true; break;}
  69. total = total - 29 - GetBit(CalendarData[m], n);
  70. }
  71. if (isEnd) break;
  72. }
  73. cYear = 1921 + m;
  74. cMonth = k - n + 1;
  75. cDay = total;
  76. if (k == 12) {
  77. if (cMonth == Math.floor(CalendarData[m] / 0x10000) + 1) {cMonth = 1 - cMonth;}
  78. if (cMonth > Math.floor(CalendarData[m] / 0x10000) + 1) {cMonth--;}
  79. }
  80. }
  81. function GetcDateString() {
  82. var tmp = "";
  83. tmp += (cDay < 11) ? "初" : ((cDay < 20) ? "十" : ((cDay < 30) ? "廿" : "三十"));
  84. if (cDay % 10 != 0 || cDay == 10) {tmp += numString.charAt((cDay - 1) % 10);}
  85. return tmp;
  86. }
  87. function GetLunarDay(solarYear, solarMonth, solarDay) {
  88. if (solarYear < 1921) {return "";}
  89. solarMonth = (parseInt(solarMonth) > 0) ? (solarMonth - 1) : 11;
  90. e2c(solarYear, solarMonth, solarDay);
  91. return GetcDateString();
  92. }
  93. export default {
  94. name: "graceCountd",
  95. props: {
  96. show : {type:Boolean, default:false },
  97. currentDate : {type:String, default:"" },
  98. isTime : {type:Boolean, default:true },
  99. top : {
  100. type : String,
  101. // #ifdef H5
  102. default : '44px'
  103. // #endif
  104. // #ifndef H5
  105. default : '0'
  106. // #endif
  107. },
  108. zIndex : {type:String, default:"1"},
  109. bgColor : {type:String, default:"#F6F7F8"},
  110. activeBgColor : {type:String, default:"#3688FF"},
  111. borderRadius : {type:String, default:"6rpx"},
  112. isLunar : {type:Boolean, default:true }
  113. },
  114. data(){
  115. return {
  116. weeks : ['一', '二', '三', '四', '五', '六', '日'],
  117. cYear : 2016,
  118. cMonth : 6,
  119. cMonthStr : "06",
  120. cDay : "01",
  121. days : '',
  122. currentDayIn : '',
  123. currentTimeIn: ''
  124. }
  125. },
  126. methods: {
  127. timechange : function(e){
  128. this.currentTimeIn = e.detail.value;
  129. },
  130. getDaysInOneMonth : function (){
  131. var d = new Date(this.cYear, this.cMonth, 0);
  132. return d.getDate();
  133. },
  134. getDay : function (){
  135. var d = new Date(this.cYear, this.cMonth - 1, 0);
  136. return d.getDay();
  137. },
  138. prevYear : function(){
  139. this.cYear = this.cYear - 1;
  140. this.changeMonth();
  141. },
  142. prevMonth : function(){
  143. this.cMonth = this.cMonth - 1;
  144. if (this.cMonth < 1) { this.cMonth = 12; this.cYear = this.cYear - 1; }
  145. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  146. this.changeMonth();
  147. },
  148. nextMonth : function(){
  149. this.cMonth = this.cMonth + 1;
  150. if (this.cMonth > 12){this.cMonth = 1; this.cYear = this.cYear + 1;}
  151. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  152. this.changeMonth();
  153. },
  154. nextYear : function(){
  155. this.cYear = this.cYear + 1;
  156. this.changeMonth();
  157. },
  158. changeMonth:function(){
  159. var daysList = [];
  160. var days = this.getDaysInOneMonth();
  161. var startWeek = this.getDay();
  162. var forSteps = 0;
  163. for (var i = (0 - startWeek); i < days; i++){
  164. if(i >= 0){
  165. daysList[forSteps] = {date : i >= 9 ? i + 1 : '0' + (i+1), nl : ''};
  166. daysList[forSteps].nl = GetLunarDay(this.cYear, this.cMonth, i + 1);
  167. }else{
  168. daysList[forSteps] = '';
  169. }
  170. forSteps++;
  171. }
  172. this.days = daysList;
  173. },
  174. chooseDate: function (sedDate, isday) {
  175. if(!isday){return ;}
  176. this.currentDayIn = sedDate;
  177. if(this.isTime){return ;}
  178. this.$emit('changeDate', sedDate);
  179. },
  180. submit : function(){
  181. if(this.isTime){
  182. this.$emit('changeDate', this.currentDayIn+' '+this.currentTimeIn);
  183. }else{
  184. this.$emit('changeDate', this.currentDayIn);
  185. }
  186. },
  187. close : function(){
  188. this.$emit("closeDate");
  189. },
  190. //初始化时间
  191. initTime : function(){
  192. if(this.currentDate == ''){
  193. var dateObj = new Date();
  194. this.cYear = dateObj.getFullYear();
  195. this.cMonth = dateObj.getMonth() + 1;
  196. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  197. this.cDay = dateObj.getDate();
  198. this.cDay = this.cDay < 10 ? '0' + this.cDay : this.cDay;
  199. this.currentDayIn = this.cYear + '-' + this.cMonthStr + '-' + this.cDay;
  200. this.currentTimeIn = '00:00';
  201. this.changeMonth();
  202. }else{
  203. var dates = this.currentDate.split(' ');
  204. if (!dates[1]) { dates[1] = '';}
  205. var dayArr = dates[0].split('-');
  206. this.cYear = Number(dayArr[0]);
  207. this.cMonth = dayArr[1];
  208. this.cDay = dayArr[2];
  209. var reg = new RegExp('^0[0-9]+$');
  210. if(reg.test(this.cMonth)){this.cMonth = this.cMonth.substr(1,1);}
  211. this.cMonth = Number(this.cMonth);
  212. this.cMonthStr = this.cMonth < 10 ? '0'+this.cMonth : this.cMonth;
  213. this.currentDayIn = dates[0];
  214. this.currentTimeIn = dates[1];
  215. this.changeMonth();
  216. }
  217. }
  218. },
  219. created:function(){this.initTime();},
  220. watch:{
  221. currentDate : function(){this.initTime();}
  222. }
  223. }
  224. </script>
  225. <style scoped>
  226. .grace-date{position:fixed; z-index:1; left:0; top:0; bottom:0; width:730rpx; padding:0 10rpx; display:flex; flex-direction:column; align-items:center; background:#FFFFFF;}
  227. .grace-date-header{display:flex; justify-content:center; flex-direction:row; text-align:center; margin-top:20rpx;}
  228. .grace-date-header-btn{font-size:36rpx; line-height:88rpx; padding:0 10rpx; color:#888888;}
  229. .grace-date-header-date{line-height:88rpx; font-size:36rpx; margin:0 20rpx;}
  230. .grace-date-week{text-align:center; width:702rpx; display:flex; flex-wrap:nowrap; flex-direction:row;}
  231. .grace-date-weeks{display:block; width:100rpx; height:80rpx; text-align:center; font-size:32rpx; line-height:80rpx; color:#666666;}
  232. .grace-date-days{width:702rpx; display:flex; flex-direction:row; flex-wrap:wrap;}
  233. .grace-date-ditems{width:96rpx; height:96rpx; display:flex; flex-direction:column; align-items:center; justify-content:center; margin:2rpx; background-color:#F6F7F8; border-radius:5rpx;}
  234. .grace-d-current{background-color:#3688FF;}
  235. .grace-d-current-txt{color:#FFFFFF !important;}
  236. .grace-date-day{display:block; width:100%; height:38rpx; line-height:38rpx; text-align:center; font-size:32rpx;}
  237. .grace-date-nl{display:block; width:100%; height:26rpx; line-height:26rpx; color:#888888; font-size:20rpx; text-align:center;}
  238. .grace-date-btns{display:flex; flex-wrap:nowrap; flex-direction:row; justify-content:space-between; position:absolute; z-index:1; left:0; bottom:20px; width:100%;}
  239. .grace-date-btns-text{display:block; color:#3688FF; line-height:100rpx; font-size:30rpx; text-align:center; width:300rpx;}
  240. .grace-date-time{font-size:30rpx; line-height:100rpx; color:#666666; border-top:1px solid #F6F6F6; border-bottom:1px solid #F6F6F6; padding:0 20rpx;}
  241. </style>