Skip to content

Commit 6d9fa8f

Browse files
committed
feat(storage): store user in storage
1 parent bac02b9 commit 6d9fa8f

File tree

11 files changed

+200
-100
lines changed

11 files changed

+200
-100
lines changed

config.xml

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1+
<?xml version='1.0' encoding='utf-8'?>
22
<widget id="com.lanceli.cnodejs" version="3.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
3-
<name display="CNode社区">CNodeJs</name>
4-
<description>
3+
<name display="CNode社区">CNodeJs</name>
4+
<description>
55
Ionic app for https://cnodejs.org
66
</description>
7-
<author email="[email protected]" href="http://github.com/lanceli">
7+
<author email="[email protected]" href="http://github.com/lanceli">
88
Lance Li
99
</author>
10-
<content src="index.html"/>
11-
<access origin="*"/>
12-
<allow-navigation href="http://ionic.local/*"/>
13-
<allow-intent href="http://*/*"/>
14-
<allow-intent href="https://*/*"/>
15-
<allow-intent href="tel:*"/>
16-
<allow-intent href="sms:*"/>
17-
<allow-intent href="mailto:*"/>
18-
<allow-intent href="geo:*"/>
19-
<platform name="android">
20-
<allow-intent href="market:*"/>
21-
</platform>
22-
<platform name="ios">
23-
<allow-intent href="itms:*"/>
24-
<allow-intent href="itms-apps:*"/>
25-
</platform>
26-
<preference name="webviewbounce" value="false"/>
27-
<preference name="UIWebViewBounce" value="false"/>
28-
<preference name="DisallowOverscroll" value="true"/>
29-
<preference name="android-minSdkVersion" value="16"/>
30-
<preference name="BackupWebStorage" value="none"/>
31-
<preference name="SplashMaintainAspectRatio" value="true"/>
32-
<preference name="FadeSplashScreenDuration" value="300"/>
33-
<preference name="SplashShowOnlyFirstTime" value="false"/>
34-
<feature name="StatusBar">
35-
<param name="ios-package" onload="true" value="CDVStatusBar"/>
36-
</feature>
37-
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
38-
<plugin name="cordova-plugin-whitelist" spec="1.3.1"/>
39-
<plugin name="cordova-plugin-console" spec="1.0.5"/>
40-
<plugin name="cordova-plugin-statusbar" spec="2.2.1"/>
41-
<plugin name="cordova-plugin-device" spec="1.1.4"/>
42-
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1"/>
10+
<content src="index.html" />
11+
<access origin="*" />
12+
<allow-navigation href="http://ionic.local/*" />
13+
<allow-intent href="http://*/*" />
14+
<allow-intent href="https://*/*" />
15+
<allow-intent href="tel:*" />
16+
<allow-intent href="sms:*" />
17+
<allow-intent href="mailto:*" />
18+
<allow-intent href="geo:*" />
19+
<platform name="android">
20+
<allow-intent href="market:*" />
21+
</platform>
22+
<platform name="ios">
23+
<allow-intent href="itms:*" />
24+
<allow-intent href="itms-apps:*" />
25+
</platform>
26+
<preference name="webviewbounce" value="false" />
27+
<preference name="UIWebViewBounce" value="false" />
28+
<preference name="DisallowOverscroll" value="true" />
29+
<preference name="android-minSdkVersion" value="16" />
30+
<preference name="BackupWebStorage" value="none" />
31+
<preference name="SplashMaintainAspectRatio" value="true" />
32+
<preference name="FadeSplashScreenDuration" value="300" />
33+
<preference name="SplashShowOnlyFirstTime" value="false" />
34+
<feature name="StatusBar">
35+
<param name="ios-package" onload="true" value="CDVStatusBar" />
36+
</feature>
37+
<plugin name="ionic-plugin-keyboard" spec="~2.2.1" />
38+
<plugin name="cordova-plugin-whitelist" spec="1.3.1" />
39+
<plugin name="cordova-plugin-console" spec="1.0.5" />
40+
<plugin name="cordova-plugin-statusbar" spec="2.2.1" />
41+
<plugin name="cordova-plugin-device" spec="1.1.4" />
42+
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1" />
43+
<plugin name="cordova-sqlite-storage" spec="~2.0.3" />
4344
</widget>

config/webpack.common.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var webpack = require('webpack');
2+
3+
module.exports = {
4+
plugins: [
5+
new webpack.EnvironmentPlugin(['IONIC_ENV'])
6+
]
7+
};
8+

