1, 'lite' => 2, 'mini' => 3 ]; private $mini; public function catch($action) { $id = $this->client[$action]; $this->mini = Wechat::where([['id', '=', $id]])->find(); $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = $this->mini->token; $encodingAesKey = $this->mini->encodingAesKey; $appId = $this->mini->appid; $eventObj = new Event($token, $encodingAesKey, $appId); // token验证 if (isset($_GET["echostr"])) { if ($eventObj->checkToken($token, $signature, $timestamp, $nonce)) { return $_GET["echostr"]; } } $this->index($eventObj); } private function index(Event &$eventObj) { $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; // // 消息体加密 $msgSignature = $_GET['msg_signature']; // 推送消息内容获取 $postXml = file_get_contents('php://input'); trace('消息内容:' . $postXml, "debug"); $rs = $eventObj->decryptMsg($msgSignature, $timestamp, $nonce, $postXml, $msg); trace('消息内容:' . $msg, "debug"); if ($rs !== 0) { trace('消息内容解析失败:' . $postXml, "error"); return 'success'; } $xml = new DOMDocument(); $xml->loadXML($msg); $msgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue; $rs = $this->$msgType($xml); if ($rs === null) { trace($msg, 'log'); return 'success'; } $rs = json_decode($rs, true); if ($rs['errcode'] == 0) { trace($rs, 'log'); return 'success'; } trace($rs, 'error'); } /** * 文本消息 */ public function text($xml) { $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; $content = $xml->getElementsByTagName('Content')->item(0)->nodeValue; $rs = null; switch ($content) { case '关注公众号': // 关注公众号 $rs = $this->officialImg($fromUserName); break; case '分享链接': // 分享链接 $rs = $this->shareLink($fromUserName); break; } return $rs; } /** * 图片消息 */ public function image($xml) { } /** * 小程序卡消息回复 */ public function miniprogrampage($xml) { $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; $title = $xml->getElementsByTagName('Title')->item(0)->nodeValue; $rs = null; switch ($title) { case '关注公众号': // 关注公众号 $rs = $this->officialImg($fromUserName); break; case '获取分享链接': // 分享链接 $rs = $this->shareLink($fromUserName, 'h5'); break; } return $rs; } /** * 事件消息(用户进入客服事件) * @param DOMDocument $xml 消息内容,DOMDocument对象 * @return string|null 返回消息发送结果,未发送返回null */ public function event($xml) { // 获取事件发生人 $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; // 事件类型 $event = $xml->getElementsByTagName('Event')->item(0)->nodeValue; // 消息最后一次发送日期 (null, 今天以前,今天) $lastSendData = cache("${fromUserName}_${event}"); $now = date('Y-m-d'); // 如果今天发送过, 则不发送 if ($now == $lastSendData) return null; // 更新最后发送时间 cache("${fromUserName}_${event}", $now); // 消息内容 $msg = "欢迎使用营销数字化系统!!!"; $data = [ 'touser' => $fromUserName, 'msgtype' => 'text', 'text' => ['content' => $msg] ]; // 调用消息发送类,返回消息发送结果 return (new CustomerServiceMessage)->send($this->mini->accesstoken, $data); } /** * 发送公众号图片 */ private function officialImg($openid) { $serviceMsgObj = new CustomerServiceMessage(); // 素材上传 if (!$mediaId = Cache::get('zqxg_official_img_media_id')) { // 上传文件 $data = [ 'media' => curl_file_create(realpath($_SERVER['DOCUMENT_ROOT'] . '/static/img/official.jpg')) ]; $rs = $serviceMsgObj->uploadTempMedia($this->mini->accesstoken, $data); trace($rs, 'log'); $rs = json_decode($rs, true); if (isset($rs['errcode']) && isset($rs['errmsg']) && $rs['errcode'] != 0 && $rs['errmsg'] != 'ok') { trace($rs, 'error'); return; } $mediaId = $rs['media_id']; Cache::set('zqxg_official_img_media_id', $rs['media_id'], 86400 * 3); } $data = [ 'touser' => $openid, 'msgtype' => 'image', 'image' => ['media_id' => $mediaId] ]; return $serviceMsgObj->send($this->mini->accesstoken, $data); } /** * 发送分享链接 */ private function shareLink($openid, $type = "mini") { // 获取用户id $userIds = User::where('mini_openid', $openid)->column('id'); // 获取员工 $employeeIds = Employee::where([['uid', 'in', $userIds], ['state', '=', '在职']])->column('id'); // 获取经纪人 $agentIds = AgentUser::where([['uid', 'in', $userIds]])->column('id'); if (empty($employeeIds) && empty($agentIds)) { trace('员工或者经纪人不存在,无法获取链接', 'error'); return; } // 查找用户要分享的内容编号 if (!empty($employeeIds) && empty($agentIds)) { $share = ShareLog::where([['employee_id', 'in', $employeeIds], ['agent_id', '=', null]])->order('share_time desc')->find(); //$share = ShareLog::where([['employee_id', 'in', $employeeIds]])->order('share_time desc')->find(); } elseif(!empty($employeeIds) && !empty($agentIds)) { $where1 = [ ['employee_id', 'in', $employeeIds], ['agent_id', '=', null] ]; $where2 = [ ['agent_id', 'in', $agentIds] ]; $share = ShareLog::whereOr([$where1, $where2])->order('share_time desc')->find(); } else { $share = ShareLog::where([['agent_id', 'in', $agentIds]])->order('share_time desc')->find(); } if (empty($share)) { trace('分享内容获取失败', 'error'); return; } $strpos = strpos($share->type, '_vr'); if ($strpos) { $class = 'app\\model\\' . str_replace('_vr', '', $share->type); $share_type = str_replace('_vr', '', $share->type); if ($share_type == 'Company') $class = 'app\\model\\CompanyVrshow'; if ($share_type == 'Housetype') { $class = 'app\\model\\BuildingHousetype'; $share->type = $share_type = 'BuildingHousetype'; } } elseif($share->type == 'housetype' || $share->type == 'Housetype'){ $class = 'app\\model\\BuildingHousetype' ; $share->type = $share_type = 'BuildingHousetype'; } else { $class = 'app\\model\\' . $share->type; $share_type = $share->type; } if (!class_exists($class)) { trace('分享类不存在', 'error'); return; } $obj = new $class(); $data = $obj->find($share->data_id); if ($share_type == 'Construction' || $share_type == 'Building') { $data['title'] = $data['name']; } if ($share_type == 'BuildingHousetype'){ $data['title'] = $data['room'] . '室' . $data['hall'] . '厅' . $data['bathroom'] . '卫'; } if($share_type == 'Company'){ $company = Company::where([['root_id','=',$data['root_id']]])->field('company_name,logo')->find(); $data['title'] = $data['name'].'展厅全景VR'; $data['logo'] = $company['logo']; } if($share_type == 'BuildingProgress') $data['title'] = $data['name']; $imgUrlParam = [ 'Video' => [ 'param' => 'video_url', 'ext' => '?x-oss-process=video/snapshot,t_100,f_jpg,w_640,h_320,m_fast' ], 'MaterialCase' => [ 'param' => 'cover_img', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'MaterialEvidence' => [ 'param' => 'pics', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'Activity' => [ 'param' => 'cover', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'Article' => [ 'param' => 'cover_img', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'Construction' => [ 'param' => 'cover', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'Building' => [ 'param' => 'cover', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'CompanyStrength' => [ 'param' => 'pics', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'AgentArticle' => [ 'param' => 'file', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'BuildingHousetype' =>[ 'param' => 'house_img', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'Company' => [ 'param' => 'logo', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ], 'BuildingProgress' => [ 'param' => 'img', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ] ]; //口碑中有图片和视频区分 if ($share_type == 'MaterialEvidence') { $info = MaterialEvidence::where('id', $share->data_id)->find(); if ($info->difference == 1) { if ($info->cover) { //如果封面图存在 $imgUrlParam['MaterialEvidence'] = [ 'param' => 'cover', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ]; } else { //封面图不存在 $imgUrlParam['MaterialEvidence'] = [ 'param' => 'pics', 'ext' => '?x-oss-process=video/snapshot,t_100,f_jpg,w_640,h_320,m_fast' ]; } } } elseif ($share_type == 'CompanyStrength') { $info = CompanyStrengthModel::where('id', $share->data_id)->find(); if ($info->difference == 1) { if ($info->cover) { $imgUrlParam['CompanyStrength'] = [ 'param' => 'cover', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ]; } else { $imgUrlParam['CompanyStrength'] = [ 'param' => 'pics', 'ext' => '?x-oss-process=video/snapshot,t_100,f_jpg,w_640,h_320,m_fast' ]; } } } elseif ($share_type == 'AgentArticle') { //经纪人文章会出现多图或者多视频,type:1视频type2图片 $info = AgentArticle::where('id', $share->data_id)->find(); if ($info->file) { if ($info->type == 2) { $imgUrlParam['AgentArticle'] = [ 'param' => 'file', 'ext' => '?x-oss-process=image/resize,m_fill,h_320,w_640' ]; } else { $imgUrlParam['AgentArticle'] = [ 'param' => 'file', 'ext' => '?x-oss-process=video/snapshot,t_100,f_jpg,w_640,h_320,m_fast' ]; } } } if (is_array($data[$imgUrlParam[$share_type]['param']])) { $thumb = $data[$imgUrlParam[$share_type]['param']][0] . $imgUrlParam[$share_type]['ext']; } else { $thumb = $data[$imgUrlParam[$share_type]['param']] . $imgUrlParam[$share_type]['ext']; } $emp = Employee::where('id', $share->employee_id)->find(); trace('312行', 'debug'); $clientType = Miniprogram::where('root_id', $emp->root_id)->value('notify'); trace('314行', 'debug'); // $v = [ // 'type' => $share->type, // 'id' => $data->id, // 'employee_id' => $share->employee_id // ]; // $root_id = Employee::where('id', $share->employee_id)->value('root_id'); // $v = http_build_query($v); // $aec = new Aes(config('app.jwt_key')); // $key = $aec->encrypt($v); // url生成 if ($type == 'mini') { trace('326行,mini', 'debug'); $param = $this->getParam(lcfirst($share->type), $emp->uid, $share->data_id, $clientType, $share->other_data); trace($param, 'debug'); trace('328行', 'debug'); // 存储参数 $urldata = [ 'content' => json_encode($param), 'type' => 'share' ]; trace($urldata); $shortUrl = new ShortUrl(); trace($shortUrl); $shortUrl->save($urldata); // 将ID进行 $query = dec52($shortUrl->id); $shortUrl->uri = $query; $shortUrl->save(); } else { trace('341行,mini else', 'debug'); if ($strpos){ // vr $param = [ 'id' => $share->data_id, 'type' => $share->type, 'employee_id' => $share->employee_id, 'agent_id' => $share->agent_id ]; $param = http_build_query($param); $aes = new Aes(config('app.jwt_key')); $query_arr = [ 'r'=> $emp->root_id, 'p'=> $aes->encrypt($param), 'id'=> $share->data_id, 'type'=> lcfirst($share_type), 'vr_link'=> $share->other_data ]; $query = 'index/index/vr.html?' . http_build_query($query_arr); } elseif($share_type == 'BuildingHousetype'){ $param = [ 'id' => $share->data_id, 'type' => $share->type, 'employee_id' => $share->employee_id, 'agent_id' => $share->agent_id ]; $param = http_build_query($param); $aes = new Aes(config('app.jwt_key')); $query = 'index/index/housetype/id/'.$share->data_id.'/r/' . $emp->root_id . '/p/' . $aes->encrypt($param); } elseif($share_type == 'BuildingProgress') { $param = [ 'id' => $share->data_id, 'type' => $share->type, 'employee_id' => $share->employee_id, 'agent_id' => $share->agent_id ]; $param = http_build_query($param); $aes = new Aes(config('app.jwt_key')); $query = 'index/index/progress_detail/id/'.$share->data_id.'/r/' . $emp->root_id . '/p/' . $aes->encrypt($param); } else { $param = [ 'id' => $share->data_id, 'type' => $share->type, 'employee_id' => $share->employee_id, 'agent_id' => $share->agent_id ]; $param = http_build_query($param); $aes = new Aes(config('app.jwt_key')); $query = 'index/index/content/r/' . $emp->root_id . '/p/' . $aes->encrypt($param); } } $domain = request()->domain(); $url = $domain . '/' . $query; $data = [ 'touser' => $openid, 'msgtype' => 'link', 'link' => [ 'title' => $data['title'], 'description' => $data['title'], 'url' => $url, 'thumb_url' => $thumb ] ]; trace('分享链接内容:' . json_encode($data), 'debug'); return (new CustomerServiceMessage)->send($this->mini->accesstoken, $data); } public function getParam($type, $shareUid, $id, $clientType, $other_data) { if (strpos($type, '_vr')) { $data = [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/other/other", 'query' => "aid=" . $id . "&uid=" . $shareUid . "&ctp=" . $clientType, 'type' => $type, 'user_id' => $shareUid, 'other_data'=> $other_data ]; return $data; } $params = [ 'article' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/articleshare/articleshare", 'query' => "aid=" . $id . "&userid=" . $shareUid, 'type' => "article", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'video' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/videoshare/videoshare", 'query' => "vid=" . $id . "&userid=" . $shareUid, 'type' => "video", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'materialEvidence' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/publicpraisemsg/publicpraisemsg", 'query' => "eid=" . $id . "&uid=" . $shareUid, 'type' => "materialEvidence", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'materialCase' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/materialcase/materialcase", 'query' => "cid=" . $id . "&uid=" . $shareUid, 'type' => "materialCase", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'activity' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/activitymsg/activitymsg", 'query' => "aid=" . $id . "&uid=" . $shareUid, 'type' => "activity", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'construction' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/constructsite/constructsite", 'query' => "aid=" . $id . "&uid=" . $shareUid, 'type' => "construction", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'building' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/buildprogress/buildprogress", 'query' => "aid=" . $id . "&uid=" . $shareUid, 'type' => "building", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'companyStrength' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/companystrengthmsg/companystrengthmsg", 'query' => "eid=" . $id . "&uid=" . $shareUid . "&ctp=" . $clientType, 'type' => "CompanyStrength", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'buildingHousetype' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/index/pages/housetype/housetype", 'query' => "d=" . $id . "&u=" . $shareUid . "&c=" . $clientType, 'type' => "BuildingHousetype", 'user_id' => $shareUid, 'other_data'=> $other_data ], 'buildingProgress' => [ 'client_type' => $clientType, 'id' => $id, 'path' => "/share/pages/buildShare/buildShare", 'query' => "uid=" . $shareUid . "&cid=" . $id . "&ctp=" . $clientType, 'type' => "BuildingProgress", 'user_id' => $shareUid, 'other_data'=> $other_data ] ]; return $params[$type]; } }