home.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import { Component, OnInit } from '@angular/core';
  2. import * as Parse from 'parse';
  3. import { ActivatedRoute, Router } from '@angular/router';
  4. import { Swiper } from 'swiper';
  5. import { AiChatService } from '../../../services/aichart.service';
  6. import { ConnectTaskService } from '../../../services/connectTask.service';
  7. import {
  8. ionicStandaloneModules,
  9. AlertController,
  10. LoadingController,
  11. ToastController,
  12. } from '../../ionic-standalone.modules';
  13. import { CommonModule, DatePipe } from '@angular/common';
  14. import { provinceMap } from '../../../services/address';
  15. import { FormsModule } from '@angular/forms';
  16. import { BackgroundColorService } from '../../../services/background-color.service';
  17. import { DeviceService } from '../../../services/device.service';
  18. import { InfiniteScrollCustomEvent } from '@ionic/core';
  19. @Component({
  20. selector: 'app-home',
  21. templateUrl: './home.component.html',
  22. styleUrls: ['./home.component.scss'],
  23. standalone: true,
  24. imports: [...ionicStandaloneModules, CommonModule, FormsModule],
  25. providers: [DatePipe],
  26. })
  27. export class HomeComponent implements OnInit {
  28. options: Array<any> = [
  29. {
  30. label: '关注',
  31. value: 'follow',
  32. icon: 'home-outline',
  33. color: 'primary',
  34. },
  35. {
  36. label: '热门',
  37. value: 'recommend',
  38. icon: 'home-outline',
  39. color: 'primary',
  40. },
  41. {
  42. label: '新人',
  43. value: 'news',
  44. icon: 'videocam-outline',
  45. color: 'danger',
  46. },
  47. {
  48. label: '三星',
  49. value: '三星',
  50. icon: 'videocam-outline',
  51. color: 'danger',
  52. },
  53. {
  54. label: '四星',
  55. value: '四星',
  56. icon: 'videocam-outline',
  57. color: 'danger',
  58. },
  59. {
  60. label: '五星',
  61. value: '五星',
  62. icon: 'people-outline',
  63. },
  64. {
  65. label: '海外',
  66. value: '海外',
  67. icon: 'people-outline',
  68. },
  69. ];
  70. currentValue: string = 'recommend';
  71. oldCurrentValue: string = 'recommend';
  72. isOpen: boolean = false; //显示选择弹窗
  73. banner: Array<Parse.Object> = [];
  74. roomList: Array<any> = [];
  75. pageSwiper: Swiper | undefined | any;
  76. notices: Array<any> = [];
  77. viewAnchor: string = localStorage.getItem('viewSex') || '女';
  78. get sex(): string {
  79. const map: any = {
  80. all: '全部',
  81. 男: '男主播',
  82. 女: '女主播',
  83. };
  84. return map[this.viewAnchor];
  85. }
  86. isOpenCity: boolean = false;
  87. provinceColumns = [];
  88. cityColumns = [];
  89. province: string = ''; //省份
  90. city: string = ''; //市
  91. old_province: string = ''; //省份
  92. old_city: string = ''; //市
  93. isColumn: boolean = false; //单排
  94. userList: Array<string> | any = [];
  95. disbable: boolean = true;
  96. constructor(
  97. private loadingCtrl: LoadingController,
  98. private alertController: AlertController,
  99. // private activateRoute: ActivatedRoute,
  100. private connectTask: ConnectTaskService,
  101. private router: Router,
  102. private aiServ: AiChatService,
  103. private datePipe: DatePipe,
  104. private backgroundColorService: BackgroundColorService,
  105. private toastController: ToastController,
  106. public deviceSer: DeviceService
  107. ) {
  108. // province.unshift({
  109. // provinceName: '全部',
  110. // citys: [
  111. // {
  112. // cityName: '',
  113. // cityType: '',
  114. // },
  115. // ],
  116. // });
  117. this.provinceColumns = provinceMap.map((item) => item.provinceName);
  118. this.cityColumns = provinceMap[0].citys.map((item) => item.cityName);
  119. }
  120. ngOnInit() {
  121. // this.activateRoute.paramMap.subscribe(async (params) => {
  122. this.refresh();
  123. this.backgroundColorService.setBackgroundColor('#fe5559');
  124. // });
  125. }
  126. async refresh() {
  127. const loading = await this.loadingCtrl.create({
  128. message: '正在加载',
  129. });
  130. loading.present();
  131. await this.connectTask.init();
  132. await this.getBanner();
  133. await this.getNotice();
  134. await this.getRoom();
  135. setTimeout(() => {
  136. this.initSwiperTimeEvent();
  137. loading.dismiss();
  138. }, 100);
  139. }
  140. async getBanner() {
  141. let query = new Parse.Query('Banner');
  142. query.equalTo('company', this.aiServ.company);
  143. query.descending('index');
  144. query.equalTo('isEnabled', true);
  145. // query.equalTo('type', 'home');
  146. query.notEqualTo('isDeleted', true);
  147. query.containedIn('type', ['home', 'userNotice']);
  148. let banner = await query.find();
  149. this.banner = banner;
  150. }
  151. async getNotice() {
  152. let query = new Parse.Query('Notice');
  153. query.equalTo('company', this.aiServ.company);
  154. query.notEqualTo('isDeleted', true);
  155. query.equalTo('type', 'application');
  156. query.select('title', 'content');
  157. query.descending('createdAt');
  158. this.notices = await query.find();
  159. }
  160. initSwiperTimeEvent() {
  161. // 初始化轮播图
  162. let swiper = new Swiper('.mySwiper', {
  163. loop: true, // 循环模式选项
  164. observer: false, //修改swiper自己或子元素时,自动初始化swiper
  165. observeParents: true, //修改swiper的父元素时,自动初始化swiper
  166. autoplay: {
  167. delay: 3000,
  168. },
  169. pagination: {
  170. el: '.swiper-pagination',
  171. },
  172. });
  173. swiper.on('slideChange', function (event: any) {
  174. // console.log(event);
  175. });
  176. let swiperNot = new Swiper('.swiper-notice', {
  177. loop: true, // 循环模式选项
  178. observer: false, //修改swiper自己或子元素时,自动初始化swiper
  179. observeParents: true, //修改swiper的父元素时,自动初始化swiper
  180. autoplay: {
  181. delay: 3000,
  182. },
  183. direction: 'vertical',
  184. });
  185. }
  186. onChangeTab(type: string) {
  187. // console.log(type);
  188. if (type == this.oldCurrentValue) {
  189. return;
  190. }
  191. this.currentValue = type;
  192. // console.log(this.currentValue);
  193. this.roomList = [];
  194. this.disbable = true;
  195. this.getRoom();
  196. }
  197. async getRoom() {
  198. const loading = await this.loadingCtrl.create({
  199. message: '正在加载',
  200. });
  201. loading.present();
  202. let data: Array<any> = [];
  203. this.oldCurrentValue = this.currentValue;
  204. let uid = Parse.User.current()?.id;
  205. let sex = this.viewAnchor == 'all' ? null : this.viewAnchor;
  206. if (!this.connectTask.onlineUserList.size) {
  207. await this.connectTask.getOnlieUserList('user_connect_room');
  208. }
  209. this.userList = Array.from(this.connectTask.onlineUserList) || [];
  210. // console.log(userList);
  211. switch (this.currentValue) {
  212. case 'follow':
  213. data = await this.aiServ.getRooms({
  214. uid: uid,
  215. skip: this.roomList.length,
  216. // users: userList,
  217. follow: true,
  218. sex,
  219. city: this.city,
  220. });
  221. break;
  222. case 'recommend':
  223. data = await this.aiServ.getRooms({
  224. uid: uid,
  225. skip: this.roomList.length,
  226. // users: userList,
  227. recommend: true,
  228. sex,
  229. city: this.city,
  230. });
  231. break;
  232. case 'news':
  233. data = await this.aiServ.getRooms({
  234. uid: uid,
  235. skip: this.roomList.length,
  236. // users: userList,
  237. sex,
  238. city: this.city,
  239. });
  240. break;
  241. case '三星':
  242. data = await this.aiServ.getRooms({
  243. uid: uid,
  244. skip: this.roomList.length,
  245. // users: userList,
  246. star: '三星',
  247. sex,
  248. city: this.city,
  249. });
  250. break;
  251. case '四星':
  252. data = await this.aiServ.getRooms({
  253. uid: uid,
  254. skip: this.roomList.length,
  255. // users: userList,
  256. star: '四星',
  257. sex,
  258. city: this.city,
  259. });
  260. break;
  261. case '五星':
  262. data = await this.aiServ.getRooms({
  263. uid: uid,
  264. skip: this.roomList.length,
  265. // users: userList,
  266. star: '五星',
  267. sex,
  268. city: this.city,
  269. });
  270. break;
  271. case '海外':
  272. data = await this.aiServ.getRooms({
  273. uid: uid,
  274. skip: this.roomList.length,
  275. // users: userList,
  276. star: '海外',
  277. sex,
  278. city: this.city,
  279. });
  280. break;
  281. default:
  282. break;
  283. }
  284. if (data.length == 0) {
  285. loading.dismiss();
  286. const toast = await this.toastController.create({
  287. message: '已加载全部内容',
  288. duration: 1500,
  289. color: 'warning',
  290. });
  291. toast.present();
  292. return data.length;
  293. }
  294. this.roomList.push(...data);
  295. loading.dismiss();
  296. return data.length;
  297. }
  298. async presentAlert(item: any) {
  299. const alert = await this.alertController.create({
  300. header: item?.get('title') || '消息通知',
  301. subHeader: this.datePipe.transform(item?.createdAt, 'yyyy-MM-dd HH:mm')!,
  302. message: item?.get('content') || '',
  303. buttons: item?.get('url')
  304. ? [
  305. { text: '关闭' },
  306. {
  307. text: '确定',
  308. handler: () => {
  309. this.router.navigate([item?.get('url')]);
  310. },
  311. },
  312. ]
  313. : [{ text: '关闭' }],
  314. });
  315. await alert.present();
  316. }
  317. segmentChanged(e: any) {
  318. let { value } = e.detail;
  319. this.currentValue = value;
  320. }
  321. onIonChange(event: CustomEvent) {
  322. this.currentValue = event.detail.value;
  323. }
  324. /* 关闭弹窗回调 */
  325. onDidDismiss(event: CustomEvent) {
  326. this.isOpen = false;
  327. // console.log(this.currentValue);
  328. }
  329. cancel(type: string, value?: string) {
  330. console.log(type, value);
  331. if (type == 'cancel') {
  332. this.currentValue = this.oldCurrentValue;
  333. this.isOpen = false;
  334. return;
  335. } else {
  336. if (this.oldCurrentValue != this.currentValue) {
  337. this.roomList = [];
  338. this.disbable = true;
  339. this.getRoom();
  340. }
  341. this.oldCurrentValue = this.currentValue;
  342. this.isOpen = false;
  343. }
  344. }
  345. onChangeSex(e: any) {
  346. // console.log(e.detail.value);
  347. localStorage.setItem('viewSex', e.detail.value);
  348. if (e.detail.value == this.viewAnchor) return;
  349. this.viewAnchor = e.detail.value;
  350. this.roomList = [];
  351. this.disbable = true;
  352. this.getRoom();
  353. }
  354. search() {
  355. this.router.navigate(['live/search']);
  356. }
  357. toUrl(url: string) {
  358. url && this.router.navigateByUrl(url);
  359. }
  360. changeCityCancel(type: string) {
  361. if (type === 'confirm') {
  362. if (this.city !== this.old_city) {
  363. this.roomList = [];
  364. this.disbable = true;
  365. this.getRoom();
  366. }
  367. this.old_province = this.province;
  368. this.old_city = this.city;
  369. } else {
  370. this.province = this.old_province;
  371. this.city = this.old_city;
  372. }
  373. this.isOpenCity = false;
  374. }
  375. onIonChangeCity(event: CustomEvent, type: string) {
  376. let val = event.detail.value;
  377. switch (type) {
  378. case 'province':
  379. this.province = event.detail.value;
  380. this.cityColumns = provinceMap
  381. .find((item) => item.provinceName === val)
  382. ?.citys.map((item) => item.cityName)!;
  383. console.log(this.cityColumns);
  384. this.city = this.cityColumns[0];
  385. break;
  386. case 'city':
  387. this.city = event.detail.value;
  388. break;
  389. }
  390. console.log(this.province, this.city);
  391. }
  392. async onIonInfinite(ev: any) {
  393. let result = await this.getRoom();
  394. if (result == 0) {
  395. this.disbable = false;
  396. }
  397. setTimeout(() => {
  398. (ev as InfiniteScrollCustomEvent).target.complete();
  399. }, 500);
  400. }
  401. }