WechatNotify.php 2.1 KB

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