w7corp / easywechat

📦 一个 PHP 微信 SDK
https://easywechat.com
MIT License
10.26k stars 2.41k forks source link

企业微信审批结果消息通知无法解析XML #2592

Closed JoeZing closed 1 year ago

JoeZing commented 1 year ago

我用的环境

业务流程

企业微信自建应用,业务流程发起审批单,用户在审批单审批后,企业微信发起消息通知到自建应用api接口;

代码:

$server->addEventListener('sys_approval_change', function (Message $message, \Closure $next) {

            info('debug', [$message->ApprovalInfo]);

            return $next($message);
        });

问题

无法解析出 $message->ApprovalInfo['SpRecord'] 字段,查看传过来的消息原始数据是有内容的,但SpRecord解析不出来,部分字段解析成 SimpleXMLElement

返回示例:


[
    {
        "SpNo": "202209220072",
        "SpName": "审批单标题",
        "SpStatus": "3",
        "TemplateId": "C4RaBXwHyha1LojAxxxxxxxxx",
        "ApplyTime": "1663839472",
        "Applyer": {
            "UserId": "userid1234",
            "Party": "32"
        },
        "SpRecord": [
            {
                "SimpleXMLElement": ""
            },
            {
                "SimpleXMLElement": ""
            },
            {
                "SimpleXMLElement": ""
            }
        ],
        "StatuChangeEvent": "3"
    }
]
JoeZing commented 1 year ago

问题已解决: #2593

TheNorthMemory commented 1 year ago

sys_approval_change 这个事件的文档地址是?

JoeZing commented 1 year ago

sys_approval_change 这个事件的文档地址是?

@TheNorthMemory 审批申请状态变化回调通知

TheNorthMemory commented 1 year ago

问题已解决: #2593

使用 json_decode(json_encode()) 是一种比较暴力的方法,内存开销比较大;实际上是 xml 解析 出现了小问题,可能是下面这一段:

https://github.com/w7corp/easywechat/blob/a3733f38e42c9025ccae3722e7bacacd1f82e7ad/src/Kernel/Support/Xml.php#L90-L100

对应地,可以看看下面这段代码:

https://github.com/wechatpay-apiv3/wechatpay-php/blob/c34b7812d17c824d4e7ef2b2861aeba779e64578/src/Transformer.php#L73-L99

解析出来能达预期,如文档上示例解析如下:

array(7) {
  ["ToUserName"]=>
  string(18) "ww1cSD21f1e9c0caaa"
  ["FromUserName"]=>
  string(3) "sys"
  ["CreateTime"]=>
  string(10) "1571732272"
  ["MsgType"]=>
  string(5) "event"
  ["Event"]=>
  string(19) "sys_approval_change"
  ["AgentID"]=>
  string(7) "3010040"
  ["ApprovalInfo"]=>
  array(10) {
    ["SpNo"]=>
    string(12) "201910220003"
    ["SpName"]=>
    string(12) "示例模板"
    ["SpStatus"]=>
    string(1) "1"
    ["TemplateId"]=>
    string(43) "3TkaH5KFbrG9heEQWLJjhgpFwmqAFB4dLEnapaB7aaa"
    ["ApplyTime"]=>
    string(10) "1571728713"
    ["Applyer"]=>
    array(2) {
      ["UserId"]=>
      string(8) "WuJunJie"
      ["Party"]=>
      string(1) "1"
    }
    ["SpRecord"]=>
    array(2) {
      [0]=>
      array(3) {
        ["SpStatus"]=>
        string(1) "1"
        ["ApproverAttr"]=>
        string(1) "2"
        ["Details"]=>
        array(2) {
          [0]=>
          array(4) {
            ["Approver"]=>
            array(1) {
              ["UserId"]=>
              string(12) "WangXiaoMing"
            }
            ["Speech"]=>
            string(0) ""
            ["SpStatus"]=>
            string(1) "1"
            ["SpTime"]=>
            string(1) "0"
          }
          [1]=>
          array(4) {
            ["Approver"]=>
            array(1) {
              ["UserId"]=>
              string(13) "XiaoGangHuang"
            }
            ["Speech"]=>
            string(0) ""
            ["SpStatus"]=>
            string(1) "1"
            ["SpTime"]=>
            string(1) "0"
          }
        }
      }
      [1]=>
      array(3) {
        ["SpStatus"]=>
        string(1) "1"
        ["ApproverAttr"]=>
        string(1) "1"
        ["Details"]=>
        array(4) {
          ["Approver"]=>
          array(1) {
            ["UserId"]=>
            string(11) "XiaoHongLiu"
          }
          ["Speech"]=>
          string(0) ""
          ["SpStatus"]=>
          string(1) "1"
          ["SpTime"]=>
          string(1) "0"
        }
      }
    }
    ["Notifyer"]=>
    array(1) {
      ["UserId"]=>
      string(10) "ChengLiang"
    }
    ["Comments"]=>
    array(4) {
      ["CommentUserInfo"]=>
      array(1) {
        ["UserId"]=>
        string(6) "LiuZhi"
      }
      ["CommentTime"]=>
      string(10) "1571732272"
      ["CommentContent"]=>
      string(18) "这是一个备注"
      ["CommentId"]=>
      string(19) "6750538708562308220"
    }
    ["StatuChangeEvent"]=>
    string(2) "10"
  }
}
overtrue commented 1 year ago

@TheNorthMemory 把这个代码抽个包出来吧,我来用你的

TheNorthMemory commented 1 year ago

@TheNorthMemory 把这个代码抽个包出来吧,我来用你的

行嘛,周末我捋一下

JoeZing commented 1 year ago

大佬们V5

TheNorthMemory commented 1 year ago

@TheNorthMemory 把这个代码抽个包出来吧,我来用你的

切到了 https://github.com/TheNorthMemory/xml

overtrue commented 1 year ago

https://github.com/w7corp/easywechat/releases/tag/6.8.0

overtrue commented 1 year ago

@JoeZing

JoeZing commented 1 year ago

Nice 👍🏼