WechatNotify.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace crmeb\services\async\task;
  3. use crmeb\services\QyWeixinService;
  4. use think\facade\Log;
  5. /**
  6. * 异步机器人通知.
  7. * Class WechatNotify
  8. */
  9. class WechatNotify extends Task
  10. {
  11. const CMD = 'wechat';
  12. const TYPE_ORDER = 'order';
  13. const TYPE_REFUND = 'refund';
  14. const TYPE_WITHDRAW = 'withdraw';
  15. const TYPE_MESSAGE = 'message';
  16. const TYPE_COMPLIANT = 'compliant';
  17. const TYPE_RECHARGE = 'recharge';
  18. const TYPE_COMMENT = 'comment';
  19. const TYPE_ERROR = 'error';
  20. public static function push(string $type, string $key, string $desc = '')
  21. {
  22. self::put([
  23. 'type' => $type,
  24. 'key' => $key,
  25. 'desc' => $desc,
  26. ]);
  27. }
  28. protected static function getCmd()
  29. {
  30. return self::CMD;
  31. }
  32. /**
  33. * Pop task body from beanstalk and deliver to wechat robot.
  34. * @task: array
  35. * @return boolean
  36. */
  37. public static function exec(array $task)
  38. {
  39. if (!self::checkTask($task)) {
  40. return false;
  41. }
  42. // must has value
  43. $params = $task['params'];
  44. $supported_types = [
  45. self::TYPE_REFUND => '退款',
  46. self::TYPE_ORDER => '订单',
  47. self::TYPE_COMPLIANT => '反馈建议',
  48. self::TYPE_WITHDRAW => '提现请求',
  49. self::TYPE_MESSAGE => '客服消息',
  50. self::TYPE_RECHARGE => '充值',
  51. self::TYPE_COMMENT => '商品评论',
  52. self::TYPE_ERROR => '故障',
  53. ];
  54. if (!isset($params['type']) || !is_string($params['type'])) {
  55. Log::error('invalid type: bad format.');
  56. return false;
  57. }
  58. $type = $params['type'];
  59. $key = isset($params['key']) ? $params['key'] : '';
  60. if (!isset($supported_types[$type])) {
  61. Log::error('unsupported type:' . $type);
  62. return false;
  63. }
  64. $desc = isset($params['desc']) ? $params['desc'] : '';
  65. $msg = $supported_types[$type];
  66. $md = "### 新" . $msg . "\n "
  67. . "> 描述:" . $desc;
  68. QyWeixinService::instance()->key($key)
  69. ->markdown($md)->post();
  70. return true;
  71. }//
  72. }