123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace app\api\controller;
- use app\admin\model\SendChannelMessage;
- use app\common\controller\Api;
- use app\common\library\WeChatObject;
- use app\common\model\AdminConfig;
- use app\main\service\LogService;
- use think\Db;
- use think\Exception;
- use think\Log;
- /**
- * 高级群发 相关接口
- */
- class Sendmessage extends Api
- {
- //如果$noNeedLogin为空表示所有接口都需要登录才能请求
- //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
- //如果接口已经设置无需登录,那也就无需鉴权了
- //
- // 无需登录的接口,*表示全部
- protected $noNeedLogin = ['handwxmsgid,handdeltags,handlisttags'];
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = ['handwxmsgid,handdeltags,handlisttags'];
- /**
- * @param $id sendchannelMessage 表中的自增ID
- * @param $wx_msg_id 消息发送任务的ID(微信发送群消息后返回的id)
- * JAVA 发送群消息后,根据id 维护 wx_msg_id
- */
- public function handWxMsgId()
- {
- $sendChannelMessageModel = new SendChannelMessage();
- $params = input();
- LogService::info('[ SendMessage ] [ handWxMsgId ] [ Params ]:' . json_encode($params));
- $id = $params['id'] ?? '';
- $wx_msg_id = $params['wx_msg_id'] ?? '';
- if ($id && $wx_msg_id) {
- try {
- $sendChannelMessageModel->where('id', 'eq', $id)->update(['wx_msg_id' => $wx_msg_id]);
- $this->success('成功');
- } catch (Exception $exception) {
- LogService::error("[ SendMessage ] [ handWxMsgId ] " . $exception->getMessage());
- $this->error($exception->getMessage());
- }
- } else {
- $this->error('更新wx_msg_id失败');
- }
- }
- /**
- * 删除两天前的标签
- */
- public function handDelTags()
- {
- $sendChannelMessageModel = new SendChannelMessage();
- $timestamp = strtotime(date("Y-m-d", strtotime("-2 day")));
- $res = $sendChannelMessageModel->field('distinct channel_id')->where('createtime', '<=', $timestamp)->select();
- if ($res) {
- foreach ($res as $k => $item) {
- $channel_id = $item['channel_id'];
- $adminConfig = new AdminConfig();
- $adminInfo = $adminConfig->getAdminInfoAll($channel_id);
- try {
- $wechat = new WeChatObject($adminInfo);
- $officialAccount = $wechat->getOfficialAccount();
- if (!empty($officialAccount) && $officialAccount->user_tag) {
- $tagslist = $officialAccount->user_tag->list();
- if (!empty($tagslist['tags'])) {
- foreach ($tagslist['tags'] as $k => $tag) {
- // 找到匹配的tag
- if (preg_match('/^h_job_/', trim($tag['name']))) {
- // 解析tag中的日期,比较大小
- $tag_arr = explode('_', $tag['name']);
- if (!empty($tag_arr[2]) && $tag_arr[2] < date('Ymd', $timestamp)) {
- $res = $officialAccount->user_tag->delete($tag['id']);
- echo 'Success:' . $res;
- }
- }
- }
- }
- }
- } catch (\Exception $exception) {
- echo $exception->getMessage();
- LogService::error($exception->getMessage());
- }
- }
- }
- }
- /**
- * 查看渠道下的所有Tag标签
- * @param $channel_id
- * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function handListTags($channel_id)
- {
- $adminConfig = new AdminConfig();
- $adminInfo = $adminConfig->getAdminInfoAll($channel_id);
- $wechat = new WeChatObject($adminInfo);
- $officialAccount = $wechat->getOfficialAccount();
- $list = $officialAccount->user_tag->list();
- return $list;
- }
- }
|