• 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 http from '@ohos.net.http';
16import Logger from '../utils/Logger';
17import Constant from '../utils/Constant';
18import NetworkModel from '../model/NetworkModel';
19import R from '../data/R';
20import LoginResult from '../data/LoginResult';
21
22const TAG: string = '[LoginController]';
23
24export default class LoginController {
25  private networkModel: NetworkModel = new NetworkModel();
26
27  public async login(phoneNumber: string, password: string): Promise<R> {
28    Logger.info(TAG, `login phoneNumber->${phoneNumber},password->${password}`);
29    let extraData = {
30      username: phoneNumber,
31      password: password
32    };
33    Logger.info(TAG, `login extraData->${JSON.stringify(extraData)}`);
34    let response = await this.networkModel.request(Constant.ACTION_LOGIN, http.RequestMethod.POST, extraData);
35    // 拿到响应中服务端返回的数据
36    let data = response.result.toString();
37    // 将其转成Json数据
38    let jsonData = JSON.parse(data);
39    Logger.info(TAG, `login jsonData->${JSON.stringify(jsonData)}`);
40    // 统一的返回类型
41    let result = new R();
42    result.success = jsonData.success;
43    result.code = jsonData.code;
44    result.message = jsonData.message;
45    // result不为空则赋值其中的数据
46    if (jsonData.result) {
47      let loginResult = new LoginResult();
48      loginResult.token = jsonData.result.token;
49      loginResult.username = jsonData.result.userInfo.username;
50      loginResult.realName = jsonData.result.userInfo.realname;
51      loginResult.avatar = jsonData.result.userInfo.avatar;
52      loginResult.id = jsonData.result.userInfo.id;
53      // 设置返回的数据
54      result.data = loginResult;
55    }
56    return result;
57  }
58}