<?php
if (!defined('BASEPATH')) 
	exit('No direct script access allowed');

class login extends CI_Controller{
	public function __construct(){
        parent::__construct();
		header('Access-Control-Allow-Origin:*');
	    header('Access-Control-Allow-Methods:POST, GET');
	    header('Access-Control-Max-Age:0');
	    header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
	    header('Access-Control-Allow-Credentials:true');
		$this->load->helper('dinglogin');
		$this->load->helper('cookie');
		$this->appid = 'dingoalutppbmywhkyorfp';
		$this->appsecret = '6vAG1GwqwUE0b3g-8g0ZooKXt0SVVwcypIYbDLVy_MyS0jDV89rE68hXOV6WL0HO';
		$this->AppKey = 'dingjcbxgidah9uspeuc';
		$this->AppSecret = 'C4-8rUDK1u5Twpsw7U3yo42s_bbYxFIqzLMp2j7uI80Sa8D-OPbtSoCMgZxHxo2d';
		$this->load->model('ding_login_model');
    }
	
	//展示第三方登录页HTTP_REFERER
	public function index(){
		delete_cookie('returnurl');
		if(isset($_REQUEST['returnurl'])){
			$returnurl = $_REQUEST['returnurl'];
		}else{
			$returnurl = 'http://www.mycht.cn';
		}
		$this->input->set_cookie('returnurl',$returnurl,60);
		$this->load->view('login-indx');
	}
	
	//判断是否存在该用户
	public function auth_login(){
		$code = $_REQUEST['code'];
		$microtime = get_microtime();
		
		$signature = get_loginsign($microtime,$this->appsecret);
		$url = 'https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey='.urlencode($this->appid).'&timestamp='.urlencode($microtime).'&signature='.urlencode($signature);
		$data = array();
		$data['tmp_auth_code'] = $code;
		$userinfo = GetPost_http($url,json_encode($data),'json');
		$userinfo_data = json_decode($userinfo);
		//如果不存在unionid,则不往下执行
		if(!isset($userinfo_data->user_info->unionid)){
			header("HTTP/1.1 404 Not Found");  
			exit('{"errcode":404,"errmsg":"不存在unionid！"}');
		}
		
		$unionid = $userinfo_data->user_info->unionid;
		//获取access_token
		$access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
		$access_token = GetPost_http($access_url,'','');
		$access_token = json_decode($access_token)->access_token;
		
		//通过unionid获取userid(在公司内部进行查找,如果人员不存在则不会返回userid)
		$userid_url = 'https://oapi.dingtalk.com/user/getUseridByUnionid?access_token='.$access_token.'&unionid='.$unionid;
		
		$userid_json = GetPost_http($userid_url,'','');
		if(!isset(json_decode($userid_json)->userid)){
			header("HTTP/1.1 404 Not Found");  
			exit($userid_json);
		}
		$userid = json_decode($userid_json)->userid;
		
		$user_url = 'https://oapi.dingtalk.com/user/get?access_token='.$access_token.'&userid='.$userid;
		$user_info = GetPost_http($user_url,'','');
		
		$user_data = json_decode($user_info);
		$userinfo = array();
		$userinfo['name'] = $user_data->name;
		$userinfo['position'] = $user_data->position;
		$userinfo['unionid'] = $user_data->unionid;
		$userinfo['avatar'] = $user_data->avatar;
		$userinfo['orgEmail'] = $user_data->orgEmail;
		$userinfo['mobile'] = $user_data->mobile;
		$userinfo['datetime'] = time();
		$this->ding_login_model->addorupdateuser($userinfo);
		$this->input->set_cookie('dingname',$user_data->name,2592000);
		$this->input->set_cookie('dingunionid',$user_data->unionid,2592000);
		redirect(get_cookie('returnurl'));
	}
	
}