config/webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const webpackMerge = require('webpack-merge');
2+
3+
const commonConfig = require('./webpack.common.js');
4+
const ionicConfig = require('../node_modules/@ionic/app-scripts/config/webpack.config.js');
5+
6+
module.exports = webpackMerge(
7+
commonConfig,
8+
ionicConfig
9+
);

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ionicons": "3.0.0",
2929
"rxjs": "5.1.1",
3030
"sw-toolbox": "3.4.0",
31+
"webpack-merge": "^4.1.0",
3132
"zone.js": "0.8.4"
3233
},
3334
"devDependencies": {
@@ -50,5 +51,8 @@
5051
"locator": "ios"
5152
}
5253
],
54+
"config": {
55+
"ionic_webpack": "./config/webpack.config.js"
56+
},
5357
"description": "cnodejs-ionic2: An Ionic project"
5458
}

src/app/app.component.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Nav, Platform, AlertController } from 'ionic-angular';
33
import { StatusBar } from '@ionic-native/status-bar';
44
import { SplashScreen } from '@ionic-native/splash-screen';
55

6+
import { ConfigService } from '../providers/config-service';
7+
68
import { TabsService } from '../providers/tabs-service';
79
import { UserService } from '../providers/user-service';
810
import { MessageService } from '../providers/message-service';
@@ -32,6 +34,7 @@ export class MyApp {
3234
public platform: Platform,
3335
statusBar: StatusBar,
3436
splashScreen: SplashScreen,
37+
private configService: ConfigService,
3538
private tabsService: TabsService,
3639
private userService: UserService,
3740
private messageService: MessageService,
@@ -51,6 +54,9 @@ export class MyApp {
5154
ngAfterViewInit() {
5255
console.log('ng after view init');
5356

57+
this.userService.getCurrentUser().then((user) => {
58+
this.user = user;
59+
});
5460
this.tabs = this.tabsService.getTabs();
5561
}
5662

@@ -89,36 +95,43 @@ export class MyApp {
8995

9096
login() {
9197
console.log('login');
92-
let alert = this.alertCtrl.create({
93-
title: '输入Access Token',
94-
subTitle: 'PC端登录cnodejs.org后,在设置页可以找到Access Token',
95-
inputs: [
96-
{
97-
name: 'token',
98-
placeholder: 'Token'
99-
}
100-
],
101-
buttons: [
102-
{
103-
text: '取消',
104-
role: 'cancel',
105-
handler: data => {
106-
console.log('Cancel clicked');
98+
// login automatic if have token
99+
if (this.configService.token) {
100+
this.userService.login(this.configService.token).then((response) => {
101+
this.loginCallback(response);
102+
});
103+
} else {
104+
let alert = this.alertCtrl.create({
105+
title: '输入Access Token',
106+
subTitle: 'PC端登录cnodejs.org后,在设置页可以找到Access Token',
107+
inputs: [
108+
{
109+
name: 'token',
110+
placeholder: 'Token'
107111
}
108-
},
109-
{
110-
text: '提交',
111-
handler: data => {
112-
console.log(data);
113-
if (data.token) {
114-
this.userService.login(data.token).then((response) => {
115-
this.loginCallback(response);
116-
});
112+
],
113+
buttons: [
114+
{
115+
text: '取消',
116+
role: 'cancel',
117+
handler: data => {
118+
console.log('Cancel clicked');
119+
}
120+
},
121+
{
122+
text: '提交',
123+
handler: data => {
124+
console.log(data);
125+
if (data.token) {
126+
this.userService.login(data.token).then((response) => {
127+
this.loginCallback(response);
128+
});
129+
}
117130
}
118131
}
119-
}
120-
]
121-
});
122-
alert.present();
132+
]
133+
});
134+
alert.present();
135+
}
123136
}
124137
}

src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { NgModule, ErrorHandler } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
import { HttpModule } from '@angular/http';
44
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
5+
import { IonicStorageModule } from '@ionic/storage';
56
import { MomentModule } from 'angular2-moment';
67

78
import { StatusBar } from '@ionic-native/status-bar';
89
import { SplashScreen } from '@ionic-native/splash-screen';
910

1011
import { MyApp } from './app.component';
12+
import { ConfigService } from '../providers/config-service';
1113

1214
@NgModule({
1315
declarations: [
@@ -17,6 +19,7 @@ import { MyApp } from './app.component';
1719
BrowserModule,
1820
HttpModule,
1921
MomentModule,
22+
IonicStorageModule.forRoot(),
2023
IonicModule.forRoot(MyApp, {
2124
locationStrategy: 'hash'
2225
})
@@ -28,7 +31,8 @@ import { MyApp } from './app.component';
2831
providers: [
2932
StatusBar,
3033
SplashScreen,
31-
{provide: ErrorHandler, useClass: IonicErrorHandler}
34+
{provide: ErrorHandler, useClass: IonicErrorHandler},
35+
ConfigService
3236
]
3337
})
3438
export class AppModule {}

src/providers/config-service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Injectable} from '@angular/core';
2+
declare const process: any; // Typescript compiler will complain without this
3+
@Injectable()
4+
5+
export class ConfigService {
6+
public api:string;
7+
public token:string;
8+
constructor() {
9+
console.log(process);
10+
if (process.env.IONIC_ENV === 'prod') {
11+
this.api = 'https://cnodejs.org/api/v1/';
12+
} else {
13+
this.api = 'http://localhost:3000/api/v1/';
14+
this.token = ' 08bb9fe1-d42a-44ca-bae5-7c90585f8d7b';
15+
}
16+
}
17+
}
18+

src/providers/message-service.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Http } from '@angular/http';
33
import 'rxjs/add/operator/map';
44
import 'rxjs/add/operator/toPromise';
55

6+
import { ConfigService } from './config-service';
67
import { Message, Messages } from '../classes/message';
78
import { UserService } from './user-service';
89

@@ -14,41 +15,45 @@ import { UserService } from './user-service';
1415
*/
1516
@Injectable()
1617
export class MessageService {
17-
private countUrl: string = 'https://cnodejs.org/api/v1/message/count'
18-
private messagesUrl: string = 'https://cnodejs.org/api/v1/messages'
18+
private countUrl: string
19+
private messagesUrl: string
1920

20-
constructor(public http: Http, private userService: UserService) {
21+
constructor(public http: Http, public configService: ConfigService, private userService: UserService) {
2122
console.log('Hello MessageService Provider');
23+
this.countUrl = this.configService.api + 'message/count';
24+
this.messagesUrl = this.configService.api + 'messages';
2225
}
2326

2427
getMessageCount(): Promise<any> {
2528

2629
console.log('get messages count');
27-
let currentUser = this.userService.getCurrentUser();
28-
return this.http.get(this.countUrl, {
29-
params: {
30-
accesstoken: currentUser.accesstoken
31-
}
32-
}).toPromise().then(
33-
(resposne) => {
34-
console.log(resposne);
35-
return resposne.json().data;
36-
}
37-
)
30+
return this.userService.getCurrentUser().then((currentUser) => {
31+
return this.http.get(this.countUrl, {
32+
params: {
33+
accesstoken: currentUser.accesstoken
34+
}
35+
}).toPromise().then(
36+
(resposne) => {
37+
console.log(resposne);
38+
return resposne.json().data;
39+
}
40+
);
41+
});
3842
}
3943

4044
getMessages(): Promise<Messages> {
4145
console.log('get messages');
42-
let currentUser = this.userService.getCurrentUser();
43-
return this.http.get(this.messagesUrl, {
44-
params: {
45-
accesstoken: currentUser.accesstoken
46-
}
47-
}).toPromise().then(
48-
(resposne) => {
49-
return resposne.json().data as Messages;
50-
}
51-
);
46+
return this.userService.getCurrentUser().then((currentUser) => {
47+
return this.http.get(this.messagesUrl, {
48+
params: {
49+
accesstoken: currentUser.accesstoken
50+
}
51+
}).toPromise().then(
52+
(resposne) => {
53+
return resposne.json().data as Messages;
54+
}
55+
);
56+
});
5257
}
5358

5459
}

src/providers/storage-service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { Http } from '@angular/http';
3+
import 'rxjs/add/operator/map';
4+
5+
/*
6+
Generated class for the StorageService provider.
7+
8+
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
9+
for more info on providers and Angular 2 DI.
10+
*/
11+
@Injectable()
12+
export class StorageService {
13+
14+
constructor(public http: Http) {
15+
console.log('Hello StorageService Provider');
16+
}
17+
18+
}

src/providers/topic-service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Http } from '@angular/http';
33
import 'rxjs/add/operator/map';
44
import 'rxjs/add/operator/toPromise';
55

6+
import { ConfigService } from '../providers/config-service';
67
import { Topic } from '../classes/topic';
78

89
/*
@@ -13,15 +14,17 @@ import { Topic } from '../classes/topic';
1314
*/
1415
@Injectable()
1516
export class TopicService {
16-
private topicsUrl: string = 'https://cnodejs.org/api/v1/topics'
17-
private topicUrl: string = 'https://cnodejs.org/api/v1/topic'
17+
private topicsUrl: string
18+
private topicUrl: string
1819
currentTab: string = 'all'
1920
private nextPage: number = 2
2021
private hasNextPage: boolean = true
2122
topics: Topic[] = []
2223

23-
constructor(public http: Http) {
24+
constructor(public http: Http, public configService: ConfigService) {
2425
console.log('Hello TopicService Provider');
26+
this.topicsUrl = configService.api + 'topics';
27+
this.topicUrl = configService.api + 'topic';
2528
}
2629

2730
getTopicById(id: string): Promise<Topic>{

0 commit comments

Comments
 (0)