ErrorMessage.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\wechat;
  12. use crmeb\utils\ApiErrorCode;
  13. /**
  14. * 错误消息处理
  15. * Class ErrorMessage
  16. * @package crmeb\services\wechat
  17. */
  18. class ErrorMessage
  19. {
  20. const MSG_CODE = [
  21. '1' => '未创建直播间',
  22. '1003' => '商品id不存在',
  23. '47001' => '入参格式不符合规范',
  24. '200002' => '入参错误',
  25. '300001' => '禁止创建/更新商品 或 禁止编辑&更新房间',
  26. '300002' => '名称长度不符合规则',
  27. '300006' => '图片上传失败',
  28. '300022' => '此房间号不存在',
  29. '300023' => '房间状态 拦截',
  30. '300024' => '商品不存在',
  31. '300025' => '商品审核未通过',
  32. '300026' => '房间商品数量已经满额',
  33. '300027' => '导入商品失败',
  34. '300028' => '房间名称违规',
  35. '300029' => '主播昵称违规',
  36. '300030' => '主播微信号不合法',
  37. '300031' => '直播间封面图不合规',
  38. '300032' => '直播间分享图违规',
  39. '300033' => '添加商品超过直播间上限',
  40. '300034' => '主播微信昵称长度不符合要求',
  41. '300035' => '主播微信号不存在',
  42. '300036' => '主播微信号未实名认证',
  43. '300037' => '购物直播频道封面图不合规',
  44. '300038' => '未在小程序管理后台配置客服',
  45. '9410000' => '直播间列表为空',
  46. '9410001' => '获取房间失败',
  47. '9410002' => '获取商品失败',
  48. '9410003' => '获取回放失败',
  49. '300001' => '禁止创建/更新商品(如:商品创建功能被封禁)',
  50. '300002' => '名称长度不符合规则',
  51. '300003' => '价格输入不合规',
  52. '300004' => '商品名称存在违规违法内容',
  53. '300005' => '商品图片存在违规违法内容',
  54. '300007' => '线上小程序版本不存在该链接',
  55. '300008' => '添加商品失败',
  56. '300009' => '商品审核撤回失败',
  57. '300010' => '商品审核状态不对',
  58. '300011' => '操作非法',
  59. '300012' => '没有提审额度',
  60. '300013' => '提审失败',
  61. '300014' => '审核中,无法删除',
  62. '300017' => '商品未提审',
  63. '300018' => '图片尺寸不符合要求',
  64. '300021' => '商品添加成功,审核失败',
  65. '40001' => 'AppSecret错误或者AppSecret不属于这个小程序,请确认AppSecret 的正确性',
  66. '40002' => '请确保grant_type字段值为client_credential',
  67. '40013' => '不合法的AppID,请检查AppID的正确性,避免异常字符,注意大小写',
  68. '40125' => '小程序配置无效,请检查配置',
  69. '40164' => 'IP地址不在白名单中,请检查设置',
  70. '41002' => '缺少appid参数',
  71. '41004' => '缺少secret参数',
  72. '43104' => 'appid与openid不匹配',
  73. '48001' => '微信接口暂无权限,请先去获取',
  74. '-1' => '系统错误',
  75. ];
  76. const WORK_ERROR_MESSAGE = [
  77. 40098 => '成员尚未实名认证',
  78. 40068 => '不合法的标签/标签组ID'
  79. ];
  80. /**
  81. * 处理返回错误信息友好提示
  82. * @param string $message
  83. * @return array|mixed|string
  84. */
  85. public static function getMessage(string $message)
  86. {
  87. if (strstr($message, 'Request AccessToken fail') !== false || strstr($message, 'Request access_token fail') !== false) {
  88. $message = str_replace('Request AccessToken fail. response:', '', $message);
  89. $message = str_replace('Request access_token fail:', '', $message);
  90. $message = trim($message);
  91. $message = json_decode($message, true) ?: [];
  92. $errcode = $message['errcode'] ?? false;
  93. if ($errcode) {
  94. $message = ApiErrorCode::ERROR_WECHAT_MESSAGE[$errcode] ?? $message;
  95. }
  96. }
  97. return $message;
  98. }
  99. /**
  100. * 解析错误
  101. * @param \Throwable $e
  102. * @return array|mixed|string
  103. */
  104. public static function getValidMessgae(\Throwable $e)
  105. {
  106. $message = '';
  107. if (!isset(self::MSG_CODE[$e->getCode()]) && (strstr($e->getMessage(), 'Request AccessToken fail') !== false || strstr($e->getMessage(), 'Request access_token fail') !== false)) {
  108. $message = str_replace('Request AccessToken fail. response:', '', $e->getMessage());
  109. $message = str_replace('Request access_token fail:', '', $message);
  110. $message = trim($message);
  111. $message = json_decode($message, true) ?: [];
  112. $errcode = $message['errcode'] ?? false;
  113. if ($errcode) {
  114. $message = self::MSG_CODE[$errcode] ?? $message;
  115. }
  116. }
  117. return $message ? $message : self::MSG_CODE[$e->getCode()] ?? $e->getMessage();
  118. }
  119. /**
  120. * 获取企业微信错误提示
  121. * @param int $errcode
  122. * @param string|null $message
  123. * @return string|null
  124. */
  125. public static function getWorkMessage(int $errcode, string $message = null)
  126. {
  127. if (isset(self::WORK_ERROR_MESSAGE[$errcode])) {
  128. return self::WORK_ERROR_MESSAGE[$errcode];
  129. } else {
  130. return $message;
  131. }
  132. }
  133. }