加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

微信公众号开发客服接口实例代码

发布时间:2020-05-23 07:29:22 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了微信公众号开发客服接口的相关资料,并附简单实例,需要的朋友可以参考下

最近,开发微信公众号,负责开发客服功能,这里简单记录下:

Kf_account.cs代码:

public Kf_account() { BindUser(); } private void BindUser() { if (!File.Exists(as_INIFile)) { var str = new StringBuilder(); str.Append(";内容由程序自动生成,请不要修改此文件内容rn"); str.Append("[total]rn"); str.Append("total=rn"); str.Append("[count]rn"); str.Append("count=rn"); str.Append("[user]rn"); //StreamWriter sw = default(StreamWriter); //sw = File.CreateText(ls_INIFile); //sw.WriteLine(str.ToString()); //sw.Close(); File.WriteAllText(as_INIFile,str.ToString(),Encoding.Unicode); File.SetAttributes(as_INIFile,FileAttributes.Hidden); } CheckForIllegalCrossThreadCalls = false; InitializeComponent(); Icon = Resource1.ico; lkl_num.Text = INIFile.ContentValue("total","total",as_INIFile); lkl_num_c.Text = INIFile.ContentValue("count","count",as_INIFile); pictureBox1.Visible = true; var sr = new StreamReader(as_INIFile,Encoding.Unicode); String line; int li_count = 0; adt_user.Columns.Clear(); adt_user.Columns.Add("username",Type.GetType("System.String")); adt_user.Columns.Add("openid",Type.GetType("System.String")); while ((line = sr.ReadLine()) != null) { li_count++; if (li_count > 6) { line = SysVisitor.Current.GetFormatStr(line); DataRow newRow; newRow = adt_user.NewRow(); newRow["username"] = line.Substring(0,line.LastIndexOf('=')); newRow["openid"] = line.Substring(line.LastIndexOf('=') + 1); adt_user.Rows.Add(newRow); } } sr.Close(); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = adt_user; //dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells; lbl_count.Text = "共" + (li_count - 6) + "行"; pictureBox1.Visible = false; } private void btn_GetUser_Click(object sender,EventArgs e) { if (MessageBox.Show(@"拉取用户信息的速度取决于你的关注数与网络速度,

可能需要几分钟甚至更长时间。
使用此功能将消耗大量用户管理接口配额。
要继续此操作吗?","提示:",MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
var thr = new Thread(Get_user_list);
thr.Start();
}

private void Get_user_list()
{
  File.Delete(as_INIFile);
  var str = new StringBuilder();
  str.Append(";内容由程序自动生成,请不要修改此文件内容rn");
  str.Append("[total]rn");
  str.Append("total=rn");
  str.Append("[count]rn");
  str.Append("count=rn");
  str.Append("[user]rn");
  File.WriteAllText(as_INIFile,Encoding.Unicode);
  File.SetAttributes(as_INIFile,FileAttributes.Hidden);

  string ls_appid = INIFile.ContentValue("weixin","Appid");
  string ls_secret = INIFile.ContentValue("weixin","AppSecret");
  string access_token = "";
  string menu = "";
  if (ls_appid.Length != 18 || ls_secret.Length != 32)
  {
    MessageBox.Show("你的Appid或AppSecret不对,请检查后再操作");
    return;
  }
  access_token = SysVisitor.Current.Get_Access_token(ls_appid,ls_secret);
  if (access_token == "")
  {
    MessageBox.Show("Appid或AppSecret不对,请检查后再操作");
    return;
  }
  menu = SysVisitor.Current.GetPageInfo("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token);
  if (menu.Substring(2,7) == "errcode")
  {
    MessageBox.Show("拉取失败,返回消息:rn" + menu);
  }

  JObject json = JObject.Parse(menu);
  lkl_num.Text = json["total"].ToString();
  INIFile.SetINIString("total",lkl_num.Text,as_INIFile);
  lkl_num_c.Text = json["count"].ToString();
  INIFile.SetINIString("count",lkl_num_c.Text,as_INIFile);
  int li_count = int.Parse(json["count"].ToString());
  btn_GetUser.Enabled = false;
  pictureBox1.Visible = true;
  FileStream fs = null;
  Encoding encoder = Encoding.Unicode;
  for (int i = 0; i < li_count; i++)
  {
    string openid,username;
    openid = Get_UserName(json["data"]["openid"][i].ToString());
    username = json["data"]["openid"][i].ToString();
    //INIFile.SetINIString("user",openid,username,as_INIFile);
    byte[] bytes = encoder.GetBytes(openid + "=" + username + " rn");
    fs = File.OpenWrite(as_INIFile);
    //设定书写的開始位置为文件的末尾 
    fs.Position = fs.Length;
    //将待写入内容追加到文件末尾 
    fs.Write(bytes,bytes.Length);
    fs.Close();
    lab_nums.Text = "已拉取" + i + "个,还剩" + (li_count - i) + "个,请耐心等待";
  }
  lab_nums.Text = "";
  //BindUser();
  btn_GetUser.Enabled = true;
  pictureBox1.Visible = false;
  MessageBox.Show("已全部拉取完毕,请重新打开该窗口");
}

/// <summary>
///   获取用户信息详情,返回json
/// </summary>
///<param name="as_openid"&gt;
private string Get_User(string as_openid)
{
  string ls_json = "";
  string access_token = "";
  access_token = SysVisitor.Current.Get_Access_token();
  ls_json =
    SysVisitor.Current.GetPageInfo("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + as_openid + "&lang=zh_CN");
  return ls_json;
}

/// <summary>
///   获取用户用户的昵称
/// </summary>
private string Get_UserName(string as_openid)
{
  string ls_json = "";
  ls_json = Get_User(as_openid);
  string username = "";
  JObject json = JObject.Parse(ls_json);
  username = json["nickname"].ToString();
  username = SysVisitor.Current.GetFormatStr(username);
  return username;
}

private void btn_search_Click(object sender,EventArgs e)
{
  string username = txt_search.Text.Trim();
  if (string.IsNullOrWhiteSpace(username))
  {
    return;
  }
  DataRow[] datarows = adt_user.Select("username like '%" + username + "%'");

  var ldt = new DataTable();
  ldt.Columns.Clear();
  ldt.Columns.Add("username",Type.GetType("System.String"));
  ldt.Columns.Add("openid",Type.GetType("System.String"));
  ldt = ToDataTable(datarows);
  try
  {
    lbl_count.Text = ldt.Rows.Count.ToString();
  }
  catch
  {
  }
  dataGridView1.AutoGenerateColumns = false;
  dataGridView1.DataSource = ldt;
}

public DataTable ToDataTable(DataRow[] rows)
{
  if (rows == null || rows.Length == 0) return null;
  DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构 
  foreach (DataRow row in rows)
    tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中 
  return tmp;
}

private void dataGridView1_CellMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
  try
  {
    SysVisitor.Current.Wx_openid =
      dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
    SysVisitor.Current.Wx_username =
      dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
    //MessageBox.Show(str);
    grb_chat.Enabled = true;
    grb_chat.Text = SysVisitor.Current.Wx_username;
  }
  catch
  {

  }
  webBrowser_msg.DocumentText = "";
  string url = string.Format("https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token={0}",SysVisitor.Current.Get_Access_token());
  string ls_text = @"{";
  ls_text += ""starttime" : " + DateTime.Now.AddDays(-3).Ticks + ",";
  ls_text += ""endtime" : " + DateTime.Now.Ticks + ",";
  ls_text += ""openid" : "" + SysVisitor.Current.Wx_openid + "",";
  ls_text += ""pagesize" : 1000,";
  ls_text += ""pageindex" : 1,";
  ls_text += "}";
  string ls_history = SysVisitor.Current.PostPage(url,ls_text);
  webBrowser_msg.DocumentText = ls_history;
}

private void btn_send_Click(object sender,EventArgs e)
{
  string ls_msg = richTextBox_msg.Text;
  string ls_text = @"{";
  ls_text += ""touser":"" + SysVisitor.Current.Wx_openid + "",";
  ls_text += ""msgtype":"text",";
  ls_text += ""text":";
  ls_text += "{";
  ls_text += ""content":"" + ls_msg + """;
  ls_text += "}";
  ls_text += "}";
  string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}",SysVisitor.Current.Get_Access_token());
  string ls_isright = SysVisitor.Current.PostPage(url,ls_text);

  webBrowser_msg.DocumentText += "

" + ls_isright + "

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读