private function parseText($dataObj) { $keyword = trim($dataObj->Content); if (filter_var($keyword, FILTER_VALIDATE_URL) !== FALSE) { return ['type' => "text", 'data' => $this->urlTips()]; } $cmd_tip = substr($keyword, 0, 1); switch ($cmd_tip) { case "@": //搜歌曲 return $this->searchMusicByKw($keyword); break; case "#": //微信墙 $bind_info = UserOpenidUnionid::findOne(['other_openid' => trim($dataObj->FromUserName)]); if ($bind_info) { return 'success'; } $keyword = "上墙"; break; } switch ($keyword) { case "上墙": $data = [['title' => '点击授权签到', 'description' => \Yii::$app->params['author']['nickname'] . "微信墙,授权后方可上墙留言", 'picurl' => GlobalUrlService::buildPic1Static("/20160531/7ba8f923c344a5af480cd76dd358e196.jpg", ['w' => 800]), 'url' => GlobalUrlService::buildWapUrl("/wechat_wall/sign", ['woid' => trim($dataObj->FromUserName)])]]; return ['type' => "rich", "data" => $this->getRichXml($data)]; break; } return $this->getDataByKeyword($keyword, trim($dataObj->FromUserName)); }
public static function add($xml, $source = "") { $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $type = trim($data->MsgType); $from_openid = $data->FromUserName; $to_openid = $data->ToUserName; $date_now = date("Y-m-d H:i:s"); switch ($type) { case "location": $content = trim($data->Label); break; case "voice": $content = trim($data->Recognition); break; case "image": $content = trim($data->PicUrl); break; case "link": $content = trim($data->Title); break; case "shortvideo": $content = trim($data->ThumbMediaId); break; case "event": $content = trim($data->Event); break; default: $content = trim($data->Content); break; } $model_wx_history = new WxHistory(); $model_wx_history->from_openid = $from_openid; $model_wx_history->to_openid = $to_openid; $model_wx_history->type = $type; $model_wx_history->content = $content; $model_wx_history->text = $xml; $model_wx_history->source = $source; $model_wx_history->created_time = $date_now; $model_wx_history->save(0); if (filter_var($content, FILTER_VALIDATE_URL) !== FALSE) { SpiderService::add($content); } if (in_array($type, ["text"]) && substr($content, 0, 1) == "#") { $bind_info = UserOpenidUnionid::findOne(['other_openid' => strval($from_openid)]); if (!$bind_info) { $unique_name = md5($from_openid); $user_info = User::findOne(['unique_name' => $unique_name]); if (!$user_info) { $model_user = new User(); $model_user->nickname = "微信用户" . substr($from_openid, -10); $model_user->unique_name = $unique_name; $model_user->updated_time = $date_now; $model_user->created_time = $date_now; $model_user->save(0); $user_info = $model_user; } $model_bind = new UserOpenidUnionid(); $model_bind->uid = $user_info['uid']; $model_bind->openid = $from_openid; $model_bind->unionid = ''; $model_bind->other_openid = $from_openid; $model_bind->updated_time = $date_now; $model_bind->created_time = $date_now; $model_bind->save(0); } if ($bind_info) { $model_message = new UserMessageHistory(); $model_message->uid = $bind_info['uid']; $model_message->type = 1; $model_message->content = ltrim($content, "#"); $model_message->status = 1; $model_message->updated_time = $date_now; $model_message->created_time = $date_now; $model_message->save(0); } } }
public function actionSign() { /*初始化用户*/ $woid = $this->get("woid", ''); $wx_user_info = ['avatar' => GlobalUrlService::buildStaticUrl("/images/wap/no_avatar.png"), 'nickname' => '匿名']; if ($woid) { $wx_user_info['nickname'] = "微信用户" . substr($woid, -10); $date_now = date("Y-m-d H:i:s"); $bind_info = UserOpenidUnionid::findOne(['other_openid' => strval($woid)]); if (!$bind_info) { $unique_name = md5($woid); $user_info = User::findOne(['unique_name' => $unique_name]); if (!$user_info) { $model_user = new User(); $model_user->nickname = "微信用户" . substr($woid, -10); $model_user->unique_name = $unique_name; $model_user->updated_time = $date_now; $model_user->created_time = $date_now; $model_user->save(0); $user_info = $model_user; } $model_bind = new UserOpenidUnionid(); $model_bind->uid = $user_info['uid']; $model_bind->openid = $woid; $model_bind->unionid = ''; $model_bind->other_openid = $woid; $model_bind->updated_time = $date_now; $model_bind->created_time = $date_now; $model_bind->save(0); } } return $this->render("sign", ["user_info" => $wx_user_info]); }
public function actionToken() { $this->setWeixinConfig(); $code = $this->get("code", ""); $state = $this->get("state", ""); if (!$code) { return $this->goHome(); } $appid = $this->appid; $appsecret = $this->appsecret; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code"; $ret = HttpClient::get($url, []); $data = @json_decode($ret, true); if (empty($data) || isset($data['errcode'])) { return $this->goHome(); } $openid = $data['openid']; $sns_user_data = []; if ($data['scope'] == "snsapi_userinfo") { $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $data['access_token'] . "&openid={$openid}&lang=zh_CN"; $ret = HttpClient::get($url, []); $sns_user_data = @json_decode($ret, true); } /*特殊处理*/ $state = str_replace("@@", "&", urldecode($state)); $state = str_replace("@|@", "?", urldecode($state)); /*微信url特殊参数处理*/ $state = str_replace("from=groupmessage", "", $state); $state = str_replace("isappinstalled=0", "", $state); $state = str_replace("connect_redirect=", "", $state); $state = rtrim($state, "?"); $state = rtrim($state, "&"); /*看看有没有owid:公众号wx opendid*/ $woid = ''; $question_mark_idx = stripos($state, "?"); if ($question_mark_idx !== false) { $parse_str = mb_substr($state, $question_mark_idx + 1); parse_str($parse_str, $get_params); if (isset($get_params['woid'])) { $woid = $get_params['woid']; } } $reg_bind = UserOpenidUnionid::findOne(["openid" => $openid]); if (!$reg_bind) { $date_now = date("Y-m-d H:i:s"); $unique_name = md5($openid); $user_info = User::findOne(['unique_name' => $unique_name]); if (!$user_info) { $model_user = new User(); if ($sns_user_data) { $model_user->nickname = $sns_user_data['nickname']; $model_user->avatar = $sns_user_data['headimgurl']; } else { $model_user->nickname = "微信用户" . substr($openid, -10); } $model_user->unique_name = $unique_name; $model_user->updated_time = $date_now; $model_user->created_time = $date_now; $model_user->save(0); $user_info = $model_user; } $model_bind = new UserOpenidUnionid(); $model_bind->uid = $user_info['uid']; $model_bind->openid = $openid; $model_bind->unionid = ''; $model_bind->other_openid = $woid; $model_bind->updated_time = $date_now; $model_bind->created_time = $date_now; $model_bind->save(0); $reg_bind = $model_bind; } else { $user_info = User::findOne(['uid' => $reg_bind['uid']]); } if ($sns_user_data && (stripos("微信用户", $user_info['nickname'] !== false) || !$user_info['avatar'])) { $user_info->nickname = $sns_user_data['nickname']; $user_info->avatar = $sns_user_data['headimgurl']; $user_info->update(0); } if ($woid && !$reg_bind['other_openid']) { //为了修复以前的数据 $reg_bind->other_openid = $woid; $reg_bind->update(0); } $this->createLoginStatus($user_info); $url = $state ? $state : UrlService::buildWapUrl("/default/index"); return $this->redirect($url); }