import {Injectable} from '@angular/core'; import {LoadingController, NavController, ToastController} from '@ionic/angular'; import { Constants } from './Constant'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import { map } from 'rxjs/operators'; import * as EventEmitter from 'eventemitter3'; let newVariable: any; @Injectable({ providedIn: 'root' }) export class PublicService { public user: any; public token: any; public event: any; public constructor(private readonly toastController: ToastController, private readonly navController: NavController, private readonly http: HttpClient) { this.event = new EventEmitter(); if (window.localStorage.getItem('odcUser')) { this.setCurrentUser(JSON.parse(window.localStorage.getItem('odcUser'))); } if (window.localStorage.getItem('token')) { this.setToken(JSON.parse(window.localStorage.getItem('token'))); } const addressServeArr = this.getServeAddress(); if (addressServeArr.indexOf(Constants.hostServe) === -1){ addressServeArr.push(Constants.hostServe); this.setServeAddress(addressServeArr); } } public setCurrentUser(user) { this.user = user; window.localStorage.setItem('odcUser', JSON.stringify(user)); } public getCurrentUser() { if (window.localStorage.getItem('odcUser')) { return JSON.parse(window.localStorage.getItem('odcUser')); }else{ return null; } } public setToken(token) { this.token = token; window.localStorage.setItem('token', JSON.stringify(token)); } public getToken() { if (window.localStorage.getItem('token')) { return window.localStorage.getItem('token'); }else{ return null; } } public setApiUrl(apiUrl){ window.localStorage.setItem('apiUrl', apiUrl); } public getApiUrl(){ if (window.localStorage.getItem('apiUrl')) { return window.localStorage.getItem('apiUrl'); }else{ return Constants.hostServe; } } public setServeAddress(serveAddressArr) { window.localStorage.setItem('serveAddress', JSON.stringify(serveAddressArr)); } public getServeAddress() { if (window.localStorage.getItem('serveAddress')) { return JSON.parse(window.localStorage.getItem('serveAddress')); }else{ return []; } } public addViewRecord(record) { const records = this.getViewRecords(); let selectedRecord = null; for ( const re of records){ if (re.id === record.id){ selectedRecord = re; } } if (selectedRecord){ selectedRecord.count++ ; }else{ record.count = 1; records.push(record); } records .sort(this.sortTime) const userConfig = this.user.userConfig; userConfig.oftenVisits = JSON.stringify(records.slice(0, 10)); this.updateUserConfig(userConfig).then(res => { if (res.code === Constants.SUCCESS){ this.user.userConfig.oftenVisits = JSON.stringify(records); this.setCurrentUser(this.user); } }); } public getViewRecords() { const userConfig = this.user.userConfig; let viewRecords = []; if (userConfig.oftenVisits){ viewRecords = JSON.parse(userConfig.oftenVisits); } return viewRecords.sort(this.sortTime); } public sortTime(b , a) { return a.count - b.count; } // 提示框 public async presentToast(msg: string) { const toast = await this.toastController.create({ message: msg, duration: 2000, position: 'middle', cssClass: 'custom-toast' }); toast.present(); } // 显示loading public async showLoading(loadingCtrl: LoadingController, message: string) { const loading = await loadingCtrl.create({ message: message ? message : '努力加载中...' }); loading.present(); return loading; } // 网络检查,并提示 public checkNetworkToast(err) { newVariable = window.navigator; const networkState = newVariable.connection.type; // console.log(networkState); if (networkState === Constants.NETSTA_NONE) { this.presentToast('网络错误'); } else if (err.status === Constants.NOAUTH){ this.presentToast('token过期'); this.navController.navigateRoot('/login'); }else if (err.msg){ this.presentToast(err.msg); } else{ this.presentToast('服务器错误'); } } // 测试服务是否可用 public apiTest() { const url = this.getApiUrl() + `/swagger-resources`; return this.http .get(url) .pipe(map(result => result as any)) .toPromise(); } // 刷新token refreshToken(){ return this.http .post(this.getApiUrl() + '/user/token/refresh', {}, { headers: new HttpHeaders({ Authorization: this.token }) } ) .pipe(map(result => result as any)) .toPromise(); } // 获取手机验证码 getTelCode(phone, realName){ return this.http .post(this.getApiUrl() + '/user/sendCodeByPhone', {phone, realName}, {} ) .pipe(map(result => result as any)) .toPromise(); } // 验证验证码 checkPhoneCode(code, phone, realName){ return this.http .post(this.getApiUrl() + '/user/checkPhoneCode', { code, phone, realName}, {} ) .pipe(map(result => result as any)) .toPromise(); } // 获取APP的参数 getAppParameter(){ return this.http .post(this.getApiUrl() + '/analysis/app/config/common/imgParameter', {}, { headers: new HttpHeaders({ Authorization: this.token }) } ) .pipe(map(result => result as any)) .toPromise(); } // 修改密码 updatePassword(param){ return this.http .post(this.getApiUrl() + '/user/updatePassword', param, { headers: new HttpHeaders({ Authorization: this.token }) } ) .pipe(map(result => result as any)) .toPromise(); } // 重置密码 resetPasswordByUser(param){ return this.http .post(this.getApiUrl() + '/sys/sysUser/resetPasswordByUser', param, {} ) .pipe(map(result => result as any)) .toPromise(); } // 用户信息 public getUserInfo(userId, token){ return this.http .post(this.getApiUrl() + '/user/getUserInfobyId', {id: userId}, {headers: new HttpHeaders({ Authorization: token }) }) .pipe(map(result => result as any)) .toPromise(); } // 获取用户配置 public getUserConfig(userId, token) { const url = this.getApiUrl() + `/analysis/app/user/config/query?userId=` + userId; return this.http .get(url, {headers: new HttpHeaders({ Authorization: token }) }) .pipe(map(result => result as any)) .toPromise(); } // 修改用户配置 public updateUserConfig(param) { return this.http .post(this.getApiUrl() + '/analysis/app/user/config/update', param, {headers: new HttpHeaders({ Authorization: this.token }) }) .pipe(map(result => result as any)) .toPromise(); } // 获取发布搜索 public queryAppPublish(param) { return this.http .post(this.getApiUrl() + `/analysis/app/publish/queryAppPublish`, param, {headers: new HttpHeaders({ Authorization: this.token }) }) .pipe(map(result => result as any)) .toPromise(); } // 获取发布详情 public getPublicDetail(publishId) { const url = this.getApiUrl() + `/analysis/app/publish/publicDetail?publishId=` + publishId; return this.http .get(url, {headers: new HttpHeaders({ Authorization: this.token }) }) .pipe(map(result => result as any)) .toPromise(); } // 获取手机应用的发布树形结构数据 public getPublishTree(keyWord) { const url = this.getApiUrl() + `/analysis/app/publish/getPublishTree?keyWord=` + keyWord; return this.http .get(url, {headers: new HttpHeaders({ Authorization: this.token }) }) .pipe(map(result => result as any)) .toPromise(); } // 根据父id和关键字查询发布列表 public queryByParentAndKey(keyWord) { const url = this.getApiUrl() + `/analysis/app/publish/queryByParentAndKey?keyWord=` + keyWord; return this.http .get(url, {headers: new HttpHeaders({ Authorization: this.token }) }) .pipe(map(result => result as any)) .toPromise(); } }