# 微信推送接口对接示例含扫描登录微信端部分源码 >[info] 上源码 不解释了 ~~~ <?php /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/4/27 * Time: 13:33 */ namespace app\center\controller; use app\base\controller\Curl; use app\base\controller\Rsa; use app\base\model\we\WeFans; use app\base\model\we\WeLocation; use app\base\model\we\WeMessage; use app\base\model\we\WeTemplateMessage; use mikkle\tp_wechat\WechatApi; use think\Db; use think\Exception; use think\Cache; use think\Log; class ErpApi extends WechatApi { protected $valid = false; //网站第一次匹配 true 1为匹配 protected $isHook = false; //是否开启钩子 public function index() { try{ // Log::notice($this->request); parent::index(); }catch (Exception $e){ Log::error($e->getMessage()); } } protected function messageApplyEvent($EventKey, $subscribe = false){ $news_array = [ [ 'Title' => '申请云管家ERP系统', 'Description' => '现在去申请', 'PicUrl' => '', 'Url' => $this->request->domain() . '/center/apply/apply?event_key=' . $EventKey, ], ]; $reply = ['type' => 'news', 'message' => $news_array]; return $reply; } /** * 微信API接口 扫码登录 * Power by Mikkle * QQ:776329498 * @param $EventKey * @param bool|false $subscribe * @return array */ protected function messageLogEvent($EventKey, $subscribe = false) { $domain =Db::table("mk_we_fans_domain")->where(["status"=>1,"openid"=>$this->openid])->value("domain"); if ($domain) { //写入登录缓存 $log_data = [ 'code' => 1001, 'content' => '登录成功', 'data' => $this->openid, ]; // Cache::set('log_' . $EventKey, $log_data, 120); $open_id = Rsa::instance()->encrypt($this->openid); try { $url = "http://{$domain}/api/system/loginWechat"; $push_data = [ "open_id" => $open_id, "event_key" => $EventKey ]; $result_code = json_decode(Curl::curlPost($url, $push_data)); if (!is_object($result_code)) { $result_code = json_decode(Curl::curlPost($url, $push_data)); } if (is_object($result_code)) { if (isset($result_code->code)) { if ($result_code->code == 1001) { if ($subscribe) { $reply = ['type' => 'text', 'message' => '感谢你的关注,你正在进行微信扫码成功.登录ID[' . $EventKey . ']']; } else { $reply = ['type' => 'text', 'message' => '你正在进行微信扫码.登录ID[' . $EventKey . ']']; } }else{ throw new Exception($result_code); } } }else{ throw new Exception($result_code); } }catch (Exception $e){ Log::error($e->getMessage()); $reply=['type' => 'text', 'message' => '感谢你的关注']; return $reply; } } else { $news_array = [ [ 'Title' => '你的微信暂时未绑定ERP系统', 'Description' => '现在去绑定', 'PicUrl' => '', 'Url' => $this->request->domain() . '/center/we_action/binding?sid=' . $EventKey, ], ]; $reply = ['type' => 'news', 'message' => $news_array]; } return $reply; } protected function returnEventLocation() { return ""; } protected function returnEventSubscribe(){ if (isset($this->data['EventKey'])&&is_string($this->data['EventKey'])) { $EventKeyArray = explode('_', $this->data['EventKey']); if(count($EventKeyArray)==2){ $EventKey=$EventKeyArray[1]; switch(true){ case (stristr($EventKey,"apply")): return $this->messageApplyEvent($EventKey,true); break; default: //判断登录缓存是否存在 if (Cache::has('log_' . $EventKey)) { $reply = $this->messageLogEvent($EventKey,true); } else { $reply = ['type' => 'text', 'message' => '感谢你的关注']; } } } $reply = ['type' => 'text', 'message' => '感谢你的关注']; } else { $reply = ['type' => 'text', 'message' => '感谢你的关注']; } return $reply; } protected function returnEventUnsubscribe(){ Db::name('WeFans')->where('openid', $this->openid)->update(['subscribe' => 0, 'unsubscribe_time' => time()]); return ['type' => 'text', 'message' => '期待你的再次关注']; } protected function returnEventScan(){ //记录扫码获取机器人回答 $EventKey = $this->data['EventKey']; switch(true){ case (stristr($EventKey,"apply")): $reply = $this->messageApplyEvent($EventKey); break; default: //判断登录缓存是否存在 if (Cache::has('log_' . $EventKey)) { $reply = $this->messageLogEvent($EventKey);; } else { $reply = ['type' => 'text', 'message' =>"你的扫码成功-$EventKey"]; } } return $reply; } protected function saveWeMessage() { //定义message的Model $model_message = $this->getWeMessageModel(); //查询是否已经接受该消息 if (isset($this->data['MsgId'])) { if ($model_message->infoByMsgId($this->data['MsgId'])) { return false; } } //记录消息 if (isset($this->data['Recognition'])) { if (is_string($this->data['Recognition'])) { $this->data['Content'] = $this->data['Recognition']; } else { $this->data['Content'] = 'Translation failure!'; unset($this->data['Recognition']); } } if (isset($this->data['Latitude'])) { $this->getWeLocationModel()->editData($this->data); } else if (isset($this->data['MsgID'])) { $this->getWeTemplateMessageModel()->updateTemplateMessage($this->data['MsgID']); } else { $model_message->editData($this->data); } } public function hasSaveFans($openid = '') { try { $openid = $openid ? $openid : $this->openid; if (empty($openid)) { // $this->we_dump($openid); return false; } $model_fans = $this->getWeFansModel(); $fans = $model_fans->infoToArray($openid); if (!$fans) { $fans = $this->weObj->getUserInfo($openid); if ($fans) { if (is_object($fans)) { $fans = $fans->toArray(); } $fans['appid'] = isset($this->options['appid'])?$this->options['appid']:""; // Log::notice($fans); $model_fans->editData($fans); } else { //todo 报警 throw new Exception("获取用户信息失败"); } } else { //记录心跳 $model_fans->where('openid', $this->openid)->setField('update_time', time()); } return $fans; } catch (Exception $e) { Log::error($e->getMessage()); return false; } } protected function getWeFansModel() { return new WeFans(); } protected function getWeMessageModel() { return new WeMessage(); } protected function getWeTemplateMessageModel() { return new WeTemplateMessage(); } protected function getWeLocationModel() { return new WeLocation(); } } ~~~