美文应用

2024-12-08 13:53:40
163次阅读
0个评论

美文应用

import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { http } from '@kit.NetworkKit';

/**
 *
 * @author: 坚果派
 * @date: 2024/12/8
 * @phone:17752170152
 * website:nutpi.net
 * @organization:坚果派
 */
export class Artils {
  code?: number = 0;
  msg?: string = "";
  data?: ArtilsData[] = [];
  time?: number = 0;
  usage?: number = 0;
  log_id?: string = "";
}

export class ArtilsData {
  title?: string = "";
  desc?: string = "";
  author?: string = "";
  content?: string = "";
  cate?: string = "";
  created_at?: string = "";
}
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State artils: ArtilsData[] = [];

  getHttpData() {
    //2. 每一个httpRequest对应一个HTTP请求任务,不可复用
    let httpRequest = http.createHttp();
    // 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
    // 3.从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
    httpRequest.on('headersReceive', (header) => {
      console.info('header: ' + JSON.stringify(header));
    });
    httpRequest.request(
      //4. 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
      "https://v2.alapi.cn/api/mryw/list",
      {
        method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
        // 开发者根据自身业务需要添加header字段
        header: {
          'Content-Type': 'application/json'
        },
        // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定
        extraData: {
          page: "1",
          token: "你的token",
          cate: "yilin"


        },
      }, (err: BusinessError, data: http.HttpResponse) => {
      if (!err) {

        // 5.data.result为HTTP响应内容,可根据业务需要进行解析
        console.info('Result:' + JSON.stringify(data.result));
        console.info('code:' + JSON.stringify(data.responseCode));

        let result: Artils = JSON.parse(data.result.toString())
        this.artils = result.data!

        // data.header为HTTP响应头,可根据业务需要进行解析
        console.info('header:' + JSON.stringify(data.header));
        console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
        // 当该请求使用完毕时,调用destroy方法主动销毁
        httpRequest.destroy();
      } else {
        console.error('error:' + JSON.stringify(err));

        // 6.取消订阅HTTP响应头事件
        httpRequest.off('headersReceive');
        // 7.当该请求使用完毕时,调用destroy方法主动销毁
        httpRequest.destroy();
      }
    }
    );
  }

  build() {
    Column() {


      ForEach(this.artils, (item: ArtilsData) => {
        Text(item.title)
        Text(item.author)
      })
    }

    .height('100%')
    .width('100%')
  }

  aboutToAppear() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.loginWithHuaweiID();

    this.getHttpData()
  }

  /**
   * Sample code for using HUAWEI ID to log in to atomic service.
   * According to the Atomic Service Review Guide, when a atomic service has an account system,
   * the option to log in with a HUAWEI ID must be provided.
   * The following presets the atomic service to use the HUAWEI ID silent login function.
   * To enable the atomic service to log in successfully using the HUAWEI ID, please refer
   * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.
   */
  private loginWithHuaweiID() {
    // Create a login request and set parameters
    let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
    loginRequest.forceLogin = false;
    // Execute login request
    let controller = new authentication.AuthenticationController();
    controller.executeRequest(loginRequest).then((data) => {
      let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
      let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;
      // Send authCode to the backend in exchange for unionID, session

    }).catch((error: BusinessError) => {
      hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));
      if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {
        // HUAWEI ID is not logged in, it is recommended to jump to the login guide page

      }
    });
  }
}
收藏00

登录 后评论。没有帐号? 注册 一个。