Esempio n. 1
0
            $database_user = D('User');
            if ($database_user->field('`uid`')->where(array('phone' => $_POST['phone']))->find()) {
                json_return(1014, '手机号码已存在');
            }
            $data = array();
            $data['phone'] = trim($_POST['phone']);
            $data['nickname'] = '';
            $data['password'] = md5(trim($_POST['pwd']));
            $data['check_phone'] = 1;
            $data['login_count'] = 1;
            if (!empty($_SESSION['openid'])) {
                $data['openid'] = $_SESSION['openid'];
            }
            $add_result = M('User')->add_user($data);
            if ($add_result['err_code'] == 0) {
                $_SESSION['wap_user'] = $add_result['err_msg'];
                mergeSessionUserInfo(session_id(), $add_result['err_msg']['uid']);
                json_return(0, '注册成功');
            } else {
                json_return(1, $add_result['err_msg']);
            }
    }
} else {
    //回调地址
    $redirect_uri = $_GET['referer'] ? $_GET['referer'] : ($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : ($_COOKIE['wap_store_id'] ? './home.php?id=' . $_COOKIE['wap_store_id'] : $config['site_url']));
    if (!empty($_SESSION['wap_user'])) {
        redirect($redirect_uri);
    }
    include display('login');
    echo ob_get_clean();
}
Esempio n. 2
0
 public function wap_getStore($store_id)
 {
     $store = $this->db->where(array('store_id' => $store_id, 'status' => 1))->find();
     if (!empty($store)) {
         $_SESSION['tmp_store_id'] = $store_id;
         //解决用户访问不同店铺重复授权生成新用户问题
         /*if (empty($_SESSION['wap_user']) && !empty($_COOKIE['uid'])) { //COOKIE中有用户信息
               $tmp_user = M('User')->checkUser(array('uid' => $_COOKIE['uid']));
               if (!empty($tmp_user)) {
                   $_SESSION['wap_user'] = $tmp_user;
                   $tmp_seller = D('Store')->where(array('drp_supplier_id' => $store_id, 'uid' => $_COOKIE['uid'], 'status' => 1))->find();
                   if (!empty($tmp_seller)) {
                       $_SESSION['wap_drp_store'] = $tmp_seller;
                       if (!empty($tmp_seller['oauth_url'])) { //对接微店
                           $_SESSION['sync_user'] = true;
                       }
                   }
                   setcookie('uid', $_COOKIE['uid'], $_SERVER['REQUEST_TIME']+10000000, '/'); //延长cookie有效期
               } else {
                   unset($_SESSION['sync_user']); //删除同步标识
                   unset($_SESSION['wap_user']); //删除用户登录状态
               }
           }*/
         //判断是否为对接微店
         if (!empty($store['oauth_url'])) {
             if (!empty($_SESSION['wap_user']) && $_SESSION['wap_user']['store_id'] != $store_id) {
                 //非当前店铺粉丝,重新授权登陆
                 unset($_SESSION['sync_user']);
                 //删除同步标识
                 unset($_SESSION['wap_user']);
                 //删除用户登录状态
             }
         } else {
             unset($_SESSION['sync_user']);
             //非对接店铺 删除同步标识
         }
         //对接网站用户授权登陆
         //授权条件:非对接同步用户,是对接店铺,店铺管理后台未登录(不加此条件,店铺管理后台的所有链接无法在pc端打开,都会跳转授权)
         if (empty($_SESSION['sync_user']) && !empty($store['oauth_url']) && empty($_SESSION['sync_store'])) {
             $return_url = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
             if (!empty($store['oauth_url'])) {
                 if (stripos($store['oauth_url'], '?') === false) {
                     redirect($store['oauth_url'] . '?return_url=' . $return_url . '&store_id=' . $store_id . '&token=' . $store['token']);
                 } else {
                     redirect($store['oauth_url'] . '&return_url=' . $return_url . '&store_id=' . $store_id . '&token=' . $store['token']);
                 }
             }
         } else {
             if (empty($_SESSION['sync_user']) && empty($store['oauth_url']) && empty($_SESSION['store'])) {
                 //默认授权
                 //授权条件:非对接同步用户,非对接店铺,店铺管理后台未登录(不加此条件,店铺管理后台的所有链接无法在pc端打开,都会跳转授权)
                 /*是否移动端*/
                 $is_mobile = is_mobile();
                 /*是否微信端*/
                 $is_weixin = is_weixin();
                 //调试  清除登录信息
                 //setcookie('pigcms_sessionid','',$_SERVER['REQUEST_TIME']-10000000,'/');
                 //$_SESSION = null;
                 //session_destroy();
                 /*如果是微信端,且配置文件中配置了微信信息,得到openid*/
                 if ($is_weixin && (empty($_SESSION['openid']) || empty($_SESSION['wap_user']))) {
                     //openid存在 通过openid查找用户
                     if (!empty($_SESSION['openid'])) {
                         $userinfo = M('User')->get_user('openid', $_SESSION['openid']);
                         $_SESSION['wap_user'] = $userinfo['user'];
                         mergeSessionUserInfo(session_id(), $userinfo['user']['uid']);
                         unset($_SESSION['wap_drp_store']);
                     }
                     //用户未登录 调用授权获取openid, 通过openid查找用户,如果已经存在,设置登录,如果不存在,添加一个新用户和openid关联
                     if (empty($_SESSION['wap_user'])) {
                         $customeUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                         //判断店铺是否绑定过认证服务号
                         $wx_bind = D('Weixin_bind')->where(array('store_id' => $store['store_id']))->find();
                         if (empty($_GET['code'])) {
                             $_SESSION['weixin']['state'] = md5(uniqid());
                             if (!empty($wx_bind) && $wx_bind['service_type_info'] == 2 && $wx_bind['verify_type_info'] == 0) {
                                 $oauthUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $wx_bind['authorizer_appid'] . '&redirect_uri=' . urlencode($customeUrl) . '&response_type=code&scope=snsapi_userinfo&state=' . $_SESSION['weixin']['state'] . '&component_appid=' . option('config.wx_appid') . '#wechat_redirect';
                             } else {
                                 //店铺非认证服务号走总后台授权
                                 $oauthUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . option('config.wechat_appid') . '&redirect_uri=' . urlencode($customeUrl) . '&response_type=code&scope=snsapi_userinfo&state=' . $_SESSION['weixin']['state'] . '#wechat_redirect';
                             }
                             redirect($oauthUrl);
                             exit;
                         } else {
                             if (isset($_GET['code']) && isset($_GET['state']) && $_GET['state'] == $_SESSION['weixin']['state']) {
                                 unset($_SESSION['weixin']);
                                 import('Http');
                                 $http = new Http();
                                 if (!empty($wx_bind) && $wx_bind['service_type_info'] == 2 && $wx_bind['verify_type_info'] == 0) {
                                     $component_token = M('Weixin_bind')->get_access_token($store['store_id'], true);
                                     $tokenUrl = 'https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=' . $wx_bind['authorizer_appid'] . '&code=' . $_GET['code'] . '&grant_type=authorization_code&component_appid=' . option('config.wx_appid') . '&component_access_token=' . $component_token;
                                 } else {
                                     $tokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . option('config.wechat_appid') . '&secret=' . option('config.wechat_appsecret') . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
                                 }
                                 $return = $http->curlGet($tokenUrl);
                                 $jsonrt = json_decode($return, true);
                                 if ($jsonrt['errcode']) {
                                     $error_msg_class = new GetErrorMsg();
                                     exit('授权发生错误:' . $jsonrt['errcode']);
                                 }
                                 if ($jsonrt['openid']) {
                                     //微信中打开直接登陆
                                     $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $jsonrt['access_token'] . '&openid=' . $jsonrt['openid'] . '&lang=zh_CN';
                                     $wxuser = $http->curlGet($url);
                                     $wxuser = json_decode($wxuser, true);
                                     $_SESSION['openid'] = $jsonrt['openid'];
                                     $userinfo = M('User')->get_user('openid', $_SESSION['openid']);
                                     if (empty($userinfo['user'])) {
                                         //用户不存在,添加新用户,并设置登录
                                         $data = array();
                                         $data['phone'] = '';
                                         $data['nickname'] = $wxuser['nickname'];
                                         $data['openid'] = $_SESSION['openid'];
                                         $data['avatar'] = $wxuser['headimgurl'];
                                         $data['password'] = '';
                                         $data['check_phone'] = 1;
                                         $data['login_count'] = 1;
                                         $add_result = M('User')->add_user($data);
                                         if ($add_result['err_code'] == 0) {
                                             $_SESSION['wap_user'] = $add_result['err_msg'];
                                             $_SESSION['wap_user']['sex'] = $wxuser['sex'];
                                             $_SESSION['wap_user']['province'] = $wxuser['province'];
                                             $_SESSION['wap_user']['city'] = $wxuser['city'];
                                             mergeSessionUserInfo(session_id(), $add_result['err_msg']['uid']);
                                         }
                                     } else {
                                         //用户已存在,设置登录
                                         $_SESSION['wap_user'] = $userinfo['user'];
                                         $_SESSION['wap_user']['sex'] = $wxuser['sex'];
                                         $_SESSION['wap_user']['province'] = $wxuser['province'];
                                         $_SESSION['wap_user']['city'] = $wxuser['city'];
                                         mergeSessionUserInfo(session_id(), $userinfo['user']['uid']);
                                     }
                                     unset($_SESSION['wap_drp_store']);
                                     //删除保存在session中的分销店铺
                                 }
                             }
                         }
                     }
                 }
                 //}
             }
         }
         $store['url'] = option('config.wap_site_url') . '/home.php?id=' . $store['store_id'];
         if (empty($store['logo'])) {
             $store['logo'] = getAttachmentUrl('images/default_shop_2.jpg', false);
         } else {
             if (stripos($store['logo'], 'http://') === false && stripos($store['logo'], 'https://') === false) {
                 $store['logo'] = getAttachmentUrl($store['logo']);
             }
         }
         $store['ucenter_url'] = option('config.wap_site_url') . '/ucenter.php?id=' . $store['store_id'];
         $store['physical_url'] = option('config.wap_site_url') . '/physical.php?id=' . $store['store_id'];
         option('now_store', $store);
     }
     return $store;
 }