沈斌
2017-12-15 f9b157566af34b8dc28ba10b34d025ac04f3168b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Injectable } from '@angular/core';
 
@Injectable()
export class UserService {
 
    private _isLogin = false;
 
    private _user = '';
 
    private _role: string[] = [];
 
    private _token = '';
 
    get isLogin(): boolean {
        return this._isLogin;
    }
 
    get token(): string {
        return this._token;
    }
 
    hasRole(name: 'admin' | 'employee'): boolean {
        return this._role.includes(name);
    }
 
    login(user: string) {
        this._user = user;
        // mock
        if (user === 'admin') {
            this._role = [ 'admin' ];
        } else {
            this._role = [ 'employee' ];
        }
        this._isLogin = true;
        this._token = '' + Math.random();
        console.log(user, this._isLogin, this._role);
    }
 
    logout() {
        this._user = '';
        this._isLogin = false;
        this._token = '';
    }
}