• 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 router from '@ohos.router';
16import promptAction from '@ohos.promptAction';
17import LoginController from '../../controller/LoginController';
18import LoginResult from '../../data/LoginResult';
19import Logger from '../../utils/Logger';
20import { getStringData } from '../../utils/ResourceDataHandle';
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 = getStringData($r('app.string.login_phone_input')); // 13111111111
30  @State password: string = getStringData($r('app.string.login_pass_input')); // 123456
31  @State isLoginSuccess: boolean = false;
32  @State isPass: boolean = false;
33
34  build() {
35    Column() {
36      Row() {
37        Image($r('app.media.icon'))
38          .width(24)
39          .height(24)
40        Blank()
41        Text($r('app.string.login_help'))
42          .fontSize(16)
43      }
44      .padding({ left: 24, right: 24 })
45      .width('100%')
46      .height('8%')
47
48      Row() {
49        Text($r('app.string.login_welcome'))
50          .fontSize(32)
51          .padding({ top: 30 })
52      }
53      .width('75%')
54      .height('16%')
55
56      Row() {
57        Column() {
58          Row() {
59            Text($r('app.string.login_86'))
60              .fontColor($r('app.color.login_86'))
61              .fontSize(18)
62            Image($r('app.media.icon'))
63              .width(18)
64              .height(18)
65              .objectFit(ImageFit.Contain)
66              .margin({ left: 8 })
67            TextInput({ placeholder: $r('app.string.login_phone') })
68              .enableKeyboardOnFocus(false)
69              .width('75%')
70              .margin({ left: 16 })
71              .padding({ left: 0 })
72              .maxLength(11)
73              .placeholderFont({ size: 24 })
74              .fontSize(24)
75              .placeholderColor($r('app.color.login_input'))
76              .type(InputType.PhoneNumber)
77              .borderRadius(0)
78              .backgroundColor(Color.White)
79              .onChange((value: string) => {
80                this.phoneNumber = value;
81                if (this.password !== '' && this.phoneNumber !== '') {
82                  this.isPass = true;
83                }else {
84                  this.isPass = false;
85                }
86              })
87            Blank()
88            Image($r('app.media.icon'))
89              .width(24)
90              .height(24)
91              .objectFit(ImageFit.Contain)
92              .margin({ right: 10 })
93          }
94          .layoutWeight(1)
95          .width('100%')
96
97          Row() {
98            Divider()
99              .strokeWidth(1)
100              .color($r('app.color.login_divider'))
101          }
102          .width('100%')
103        }
104      }
105      .width('75%')
106      .height('9%')
107
108      Row() {
109        Column() {
110          Row() {
111            TextInput({ placeholder: $r('app.string.login_pass') })
112              .focusable(true)
113              .padding({ left: 0 })
114              .maxLength(11)
115              .placeholderFont({ size: 24 })
116              .placeholderColor($r('app.color.login_input'))
117              .type(InputType.Password)
118              .borderRadius(0)
119              .style(TextInputStyle.Inline)
120              .backgroundColor(Color.White)
121              .onChange((value: string) => {
122                this.password = value;
123                if (this.password !== '' && this.phoneNumber !== '') {
124                  this.isPass = true;
125                }else {
126                  this.isPass = false;
127                }
128              })
129          }
130          .layoutWeight(1)
131          .width('100%')
132
133          Row() {
134            Divider()
135              .strokeWidth(1)
136              .color($r('app.color.login_divider'))
137          }
138          .width('100%')
139        }
140      }
141      .width('75%')
142      .height('9%')
143
144      Row() {
145        Radio({ value: 'Radio1', group: 'radioGroup' })
146          .checked(true)
147        Text($r('app.string.login_agree'))
148          .fontSize(16)
149          .margin({ left: 12 })
150      }
151      .margin({ top: 50 })
152      .width('75%')
153      .height('5%')
154
155      Row() {
156        Button($r('app.string.login_login'), { type: ButtonType.Normal })
157          .borderRadius(8)
158          .fontSize(18)
159          .fontWeight(6)
160          .fontColor(this.isPass ? $r('app.color.login_button_font_color_yes') : $r('app.color.login_button_font_color_no')) // #47370E
161          .backgroundColor(this.isPass ? $r('app.color.login_button_bg_yes') : $r('app.color.login_button_bg_no')) // #FECF05
162          .width('100%')
163          .height('100%')
164          .onClick(() => {
165            Logger.info(TAG,`phone=${this.phoneNumber},password=${this.password}`);
166            this.loginController.login(this.phoneNumber, this.password).then(res => {
167              Logger.info(TAG, `login then: ${JSON.stringify(res)}`);
168              // 提示服务端返回的登录信息
169              promptAction.showToast({ message: res.message, duration: 1000, bottom: 500 });
170              setTimeout(() => {
171                if (res.code === 200) {
172                  let data: LoginResult = res.data;
173                  Logger.info(TAG, `login success: ${JSON.stringify(data.token)}`);
174                  // 存储用户信息, 包括token
175                  AppStorage.setOrCreate('userInfo', data)
176                  // 跳转页面
177                  router.pushUrl({ url: 'pages/Index' });
178                  return;
179                }
180                Logger.info(TAG, `login failed: ${JSON.stringify(res)}`);
181              }, 800);
182            }).catch((err: BusinessError) => {
183              Logger.info(TAG, `login err: ${JSON.stringify(err)}`);
184              promptAction.showToast({ message: $r('app.string.Connection_timesout'), duration: 1000, bottom: 500 });
185            })
186          })
187          .id('login')
188      }
189      .margin({ top: 18 })
190      .width('75%')
191      .height('8%')
192
193      Row() {
194        Text($r('app.string.login_captcha'))
195          .fontSize(16)
196        Blank()
197        Text($r('app.string.login_question'))
198          .fontSize(16)
199      }
200      .margin({ top: 24 })
201      .width('75%')
202      .height('8%')
203    }
204    .width('100%')
205    .height('100%')
206  }
207}