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 LoginResult from '../appsampled/data/LoginResult'; 20import R from '../appsampled/data/R'; 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: ESObject = { 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: ESObject = JSON.parse(data); 39 Logger.info(TAG, `login jsonData->${JSON.stringify(jsonData)}`) 40 // 统一的返回类型 41 let result = new R(); 42 let loginResult: ESObject ; 43 // result不为空则赋值其中的数据 44 if (jsonData.result) { 45 loginResult = new LoginResult(); 46 loginResult.setToken(jsonData.result.token); 47 loginResult.setUsername(jsonData.result.userInfo.username); 48 loginResult.setRealName(jsonData.result.userInfo.realname); 49 loginResult.setAvatar(jsonData.result.userInfo.avatar); 50 loginResult.setId(jsonData.result.userInfo.id); 51 } 52 result.setSuccess(jsonData.success); 53 result.setCode(jsonData.code); 54 result.setMessage(jsonData.message); 55 // 设置返回的数据 56 result.setData(loginResult); 57 return result; 58 } 59}