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