<?php
namespace apps\oauth\controllers;
use apps\base\controllers\FrontendController;
class IndexController extends FrontendController
{
public function actionIndex()
{
$type = I('get.type');
$back_url = I('get.back_url', '', 'urldecode');
$file = ROOT_PATH . 'plugins/connect/' . $type . '.php';
if (file_exists($file)) {
include_once ($file);
} else {
show_message('插件未启用', '返回上一页', U('user/login/index'));
}
$url = U('oauth/index/index', array(
'type' => $type,
'back_url' => empty($back_url) ? U('site/index/index') : $back_url
), true);
$config = $this->getOauthConfig($type);
// 判断是否安装
if (! $config) {
show_message('插件未启用', '返回上一页', U('user/login/index'));
}
$obj = new $type($config);
// 授权回调
if (isset($_GET['code']) & & $_GET['code'] != '') {
if ($res = $obj->callback($url, $_GET['code'])) {
if ($this->oauthLogin($res)) {
$this->redirect($_GET['back_url']);
}
$_SESSION['oauth_userinfo'] = $res;
$_SESSION['oauth_back_act'] = $_GET['back_url'];
$this->redirect(U('bind'));
} else {
show_message('授权回调失败', '返回上一页', U('user/login/index'));
}
return;
}
// 授权开始
$url = $obj->redirect($url);
ecs_header("Location: " . $url . "\n");
exit();
}
/**
* 用户绑定
*/
public function actionBind()
{
$userinfo = isset($_SESSION['oauth_userinfo']) ? $_SESSION['oauth_userinfo']:'';
if(empty($userinfo)){
$this->redirect(U('site/index/index'));
}
if (IS_POST){
$username = I('username');
$password = I('password');
if ($this->users->login($username, $password)) {
$data = array(
'aite_id' => $userinfo['openid'],
'sex' => $userinfo['sex'],
'user_picture' => $userinfo['avatar']
);
//微信用户绑定
if(class_exists('\apps\wechat\controllers\IndexController')) {
if (isset($_SESSION['openid']) & & !empty($_SESSION['openid'])) {
$wechat = $this->model->table('wechat_user')->field('parent_id, ect_uid')->where(array('openid' => $_SESSION['openid']))->find();
if ($wechat) {
$data['parent_id'] = $wechat['parent_id'];
}
if (empty($wechat['ect_uid'])) {
$this->db->table('wechat_user')->data(array('ect_uid' => $_SESSION['user_id']))->where(array('openid' => $_SESSION['openid']))->update();
//关注送红包
$this->sendBonus();
}
}
}
$condition['user_name'] = $username;
$this->db->table('users')->data($data)->where($condition)->update();
unset($_SESSION['oauth_userinfo']);
$back_url = empty($_SESSION['oauth_back_act']) ? U('site/index/index'):$_SESSION['oauth_back_act'];
$this->redirect($back_url);
} else {
show_message('授权绑定失败', '重新绑定', '', 'error');
}
}
$this->assign('page_title', '绑定已有帐号');
$this->display();
}
/**
* 用户注册
*/
public function actionRegister()
{
$userinfo = isset($_SESSION['oauth_userinfo']) ? $_SESSION['oauth_userinfo']:'';
if(empty($userinfo)){
$this->redirect(U('site/index/index'));
}
if(IS_POST){
$username = I('username');
$password = I('password');
$email = time(). rand(1, 9999) . '@' . $_SERVER['HTTP_HOST'];
require BASE_PATH . 'helpers/passport_helper.php';
if (register($username, $password, $email) !== false) {
$data = array(
'aite_id' => $userinfo['openid'],
'sex' => $userinfo['sex'],
'user_picture' => $userinfo['avatar']
);
//微信用户绑定
if(class_exists('\apps\wechat\controllers\IndexController')) {
if (isset($_SESSION['openid']) & & !empty($_SESSION['openid'])) {
$wechat = $this->model->table('wechat_user')->field('parent_id, ect_uid')->where(array('openid' => $_SESSION['openid']))->find();
if ($wechat) {
$data['parent_id'] = $wechat['parent_id'];
}
if (empty($wechat['ect_uid'])) {
$this->db->table('wechat_user')->data(array('ect_uid' => $_SESSION['user_id']))->where(array('openid' => $_SESSION['openid']))->update();
//关注送红包
$this->sendBonus();
}
}
}
$condition['user_name'] = $username;
$this->db->table('users')->data($data)->where($condition)->update();
unset($_SESSION['oauth_userinfo']);
$back_url = empty($_SESSION['oauth_back_act']) ? U('site/index/index'):$_SESSION['oauth_back_act'];
$this->redirect($back_url);
} else {
show_message('授权注册失败', '重新注册', '', 'error');
}
return;
}
$this->assign('page_title', '注册新用户');
$this->display();
}
/**
* 获取第三方登录配置信息
*
* @param type $type
* @return type
*/
private function getOauthConfig($type)
{
$sql = "SELECT auth_config FROM {pre}touch_auth WHERE `type` = '$type'";
$info = $this->db->getRow($sql);
if ($info) {
$res = unserialize($info['auth_config']);
$config = array();
foreach ($res as $key => $value) {
$config[$value['name']] = $value['value'];
}
return $config;
}
return false;
}
/**
* 授权自动登录
* @param unknown $res
*/
private function oauthLogin($res)
{
$condition['aite_id'] = $res['openid']; // 登录标识
$userinfo = $this->db->table('users')
->field('user_name')
->where($condition)
->find();
if ($userinfo) {
$this->doLogin($userinfo['user_name']);
return true;
} else {
return false;
}
}
/**
* 设置成登录状态
* @param unknown $username
*/
private function doLogin($username)
{
$this->users->set_session($username);
$this->users->set_cookie($username);
update_user_info();
recalculate_price();
}
/**
* 关注送红包
*/
private function sendBonus(){
$rs = $this->db->query("SELECT name, keywords, command, config FROM {pre}wechat_extend WHERE keywords like '%$keywords%' and enable = 1 and wechat_id = 1 ORDER BY id ASC");
$file = ROOT_PATH . 'plugins/wechat/' . $rs[0]['command'] . '/' . $rs[0]['command'] . '.class.php';
if (file_exists($file)) {
require_once ($file);
$wechat = new $rs[0]['command']();
$data = $wechat->show($_SESSION['openid'], $rs);
if (! empty($data)) {
$wxinfo = model()->table('wechat')
->field('id, token, appid, appsecret, encodingaeskey')
->where(array('id'=>1, 'status'=>1))
->find();
$config['token'] = $wxinfo['token'];
$config['appid'] = $wxinfo['appid'];
$config['appsecret'] = $wxinfo['appsecret'];
$config['encodingaeskey'] = $wxinfo['encodingaeskey'];
$weObj = new \vendor\Wechat($config);
$weObj->sendCustomMessage($data['content']);
}
}
}
}