• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import promptAction from '@ohos.promptAction';
16import router from '@ohos.router';
17import Logger from '../../utils/Logger';
18import LoginController from '../../controller/LoginController';
19import LoginResult from '../data/LoginResult';
20import User from '../data/User';
21import { BusinessError } from '@ohos.base';
22
23const TAG: string = '[Login]';
24
25@Entry
26@Component
27struct Login {
28  private loginController: LoginController = new LoginController();
29  @State phoneNumber: string = '13111111111';
30  @State password: string = '123456';
31  @State isLoginSuccess: boolean = false;
32
33  pageTransition() {
34    // 登录页面从底部滑入滑出
35    PageTransitionEnter({ type: RouteType.Push, duration: 300 })
36      .slide(SlideEffect.Bottom)
37    PageTransitionExit({ type: RouteType.Pop, duration: 300 })
38      .slide(SlideEffect.Bottom)
39  }
40
41  build() {
42    Column() {
43      Row() {
44        Text($r('app.string.LoginByPhone'))
45          .height('100%')
46          .fontColor($r('app.color.COLOR_E6000000'))
47          .fontSize(24)
48          .fontFamily($r('app.string.Font_family_medium'))
49      }
50      .width('80%')
51      .height('8%')
52      .margin({ bottom: 10 })
53      .justifyContent(FlexAlign.Start)
54
55      Column({ space: 10 }) {
56        Stack() {
57          TextInput({ placeholder: $r('app.string.Input_phone') })
58            .width('100%')
59            .height(50)
60            .borderRadius(5)
61            .type(InputType.PhoneNumber)
62            .onChange(value => {
63              this.phoneNumber = value;
64            })
65        }
66        .width('80%')
67        .height(50)
68
69        TextInput({ placeholder: $r('app.string.Input_password') })
70          .width('80%')
71          .height(50)
72          .borderRadius(5)
73          .type(InputType.Password)
74          .onChange(value => {
75            this.password = value;
76          })
77        Row() {
78          Image($r('app.media.app_icon'))
79            .width(240)
80            .height(25)
81            .objectFit(ImageFit.Contain)
82        }
83        .width('80%')
84        .height(20)
85        .justifyContent(FlexAlign.Start)
86
87        Text($r('app.string.Login'))
88          .id('login')
89          .width('80%')
90          .height(50)
91          .borderRadius(10)
92          .textAlign(TextAlign.Center)
93          .backgroundColor($r('app.color.COLOR_FF785F'))
94          .fontColor($r('app.color.COLOR_FFFFFF'))
95          .fontSize(20)
96          .fontFamily($r('app.string.Font_family_medium'))
97          .onClick(e => {
98            this.loginController.login(this.phoneNumber, this.password).then(res => {
99              Logger.info(TAG, `login then: ${JSON.stringify(res)}`);
100              // 提示服务端返回的登录信息
101              promptAction.showToast({ message: res.getMessage(), duration: 1000, bottom: 500 });
102              setTimeout(() => {
103                if (res.getCode() === 200) {
104                  let data: LoginResult = res.getData();
105                  Logger.info(TAG, `login success: ${JSON.stringify(data.getToken())}`);
106                  // 存储用户信息, 包括token
107                  AppStorage.setOrCreate("userInfo", data);
108
109                  // 分别存储当前用户和对端用户的用户名和头像
110                  if (data.getUsername() === '13111111111') {
111                    let currUser = new User(data.getUsername(), $r('app.media.app_icon'))
112                    let oppositeUser = new User('13122222222', $r('app.media.app_icon'))
113                    AppStorage.setOrCreate("currentUser", currUser);
114                    AppStorage.setOrCreate("oppositeUser", oppositeUser);
115                  } else {
116                    let currentUser = new User(data.getUsername(), $r('app.media.app_icon'))
117                    let oppositeUser = new User('13111111111', $r('app.media.app_icon'))
118                    AppStorage.setOrCreate("currentUser", currentUser);
119                    AppStorage.setOrCreate("oppositeUser", oppositeUser);
120                  }
121
122                  // 跳转页面
123                  router.pushUrl({ url: 'pages/Index' });
124                  return;
125                }
126                Logger.info(TAG, `login failed: ${JSON.stringify(res)}`);
127              }, 800)
128            }).catch((err: BusinessError)=> {
129              Logger.info(TAG, `login err: ${JSON.stringify(err)}`);
130              promptAction.showToast({ message: $r('app.string.Connection_timesout'), duration: 1000, bottom: 500 });
131            })
132          })
133
134      }
135      .width('100%')
136      .height('92%')
137    }
138    .width('100%')
139    .height('100%')
140    .backgroundColor($r('app.color.COLOR_FFFFFF'))
141  }
142}