<?php
function send_message($wapid,$type,$sendid,$toid,$title,$str){
/*
$wapid   网站ID
$type    消息类型
$sendid  发送者的虚拟ID
$toid    接收者的虚拟ID
$title   标题【可选】
$str     内容【必选】 
ask_1    文字消息模式
ask_2    图文消息模式
ask_3    文件消息模式
ask_4    短信消息模式
*/
if($type=='ask_1'){
	$title = NULL;//删除标题
}else{
	if($title==NULL||$title==' '){
	$title='未命名的消息';//为空标题添加标题
	}
}
$sendtime = time();//初始化发送时间
$redtime  = time();//初始化阅读时间
if($str == NULL){
	return  2;//返回2 内容为空
}else{
if(get_id($wapid,$toid)!=0){
$sendinfo = get_id($wapid,$sendid).':'.get_id($wapid,$toid);
	$sql = 'INSERT INTO `ask_list` (`sendinfo`,`type`, `title`, `content`, `sendtime`, `readtime`, `isread`, `filepath`, `spic`, `frominfo`, `siteid`) VALUES (\''.$sendinfo.'\',\''.$type.'\', \'NULL\', \''.$str.'\', \''.$sendtime.'\', \''.$readtime.'\', \'0\', \'\', \'\', \'\', \'1000\');'; 
	$send = mysql_query($sql);
	if($send && mysql_affected_rows()>0){
	return 1;//返回1 发送成功
	}else{
	return 0;//返回0 发送失败
		}
	}else{
	return 3;//返回3 接收对象不存在
		}
	}
}
function get_id($wapid,$xid){
$sql="SELECT id,name,nc,xid,site_u FROM user WHERE xid='{$xid}' AND site_u='{$wapid}'";
$result = mysql_query($sql);
if($result && mysql_num_rows($result)>0){
	list($b_id,$b_name,$b_nc,$_xid,$_site_u) = mysql_fetch_row($result);
	return $b_id;
}else{
	return NULL;
	}
}
function id_ex($str,$type){
 list($userinfo['sendid'],$userinfo['toid']) = explode(':',$str);
 if($type == 1){
	return $userinfo['sendid'];
	}else{
	return $userinfo['toid'];
	}
}
function get_head($id){
$sql="SELECT id,name,head,xid,site_u FROM user WHERE id='{$id}'";
$result = mysql_query($sql);
if($result && mysql_num_rows($result)>0){
	list($b_id,$b_name,$head,$_xid,$site_u) = mysql_fetch_row($result);
	return $head;
}else{
	return 0;
	}
}
function get_message_list($sendid,$toid,$wapid){
$sendide = get_id($wapid,$sendid);
$toide = get_id($wapid,$toid);
$sendinfo1 = $sendide.':'.$toide;
$sendinfo2 = $toide.':'.$sendide;
$sql = "SELECT * FROM ask_list WHERE type='ask_1' AND (sendinfo='{$sendinfo1}' XOR sendinfo='{$sendinfo2}')"; 
$lists = mysql_query($sql);
if($lists && mysql_num_rows($lists)>0){
	$nums = mysql_num_rows($lists);
	$i = 1;
	while(list($sendid,$sendinfo,$type,$title,$content,$sendtime,$readtime,$isread,$filepath,$spic,$frominfo,$siteids)=mysql_fetch_row($lists)){
	
		if($sendinfo == $sendinfo1){
			$head = get_head(id_ex($sendinfo,1));
			$type = 2;
		}else{
		//查看列表则自动把对方发来的消息标记为已读
		mysql_query("UPDATE ask_list SET isread='1' WHERE askid='{$sendid}'");
			$head = get_head(id_ex($sendinfo,2));
			$type = 1;
		}
		if($i == $nums){
			$content = '<a name="ask_bottom" id="ask_bottom"></a>'.$content;
		}
		$list .= get_ask_view($content,$type,$head);  
		$i++;
	 }
	}else{
		$list ='暂无聊天记录';
	}
	return $list;
}
function get_ask_view($content,$type,$head){
if($type==1){
$view = '
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="50" align="center" valign="top" style="padding-top:10px;" >
<img src="'.$head.'" style="height:40px;width:40px;">
</td><td>

<table  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="38" height="17" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_01.jpg)"></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_02.jpg)"></td>
    <td width="23" height="100%"  style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_03.jpg)"></td>
  </tr>
  <tr>
    <td valign="middle" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_04.jpg)">
    
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td></td></tr>
      <tr>
        <td><img src="http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_07.jpg" width="38" height="27" alt=""></td>
      </tr>
      <tr>
        <td></td>
      </tr>
    </table>
    
    
    </td>
    <td style="background-color:#efefef;"> <div class="ask_main_content" >'.$content.'</div></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_06.jpg)"></td>
  </tr>
  <tr>
    <td  height="13" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_09.png)"></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_10.jpg)"></td>
    <td  style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_11.jpg)"></td>
  </tr>
</table></td></tr></table>';
}else{
$view = '<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="right">

<table  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="38" height="17" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_01.jpg)"></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_02.jpg)"></td>
    <td width="23" height="100%"  style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask2_01.png)"></td>
  </tr>
  <tr>
    <td valign="middle" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_04.jpg)"></td>
    <td style="background-color:#efefef;"> <div class="ask_main_content" >'.$content.'</div></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask2_04.png)"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td></td>
      </tr>
      <tr>
        <td><img src="http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask2_07.png" width="38" height="27" alt="" /></td>
      </tr>
      <tr>
        <td></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td  height="13" style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_09.png)"></td>
    <td style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask_10.jpg)"></td>
    <td  style="background-image:url(http://'.$_SERVER['HTTP_HOST'].'/style/ask/ask2_09.png)"></td>
  </tr>
</table></td>
<td width="50" align="center" valign="top" style="padding-top:10px;" >
<img src="'.$head.'" style="height:40px;width:40px;">
</td>
</tr></table>';
}
 return $view;
}
?>