<?php
/*
	[Office 515158] (C) 2009-2019 天生创想 Inc.
	$Id: sms.class.php 1209087 2018-01-08 08:58:28Z baiwei.jiang $
*/
!defined('IN_TOA') && exit('Access Denied!');
class SMS
{
	//消息发送
	public function sms_add($receiveuser,$content){
		global $db;
		global $_USER;
		$sms_send = array();
		$sms_send['receiveuser']=$receiveuser;
		$sms_send['content']=trim($content);
		$sms_send['senduser']=$_USER->id;
		$sms_send['senddate']=get_date('Y-m-d H:i:s',PHP_TIME);
		insert_db('sms_send',$sms_send);
		$sendid=$db->insert_id();
		//获取字符串
		$receiweixin=$receiveuser; 
		$receiveuser=explode(',',$receiveuser); 
		//发送消息表
		for($i=0;$i<sizeof($receiveuser);$i++){
			//接收消息表
			$sms_receive = array();
			$sms_receive['senduser']=$_USER->id;
			$sms_receive['senddate']=get_date('Y-m-d H:i:s',PHP_TIME);
			$sms_receive['content']=trim($content);
			$sms_receive['receiveuser']=get_userid($receiveuser[$i]);
			$sms_receive['type']=3;
			$sms_receive['smskey']=1;
			$sms_receive['sendid']=$sendid;
			//接收消息表
			insert_db('sms_receive',$sms_receive);
			SMS::new_add($_USER->id,get_userid($receiveuser[$i]),get_date('Y-m-d H:i:s',PHP_TIME),$content,'0');
		}
		//更新微信通知
		$c=unserialize(trim($content));
		weixinsms($receiweixin,$c['content']);
		dingtalksms($receiweixin,$c['content']);
		return $sendid;
	}
	
	//更新消息服务表
	public function new_add($uid,$user,$updatedate,$content,$online){
		if($uid){
			global $db;
			$sms_service = array();
			$sms_service['updatedate']=$updatedate;
			$sms_service['content']=$content;
			$sms_service['online']=$online;
			$sql = "SELECT sid FROM toa_sms_service  WHERE uid = '".$uid."' and user = '".$user."'";
			$row = $db->fetch_one_array($sql);
			if(!$row['sid'] && $uid!=$user){
				$sms_service['uid']=$uid;
				$sms_service['user']=$user;
				insert_db('sms_service',$sms_service);
				
				$sms_service['uid']=$user;
				$sms_service['user']=$uid;
				insert_db('sms_service',$sms_service);
			}else{
				update_db('sms_service',$sms_service, array('uid' => $uid,'user' => $user));
			}
			
		}
		return;
	}
	//手机短信发送
	public function phone_add($phone,$content,$name){
		//接口信息 url
		//手机 $phone[$i]
		//内容 $content
		global $db;
		global $_USER;
		$phone=explode(',',$phone);//手机
		$name=explode(',',$name);//姓名
		$channelid = random(6);
		$ugcode_vod= new ugcode();
		$contentgdb2312=$ugcode_vod->ugcode_vod(1,$content);
		for($i=0;$i<sizeof($phone);$i++){
			if(trim($phone[$i])){
				$date=get_date('Y-m-d H:i:s',PHP_TIME);
				$url=''; //HTTP发送接口
				$res = Utility::HttpRequest($url);//GET方式提交
				if($res!='' && $url!=''){
					$type='1';
				}else{
					$type='2';
				}
				//数据入库
				$send = array();
				$send['content']=$content;
				$send['receivephone']=$phone[$i];
				$send['sendperson']=$_USER->id;
				$send['receiveperson']=$name[$i];
				$send['date']=$date;
				$send['type']=$type;
				$send['channelid']=$channelid;
				insert_db(DB_JT.'phone_send',$send);
				sleep(1);
			}
		}	
	}
	/*
	$content：发送消息的内容
	$user：接收用户
	$phone：手机号
	$mail：邮箱
	
	*/
	function Box_Add($content,$user,$phone,$mail,$sms_box,$sms_phone,$sms_mail,$pcontent){
		if($sms_box){
			//SMS_ADD_POST($user,$content,0,0,$UserId);
			$sendid=sms::sms_add($user,$content);
		}
		if($sms_phone){
			//PHONE_ADD_POST($phone,$content,$user,0,0,$UserId);
			//$pcontent=SmsTpl('任务管理',$title,'sms');
			sms::phone_add($phone,$pcontent,$user);
		}
	}
	//显示消息
	public function Curl($url,$name){
		if($url){
			return '    <a href="'.$url.'" target="_blank">'.$name.'>></a>';
		}
	}
	public function ContentShow($name,$content,$url,$type){
		switch ($type)
		{
			case 1:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'审批办理');
			  break;
			case 2:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'审批办理');
			  break;
			case 3:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'审批办理');
			  break;
			case 4:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'审批办理');
			  break;
			case 5:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看');
			  break;
			case 6:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看处理');
			  break;
			case 7:
			  $html='【'.$name.'】'.$content;
			  break;
			case 8:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看');
			  break;
			case 9:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看处理');
			  break;
			case 10:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看处理');
			  break;
			case 11:
			  $html='【'.$name.'】'.$content.sms::Curl($url,'查看处理');
			  break;
			default:
			  return;
		}
		return $html;
	}
	
	//获取当前消息用户
	function UserList($user=0){
		global $db;
		global $_USER;
		$user1='';//
		$sql = "SELECT id FROM ".DB_TABLEPRE."user where id!='".$user."' and ischeck=1 and jituanid='".$_USER->jituanid."' order by numbers asc";
		$query = $db->query($sql);
		while ($row = $db->fetch_array($query)) {
			$user1.= $row['id'].',';
		}
		return substr($user1, 0, -1);
	
	}

}

?>