- $appid = '';
- $secret ='';
- if((!isset($_SESSION['wechat_id']) || $_SESSION['wechat_id']==null) && !$_SESSION['user_id'] && !$_CFG['shop_reg_closed']){
- $code=isset($_GET['code'])?$_GET['code']:null;
- $url = urlencode("http://mall.58zcm.com/mobile/index.php");
- if($code==null){
- $str="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri=".$url."&scope=snsapi_base&state=123&response_type=code#wechat_redirect";
- //echo $str;exit;
- header ("Location:".$str);
- }else{
- $str="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code=".$code."&grant_type=authorization_code";
- $ch = curl_init() ;
- curl_setopt($ch, CURLOPT_URL, $str);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
- $output = curl_exec($ch);
- $output=json_decode($output,true);
- $_SESSION['wechat_id']=$output['openid'];
- }
- }
- //unset($_SESSION['user_id']);exit;
- /*授权登陆 注册*/
- if(!$_SESSION['user_id'] && $_SESSION['wechat_id'] && !$_CFG['shop_reg_closed']){
- $openid = $_SESSION['wechat_id'];
- $sql = "SELECT * FROM".$ecs->table('users')." where wx_open_id = '".$openid."'";
- $row = $db->getRow($sql);
- if($row){
- $username = $row['user_name'];
- $password = $row['user_name'];
- if ($user->login($username, $password)){
- update_user_info();
- recalculate_price();
- }
- }else{//注册
- include_once(ROOT_PATH . 'mobile/includes/lib_passport.php');
- $username = 'wx_'.time();
- $password = 'wx_'.time();
- $email = 'wx_login'.time().'@qq.com';
- if (register($username, $password, $email) !== false){
- $sql = "UPDATE ".$ecs->table('users')." SET wx_open_id = '".$openid."' WHERE user_id = '".$_SESSION['user_id']."'";
- if(!$db->query($sql)){
- unset($_SESSION['user_id']);
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1812201411344.html
来源: http://www.codesnippet.cn/detail/1812201411344.html