WechatNotify.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 TYPE_ORDER = 'order';
  12. const TYPE_REFUND = 'refund';
  13. const TYPE_WITHDRAW = 'withdraw';
  14. const TYPE_MESSAGE = 'message';
  15. const TYPE_COMPLIANT = 'compliant';
  16. const TYPE_RECHARGE = 'recharge';
  17. const TYPE_COMMENT = 'comment';
  18. const TYPE_ERROR = 'error';
  19. public static function push(string $type, string $key, string $desc = '')
  20. {
  21. $inst = new self();
  22. return $inst->put([
  23. 'type' => $type,
  24. 'key' => $key,
  25. 'desc' => $desc,
  26. ]);
  27. }
  28. public function getCmd():string
  29. {
  30. return 'wechat';
  31. }
  32. /**
  33. * Pop task body from beanstalk and deliver to wechat robot.
  34. * @task: array
  35. * @return boolean
  36. */
  37. protected function _exec(array $params)
  38. {
  39. $supported_types = [
  40. self::TYPE_REFUND => '退款',
  41. self::TYPE_ORDER => '订单',
  42. self::TYPE_COMPLIANT => '反馈建议',
  43. self::TYPE_WITHDRAW => '提现请求',
  44. self::TYPE_MESSAGE => '客服消息',
  45. self::TYPE_RECHARGE => '充值',
  46. self::TYPE_COMMENT => '商品评论',
  47. self::TYPE_ERROR => '故障',
  48. ];
  49. if (!isset($params['type']) || !is_string($params['type'])) {
  50. Log::error('invalid type: bad format.');
  51. return false;
  52. }
  53. $type = $params['type'];
  54. $key = isset($params['key']) ? $params['key'] : '';
  55. if (!isset($supported_types[$type])) {
  56. Log::error('unsupported type:' . $type);
  57. return false;
  58. }
  59. $desc = isset($params['desc']) ? $params['desc'] : '';
  60. $msg = $supported_types[$type];
  61. $md = "### 新" . $msg . "\n "
  62. . "> 描述:" . $desc;
  63. QyWeixinService::instance()->key($key)
  64. ->markdown($md)->post();
  65. return true;
  66. }//
  67. }