lys-date.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view class="lys-date">
  3. <view class="lys-date-con">
  4. <view class="lys-date-con-title">
  5. <view class="" @tap="cancleTime">取 消</view>
  6. <view class="">
  7. <!-- <view class="">选择时间</view>
  8. <view class="">{{ showSelectDayStr }}</view> -->
  9. </view>
  10. <view class="" @tap="submitTime">确 定</view>
  11. </view>
  12. <!-- <view class="lys-date-con-sel">
  13. <view :class="selType == 1 ? 'sel' : ''" @tap="changeType(1)">日</view>
  14. <view :class="selType == 2 ? 'sel' : ''" @tap="changeType(2)">周</view>
  15. <view :class="selType == 3 ? 'sel' : ''" @tap="changeType(3)">月</view>
  16. <view :class="selType == 4 ? 'sel' : ''" @tap="changeType(4)">年</view>
  17. </view> -->
  18. <view class="lys-date-con-picker">
  19. <picker-view
  20. :value="selectValue"
  21. @change="bindChange"
  22. class="picker-view"
  23. indicator-style="height: 50px;"
  24. >
  25. <picker-view-column v-for="(item, index) in columns" :key="index">
  26. <view
  27. class="item"
  28. v-for="(childItem, childIndex) in item"
  29. :key="childIndex"
  30. >{{ childItem }}</view
  31. >
  32. </picker-view-column>
  33. </picker-view>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import { dateFormat } from "./format.js";
  40. export default {
  41. props: {
  42. type: {
  43. type: Number | String,
  44. default: 1,
  45. },
  46. time: {
  47. type: String,
  48. default: "",
  49. },
  50. startTime: {
  51. type: String,
  52. default: "",
  53. },
  54. endTime: {
  55. type: String,
  56. default: "",
  57. },
  58. },
  59. data() {
  60. return {
  61. selType: 1,
  62. selectYear: "",
  63. selectYearIndex: "",
  64. selectMonth: "",
  65. selectMonthIndex: "",
  66. selectWeek: "",
  67. selectWeekIndex: "",
  68. selectDay: "",
  69. selectDayIndex: "",
  70. timeStr: "",
  71. selectValue: [],
  72. columns: [],
  73. showSelectDayStr: "",
  74. };
  75. },
  76. mounted() {
  77. // 处理类型
  78. this.selType = this.type;
  79. // 所有的都转化为,以天为维度,这样统一度量尺
  80. // 自定义的以开始时间为准,转化为日,周,月
  81. // 处理时间问题
  82. if (this.type == 1) {
  83. // 类型为,日
  84. if (this.time == "") {
  85. this.timeStr = new Date().getTime();
  86. } else {
  87. this.timeStr = new Date(this.time.replace(/-/g, "/")).getTime();
  88. }
  89. }
  90. if (this.type == 2) {
  91. // 类型为,周
  92. // 2022年 第3周,根据传过来的字符串,找出本周的开始时间
  93. if (this.time == "") {
  94. this.timeStr = new Date().getTime();
  95. } else {
  96. // 分割字符串,获取到数字
  97. let year = this.time.substr(0, 4); // 年
  98. let week = this.time.substr(6).replace("周", ""); // 周
  99. let day = this.changeWeekToFirstDay(year, week);
  100. this.timeStr = new Date(day.replace(/\-/g, "/")).getTime();
  101. }
  102. }
  103. // 类型为,月
  104. if (this.type == 3) {
  105. // 2022年 3月,根据传过来的字符串,找出本周的开始时间
  106. if (this.time == "") {
  107. this.timeStr = new Date().getTime();
  108. } else {
  109. // 分割字符串,获取到数字
  110. let year = this.time.substr(0, 4); // 年
  111. let month = this.time.substr(6).replace("月", ""); // 周
  112. let day = year + "-" + month + "-01";
  113. this.timeStr = new Date(day.replace(/\-/g, "/")).getTime();
  114. }
  115. }
  116. // 类型为,年
  117. if (this.type == 4) {
  118. if (this.time == "") {
  119. this.timeStr = new Date().getTime();
  120. } else {
  121. // 分割字符串,获取到数字
  122. let year = this.time.substr(0, 4); // 年
  123. let day = year + "-01-01";
  124. this.timeStr = new Date(day.replace(/\-/g, "/")).getTime();
  125. }
  126. }
  127. this.changeType(this.type);
  128. },
  129. watch: {
  130. selectYearIndex() {
  131. if (this.selType == 1) this.daysFun();
  132. if (this.selType == 2) this.weeksFun();
  133. if (this.selType == 3) this.monthsFun();
  134. this.columnsFun();
  135. this.selectValueFun();
  136. this.showSelectDay();
  137. },
  138. selectMonthIndex() {
  139. if (this.selType == 1) this.daysFun();
  140. this.columnsFun();
  141. this.selectValueFun();
  142. this.showSelectDay();
  143. },
  144. selectDayIndex() {
  145. this.selectValueFun();
  146. this.showSelectDay();
  147. },
  148. selectWeekIndex() {
  149. this.selectValueFun();
  150. this.showSelectDay();
  151. },
  152. },
  153. methods: {
  154. changeType(flg) {
  155. this.selType = flg;
  156. if (flg == 1) {
  157. this.yearsFun();
  158. this.monthsFun();
  159. this.daysFun();
  160. }
  161. if (flg == 2) {
  162. this.yearsFun();
  163. this.weeksFun();
  164. }
  165. if (flg == 3) {
  166. this.yearsFun();
  167. this.monthsFun();
  168. }
  169. if (flg == 4) {
  170. this.yearsFun();
  171. }
  172. this.columnsFun();
  173. this.selectValueFun();
  174. this.showSelectDay();
  175. },
  176. columnsFun() {
  177. let data = [];
  178. // 日
  179. if (this.selType == 1) {
  180. data.push(this.years);
  181. data.push(this.months);
  182. data.push(this.days);
  183. }
  184. // 周
  185. if (this.selType == 2) {
  186. data.push(this.years);
  187. data.push(this.weeks);
  188. }
  189. // 月
  190. if (this.selType == 3) {
  191. data.push(this.years);
  192. data.push(this.months);
  193. }
  194. // 年
  195. if (this.selType == 4) {
  196. data.push(this.years);
  197. }
  198. this.columns = data;
  199. },
  200. selectValueFun() {
  201. this.selectValue = [];
  202. this.$nextTick(() => {
  203. if (this.selType == 1)
  204. this.selectValue = [
  205. this.selectYearIndex,
  206. this.selectMonthIndex,
  207. this.selectDayIndex,
  208. ];
  209. if (this.selType == 2)
  210. this.selectValue = [this.selectYearIndex, this.selectWeekIndex];
  211. if (this.selType == 3)
  212. this.selectValue = [this.selectYearIndex, this.selectMonthIndex];
  213. if (this.selType == 4) this.selectValue = [this.selectYearIndex];
  214. });
  215. },
  216. showSelectDay() {
  217. // 日
  218. if (this.selType == 1) {
  219. let day = `${this.selectYear}-${this.selectMonth}-${this.selectDay}`;
  220. let week = new Date(day.replace(/\-/g, "/")).getDay();
  221. let weekArray = [
  222. "星期日",
  223. "星期一",
  224. "星期二",
  225. "星期三",
  226. "星期四",
  227. "星期五",
  228. "星期六",
  229. ];
  230. this.showSelectDayStr = `${day} ${weekArray[week]}`;
  231. this.timeStr = new Date(day.replace(/\-/g, "/")).getTime();
  232. }
  233. // 周
  234. if (this.selType == 2) {
  235. let startDay = dateFormat(
  236. "m-d",
  237. this.changeWeekToFirstDay(this.selectYear, this.selectWeekIndex + 1)
  238. );
  239. let endDay = dateFormat(
  240. "m-d",
  241. this.changeWeekToEndDay(this.selectYear, this.selectWeekIndex + 1)
  242. );
  243. this.showSelectDayStr = `${this.selectYear}年 ${this.selectWeek} (${startDay} 至 ${endDay})`;
  244. this.timeStr = new Date(
  245. this.changeWeekToFirstDay(
  246. this.selectYear,
  247. this.selectWeek.replace("第", "").replace("周", "")
  248. ).replace(/\-/g, "/")
  249. ).getTime();
  250. }
  251. // 月
  252. if (this.selType == 3) {
  253. this.showSelectDayStr = `${this.selectYear}年 ${this.selectMonth}月`;
  254. this.timeStr = new Date(
  255. `${this.selectYear}/${this.selectMonth}/01`
  256. ).getTime();
  257. }
  258. // 年
  259. if (this.selType == 4) {
  260. this.showSelectDayStr = `${this.selectYear}年`;
  261. this.timeStr = new Date(
  262. `${this.selectYear}/${this.selectMonth}/01`
  263. ).getTime();
  264. }
  265. console.log(
  266. "当前所选时间==>" +
  267. this.timeStr +
  268. "==>" +
  269. dateFormat("Y-m-d", this.timeStr)
  270. );
  271. },
  272. yearsFun() {
  273. // 年初始化
  274. let year = new Date(this.timeStr).getFullYear();
  275. let endYear = new Date().getFullYear();
  276. let years = [];
  277. let j = 0;
  278. for (var i = 2000; i <= endYear; i++) {
  279. years.push(i);
  280. if (year == i) {
  281. this.selectYearIndex = j;
  282. this.selectYear = i;
  283. }
  284. j++;
  285. }
  286. this.years = years;
  287. console.log("yearsFun===>", this.selectYearIndex, this.selectYear);
  288. },
  289. monthsFun() {
  290. // 月初始化
  291. let month = new Date(this.timeStr).getMonth() + 1;
  292. let months = [];
  293. for (var i = 1; i <= 12; i++) {
  294. months.push(("" + i).padStart(2, "0"));
  295. if (month == i) {
  296. this.selectMonthIndex = i - 1;
  297. this.selectMonth = ("" + i).padStart(2, "0");
  298. }
  299. }
  300. this.months = months;
  301. },
  302. weeksFun() {
  303. let weeks = [];
  304. let year = this.years[this.selectYearIndex];
  305. // 判断今年有多少天
  306. let yearStart = new Date(`${year}/01/01 00:00:00`).getTime();
  307. let yearEnd = new Date(`${year}/12/31 23:59:59`).getTime();
  308. let allDday = (yearEnd - yearStart + 1000) / (1 * 24 * 60 * 60 * 1000);
  309. // 计算有多少周
  310. let week = Math.ceil(allDday / 7);
  311. // 计算当前属于第几周,+1s,
  312. let selWeek = Math.ceil(
  313. (this.timeStr + 1000 - yearStart) / (1 * 24 * 60 * 60 * 1000) / 7
  314. );
  315. for (var i = 1; i <= week; i++) {
  316. weeks.push("第" + i + "周");
  317. if (selWeek == i) {
  318. this.selectWeekIndex = i - 1;
  319. this.selectWeek = "第" + i + "周";
  320. }
  321. }
  322. this.weeks = weeks;
  323. },
  324. daysFun() {
  325. // 日初始化
  326. let day = new Date(this.timeStr).getDate();
  327. let days = [];
  328. let monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  329. if (this.selectMonth == "02" && this.selectYear % 4 == 0) {
  330. // 闰年二月份需要特殊处理
  331. monthDays[1] = 29;
  332. }
  333. let maxDay = monthDays[this.selectMonthIndex];
  334. for (let i = 1; i <= maxDay; i++) {
  335. days.push(("" + i).padStart(2, "0"));
  336. if (day == i) {
  337. this.selectDayIndex = i - 1;
  338. this.selectDay = ("" + i).padStart(2, "0");
  339. }
  340. }
  341. this.days = days;
  342. },
  343. changeWeekToFirstDay(year, selWeek) {
  344. let yearStart = new Date(`${year}/01/01 00:00:00`).getTime();
  345. yearStart += (selWeek - 1) * 7 * 1 * 24 * 60 * 60 * 1000;
  346. return dateFormat("Y-m-d", yearStart);
  347. },
  348. changeWeekToEndDay(year, selWeek) {
  349. let yearStart = new Date(`${year}/01/01 00:00:00`).getTime();
  350. yearStart += selWeek * 7 * 1 * 24 * 60 * 60 * 1000 - 1000;
  351. return dateFormat("Y-m-d", yearStart);
  352. },
  353. dayToWeek(day) {},
  354. bindChange(e) {
  355. let value = e.detail.value;
  356. if (this.selType == 1) {
  357. this.selectYearIndex = value[0];
  358. this.selectYear = this.years[value[0]];
  359. this.selectMonthIndex = value[1];
  360. this.selectMonth = this.months[value[1]];
  361. this.selectDayIndex = value[2];
  362. this.selectDay = this.days[value[2]];
  363. }
  364. if (this.selType == 2) {
  365. this.selectYearIndex = value[0];
  366. this.selectYear = this.years[value[0]];
  367. this.selectWeekIndex = value[1];
  368. this.selectWeek = this.weeks[value[1]];
  369. }
  370. if (this.selType == 3) {
  371. this.selectYearIndex = value[0];
  372. this.selectYear = this.years[value[0]];
  373. this.selectMonthIndex = value[1];
  374. this.selectMonth = this.months[value[1]];
  375. }
  376. if (this.selType == 4) {
  377. this.selectYearIndex = value[0];
  378. this.selectYear = this.years[value[0]];
  379. }
  380. },
  381. cancleTime() {
  382. this.$emit("cancleTime");
  383. },
  384. submitTime() {
  385. let time = "";
  386. if (this.selType == 1)
  387. time = `${this.selectYear}-${this.selectMonth}-${this.selectDay}`;
  388. if (this.selType == 2) time = `${this.selectYear}${this.selectWeek}`;
  389. if (this.selType == 3)
  390. time = `${this.selectYear}年 ${this.selectMonth}月`;
  391. if (this.selType == 4) time = `${this.selectYear}年`;
  392. this.$emit("submitTime", time, this.selType, this.timeStr);
  393. },
  394. },
  395. };
  396. </script>
  397. <style lang="scss" scoped src="./index.scss"></style>