123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- <?php
- namespace app\api\controller;
- use app\model\Employee;
- use app\model\Miniprogram;
- use app\model\ShareLog;
- use app\model\ShortUrl;
- use app\model\User;
- use DOMDocument;
- use openssl\Aes;
- use wx\offiaccount\Event;
- use think\facade\Cache;
- use wx\miniprogram\msg\CustomerServiceMessage;
- use app\model\AgentUser;
- use app\model\MaterialEvidence;
- use app\model\CompanyStrength as CompanyStrengthModel;
- use app\model\Wechat;
- use app\model\AgentArticle;
- use app\model\Company;
- /**
- * 微信事件接受类
- *
- */
- class MiniEventPush
- {
- private $client = [
- 'zyzs' => 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];
- }
- }
|