• SAP 接口主动推送企业微信异常消息


    在这里插入图片描述
    在这里插入图片描述

        "推送企业微信格式
        lv_json = '{ "msgtype": "markdown", "markdown": ' &&
        '{  "content": "### 异常JOB通知\n >JOB名称:' && gt_alv-jobname &&
        ' \n 程序名称:' && gt_alv-progname &&
        ' \n 变式名称:' && gt_alv-variant &&
        ' \n 最后一次执行时间:'
        && gt_alv-sdldate+0(4) &&'/'&& gt_alv-sdldate+4(2) &&'/'&& gt_alv-sdldate+6(2) && ','
        && gt_alv-sdltime+0(2) &&':'&& gt_alv-sdltime+2(2) &&':'&& gt_alv-sdltime+4(2) &&
        ' \n 频率:**'&& gt_alv-pl &&'** \n ' &&
        '距最后一次执行时间: **' && gt_alv-min_c && '** '&&
        ' \n 检查时间:'
        && gt_alv-datum_check+0(4) &&'/'&& gt_alv-datum_check+4(2) &&'/'&& gt_alv-datum_check+6(2) && ','
        && gt_alv-uzeit_check+0(2) &&':'&& gt_alv-uzeit_check+2(2) &&':'&& gt_alv-uzeit_check+4(2) &&
        '"  }'.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    源码
    FUNCTION zfm_send_check_job.
    *“----------------------------------------------------------------------
    "“本地接口:
    *” IMPORTING
    *” VALUE(IV_JSON) TYPE STRING OPTIONAL
    *" EXPORTING
    *" VALUE(EV_RESULTMSG) TYPE STRING
    *" VALUE(EV_STATUS) TYPE I
    *" VALUE(EV_MESSAGE) TYPE CHAR100
    *" VALUE(EV_STATUS_C) TYPE CHAR1
    *"----------------------------------------------------------------------
    DATA:lv_url TYPE string.
    DATA:lv_http_client TYPE REF TO if_http_client.
    DATA:lv_http_method TYPE string.

    • iv_json = ‘{ “msgtype”: “markdown”, “markdown”: { “content”: "### 异常JOB通知\n >JOB名称:’ &&

    •        'XXXXXX \n 程序名称:XXXXXX \n 最后一次执行时间:\n 频率:**5分钟** \n ' &&
      
      • 1
    •        '距最后一次执行时间: **50分钟** \n 检查时间:"  }'.
      
      • 1

      SELECT SINGLE url
      INTO lv_url
      FROM ztip
      WHERE proname = ‘ZFM_SEND_CHECK_JOB’.
      IF sy-subrc <> 0.
      MESSAGE ‘ZTIP配置表没有地址’ TYPE ‘E’ DISPLAY LIKE ‘S’.
      RETURN.
      ENDIF.

      "记录日志使用
      DATA: ls_log_tab TYPE zcat_log_info.
      DATA: lv_nr_range_nr TYPE inri-nrrangenr,
      lv_object TYPE inri-object.

    • *写入日志信息。
      lv_object = ‘ZCANR001’.
      lv_nr_range_nr = ‘01’.
      CALL FUNCTION ‘NUMBER_GET_NEXT’
      EXPORTING
      nr_range_nr = lv_nr_range_nr
      object = lv_object
      quantity = ‘1’
      IMPORTING
      number = ls_log_tab-zlogn "日志编号。
      EXCEPTIONS
      interval_not_found = 1
      number_range_not_intern = 2
      object_not_found = 3
      quantity_is_0 = 4
      quantity_is_not_1 = 5
      interval_overflow = 6
      buffer_overflow = 7
      OTHERS = 8.
      CALL TRANSFORMATION id
      SOURCE
      lv_url = lv_url
      iv_json = iv_json
      RESULT XML ls_log_tab-reque .
      ls_log_tab-zsyss = ‘企业微信消息’ .
      ls_log_tab-funco = ‘ZFM_SEND_CHECK_JOB’.
      ls_log_tab-intern = ‘后台JOB检查结果自动推送企业微信’ .
      ls_log_tab-ernam = sy-uname .
      ls_log_tab-erdat = sy-datum .
      ls_log_tab-ertim = sy-uzeit .
      ls_log_tab-ftype = ‘SOAP’ .
      MODIFY zcat_log_info FROM ls_log_tab .
      COMMIT WORK AND WAIT .

    "URL接连
    CALL METHOD cl_http_client=>create_by_url
    EXPORTING
    url = lv_url
    IMPORTING
    client = lv_http_client
    EXCEPTIONS
    argument_not_found = 1
    plugin_not_active = 2
    internal_error = 3
    OTHERS = 4.
    IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
    sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    CHECK lv_http_client IS NOT INITIAL.

    lv_http_method = if_http_request=>co_request_method_post.
    lv_http_client->request->set_method( lv_http_method ).

    lv_http_client->propertytype_logon_popup = lv_http_client->co_disabled.

    CALL METHOD lv_http_client->request->set_header_field
    EXPORTING
    name = ‘Content-Type’
    value = ‘application/json’.

    DATA(lv_len) = strlen( iv_json ).

    CALL METHOD lv_http_client->request->set_cdata
    EXPORTING
    data = iv_json
    offset = 0
    length = lv_len.

    "发送请求
    lv_http_client->send( EXCEPTIONS http_communication_failure = 1
    http_invalid_state = 2 ).

    IF sy-subrc <> 0.
    "操作失败,获取失败原因
    lv_http_client->get_last_error( IMPORTING message = ev_resultmsg ).

    ELSE.
    "读取远程服务返回的处理过结果。
    lv_http_client->receive( EXCEPTIONS http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3 ).
    IF sy-subrc <> 0 .
    lv_http_client->get_last_error( IMPORTING message = ev_resultmsg ).
    ELSE.
    "读取返回返回内容
    ev_resultmsg = lv_http_client->response->get_cdata( ).
    "获取HTTP返回值
    lv_http_client->response->get_status( IMPORTING code = ev_status ).
    ENDIF.
    ENDIF.

    IF lv_http_client IS NOT INITIAL.
    CALL METHOD lv_http_client->close.
    ENDIF.

    "{“errcode”:0,“errmsg”:"ok. WARNING: json missing last curly brackets. "}
    TYPES:BEGIN OF ty_json,
    errcode TYPE string,
    errmsg TYPE string,
    END OF ty_json.

    DATA:ls_json TYPE ty_json.
    "解析异常捕捉
    TRY.
    CALL METHOD /ui2/cl_json=>deserialize
    EXPORTING
    json = ev_resultmsg
    CHANGING
    data = ls_json.

      IF ls_json-errcode = '0'.
        ev_message = ls_json-errmsg.
        ev_status_c = 'S'.
    
      ELSE.
        ev_message = ev_resultmsg.
        ev_status_c = 'E'.
    
      ENDIF.
    
    CATCH cx_sy_move_cast_error.
      ev_message = ev_resultmsg.
      ev_status_c = 'E'.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ENDTRY.

    *调用日志。
    CALL TRANSFORMATION id
    SOURCE
    ev_message = ev_message
    ev_status_c = ev_status_c
    ev_resultmsg = ev_resultmsg
    ev_status = ev_status
    RESULT XML ls_log_tab-respo .
    REPLACE FIRST OCCURRENCE OF ‘/’ IN ls_log_tab-innid WITH space .
    CONDENSE ls_log_tab-innid .

    UPDATE zcat_log_info
    SET respo = ls_log_tab-respo
    innid = ls_log_tab-innid
    ifmsg = ev_status_c
    WHERE zlogn = ls_log_tab-zlogn.
    IF sy-subrc = 0 .
    COMMIT WORK AND WAIT .
    ENDIF .

    ENDFUNCTION.

  • 相关阅读:
    vue搭建脚手架遇到的一个bug
    从输入URL到展示出页面
    促进国内凝聚态物理理论与统计物理及交叉学科的学术交流及促进学科发展,与龙讯旷腾相约济南
    工作中需要用到的Java知识(AQS篇)
    这个 人工智能学习路径靠谱吗?
    字节码进阶之java Instrumentation原理详解
    通用接口适配器使用文档须知
    发布npm包质量分测试
    uboot启动流程-uboot代码重定位说明二
    LeetCode【78. 子集】
  • 原文地址:https://blog.csdn.net/FQHX_116/article/details/128150650