time.component.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
  2. import { IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonLabel, IonItem, IonList, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemOption, IonItemSliding, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime, IonFooter, IonCardContent, IonCardTitle, IonCardHeader, IonCard, IonCol, IonRow, IonGrid, IonChip, IonImg, PickerController, IonPicker } from '@ionic/angular/standalone';
  3. import { CommonModule } from '@angular/common';
  4. import { HttpClient } from '@angular/common/http';
  5. import { pinyin } from 'pinyin-pro';
  6. import { CloudQuery, CloudUser } from 'src/lib/ncloud';
  7. @Component({
  8. selector: 'time',
  9. templateUrl: './time.component.html',
  10. styleUrls: ['./time.component.scss'],
  11. standalone: true,
  12. imports: [IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonInput, IonItem, IonList, IonIcon, IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonLabel, IonLabel, IonList, IonItem, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemSliding, IonItemOption, IonItemOptions, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime, IonFooter, IonCardContent, IonCardTitle, IonCardHeader, IonCard, IonCol, IonRow, IonGrid, IonChip, IonImg, CommonModule, IonPicker]
  13. })
  14. export class TimeComponent implements OnInit {
  15. curYear: number;
  16. curMonth: number;
  17. week: string[] = ['一', '二', '三', '四', '五', '六', '日'];
  18. firstDays: number[] = [];
  19. curDays: number[] = [];
  20. nextDaysArray: number[] = [];
  21. selectedDate: number | null = null;
  22. pickerColumns: any[] = [];
  23. pickerButtons: any[] = [];
  24. checkedDates: string[] = [
  25. ];
  26. selectTime: string = ""//选中元素的时间按照“2024-12-20”格式
  27. async loadDates() {
  28. // let currentUser = new CloudUser();
  29. // const cloudQuery = new CloudQuery("fitUser");
  30. // cloudQuery.equalTo("user", currentUser.toPointer());
  31. // const userData = await cloudQuery.find();
  32. // console.log("userData::",userData[0].data.checkeddays);
  33. // this.checkedDates=userData[0].data.checkeddays
  34. }
  35. @Input() value2: string = ""; // 接收父组件传递的值
  36. @Output() ionChange2 = new EventEmitter<string>(); // 输出事件
  37. // async fetchWeatherData() {
  38. // try {
  39. // this.weatherData = await this.weatherService.getWeatherData(); // 提取 data 字段
  40. // console.log('天气数据:', this.weatherData); // 打印天气数据
  41. // } catch (error) {
  42. // console.error('获取天气数据失败:', error);
  43. // alert('无法获取天气数据,请检查网络连接或API状态。');
  44. // }
  45. // }
  46. weather: any = [
  47. ];
  48. fetchWeatherData() {
  49. // 第一个请求,获取 adcode
  50. // const apiUrl = '/api/api/weather/city/101240101';
  51. // this.http.get(apiUrl)
  52. // .subscribe((response: any) => {
  53. // const adcode = response; // 获取 adcode 字段
  54. // if (adcode) {
  55. // this.weather = adcode.data.forecast
  56. // console.log(this.weather);
  57. // console.log("adcode",adcode);
  58. // // 使用 adcode 获取天气信息
  59. // } else {
  60. // console.error('adcode not found in response:', response);
  61. // }
  62. // }, error => {
  63. // console.error('Error fetching adcode:', error);
  64. // });
  65. let a={
  66. "message": "success感谢又拍云(upyun.com)提供CDN赞助",
  67. "status": 200,
  68. "date": "20241225",
  69. "time": "2024-12-25 17:35:09",
  70. "cityInfo": {
  71. "city": "南昌市",
  72. "citykey": "101240101",
  73. "parent": "江西",
  74. "updateTime": "13:32"
  75. },
  76. "data": {
  77. "shidu": "43%",
  78. "pm25": 110,
  79. "pm10": 137,
  80. "quality": "轻度",
  81. "wendu": "11.3",
  82. "ganmao": "儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼",
  83. "forecast": [
  84. {
  85. "date": "25",
  86. "high": "高温 13℃",
  87. "low": "低温 5℃",
  88. "ymd": "2024-12-25",
  89. "week": "星期三",
  90. "sunrise": "07:07",
  91. "sunset": "17:26",
  92. "aqi": 93,
  93. "fx": "北风",
  94. "fl": "2级",
  95. "type": "晴",
  96. "notice": "愿你拥有比阳光明媚的心情"
  97. },
  98. {
  99. "date": "26",
  100. "high": "高温 12℃",
  101. "low": "低温 6℃",
  102. "ymd": "2024-12-26",
  103. "week": "星期四",
  104. "sunrise": "07:07",
  105. "sunset": "17:26",
  106. "aqi": 75,
  107. "fx": "北风",
  108. "fl": "3级",
  109. "type": "晴",
  110. "notice": "愿你拥有比阳光明媚的心情"
  111. },
  112. {
  113. "date": "27",
  114. "high": "高温 12℃",
  115. "low": "低温 7℃",
  116. "ymd": "2024-12-27",
  117. "week": "星期五",
  118. "sunrise": "07:07",
  119. "sunset": "17:27",
  120. "aqi": 61,
  121. "fx": "北风",
  122. "fl": "3级",
  123. "type": "晴",
  124. "notice": "愿你拥有比阳光明媚的心情"
  125. },
  126. {
  127. "date": "28",
  128. "high": "高温 10℃",
  129. "low": "低温 3℃",
  130. "ymd": "2024-12-28",
  131. "week": "星期六",
  132. "sunrise": "07:08",
  133. "sunset": "17:28",
  134. "aqi": 48,
  135. "fx": "东北风",
  136. "fl": "3级",
  137. "type": "晴",
  138. "notice": "愿你拥有比阳光明媚的心情"
  139. },
  140. {
  141. "date": "29",
  142. "high": "高温 12℃",
  143. "low": "低温 1℃",
  144. "ymd": "2024-12-29",
  145. "week": "星期日",
  146. "sunrise": "07:08",
  147. "sunset": "17:28",
  148. "aqi": 59,
  149. "fx": "东北风",
  150. "fl": "1级",
  151. "type": "晴",
  152. "notice": "愿你拥有比阳光明媚的心情"
  153. },
  154. {
  155. "date": "30",
  156. "high": "高温 13℃",
  157. "low": "低温 7℃",
  158. "ymd": "2024-12-30",
  159. "week": "星期一",
  160. "sunrise": "07:08",
  161. "sunset": "17:29",
  162. "aqi": 65,
  163. "fx": "西北风",
  164. "fl": "1级",
  165. "type": "阴",
  166. "notice": "不要被阴云遮挡住好心情"
  167. },
  168. {
  169. "date": "31",
  170. "high": "高温 15℃",
  171. "low": "低温 6℃",
  172. "ymd": "2024-12-31",
  173. "week": "星期二",
  174. "sunrise": "07:09",
  175. "sunset": "17:30",
  176. "aqi": 57,
  177. "fx": "东北风",
  178. "fl": "2级",
  179. "type": "晴",
  180. "notice": "愿你拥有比阳光明媚的心情"
  181. },
  182. {
  183. "date": "01",
  184. "high": "高温 13℃",
  185. "low": "低温 7℃",
  186. "ymd": "2025-01-01",
  187. "week": "星期三",
  188. "sunrise": "07:09",
  189. "sunset": "17:30",
  190. "aqi": 50,
  191. "fx": "东北风",
  192. "fl": "2级",
  193. "type": "多云",
  194. "notice": "阴晴之间,谨防紫外线侵扰"
  195. },
  196. {
  197. "date": "02",
  198. "high": "高温 9℃",
  199. "low": "低温 5℃",
  200. "ymd": "2025-01-02",
  201. "week": "星期四",
  202. "sunrise": "07:09",
  203. "sunset": "17:31",
  204. "aqi": 45,
  205. "fx": "东北风",
  206. "fl": "3级",
  207. "type": "阴",
  208. "notice": "不要被阴云遮挡住好心情"
  209. },
  210. {
  211. "date": "03",
  212. "high": "高温 11℃",
  213. "low": "低温 4℃",
  214. "ymd": "2025-01-03",
  215. "week": "星期五",
  216. "sunrise": "07:09",
  217. "sunset": "17:32",
  218. "aqi": 43,
  219. "fx": "东北风",
  220. "fl": "3级",
  221. "type": "多云",
  222. "notice": "阴晴之间,谨防紫外线侵扰"
  223. },
  224. {
  225. "date": "04",
  226. "high": "高温 12℃",
  227. "low": "低温 4℃",
  228. "ymd": "2025-01-04",
  229. "week": "星期六",
  230. "sunrise": "07:10",
  231. "sunset": "17:32",
  232. "aqi": 47,
  233. "fx": "北风",
  234. "fl": "2级",
  235. "type": "晴",
  236. "notice": "愿你拥有比阳光明媚的心情"
  237. },
  238. {
  239. "date": "05",
  240. "high": "高温 14℃",
  241. "low": "低温 6℃",
  242. "ymd": "2025-01-05",
  243. "week": "星期日",
  244. "sunrise": "07:10",
  245. "sunset": "17:33",
  246. "aqi": 56,
  247. "fx": "北风",
  248. "fl": "2级",
  249. "type": "阴",
  250. "notice": "不要被阴云遮挡住好心情"
  251. },
  252. {
  253. "date": "06",
  254. "high": "高温 12℃",
  255. "low": "低温 7℃",
  256. "ymd": "2025-01-06",
  257. "week": "星期一",
  258. "sunrise": "07:10",
  259. "sunset": "17:34",
  260. "aqi": 43,
  261. "fx": "东北风",
  262. "fl": "3级",
  263. "type": "晴",
  264. "notice": "愿你拥有比阳光明媚的心情"
  265. },
  266. {
  267. "date": "07",
  268. "high": "高温 12℃",
  269. "low": "低温 5℃",
  270. "ymd": "2025-01-07",
  271. "week": "星期二",
  272. "sunrise": "07:10",
  273. "sunset": "17:35",
  274. "aqi": 46,
  275. "fx": "东北风",
  276. "fl": "2级",
  277. "type": "晴",
  278. "notice": "愿你拥有比阳光明媚的心情"
  279. },
  280. {
  281. "date": "08",
  282. "high": "高温 14℃",
  283. "low": "低温 6℃",
  284. "ymd": "2025-01-08",
  285. "week": "星期三",
  286. "sunrise": "07:10",
  287. "sunset": "17:35",
  288. "aqi": 61,
  289. "fx": "北风",
  290. "fl": "1级",
  291. "type": "阴",
  292. "notice": "不要被阴云遮挡住好心情"
  293. }
  294. ],
  295. "yesterday": {
  296. "date": "24",
  297. "high": "高温 12℃",
  298. "low": "低温 3℃",
  299. "ymd": "2024-12-24",
  300. "week": "星期二",
  301. "sunrise": "07:06",
  302. "sunset": "17:25",
  303. "aqi": 94,
  304. "fx": "北风",
  305. "fl": "2级",
  306. "type": "晴",
  307. "notice": "愿你拥有比阳光明媚的心情"
  308. }
  309. }
  310. }
  311. this.weather = a.data.forecast
  312. }
  313. //转化成拼音
  314. convertToPinyin(text: string): string {
  315. const result = pinyin(text, {
  316. toneType: 'none', // 不带声调
  317. type: 'array', // 返回拼音数组
  318. });
  319. return result.join(''); // 将拼音数组转换为字符串
  320. }
  321. getWeatherType(year: number, month: number, day: number): string {
  322. // 格式化日期为 "YYYY-MM-DD"
  323. const dateStr = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  324. // 查找对应的天气数据
  325. const weatherData = this.weather.find((item: { ymd: string; }) => item.ymd === dateStr);
  326. // 如果找到天气数据,返回类型,否则返回空字符串
  327. return weatherData ? "assets/durian/" + this.convertToPinyin(weatherData.type) + ".png" : '';
  328. }
  329. constructor(private pickerCtrl: PickerController, private http: HttpClient) {
  330. const today = new Date();
  331. this.curYear = today.getFullYear();
  332. this.curMonth = today.getMonth() + 1; // 当前月,0-11
  333. this.selectedDate = today.getDate(); // 选中当天的日期
  334. this.updateCalendar();
  335. this.initializePicker();
  336. this.loadDates()
  337. }
  338. ngOnInit() {
  339. this.fetchWeatherData();
  340. }
  341. daka() {
  342. //打卡,点击之后,被选中的日期元素背景色被标为绿色
  343. if (this.checkedDates.includes(this.selectTime)) {
  344. console.log("之前就有记录");
  345. return
  346. }
  347. this.checkedDates.push(this.selectTime)
  348. console.log(this.checkedDates);
  349. }
  350. initializePicker() {
  351. this.pickerColumns = [
  352. {
  353. name: 'year',
  354. options: this.createYearOptions(),
  355. },
  356. {
  357. name: 'month',
  358. options: this.createMonthOptions(),
  359. },
  360. ];
  361. this.pickerButtons = [
  362. {
  363. text: '取消',
  364. role: 'cancel',
  365. },
  366. {
  367. text: '确定',
  368. handler: (value: { year: { value: number; }; month: { value: number; }; }) => {
  369. this.curYear = value.year.value;
  370. this.curMonth = value.month.value;
  371. this.updateCalendar();
  372. },
  373. },
  374. ];
  375. }
  376. updateCalendar() {
  377. const firstDay = this.getFirstDayOfMonth(this.curYear, this.curMonth);
  378. const daysInMonth = this.getDaysInMonth(this.curYear, this.curMonth);
  379. this.firstDays = this.createArray(firstDay === 0 ? 6 : firstDay - 1);
  380. this.curDays = this.createArray(daysInMonth);
  381. this.nextDaysArray = this.createArray(7 - this.getLastDayOfMonth(this.curYear, this.curMonth));
  382. }
  383. preMonth() {
  384. this.curMonth--;
  385. if (this.curMonth < 1) {
  386. this.curYear--;
  387. this.curMonth = 12;
  388. }
  389. this.updateCalendar();
  390. }
  391. nextMonth() {
  392. this.curMonth++;
  393. if (this.curMonth > 12) {
  394. this.curYear++;
  395. this.curMonth = 1;
  396. }
  397. this.updateCalendar();
  398. }
  399. formatDate(year: number, month: number, day: number): string {
  400. return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  401. }
  402. selectDate(day: number) {
  403. this.selectedDate = day; // 选中日期
  404. this.selectTime = `${this.curYear}-${this.curMonth < 10 ? '0' + this.curMonth : this.curMonth}-${day < 10 ? '0' + day : day}`; // 格式化时间
  405. console.log("组件运作", this.selectTime);
  406. this.ionChange2.emit(this.selectTime);
  407. }
  408. tolast(item: number) {
  409. this.nextMonth();
  410. this.selectedDate = item; // 选中日期
  411. }
  412. topre(item: number) {
  413. this.preMonth();
  414. this.selectedDate = item; // 选中日期
  415. }
  416. async openPicker() {
  417. const picker = await this.pickerCtrl.create({
  418. columns: this.pickerColumns,
  419. buttons: this.pickerButtons,
  420. });
  421. await picker.present();
  422. }
  423. createArray(n: number): number[] {
  424. return Array.from({ length: n }, (_, index) => index + 1); // 从1开始
  425. }
  426. getDaysInMonth(year: number, month: number): number {
  427. return new Date(year, month, 0).getDate();
  428. }
  429. getFirstDayOfMonth(year: number, month: number): number {
  430. return new Date(year, month - 1, 1).getDay(); // 注意:月份是0-11
  431. }
  432. getLastDayOfMonth(year: number, month: number): number {
  433. return new Date(year, month, 0).getDay(); // 获取当月最后一天是星期几
  434. }
  435. createYearOptions() {
  436. const currentYear = new Date().getFullYear();
  437. const years = [];
  438. for (let i = currentYear - 10; i <= currentYear + 10; i++) {
  439. years.push({ text: `${i}`, value: i });
  440. }
  441. return years;
  442. }
  443. createMonthOptions() {
  444. const months = [];
  445. for (let i = 1; i <= 12; i++) {
  446. months.push({ text: `${i}`, value: i });
  447. }
  448. return months;
  449. }
  450. }