WxxcxPush.php 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2022/06/13 05:55
 * Time: 05:55
 */

namespace app\api\controller\v1;

use app\admin\model\tytcm\Reservation;
use app\api\controller\Common;
use think\Db;
use think\exception\PDOException;
use think\Request;

/**
 * 微信小程序订阅消息推送
 * @package app\api\controller\v1
 */
class WxxcxPush extends Base
{
    /**
     * 1.提醒社区管理员处理新的入州申请,发送订阅消息
     */
    public function sendDealApplyMessage($openid_arr, $apply_info)
    {
        if (empty($openid_arr)) {
            return array_callback(false, "未找到需要推送的人员");
        }
        $sum_count = 0;//总共需要推送的人
        $react_count = 0;//实际推送人数
        if (!empty($openid_arr)) {
            $sum_count = count($openid_arr);//总共需要推送的人数
            //通知
            $page = 'pages/admin_list/detail?apply_id=' . $apply_info['id'];

            $value1 = "入州报备审核";

            if ($apply_info['is_transfer'] == 1) {
                //判断是否是转办
                $value1 = "入州报备审核【转办】";
            } elseif ($apply_info['user_reapply'] == 1) {
                //判断是否是用户更新申报
                $value1 = "入州报备审核【用户更新】";
            }

            $temp = array(
                'value1' => $value1,
                'value2' => "待处理",
                'value3' => $apply_info['u_name'],
                'value4' => date("Y年m月d日 H:i", $apply_info['createtime']),
                'value5' => "点击立即处理",
            );
            // 启动事务
            Db::startTrans();
            try {
                $createtime = $apply_info['createtime'];
                foreach ($openid_arr as $uk => $uv) {
                    $user = [];
                    $insert_message_log_data = [];

                    $user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
                    $insert_message_log_data = [
                        "unionid" => $user['unionid'],
                        "wechat_xcx_openid" => $uv,
                        "corp_user_id" => 0,//发送人
                        "createtime" => $createtime,
                    ];
                    if (!empty($uv)) {
                        $res = beingPushed(5, $uv, $page, $temp);
//                        file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                        $res_arr = !empty($res) ? json_decode($res, true) : [];
                        if (!empty($res_arr) && $res_arr['errcode'] == "0") {
                            //推送成功
                            $react_count = $react_count + 1;
                        } else {
                            //推送失败,记录问题
                            $insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
                        }

                    } else {
                        $error = [];
                        $error = ["errcode" => "-1", "errmsg" => "社区管理员未授权小程序"];
                        $insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
                    }
                    //通知已读未读、推送日志记录起来
//                    Db::name("push_log")->insertGetId($insert_message_log_data);
                }

                // 执行提交操作
                Db::commit();
                return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);

            } catch (PDOException $e) {
                Db::rollback();
                return array_callback(false, "请求异常" . $e->getMessage());
            }

        } else {
            return array_callback(false, "未找到可通知的社区管理员!");
        }


    }

    /**
     * 2.提醒申请人员入州报备已处理完毕,发送订阅消息
     */
    public function sendDealSuccessMessage($openid_arr, $deal_info)
    {
        if (empty($openid_arr)) {
            return array_callback(false, "未找到需要推送的人员");
        }
        $sum_count = 0;//总共需要推送的人
        $react_count = 0;//实际推送人数

        $apply_model = new \app\admin\model\AdmissionStateApply;
        $type_stu = Db::name("admission_state_apply")->where("id", $deal_info['id'])->value("type_stu");
        $type_stu_text_arr = $apply_model->getIncludeOldStatuTypeStuList();//处理结果['0'=> '正常','1'=> '健康监测','2'=> '已解除隔离','3'=> '高风险7天集中隔离5次核酸','4'=> '中风险7天居家3次核酸','5'=> '低风险3天2检']
        $type_stu_text = $type_stu_text_arr[$type_stu];
        $tips_text =  $type_stu_text;//"结果:" .

        if (!empty($openid_arr)) {
            $sum_count = count($openid_arr);//总共需要推送的人数
            //通知
            $page = 'pages/declare/detail?apply_id=' . $deal_info['id'];
            $temp = array(
                'value1' => "已审核(" . date("m-d H:i", $deal_info['deal_time']) . ")",
                'value2' => $deal_info['mng_user_name'],
                'value3' => sub_str($tips_text, 20, false)//date("Y-m-d H:i",$deal_info['deal_time'])
            );
            // 启动事务
            Db::startTrans();
            try {
                $createtime = $deal_info['deal_time'];
                foreach ($openid_arr as $uk => $uv) {
                    $user = [];
                    $insert_message_log_data = [];

                    $user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
                    $insert_message_log_data = [
                        "unionid" => $user['unionid'],
                        "wechat_xcx_openid" => $uv,
                        "corp_user_id" => $deal_info['deal_mng_user_id'],//发送人
                        "createtime" => $createtime,
                    ];
                    if (!empty($uv)) {
                        $res = beingPushed(2, $uv, $page, $temp);
//                        file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "给用户推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                        $res_arr = !empty($res) ? json_decode($res, true) : [];
                        if (!empty($res_arr) && $res_arr['errcode'] == "0") {
                            //推送成功
                            $react_count = $react_count + 1;
                        } else {
                            //推送失败,记录问题
                            $insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
                        }

                    } else {
                        $error = [];
                        $error = ["errcode" => "-1", "errmsg" => "申报人未授权小程序"];
                        $insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
                    }
                    //通知已读未读、推送日志记录起来
//                    Db::name("push_log")->insertGetId($insert_message_log_data);
                }

                // 执行提交操作
                Db::commit();
                return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);

            } catch (PDOException $e) {
                Db::rollback();
                return array_callback(false, "请求异常" . $e->getMessage());
            }

        } else {
            return array_callback(false, "未找到可通知的申报用户!");
        }

    }

    /**
     * 3.提醒监测人员上传核酸报告,发送订阅消息
     */
    public function sendUploadReportMessage($openid_arr, $deal_info,$report)
    {
        if (empty($openid_arr)) {
            return array_callback(false, "未找到需要推送的人员");
        }
        $sum_count = 0;//总共需要推送的人
        $react_count = 0;//实际推送人数

        $apply_model = new \app\admin\model\AdmissionStateApply;
        $type_stu = Db::name("admission_state_apply")->where("id", $deal_info['id'])->value("type_stu");
        $type_stu_text_arr = $apply_model->getIncludeOldStatuTypeStuList();//处理结果['0'=> '正常','1'=> '健康监测','2'=> '已解除隔离','3'=> '高风险7天集中隔离5次核酸','4'=> '中风险7天居家3次核酸','5'=> '低风险3天2检']
        $type_stu_text = $type_stu_text_arr[$type_stu];
        $tips_text = $type_stu_text;

        if (!empty($openid_arr)) {
            $sum_count = count($openid_arr);//总共需要推送的人数
            //通知
            $page = 'pages/declare/detail?apply_id=' . $deal_info['id'];
            $temp = array(
                'value1' => "核酸报告上传提醒(".$report['report_num']."/".$report['need_report_num'].")",
                'value2' => $deal_info['mng_user_name'],
                'value3' => sub_str($tips_text, 20, false)//date("Y-m-d H:i",$deal_info['deal_time'])
            );
            // 启动事务
            Db::startTrans();
            try {
                foreach ($openid_arr as $uk => $uv) {
                    $user = [];
                    $insert_message_log_data = [];

                    $user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
                    $insert_message_log_data = [
                        "unionid" => $user['unionid'],
                        "wechat_xcx_openid" => $uv,
                        "corp_user_id" => $deal_info['deal_mng_user_id'],//发送人
                        "createtime" => time(),
                    ];
                    if (!empty($uv)) {
                        $res = beingPushed(2, $uv, $page, $temp);
//                        file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "给用户推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                        $res_arr = !empty($res) ? json_decode($res, true) : [];
                        if (!empty($res_arr) && $res_arr['errcode'] == "0") {
                            //推送成功
                            $react_count = $react_count + 1;
                        } else {
                            //推送失败,记录问题
                            $insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
                        }

                    } else {
                        $error = [];
                        $error = ["errcode" => "-1", "errmsg" => "申报人未授权小程序"];
                        $insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
                    }
                    //通知已读未读、推送日志记录起来
//                    Db::name("push_log")->insertGetId($insert_message_log_data);
                }

                // 执行提交操作
                Db::commit();
                return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);

            } catch (PDOException $e) {
                Db::rollback();
                return array_callback(false, "请求异常" . $e->getMessage());
            }

        } else {
            return array_callback(false, "未找到可通知的监测人员!");
        }

    }

    /**
     * 1.问政小程序发送工单订阅消息
     * $id 工单id
     */
    public function sendWorkorderMessage($id,$temp)
    {
        if (empty($id)) {
            return array_callback(false, "id不能为空");
        }
        $workorderInfo = Db::name("workorder_orders")->where("id", $id)->find();

        $sum_count = 0;//总共需要推送的人
        $react_count = 0;//实际推送人数
        if (!empty($workorderInfo)) {
            if (!empty($workorderInfo['user_id'])) {
                //找到用户
                $where["id"] = ["=", $workorderInfo['user_id']];
                $user = Db::name("user")->where($where)->order("id desc")->find();
                if (!empty($user) && !empty($user['wx_xcx_openid'])) {
                    $sum_count = 1;//总共需要推送的人数
                    //通知
                    $page = 'pages/personal/speed/pading'."?id=".$id;


                    // 启动事务
                    Db::startTrans();
                    try {
                        $createtime = time();
                        $insert_message_log_data = [
                            "unionid" => $user['unionid'],
                            "wechat_xcx_openid" => $user['wx_xcx_openid'],
                            "message_id" => $id,
                            "status" => "0",//状态:0=未读,1=已读
                            "createtime" => $createtime,
                            "type"=>"2",//类型 1通知 2网络问政工单进度
                        ];
                        if (!empty($user) && !empty($user['wx_xcx_openid'])) {
                            $res = beingPushed(1, $user['wx_xcx_openid'], $page, $temp);
                            file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . "《" . $id . "》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                            $res_arr = !empty($res) ? json_decode($res, true) : [];
                            if (!empty($res_arr) && $res_arr['errcode'] == "0") {
                                //推送成功
                                $react_count = $react_count + 1;
                            } else {
                                //推送失败,记录问题
                                $insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
                            }

                        } else {
                            $error = [];
                            $error = ["errcode" => "-1", "errmsg" => "提工单用户未授权小程序"];
                            $insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
                        }
                        //推送日志记录起来
                        Db::name("workorder_push_log")->insertGetId($insert_message_log_data);

                        // 执行提交操作
                        Db::commit();
                        return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);

                    } catch (PDOException $e) {
                        Db::rollback();
                        return array_callback(false, "请求异常" . $e->getMessage());
                    }

                } else {
                    return array_callback(false, "未找到可通知的成员!");
                }

            } else {
                return array_callback(false, "未找到推送人员");
            }


        } else {
            return array_callback(false, "未查找到有效的工单数据");
        }


    }

    public function sendMessage($data = [])
    {
        $res=[
            'appid'=>$this->AppID,
            'appsecret'=>$this->AppSecret,
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $res['appid'] . '&secret=' . $res['appsecret'];
        //json_encode 	对变量进行 JSON 编码     file_get_contents() 把整个文件读入一个字符串中。
        $res = json_decode(file_get_contents($url),true);
        $access_token = $res['access_token'] ;
        //请求url
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;
        return self::curlPost($url,json_encode($data)) ;
    }


//发送post请求 小程序发送一次性订阅消息
    static function curlPost($url,$data)
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    //小程序发送一次性订阅消息
    public function Message($openid){
        if(!empty($openid)){
            // 启动事务

                $createtime = time();

                //通知
                $page = '/pageOne/moreRes/moreRes';
                $temp='XKfTFd23w3dzzoRHPQajIq8MxQ2usw6n9W4FuDBZvh4';
                $insert_message_log_data = array(
                    'value1' => "消费卷名称",
                    'value2' => "缴费项目",
                    'value3' => "项目原价",
                    'value4' => "客户支付",
                    'value5' => "优惠号码",
                    'value6' => "实际费用",
                    'value7' => "会员详情请点击"
                );

                    $res = beingPushed(5, $openid, $page, $insert_message_log_data);

                file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . "" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                    $res_arr = !empty($res) ? json_decode($res, true) : [];
                    if (!empty($res_arr) && $res_arr['errcode'] == "0") {
                    } else {
                        //推送失败,记录问题
                        return array_callback(true, "推1送成功".json_encode($res_arr));
                        $insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
                    }



        }
    }
}