1、在请求微信接口时,返回码:45028,返回的英文信息:has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c。如图1
{ "errcode": 45028, "errmsg": "has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c" }
2、代码实现如下
$responseData = $wxServiceApi::wxMessageSendAll($requestData); if (!isset($responseData['msg_id'])) { $errcode = array_key_exists('errcode',$responseData)?$responseData['errcode']:205100; throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $responseData['errmsg']])), 205100); }
3、计划将错误信息:has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c 翻译为:没有群发配额。rid: 6103df2e-1505ba73-0eb14e1c。如图2
4、添加文件 common/messages/en-US/wx.php、common/messages/zh-CN/wx.php、wx/messages/en-US/wx.php、wx/messages/zh-CN/wx.php。如图3
common/messages/en-US/wx.php
<?php return [ ];
common/messages/zh-CN/wx.php
<?php return [ 'has no masssend quota' => '没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。', 'invalid content' => '无效内容(内容中包含音频)。', ];
wx/messages/en-US/wx.php
<?php $commonMessages = require __DIR__ . '/../../../common/messages/en-US/wx.php'; $messages = [ ]; return $commonMessages + $messages;
wx/messages/zh-CN/wx.php
<?php $commonMessages = require __DIR__ . '/../../../common/messages/zh-CN/wx.php'; $messages = [ ]; return $commonMessages + $messages;
5、代码实现如下。其中代码:substr($responseData[‘errmsg’], 0, $pos) 的结果为:has no masssend quota。其中代码:substr($responseData[‘errmsg’], $pos) 的结果为: rid: 6103df2e-1505ba73-0eb14e1c。将:has no masssend quota 翻译为:没有群发配额。后,再拼接上: rid: 6103df2e-1505ba73-0eb14e1c。
$responseData = $wxServiceApi::wxMessageSendAll($requestData); if (!isset($responseData['msg_id'])) { $errcode = $responseData['errcode'] ?? 205100; $pos = strpos($responseData['errmsg'], ' rid:'); $errorMsg = Yii::t('wx', substr($responseData['errmsg'], 0, $pos)) . substr($responseData['errmsg'], $pos); throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $errorMsg])), 205100); }
6、针对返回的英文信息:invalid content hint: [pzRHda0286d228] rid: 61041ffe-5cefd269-6c9611ff,完善后的代码如下。根源在于源语言的消息不支持模糊匹配。
$responseData = $wxServiceApi::wxMessageSendAll($requestData); if (!isset($responseData['msg_id'])) { $errcode = $responseData['errcode'] ?? 205100; $errorMsg = ''; $pos = strpos($responseData['errmsg'], ' hint:'); if ($pos === false) { $pos = strpos($responseData['errmsg'], ' rid:'); } if ($pos !== false) { $errorMsg = Yii::t('wx', substr($responseData['errmsg'], 0, $pos)) . substr($responseData['errmsg'], $pos); } throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $errorMsg])), 205100); }
7、测试翻译结果,has no masssend quota rid: 610a50c5-26966176-7cceb46e 已经被翻译为:没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。rid: 610a50c5-26966176-7cceb46e。符合预期。如图4
8、测试翻译结果,invalid content hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278 已经被翻译为:无效内容(内容中包含音频)。 hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278。符合预期。如图5
9、由于 invalid image size 还未被添加至语言包中,因此:invalid image size hint: [cl_6Oa0457mp13] rid: 6103c6d0-481a4bed-73e7120a 保持原样。符合预期。
<?php return [ 'has no masssend quota' => '没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。', 'invalid content' => '无效内容(内容中包含音频)。', 'invalid image size' => '图片尺寸太大(大小必须在10MB以下)。', ];
11、测试翻译结果,invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755 已经被翻译为:invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755。蓝框中是翻译前的英文信息,红框中是翻译后的中文信息。符合预期。如图6
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250604.html