WechatNotify.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace crmeb\services\async;
  3. use crmeb\utils\Redis;
  4. use crmeb\services\QyWeixinService;
  5. use think\facade\Config;
  6. /**
  7. * 异步机器人通知.
  8. * 消息格式:
  9. * {"type":"type_name", "key":"1234", "desc":{}}
  10. * Class WechatNotify
  11. */
  12. class WechatNotify
  13. {
  14. public static $TYPE_ORDER = 'order';
  15. public static $TYPE_REFUND = 'refund';
  16. public static $TYPE_WITHDRAW = 'withdraw';
  17. public static $TYPE_MESSAGE = 'message';
  18. public static $TYPE_COMPLIANT = 'compliant';
  19. public static $TYPE_RECHARGE = 'recharge';
  20. public static $TYPE_COMMENT = 'comment';
  21. public static function push(string $type, string $key, string $desc = '')
  22. {
  23. $r = Redis::instance();
  24. $rk = Config::get('app.redis_robot_msg_key');
  25. $arr = [
  26. 'type' => $type,
  27. ];
  28. if ($key) {
  29. $arr['key'] = $key;
  30. }
  31. if ($desc) {
  32. $arr['desc'] = $desc;
  33. }
  34. $json = json_encode($arr);
  35. $r->lpush($rk, $json);
  36. }
  37. /**
  38. * this function should be called in timer process.
  39. */
  40. public static function notify()
  41. {
  42. $r = Redis::instance();
  43. $k = Config::get('app.redis_robot_msg_key');
  44. $aristotle = Config::get('app.qy_weixin_robot_aristotle');
  45. $supported_types = [
  46. 'refund' => '退款',
  47. 'order' => '订单',
  48. 'compliant' => '反馈建议',
  49. 'withdraw' => '提现请求',
  50. 'message' => '客服消息',
  51. 'recharge' => '充值',
  52. 'comment' => '商品评论',
  53. ];
  54. $total = $r->llen($k);
  55. while ($total > 0) {
  56. $json = $r->rpop($k);
  57. $arr = json_decode($json, true);
  58. if (!isset($arr['type']) || !is_string($arr['type'])) {
  59. continue;
  60. }
  61. $type = $arr['type'];
  62. $key = isset($arr['key']) ? $arr['key'] : '';
  63. if (!isset($supported_types[$type])) {
  64. continue;
  65. }
  66. $desc = isset($arr['desc']) ? $arr['desc'] : '';
  67. $msg = $supported_types[$type];
  68. $md = "### 新" . $msg . "\n>key: " . $key
  69. . "\n备注:" . $desc;
  70. QyWeixinService::instance()->key($aristotle)
  71. ->markdown($md)->post();
  72. $total = $r->llen($k);
  73. }// while
  74. }//
  75. }