<?php
!defined('IN_TOA') && exit('Access Denied!');
/*
	[Office 515158] (C) 2009-2015 天生创想 Inc.
	$Id: function_dingtalk.php 1209087 2015-01-08
*/
function dingtalk($data=0){
	global $_CACHE;
	get_cache('dingtalk_config');
	foreach ( $_CACHE['dingtalk_config'] as $row) {
		if($row['name']==$data){
			$html=$row[value];
		}
	}
	return $html;
		
}
//获取Access_Token
function dingtalk_Token(){
	global $db;
	$assdate=strtotime(get_date('Y-m-d H:i:s',PHP_TIME))-strtotime(dingtalk('assdate'));
	if($assdate>=7000){
		$url="https://oapi.dingtalk.com/gettoken?corpid=".dingtalk('CorpID')."&corpsecret=".dingtalk('Secret')."";
		$res = Utility::HttpRequest($url);
		$res=json_decode($res);
		$db->query("UPDATE ".DB_TABLEPRE."dingtalk_config set value='".$res->access_token."' WHERE name = 'Access_Token'");
		$db->query("UPDATE ".DB_TABLEPRE."dingtalk_config set value='".get_date('Y-m-d H:i:s',PHP_TIME)."' WHERE name = 'assdate'");
		oa_mana_recache('dingtalk_config','name','id');
		//return $res->access_token;
	}
	return dingtalk('Access_Token');
}
function dingtalk_code($code){
	dingtalk_Token();
	global $db;
	global $_USER;
	if(!$_USER->Id){
		//setcookie("UserId",'', time()-3600);
		$urllist="https://oapi.dingtalk.com/user/getuserinfo?access_token=".dingtalk('Access_Token')."&code=".trim($code)."";
		$res1 = Utility::HttpRequest($urllist);
		$res=json_decode($res1);
		$row = $db->fetch_one_array("SELECT a.id,a.jituanid,a.username,a.password FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."dingtalk_member b WHERE b.userid=a.id and b.dingtalkid = '".$res->userid."'");
		if (trim($row['id'])) {
			//setcookie("tUserId",trim($row['id']), time()+3600*10);
			//setcookie("Userjituanid",trim($row['jituanid']), time()+3600*10);
			//setcookie("pUserId", trim($row['id']),time()+3600*10, '/');
			//setcookie("pjituanid",trim($row['jituanid']),time()+3600*10, "/");
			$flag = $_USER->publiclogin($row['username'], $row['password']);
			return trim($row['id']);
		}
	}
	
}

function dingtalksms($person=0,$content){
	global $db;
	global $_USER;
	if(mysql_num_rows(mysql_query("SHOW TABLES LIKE 'toa_dingtalk_config'"))==1){
		//获取数据，判断在线的不发，没有关注的不发送
		$persons=get_realid($person);
		if($person=='0'){
			$sql = "SELECT dingtalkid FROM ".DB_TABLEPRE."dingtalk_member where userid!='0'";
		}else{
			if(trim($persons)!=''){
				$where="  userid in(".$persons.")";
			}else{
				$where=" userid=-1";
			}
			$sql = "SELECT dingtalkid FROM ".DB_TABLEPRE."dingtalk_member where userid!='0' and ".$where;
		}
		$query = $db->query($sql);
		//$department='';
		$userid='';
		while ($row = $db->fetch_array($query)) {
			//$department.=$row['department'].'|';
			$userid.=$row['dingtalkid'].'|';
		}
		if(substr($userid, 0, -1)!=''){
			dingtalk_Token();
			$url="https://oapi.dingtalk.com/message/send?access_token=".dingtalk('Access_Token')."";
			$option = array(
				"touser"=>substr($userid, 0, -1),
				"cid"=>dingtalk('CorpID'),
				"msgtype"=>"text",
				"text"=>array("content"=>$content)
			);
			Utility::POST($url,json_encode($option));
		}
	}
	return;
}

?>