困难重重,首先是返回各种状态,随时在过程中修改,有图有真相
注:这里需要用到子佩录音盒子,相关驱动,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using Hcl;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Text.RegularExpressions;
namespace pstndemo
{
public partial class Form1 : Form
{
protected dbo conn = common.make();
[StructLayout(LayoutKind.Sequential)]
public struct tag_pstn_Data
{
public Int16 uChannelID;//设备通道
public Int32 lPlayFileHandle;//播放句柄
public Int32 lRecFileHandle;//录音句柄
}
public string[] args = null;
public string voiceurl = String.Empty;
public string voiceurl_server = String.Empty;
public string mobile = String.Empty;
public int type=0;
public int state = 1;
public DateTime start = default(DateTime);
public DateTime end = default(DateTime);
tag_pstn_Data[] m_tagpstnData = new tag_pstn_Data[BriSDKLib.MAX_CHANNEL_COUNT];
public Form1()
{
InitializeComponent();
}
public Form1(string[] args)
{
InitializeComponent();
this.args = args;
}
public void Form1_Load(object sender, EventArgs e)
{
opendev_Click(null, null);
this.Text = "***,电话营销小助手。";
}
private void AppendStatus(string ms)
{
if (msg.Text.Length != 0) msg.Text += "\r\n";
msg.Text = ms;
if (isend)
{
msg.Text =ms+""+ ",本次通话结束";
}
}
public static string FromUnicodeByteArray(byte[] characters)
{
UnicodeEncoding u = new UnicodeEncoding();
string ustring = u.GetString(characters);
return ustring;
}
public static string FromASCIIByteArray(byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
public bool isend = true;
public void stopDeal() {
try
{
stoprecfile_Click();
PostMusicToServer(voiceurl);
isend = true;
refusecallin_Click();
DataTable dt = conn.GetDataTable("select TOP 1 * from DW_PHONE WHERE 1<>1"); ;
DataRow dr = dt.NewRow();
//要么ajax传递过来,要么获取登陆ehr域号码,
dr["ID"] = Guid.NewGuid().ToString();
dr["WorkNo"] = "admin";
dr["Mobile"] = mobile;
dr["Type"] = type;
dr["CustomerSerial"] = conn.GetScalar("select top 1 CustomerSerial from WFT_Customer_All where CustomerMobile='" + mobile + "'");
int seconds = (int)Math.Round(((DateTime.Now - start).TotalSeconds),0);//四舍五入
dr["Times"] = seconds;
//dr["Times"] = (int)(seconds / 60) + "分" + (seconds % 60) + '秒';
dr["VideoFileUrl"] = voiceurl_server;
//结束录音需要将文件上传到服务器
dr["VideoFileUpladTime"] = DateTime.Now;
//dr["FollowAbstract"] = type;
//dr["AbstractContent"] = "admin";
dr["AddTime"] = DateTime.Now;
dr["State"] = state;
dt.Rows.Add(dr);
conn.Insert(dt, "DW_PHONE");
//conn.Update("insert into ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
public void PostMusicToServer( string voiceurl) {
string tst = voiceurl;
FileStream fs = new FileStream(voiceurl, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
fs.Dispose();
// 将文件流加入到请求的文件流当中
string voice2serverurl = ConfigurationManager.ConnectionStrings["voice2serverurl"].ConnectionString + "&filename=" + voiceurl_server;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(voice2serverurl);//voice2serverurl
req.Method = "POST";
req.ContentLength = bytes.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
string res = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
if (res=="ok")
{
if (File.Exists(voiceurl))
{
File.Delete(voiceurl);
}
}
}
public void recordVoice(string mobile){
voiceurl = ConfigurationManager.ConnectionStrings["voiceurl"].ConnectionString + mobile + "_" + Guid.NewGuid().ToString() + ".wav";
voiceurl_server = ConfigurationManager.ConnectionStrings["voiceurl"].ConnectionString + mobile + "_server_" + Guid.NewGuid().ToString() + ".wav";
m_tagpstnData[0].lRecFileHandle = BriSDKLib.QNV_RecordFile(0, BriSDKLib.QNV_RECORD_FILE_START, 0, 0, voiceurl);
start = DateTime.Now;
}
//窗体执行覆盖系统方法,非常重要的一个方法
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case BriSDKLib.BRI_EVENT_MESSAGE:
{
BriSDKLib.TBriEvent_Data EventData = (BriSDKLib.TBriEvent_Data)Marshal.PtrToStructure(m.LParam, typeof(BriSDKLib.TBriEvent_Data));
string strValue = "";
switch (EventData.lEventType)
{
case BriSDKLib.BriEvent_PhoneHook:
{
strValue = "座机已接起";
isend = false;
} break;
case BriSDKLib.BriEvent_PhoneHang:
{
strValue = "座机已挂机";
stopDeal();
} break;
case BriSDKLib.BriEvent_CallIn:
{
//两声响铃结束后开始呼叫转移到CC
//isend = false;
//strValue = "来电响铃";
} break;
case BriSDKLib.BriEvent_GetCallID:
case BriSDKLib.BriEvent_RecvedFSK:
{
//接收到来电,显示手机号码机主相关信息
//GetPhonePersonInfo();
mobile=FromASCIIByteArray(EventData.szData);
//mobile = mobile.Substring(0,mobile.IndexOf('\\'));
//mobile = mobile.Replace(@"\0", "");//后面产生很多\0\0
//mobile = mobile.Trim();
mobile = Regex.Replace(mobile, "\0", String.Empty);
strValue = "您有一个来电: " + mobile;
BindDataByMobile(mobile);
recordVoice(mobile);
type = 1;
isend = false;
//马上找到客户信息
} break;
case BriSDKLib.BriEvent_StopCallIn:
{
//开启设备后,产生一个未接来电,
strValue = "您有一个未接电话 ";
stopDeal();
} break;
case BriSDKLib.BriEvent_GetDTMFChar:
if (FromASCIIByteArray(EventData.szData) == " *")
{
strValue = "对方拒接";
state = 0;
stopDeal();
}
break;
case BriSDKLib.BriEvent_RemoteHang:
{
strValue = "对方拒接或无人接听";
state = 0;
stopDeal();
} break;
case BriSDKLib.BriEvent_Busy:
{
strValue = "通话中断";
state = 2;
stopDeal();
} break;
case BriSDKLib.BriEvent_DialTone:
strValue = "电话拨通中,请稍等... ";
break;
case BriSDKLib.BriEvent_PhoneDial:
strValue = "电话拨号中 " + FromASCIIByteArray(EventData.szData);
mobile = FromASCIIByteArray(EventData.szData);
type = 0;
break;
//电话拨通后的状态,
case BriSDKLib.BriEvent_RingBack:
strValue = "等待接听中...";
recordVoice(textBox1.Text);
break;
case BriSDKLib.BriEvent_DevErr:
{
if (EventData.lResult == 3)
{
strValue = "电话机移除或故障 ";
state = 3;
}
} break;
default:
break;
}
if (strValue.Length > 0)
{
AppendStatus(strValue);
}
} break;
default:
base.DefWndProc(ref m);
break;
}
}
//C#动态调用非托管c++函数
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate Int32 ProcEventCallback(Int16 uChannelID, Int32 dwUserData, Int32 lType, Int32 lHandle, Int32 lResult, Int32 lParam, IntPtr pData, IntPtr pDataEx);
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
private static ProcEventCallback procEvent;
static Int32 procEventCallback(Int16 uChannelID, Int32 dwUserData, Int32 lType, Int32 lHandle, Int32 lResult, Int32 lParam, IntPtr pData, IntPtr pDataEx)
{
BriSDKLib.TBriEvent_Data tbe = new BriSDKLib.TBriEvent_Data { };
tbe.uChannelID = uChannelID;
tbe.lEventType = lType;
tbe.lEventHandle = lHandle;
tbe.lParam = lParam;
tbe.lResult = lResult;
tbe.szData = new byte[BriSDKLib.MAX_BRIEVENT_DATA];
Marshal.Copy(pData, tbe.szData, 0, BriSDKLib.MAX_BRIEVENT_DATA - 1);
int size = Marshal.SizeOf(typeof(BriSDKLib.TBriEvent_Data));
IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(tbe, bufferIntPtr, false);
SendMessage((IntPtr)dwUserData, BriSDKLib.BRI_EVENT_MESSAGE, IntPtr.Zero, bufferIntPtr);
Marshal.FreeHGlobal(bufferIntPtr);
return 1;
}
private void opendev_Click(object sender, EventArgs e)
{
if (BriSDKLib.QNV_OpenDevice(BriSDKLib.ODT_LBRIDGE, 0, "") <= 0 || BriSDKLib.QNV_DevInfo(0, BriSDKLib.QNV_DEVINFO_GETCHANNELS) <= 0)
{
AppendStatus("功能开启失败");
MessageBox.Show("功能开启失败");
return;
}
procEvent = new ProcEventCallback(procEventCallback);
for (Int16 i = 0; i < BriSDKLib.QNV_DevInfo(-1, BriSDKLib.QNV_DEVINFO_GETCHANNELS); i++)
{
BriSDKLib.QNV_Event(i, BriSDKLib.QNV_EVENT_REGCBFUNC, (Int32)this.Handle, Marshal.GetFunctionPointerForDelegate(procEvent), new StringBuilder(0), 0);
BriSDKLib.QNV_SetParam(i, BriSDKLib.QNV_PARAM_AM_LINEIN, 5); //设置线路增益
}
//AppendStatus("已准备就绪,欢迎您的使用!");
return;
}
private void closedev_Click(object sender, EventArgs e)
{
BriSDKLib.QNV_CloseDevice(BriSDKLib.ODT_ALL, 0);
AppendStatus("功能已关闭");
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
BriSDKLib.QNV_CloseDevice(BriSDKLib.ODT_ALL, 0);
}
public void BindDataByMobile(string mobile){
DataRow dr = conn.GetDataRow(@"select top 1 b.CustomerName,b.CustomerMobile,b.CustomerSex,b.CustomerBirthday,b.CustomerAge,b.PortfolioDate,b.PortfolioBalance from
WFT_Customer_All b
where b.CustomerMobile='13100000000'");
Nm.Text = dr["CustomerName"].ToString();
Mobile.Text = dr["CustomerMobile"].ToString();
Sex.Text = dr["CustomerSex"].ToString();
Age.Text = dr["CustomerAge"].ToString();
Birth.Text = dr["CustomerBirthday"].ToString();
zc.Text = dr["PortfolioBalance"].ToString();//dr["PortfolioDate"] +":"+
//select * from DW_PHONE
DataTable dt = conn.GetDataTable(@"select b.RealName 沟通人员,case when [Type]='2' then '呼出' else '来电' end 类型,
case when a.state=1 then '已接' else '未接' end 状态,
cast(Times/60 as varchar(8))+'分'+cast(Times%60 as varchar(8))+'秒' 沟通时长,FollowAbstract 产品摘要,AbstractContent 内容摘要 from DW_PHONE a
left join Tb_Common_User b on a.WorkNo=b.WorkNO");
dataGridView1.DataSource = dt;
// 调整字体
//dataGridView1.Font = new Font("宋体", 11);
// 调整行高
//dataGridView1.Rows[0].Height = 100;
dataGridView1.RowTemplate.Height = 26;
dataGridView1.Update();
// 调整列宽,注意autosizecolumnsmode属性不能设置为fill
dataGridView1.Columns[0].Width = 75;
dataGridView1.Columns[1].Width = 60;
dataGridView1.Columns[2].Width = 60;
dataGridView1.Columns[3].Width = 75;
dataGridView1.Columns[4].Width = 183;
dataGridView1.Columns[5].Width = 190;
// 设置某一列居中
dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.EnableHeadersVisualStyles = false;
//dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Green;
//dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.GreenYellow;
//'更改Header高度,必须先设置高度模式
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.ColumnHeadersHeight = 28;
//'表头文字居中
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//'字体
//Dim font As New Font(dataGridView1.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
}
public void startdial_Click()
{
AppendStatus("开始拨号:" + textBox1.Text);
isend = false;
BriSDKLib.QNV_General(0, BriSDKLib.QNV_GENERAL_STARTDIAL, 0, textBox1.Text);
BindDataByMobile(textBox1.Text);
//jh.Text = dr["GroupName"].ToString();
}
//public void startdial_Click(string mobile)
//{
// AppendStatus("开始拨号:" + mobile);
// BriSDKLib.QNV_General(0, BriSDKLib.QNV_GENERAL_STARTDIAL, 0, mobile);
//}
private void refusecallin_Click()
{
BriSDKLib.QNV_General(0, BriSDKLib.QNV_GENERAL_STARTREFUSE, BriSDKLib.REFUSE_ASYN, "");
}
private void startflash_Click(object sender, EventArgs e)
{
BriSDKLib.QNV_General(0, BriSDKLib.QNV_GENERAL_STARTFLASH, BriSDKLib.FT_ALL, "");
}
private void resumeplayfile_Click(object sender, EventArgs e)
{
if (m_tagpstnData[0].lPlayFileHandle > 0)
{
BriSDKLib.QNV_PlayFile(0, BriSDKLib.QNV_PLAY_FILE_RESUME, m_tagpstnData[0].lPlayFileHandle, 0, "");
}
}
private void stoprecfile_Click()
{
if (m_tagpstnData[0].lRecFileHandle > 0)
{
AppendStatus("停止文件录音 ID:" + m_tagpstnData[0].lRecFileHandle);
long lRet=BriSDKLib.QNV_RecordFile(0, BriSDKLib.QNV_RECORD_FILE_STOP, m_tagpstnData[0].lRecFileHandle, 0, "");
m_tagpstnData[0].lRecFileHandle = 0;
AppendStatus("停止文件录音 Ret:" + lRet);
}
}
private void pauserecfile_Click(object sender, EventArgs e)
{
if (m_tagpstnData[0].lRecFileHandle > 0)
{
BriSDKLib.QNV_RecordFile(0, BriSDKLib.QNV_RECORD_FILE_PAUSE, m_tagpstnData[0].lRecFileHandle, 0, "");
}
}
private void resumerecfile_Click(object sender, EventArgs e)
{
if (m_tagpstnData[0].lRecFileHandle > 0)
{
BriSDKLib.QNV_RecordFile(0, BriSDKLib.QNV_RECORD_FILE_RESUME, m_tagpstnData[0].lRecFileHandle, 0, "");
}
}
private void startplayfile_Click(object sender, EventArgs e)
{
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
//this.textBox1.Text = dlg.FileName;
//PLAYFILE_MASK_REPEAT 使用重复播放
m_tagpstnData[0].lPlayFileHandle = BriSDKLib.QNV_PlayFile(0, BriSDKLib.QNV_PLAY_FILE_START, 0, BriSDKLib.PLAYFILE_MASK_REPEAT, dlg.FileName);
if (m_tagpstnData[0].lPlayFileHandle <= 0)
{
AppendStatus("播放文件失败:" + dlg.FileName);
}
else
{
AppendStatus("开始播放文件:" + dlg.FileName);
}
}
}
}
private void startrecfile_Click(object sender, EventArgs e)
{
Int32 dwmask = 0;
string voiceUrl ="d:\\voice\\"+18162589351+"_"+Guid.NewGuid().ToString()+".wav"; //C:\Users\Administrator\Desktop\8\pstndemo\pstndemo\bin\x86\Debug
m_tagpstnData[0].lRecFileHandle = BriSDKLib.QNV_RecordFile(0, BriSDKLib.QNV_RECORD_FILE_START, 0, dwmask, voiceUrl);
}
private void Form1_Shown_1(object sender, EventArgs e)
{
}
private void msg_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length!=11 && textBox1.Text.Length!=12)
{
MessageBox.Show("请输入正确的电话号码...");
return;
}
if (isend)
{
startdial_Click();
}
else {
MessageBox.Show("请挂机后重试...");
return;
}
//BriSDKLib.QNV_General(0, BriSDKLib.QNV_GENERAL_STARTDIAL, 0, textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
refusecallin_Click();
}
private void Age_Click(object sender, EventArgs e)
{
}
/*
* 通过日志获取号码
byte[] buf1 = new byte[256];
int iLen = BriSDKLib.QNV_CallLog((short)0, BriSDKLib.QNV_CALLLOG_CALLID, ref buf1[0], 256);
if (iLen > 0)
{
string strGet = System.Text.Encoding.Default.GetString(buf1, 0, iLen);
MessageBox.Show("电话号码:" + strGet);
}
*/
}
}