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 '../data/LoginResult';
20import { BusinessInfo, CommentInfo } from '../data/Server'
21const TAG: string = '[BusinessController]';
22
23export default class BusinessController {
24  private networkModel: NetworkModel = new NetworkModel();
25
26  public async getBusinessList(longitude: string, latitude: string): Promise<any> {
27    Logger.info(TAG, `getBusinessList longitude->${longitude},latitude->${latitude}`);
28    let extraData = {
29      longitude: longitude,
30      latitude: latitude
31    };
32    Logger.info(TAG, `getBusinessList extraData->${JSON.stringify(extraData)}`);
33    let userInfo: LoginResult = AppStorage.get('userInfo')!
34    let response = await this.networkModel.request(Constant.ACTION_BUSINESS_LIST, http.RequestMethod.GET, extraData, userInfo.token);
35    Logger.info(TAG, `getBusinessList response->${JSON.stringify(response)}`);
36    // 拿到响应中服务端返回的数据
37    Logger.info(TAG, `getBusinessList response.result->${JSON.stringify(response.result)}`);
38    let data: string = response.result.toString();
39    Logger.info(TAG, `getBusinessList data->${JSON.stringify(data)}`);
40    // 将其转成Json数据
41    let jsonData = JSON.parse(data);
42    Logger.info(TAG, `getBusinessList jsonData->${JSON.stringify(jsonData)}`);
43    let result: BusinessInfo[] = []; // 商家数据
44    if (jsonData && jsonData.result) {
45      result = jsonData.result.records;
46    }
47    return result;
48  }
49
50  public async getCommentList(businessId: string): Promise<any> {
51    Logger.info(TAG, `getCommentList businessId->${businessId}`);
52    let extraData = {
53      businessId: businessId
54    };
55    Logger.info(TAG, `getCommentList extraData->${JSON.stringify(extraData)}`);
56    let userInfo: LoginResult = AppStorage.get('userInfo')!
57    let response = await this.networkModel.request(Constant.ACTION_GET_Comment_LIST, http.RequestMethod.GET, extraData, userInfo.token);
58    Logger.info(TAG, `getCommentList response->${JSON.stringify(response)}`);
59    // 拿到响应中服务端返回的数据
60    Logger.info(TAG, `getCommentList response.result->${JSON.stringify(response.result)}`);
61    let data: string = response.result.toString();
62    Logger.info(TAG, `getCommentList data->${JSON.stringify(data)}`);
63    // 将其转成Json数据
64    let jsonData = JSON.parse(data);
65    Logger.info(TAG, `getCommentList jsonData->${JSON.stringify(jsonData)}`);
66    let result: CommentInfo[] = []; // 商家数据
67    if (jsonData && jsonData.result) {
68      result = jsonData.result.records;
69    }
70    return result;
71  }
72}