两种方案:1、session、watchCurrentCustomer().subscribe(data => {});订阅机制;2、events机制.下面将以代码重现方案一:import {Inje
两种方案:1、session、watchCurrentCustomer().subscribe(data => {});订阅机制;2、events机制.下面将以代码重现
方案一:
import {Injectable} from '@angular/core';
import 'rxjs/Rx';
import {LocalStorage, Storage} from "ionic-angular/index";
import {Subject} from "rxjs/Subject";
import {Customer} from "../models/Customer";
import {BehaviorSubject} from "rxjs/BehaviorSubject";
import {Observable} from "rxjs/Observable";
@Injectable()export class XXXXXSession extends Observable<Customer>{private local:Storage;private currentCustomer:Subject<Customer> = new BehaviorSubject<Customer>(null);private customer;private residence;
constructor(appConfig:AppConfig) { this.local = new Storage(LocalStorage); this.local.get('Customer').then(cust=> { console.info(cust); if (cust != null) { this.login = true; this.customer = JSON.parse(cust); } }); this.local.get('Residence').then(residence=> { console.info(residence != null); if (residence != "") { this.residence = JSON.parse(residence); } });}isLogin() { return this.login;}getCustomer() { return this.customer;}setCustomer(customer) { this.currentCustomer.next(customer); this.local.set('Customer', JSON.stringify(customer)); this.login = true;} getCustomerId() { if (this.customer != null) { return this.customer.customerId; } return 1;}getResidence() { return this.residence;}getResidenceId() { if (this.residence != null) { return this.residence.residenceId; } return 1;}setResidence(residence) { this.local.set('Residence', JSON.stringify(residence)); this.residence = residence;}
}
监听者:XXXXSession.watchCurrentCustomer().subscribe(data => {});
XXXXSession.watchCurrentCustomer().subscribe(data => { if(data == null){ this.portrait = "img/not_login.png"; this.isShop = false; this.customer = null; this.nickName = null; }else { this.customer = data.customer; this.nickName = this.customer.nickName; this.portrait = data.customer.portrait; this.queryCustomerShop(this.customer.customerId); } }); XXXXSession.watchCurrentResidence().subscribe((newResidence) => { if(newResidence!=null){ this.defaultResidence=newResidence.residenceName; }else{ } }); }
方案二、Events机制import 'rxjs/Rx';
import {LocalStorage, Storage, Events} from "ionic-angular/index";
@Injectable()export class CustomerSession {
private static local:Storage = new Storage(LocalStorage);private static customer;private static residence;constructor(public events:Events) {}initSession() { CustomerSession.local.get('Customer').then(cust=> { if (cust != null) { CustomerSession.customer = JSON.parse(cust); this.events.publish("Customer:changed", CustomerSession.customer); } }); CustomerSession.local.get('Residence').then(residence=> { if (residence != null) { CustomerSession.residence = JSON.parse(residence); this.events.publish("Residence:changed", CustomerSession.residence); } });}
}
其他地方监听:this.events.subscribe('Customer:changed', (eventData) => {
if (eventData != null && eventData.length > 0) { this.customer = eventData[0]; this.nickName = this.customer.nickName; this.portrait = this.customer.portrait; this.queryCustomerShop(this.customer.customerId); }else{ this.portrait = "img/not_login.png"; this.isShop = false; this.customer = null; this.nickName = "XXXX"; } }); this.events.subscribe('Residence:changed', (eventData) => { if (eventData != null && eventData.length > 0) { this.defaultResidence=eventData[0].residenceName; } });