Sendmessage.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\SendChannelMessage;
  4. use app\common\controller\Api;
  5. use app\common\library\WeChatObject;
  6. use app\common\model\AdminConfig;
  7. use app\main\service\LogService;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Log;
  11. /**
  12. * 高级群发 相关接口
  13. */
  14. class Sendmessage extends Api
  15. {
  16. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  17. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  18. //如果接口已经设置无需登录,那也就无需鉴权了
  19. //
  20. // 无需登录的接口,*表示全部
  21. protected $noNeedLogin = ['handwxmsgid,handdeltags,handlisttags'];
  22. // 无需鉴权的接口,*表示全部
  23. protected $noNeedRight = ['handwxmsgid,handdeltags,handlisttags'];
  24. /**
  25. * @param $id sendchannelMessage 表中的自增ID
  26. * @param $wx_msg_id 消息发送任务的ID(微信发送群消息后返回的id)
  27. * JAVA 发送群消息后,根据id 维护 wx_msg_id
  28. */
  29. public function handWxMsgId()
  30. {
  31. $sendChannelMessageModel = new SendChannelMessage();
  32. $params = input();
  33. LogService::info('[ SendMessage ] [ handWxMsgId ] [ Params ]:' . json_encode($params));
  34. $id = $params['id'] ?? '';
  35. $wx_msg_id = $params['wx_msg_id'] ?? '';
  36. if ($id && $wx_msg_id) {
  37. try {
  38. $sendChannelMessageModel->where('id', 'eq', $id)->update(['wx_msg_id' => $wx_msg_id]);
  39. $this->success('成功');
  40. } catch (Exception $exception) {
  41. LogService::error("[ SendMessage ] [ handWxMsgId ] " . $exception->getMessage());
  42. $this->error($exception->getMessage());
  43. }
  44. } else {
  45. $this->error('更新wx_msg_id失败');
  46. }
  47. }
  48. /**
  49. * 删除两天前的标签
  50. */
  51. public function handDelTags()
  52. {
  53. $sendChannelMessageModel = new SendChannelMessage();
  54. $timestamp = strtotime(date("Y-m-d", strtotime("-2 day")));
  55. $res = $sendChannelMessageModel->field('distinct channel_id')->where('createtime', '<=', $timestamp)->select();
  56. if ($res) {
  57. foreach ($res as $k => $item) {
  58. $channel_id = $item['channel_id'];
  59. $adminConfig = new AdminConfig();
  60. $adminInfo = $adminConfig->getAdminInfoAll($channel_id);
  61. try {
  62. $wechat = new WeChatObject($adminInfo);
  63. $officialAccount = $wechat->getOfficialAccount();
  64. if (!empty($officialAccount) && $officialAccount->user_tag) {
  65. $tagslist = $officialAccount->user_tag->list();
  66. if (!empty($tagslist['tags'])) {
  67. foreach ($tagslist['tags'] as $k => $tag) {
  68. // 找到匹配的tag
  69. if (preg_match('/^h_job_/', trim($tag['name']))) {
  70. // 解析tag中的日期,比较大小
  71. $tag_arr = explode('_', $tag['name']);
  72. if (!empty($tag_arr[2]) && $tag_arr[2] < date('Ymd', $timestamp)) {
  73. $res = $officialAccount->user_tag->delete($tag['id']);
  74. echo 'Success:' . $res;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. } catch (\Exception $exception) {
  81. echo $exception->getMessage();
  82. LogService::error($exception->getMessage());
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * 查看渠道下的所有Tag标签
  89. * @param $channel_id
  90. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  91. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  92. */
  93. public function handListTags($channel_id)
  94. {
  95. $adminConfig = new AdminConfig();
  96. $adminInfo = $adminConfig->getAdminInfoAll($channel_id);
  97. $wechat = new WeChatObject($adminInfo);
  98. $officialAccount = $wechat->getOfficialAccount();
  99. $list = $officialAccount->user_tag->list();
  100. return $list;
  101. }
  102. }