Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JoeZing opened this issue Sep 22, 2022 · 11 comments
Closed

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

JoeZing opened this issue Sep 22, 2022 · 11 comments

Comments

@JoeZing
Copy link

JoeZing commented Sep 22, 2022

我用的环境

  • PHP 版本:8.1.6
  • overtrue/wechat 版本:6.7.4
  • 是否使用了框架?框架名称:Laravel 9.2

业务流程

企业微信自建应用,业务流程发起审批单,用户在审批单审批后,企业微信发起消息通知到自建应用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
Copy link
Author

JoeZing commented Sep 22, 2022

问题已解决: #2593

@TheNorthMemory
Copy link

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

@JoeZing
Copy link
Author

JoeZing commented Sep 23, 2022

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

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

@TheNorthMemory
Copy link

TheNorthMemory commented Sep 23, 2022

问题已解决: #2593

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

if (is_array($object)) {
foreach ($object as $key => $value) {
$value = $value instanceof SimpleXMLElement ? self::normalize($value) : $value;
if ('@attributes' === $key) {
$result = $value; // @codeCoverageIgnore
} else {
$result[$key] = $value;
}
}
}

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

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
Copy link
Collaborator

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

@TheNorthMemory
Copy link

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

行嘛,周末我捋一下

@JoeZing
Copy link
Author

JoeZing commented Sep 23, 2022

大佬们V5

@TheNorthMemory
Copy link

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

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

@overtrue
Copy link
Collaborator

@overtrue
Copy link
Collaborator

@JoeZing

@JoeZing
Copy link
Author

JoeZing commented Sep 27, 2022

Nice 👍🏼

@JoeZing JoeZing closed this as completed Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants