using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NullStudio.Fetion_SDK;
using System.Data.OleDb;
namespace PowerTalkClient
{
public partial class Fetion : Form
{
FrmMain FetionServerTemp = null;
public Fetion()
{
InitializeComponent();
}
public Fetion(FrmMain ftmp)
{
InitializeComponent();
//FetionServerTemp = ftmp;
//FetionServerTemp.PTB_Runner.Text = "运行";
//FetionServerTemp.timer1.Enabled = false;
}
///
/// 登陆飞信
///
private bool FetionLogin(string userName,string pwd)
{
sdk.AccountManager.FillUserIdAndPassword(userName, pwd, false);//第三个参数为是否自动登陆
bool isOk=sdk.AccountManager.Login();//登陆
return isOk;
}
///
/// 发送消息
///
private bool SendFetionMsg(string userName, string msg)
{
bool isOk = false;
try
{
int state = sdk.ContactControl.SendIM.SendIM(userName, msg);
}
catch { isOk = false; }
return isOk;
}
///
/// 发送手机消息
///
private bool SendFetionMobileMsg(string userName, string msg)
{
bool isOk = false;
try
{
int state = sdk.ContactControl.SendSMS.SendSMS(userName, msg);
}
catch { isOk = false; }
return isOk;
}
///
/// 加载好友列表
///
/// 飞信sdk对象
///
private List
{
List
try
{
List
for (int i = 0; i < list.Count; i++)
{
try
{
FetionFriend fetionFriend = new FetionFriend();//自定义飞信用户实体信息类
fetionFriend.UserId = int.Parse(list[i].Uri.Id);
fetionFriend.Nickname = list[i].DisplayName;
fetionFriend.Mobile = list[i].PersonalInfo.MobileNo;
fetionFriends.Add(fetionFriend);
}
catch { }
}
}
catch { fetionFriends = new List
return fetionFriends;
}
//5^1^a^s^p^x
///
/// 飞信sdk出现错误
///
private void Sdk_SDK_Error(object sender, NullStudio.Fetion_SDK.Event.SDK_ErrorEventArgs fe)
{
this.txtBox.Text += "信息:"+fe.Message.Message+"\r\n";
}
private string userName = "";
private string userPwd = "";
///
/// 当前飞信号状态
///
private void Sdk_SDK_UserSatusChange(object sender, Imps.Client.Core.UserSatusChangedEventArgs e)
{
switch (e.NewStatus.ToString())
{
case "Disconnected"://断开了网络
FetionLogin(userName, userPwd);//重新登陆
break;
case "Initialized"://已初始化
break;
case "Loginning"://登陆中
break;
case "Logon"://已登陆
sdk.AccountManager.CurrentUser.Presence.AsyncChangeMainPresence(Imps.Common.MainPresence.Online, Imps.Common.MainPresence.Online.ToString());//设置PC在线
break;
case "Logoff"://离开网络
break;
case "Logouting"://注销中
break;
case "None"://无状态
break;
case "OfflineLogon"://离线
break;
case "StandBy"://待机
break;
case "WaitReconnect"://等待
break;
}
this.txtBox.Text += "状态"+e.NewStatus.ToString() + "\r\n";
}
///
/// 接收到的消息
///
private void sdk_SDK_ReceiveMessage(object sender, NullStudio.Fetion_SDK.Event.SDK_ReceiveMessageEventArgs fe)
{
string Content = fe.Message.Substring(fe.Message.IndexOf("\r\n")+2);
string[] ContentS = Content.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
int lc=Content.IndexOf(':');
string UserTo=ContentS[0].ToString();
string Msg=Content.Substring(lc+1);
if (Msg[0] == '@')//如果是转移字符串
{
string CChar = Msg.Substring(1);
int topi = 0;
if (int.TryParse(CChar, out topi))//如果为转意数字的话
{
string ListStr = textBox6.Text;
string[] ChangeChar = ListStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string istr in ChangeChar)
{
string[] OneChar = istr.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
string One = OneChar[0];
if (CChar == One)
{
Msg = OneChar[1];
}
}
}
}
PowerTalkService.FetionInterFace PTFIF = new PowerTalkClient.PowerTalkService.FetionInterFace();
PTFIF.ChatAdd(textBox3.Text, "游客"+UserTo,Msg);
this.txtBox.Text += "" + string.Format(fe.Message, fe.Contact.PersonalInfo.Nickname +"("+ fe.Contact.Uri.Sid.ToString()+")") + "\r\n";
}
//5*1^a-s-p-x
private FetionSDK sdk = new FetionSDK();//实例化飞信sdk
private void Fetion_Load(object sender, EventArgs e)
{
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);//消息接收事件
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(Sdk_SDK_UserSatusChange);//用户状态事件
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(Sdk_SDK_Error);//sdk错误事件
OleDbConnection Conn = Common.OleSysConn;
Conn.Open();
try
{
OleDbCommand cmd = new OleDbCommand("select * from 系统设置", Conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox1.Text= dr["飞信用户名"].ToString();
textBox2.Text = dr["飞信密码"].ToString();
// textBox6.Text = dr["飞信用语"].ToString();
textBox6.Text = "1:您好!\r\n2:有什么可以帮助您的?\r\n3:是的,我们的产品是开源但不免费的。";
}
}
catch
{ }
finally
{
Conn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
userName=textBox1.Text;
userPwd = textBox2.Text;
FetionLogin(textBox1.Text, textBox2.Text);
List
foreach (FetionFriend MyUser in MyUsers)
{
this.txtBox.Text += MyUser.Mobile+"\r\n";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{ SendFetionMsg(textBox3.Text, textBox4.Text); }
else
{ SendFetionMobileMsg(textBox3.Text, textBox4.Text); }
}
private void button4_Click(object sender, EventArgs e)
{
PowerTalkService.FetionInterFace PTFIF = new PowerTalkClient.PowerTalkService.FetionInterFace();
PowerTalkService.ChatInfo[] LPCI= PTFIF.GetChatInfos(textBox3.Text);
foreach(PowerTalkService.ChatInfo PTSCI in LPCI)
{
this.txtBox.Text += "接收者:" + PTSCI.Reciver + "发送者:" + PTSCI.Sender + "内容:" + PTSCI.SendContent + "\r\n";
SendToMoble(PTSCI.Reciver, PTSCI.Sender, PTSCI.SendContent);
}
}
///
/// 发送操作
///
/// 手机号码
/// 发送者
/// 内容
public void SendToMoble(string MobleNum,string Sender,string Content)
{
if (checkBox1.Checked)
{ SendFetionMsg(MobleNum, Sender + ":" + Content); }
else
{ SendFetionMobileMsg(MobleNum, Sender + ":" + Content); }
}
private void button3_Click(object sender, EventArgs e)
{
if (timer1.Enabled == false)
{
button3.Text = "停止";
this.txtBox.Text += "服务运行";
timer1.Enabled = true;
}
else
{
button3.Text = "运行";
this.txtBox.Text += "服务停止";
timer1.Enabled = false;
}
}
//5^1^a^s^p^x
private void timer1_Tick(object sender, EventArgs e)
{
PowerTalkService.FetionInterFace PTFIF = new PowerTalkClient.PowerTalkService.FetionInterFace();
PowerTalkService.ChatInfo[] LPCI = PTFIF.GetChatInfos(textBox3.Text);
foreach (PowerTalkService.ChatInfo PTSCI in LPCI)
{
this.txtBox.Text += "接收者:" + PTSCI.Reciver + "发送者:" + PTSCI.Sender + "内容:" + PTSCI.SendContent + "\r\n";
PTSCI.SendContent = Common.MatchImg(PTSCI.SendContent);
PTSCI.SendContent = HttpContext.Current.Server.HtmlDecode(PTSCI.SendContent);
SendToMoble(PTSCI.Reciver, PTSCI.Sender, PTSCI.SendContent);
}
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
timer1.Interval =int.Parse(textBox5.Text);
}
private void txtBox_TextChanged(object sender, EventArgs e)
{
if (txtBox.Text.Length > 32767)
{
txtBox.Text = "";
}
}
private void Fetion_FormClosing(object sender, FormClosingEventArgs e)
{
//FetionServerTemp.PTB_Runner.Text = "停止";
//FetionServerTemp.timer1.Enabled = true;
//button3.Text = "运行";
//timer1.Enabled = false;
}
}
public class FetionFriend
{
public int UserId =0;
public string Nickname = "";
public string Mobile = "";
}
}
No comments:
Post a Comment