/** * 新用户从微信注册 */ public function getNewOpenID() { $weObj = new \System\lib\Wechat\Wechat($this->config("WEIXIN_CONFIG")); $this->weObj = $weObj; if (empty($_GET['code']) && empty($_GET['state'])) { $callback = getHostUrl(); $reurl = $weObj->getOauthRedirect($callback, "1"); redirect($reurl, 0, '正在发送验证中...'); exit; } elseif (intval($_GET['state']) == 1) { $accessToken = $weObj->getOauthAccessToken(); $mobile = $_GET['phone']; //用户信息 $userInfo = $this->getUserInfo($accessToken); $saveUser = $this->saveUser($userInfo, $mobile); //插入新会员数据 if (!$saveUser) { $this->R('', '40001'); } header("LOCATION:http://onebuy.ping-qu.com"); } else { //用户取消授权 $this->R('', '90006'); } }
/** * 获取微信media并保存 * @param str $mediaID media_id * @param str $path 目录 * @return bool */ public static function getWechatMedia($mediaID, $savePath = null) { if (empty($savePath)) { $savePath = 'order/' . time() . '_' . rand(1000, 9999) . ".jpg"; } $weObj = new \System\lib\Wechat\Wechat(\System\Entrance::config("WEIXIN_CONFIG")); $media = $weObj->getMedia($mediaID); if (!$media) { return false; } $temPath = '../TempImg/'; //临时保存路径 if (!dirExists($temPath)) { return false; } $fileName = basename($savePath); /* $fileName=basename($savePath); $checkPath=str_replace($fileName, '', $savePath); if(!dirExists($checkPath)) return false; */ if (!file_put_contents($temPath . $fileName, $media)) { return false; } //生成压缩图片 $image = new \System\lib\Image\Image($temPath . $fileName); $img1 = $image->size(); $return['img1']['path'] = $temPath . $fileName; $return['img1']['width'] = $img1[0]; $return['img1']['heigh'] = $img1[1]; //200*200缩略图 $img2Path = $temPath . str_replace('.', '_thumb200.', $fileName); $image->thumb(200, 10000)->save($img2Path); $img2 = $image->size(); $return['img2']['path'] = $img2Path; $return['img2']['width'] = $img2[0]; $return['img2']['heigh'] = $img2[1]; //360*360缩略图 $img3Path = $temPath . str_replace('.', '_thumb360.', $fileName); $image->open($temPath . $fileName); $image->thumb(360, 10000)->save($img3Path); $img3 = $image->size(); $return['img3']['path'] = $img3Path; $return['img3']['width'] = $img3[0]; $return['img3']['heigh'] = $img3[1]; //dump($return); //上传 foreach ($return as $key => $a) { $upload = httpPost(\System\Entrance::config('IMG_UPLOAD'), ['file' => new \CURLFile(realpath($a['path'])), 'savePath' => dirname($savePath) . '/', 'saveName' => basename($a['path'])], true); //dump($upload); $upload = json_decode($upload, true); if (!($upload && $upload['result'] == true)) { return false; } unlink($a['path']); $return[$key]['path'] = $upload['path']; } return $return; }
/** * 授权 */ private function getOpenID() { if (isset($_GET['wechat_refer'])) { //回跳地址 $_SESSION['wechat_refer'] = urldecode($_GET['wechat_refer']); } $weObj = new \System\lib\Wechat\Wechat($this->config("WEIXIN_CONFIG")); $this->weObj = $weObj; if (empty($_GET['code']) && empty($_GET['state'])) { $callback = getHostUrl(); $reurl = $weObj->getOauthRedirect($callback, "1"); redirect($reurl, 0, '正在发送验证中...'); exit; } elseif (intval($_GET['state']) == 1) { $accessToken = $weObj->getOauthAccessToken(); $isUser = $this->getUserInfo($accessToken); $_SESSION['userInfo'] = ['openid' => $isUser['openid'], 'nickname' => $isUser['nickname']]; // header("LOCATION:".$_SESSION['wechat_refer']); } else { //用户取消授权 $this->R('', '90006'); } }
public function index() { $weObj = new \System\lib\Wechat\Wechat($this->config('WEIXIN_CONFIG')); $this->weObj = $weObj; //对接验证 $weObj->valid(); $type = $weObj->getRev()->getRevType(); // 获取数据类型 $data = $weObj->getRev()->getRevData(); // 获取微信服务器发来的信息 $this->weixinData = $data; //$openid = $this->weixinData['FromUserName']; //记录日志 file_put_contents("Log/wechat.txt", json_encode($data) . " 日期:" . date("Y-m-d H:i:s") . PHP_EOL, FILE_APPEND); switch ($type) { case $weObj::MSGTYPE_TEXT: // 文本类型 // 记录文本消息到数据库 $this->_saveTextMsg(); //---特殊操作优先--- // 关键字回复 $result = $this->_getKeywordReply(); //自动回复 $auotMsg = $this->_getAutoReply(); break; case $weObj::MSGTYPE_EVENT: // 事件类型 if ($data['Event'] == "subscribe") { // 关注事件 //添加粉丝操作 //$this->_addFans(); //关注回复 $this->_getFollowReply(); } elseif ($data['Event'] == "unsubscribe") { // 取消关注事件 //记录取消关注事件 $this->_saveFollowMsg(); //粉丝操作 $this->_updateFans(); } elseif ($data['Event'] == "LOCATION") { // 获取上报的地理位置事件 //记录用户自动上传的地址位置 $this->_saveLocationMsgAuto(); } elseif ($data['Event'] == "CLICK") { // 自定义菜单 // 记录自定义菜单消息 $this->_saveMenuMsg(); //菜单点击事件 $content = $data["EventKey"]; $result = $this->_getKeywordReply($content); } elseif ($data['Event'] == "VIEW") { // 点击菜单跳转链接时的事件推送 // 记录自定义菜单消息 $this->_saveMenuMsg(); } elseif ($data['Event'] == "SCAN") { //扫二维码进入公众号 // 记录自定义菜单消息 $this->_saveScanMsg(); } elseif (!empty($data['KfAccount'])) { //客服时间 $this->_saveKfMsg(); } break; case $weObj::MSGTYPE_IMAGE: // 图片类型 // 记录图片消息 $this->_saveImgMsg(); break; case $weObj::MSGTYPE_LOCATION: // 地理位置类型 $this->_saveLocationMsg(); $weObj->text("地理位置已接收")->reply(); break; case $weObj::MSGTYPE_LINK: // 链接消息 $this->_saveLinkMsg(); $weObj->text("链接消息已接收")->reply(); break; default: $weObj->text("help info")->reply(); } }
/** * 新用户从微信注册 */ public function getNewOpenID() { $refer = $_GET['refer']; $weObj = new \System\lib\Wechat\Wechat($this->config("WEIXIN_CONFIG")); $this->weObj = $weObj; if (empty($_GET['code']) && empty($_GET['state'])) { $callback = getHostUrl(); $reurl = $weObj->getOauthRedirect($callback, "1"); redirect($reurl, 0, '正在发送验证中...'); exit; } elseif (intval($_GET['state']) == 1) { $accessToken = $weObj->getOauthAccessToken(); $mobile = $_GET['phone']; $user = $this->table('user')->where(['is_on' => 1, 'phone' => $mobile])->get(['id'], true); if (!$user) { //用户信息 $userInfo = $this->getUserInfo($accessToken); $saveUser = $this->saveUser($userInfo, $mobile); //插入新会员数据 if (!$saveUser) { $this->R('', '40001'); } //header("LOCATION:".getHost()."/Api/User/getOpenID"); $userInfo = $this->getUserInfo($accessToken); // 是否有用户记录 $isUser = $this->table('user')->where(["openid" => $accessToken['openid'], "is_on" => 1])->get(null, true); /*var_dump($isUser);exit();*/ if ($isUser == null) { //没有此用户跳转至输入注册的页面 header("LOCATION:" . getHost() . "/register.html?refer=" . $_GET['refer']); } else { $userID = $isUser['id']; $updateUser = $this->table('user')->where(['id' => $userID])->update(['last_login' => time(), 'last_ip' => ip2long(getClientIp()), 'nickname' => $userInfo['nickname'], 'user_img' => $userInfo['headimgurl']]); $_SESSION['userInfo'] = ['openid' => $isUser['openid'], 'userid' => $isUser['id'], 'nickname' => $isUser['nickname'], 'user_img' => $isUser['user_img']]; header("LOCATION:" . $refer); //进入网站成功 } } else { $this->R('', '70000'); //手机已注册 } } else { //用户取消授权 $this->R('', '90006'); } }
/** * 同步/创建自定义菜单 */ public function sysMenu() { //$this->checkPriv('Menu', 5); $data = $this->table('wechat_menu')->where(['is_on' => 1])->order("pid asc, sort asc")->get(); foreach ($data as $k => $d) { if ($d['pid'] != 0) { continue; } $tree['button'][$d['id']] = $this->_deal_data($d); unset($data[$k]); } foreach ($data as $k => $d) { $tree['button'][$d['pid']]['sub_button'][] = $this->_deal_data($d); unset($data[$k]); } $tree2 = array(); $tree2['button'] = array(); foreach ($tree['button'] as $k => $d) { $tree2['button'][] = $d; } $weObj = new \System\lib\Wechat\Wechat($this->config('WEIXIN_CONFIG')); $send = $weObj->createMenu($tree2); if ($send) { $this->R(); } else { $this->R(['errorMsg' => "菜单创建失败!失败信息:" . $weObj->errMsg . " 错误码:" . $weObj->errCode], ''); } }
/** * 新用户从微信注册 */ public function getNewOpenID() { $weObj = new \System\lib\Wechat\Wechat($this->config("WEIXIN_CONFIG")); $this->weObj = $weObj; if (empty($_GET['code']) && empty($_GET['state'])) { $callback = getHostUrl(); $reurl = $weObj->getOauthRedirect($callback, "1"); redirect($reurl, 0, '正在发送验证中...'); exit; } elseif (intval($_GET['state']) == 1) { $accessToken = $weObj->getOauthAccessToken(); $mobile = $_GET['phone']; $user = $this->table('user')->where(['is_on' => 1, 'phone' => $mobile])->get(['id'], true); if (!$user) { //用户信息 $userInfo = $this->getUserInfo($accessToken); $saveUser = $this->saveUser($userInfo, $mobile); //插入新会员数据 if (!$saveUser) { $this->R('', '40001'); } header("LOCATION:" . getHost() . "/Api/User/getOpenID"); // } else { $this->R('', '70000'); //手机已注册 } } else { //用户取消授权 $this->R('', '90006'); } }