/**
  * 授权回调地址
  */
 public function callback($type = null, $code = null)
 {
     if (!$type || !$code) {
         $this->error('参数错误');
     }
     $type = strtolower($type);
     try {
         $sns = ThinkOauth::getInstance($type);
     } catch (\Exception $e) {
         $this->error('登录失败');
     }
     $extend = null;
     //腾讯微博需传递的额外参数
     if ($type == 'tencent') {
         $extend = array('openid' => I('get.openid'), 'openkey' => I('get.openkey'));
     }
     try {
         $token = $sns->getAccessToken($code, $extend);
     } catch (\Exception $e) {
         $this->error('登录失败');
     }
     //调用不同的登录方式
     $data = self::$type($sns);
     if (!$data) {
         $this->error('登录失败');
     }
     $member_db = M('member');
     $member_oauth_db = M('member_oauth');
     //如果用户没有注册则先进行注册
     $oauthInfo = $member_oauth_db->where(array('type' => $data['type'], 'openid' => $data['openid']))->find();
     if (!$oauthInfo) {
         $memberInfo = $member_db->where(array('username' => $data['username']))->find();
         if (!$memberInfo) {
             //添加一个随机密码,防止出现用户名密码都为空的情况
             $passwordinfo = password(rand(00, 999999));
             $add = array('username' => $data['username'], 'password' => $passwordinfo['password'], 'encrypt' => $passwordinfo['encrypt'], 'typeid' => 2, 'regtime' => time(), 'lastloginip' => get_client_ip(0, true), 'lastlogintime' => time());
             $memberid = $member_db->add($add);
         } else {
             $memberid = $memberInfo['memberid'];
         }
         if (!$memberid) {
             $this->error('登录失败');
         }
         unset($data['username']);
         $data = array_merge($data, array('memberid' => $memberid, 'addtime' => time()));
         $id = $member_oauth_db->add($data);
         if (!$id) {
             $this->error('登录失败');
         }
         $oauthInfo = $data;
     }
     //修改登陆时间
     $member_db->where(array('memberid' => $oauthInfo['memberid']))->save(array('lastloginip' => get_client_ip(0, true), 'lastlogintime' => time()));
     cookie('member_id', $oauthInfo['memberid']);
     cookie('member_name', $oauthInfo['nick']);
     cookie('member_head', $oauthInfo['head']);
     cookie('member_link', $oauthInfo['link']);
     $this->success('登录成功', U('Home/Index/index'));
 }
Exemple #2
0
 /**
  * 合并默认参数和额外参数
  * @param array $params  默认参数
  * @param array/string $param 额外参数
  * @return array:
  */
 protected function param($params, $param)
 {
     $params = parent::param($params, $param);
     /* 签名 */
     ksort($params);
     $param = array();
     foreach ($params as $key => $value) {
         $param[] = "{$key}={$value}";
     }
     $sign = implode('', $param) . $this->AppSecret;
     $params['sig'] = md5($sign);
     return $params;
 }