<%@ WebHandler Language="C#" Class="receive_coupon" %>
using System;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Configuration;
public class receive_coupon : IHttpHandler
{
protected string WELiveAPIDomain = ConfigurationManager.AppSettings["CouPon_DOMAIN"];
//调用凭证
protected string WELiveApiKey = ConfigurationManager.AppSettings["CouPon_SECRET"];
//立即领取优惠券
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var info = new coupon_info();
string adid = context.Request.Form["adid"];
//info.couponId = context.Request.Form["couponid"];
//info.receiptorPhone = context.Request.Form["mobile"];
//string code = context.Request.Form["code"];
//string businessId = context.Request.Form["businessId"];
string cityname = context.Request.Form["cityname"];
string couponName = context.Request.Form["couponName"];
info.couponId = "D1C69E1D-3824-49DB-9F75-C080016EB717";
info.receiptorPhone = " ";
string code = " ";
string businessId = " ";
//string cityname = " ";
//string couponName = " ";
var code_where = "&appid=‘’ &msg_type=‘’ ";
//是否需要验证短信验证码
bool isvaid = true;
if (!string.IsNullOrEmpty(code))
{
var parameters = string.Format("mobile={0}&valid_code={1}{2}", info.receiptorPhone, code, code_where);
var valid_code = WebClient_Post("XXX", parameters);
isvaid = valid_code.code == 200 ? true : false;
//验证码失效或过期
if (!isvaid)
{
context.Response.Write(JsonConvert.SerializeObject(valid_code));
return;
}
}
var result = new result<object>();
//验证码通过后先注册潜客再提交优惠券信息
if (isvaid)
{
var marketresult = new marketresult<object>();
//如果未传入city ,就调用获取手机归属地
if (string.IsNullOrEmpty(cityname))
{
var get_city = GetAscription(info.receiptorPhone);
if (get_city != null && string.IsNullOrEmpty(get_city.city))
{
cityname = get_city.city;
}
}
string CRM_API_DOMAIN = ConfigurationManager.AppSettings["CRM_API_DOMAIN"];
//根据事业部调用不同的潜客接口
if (businessId == "webi")
{
if (string.IsNullOrEmpty(adid))
{
adid = "QGB854FB9K";
}
//成人
var register_where = "XXX";
if (!string.IsNullOrEmpty(cityname))
{
register_where += "&cityName=" + cityname;
}
var register_parameters = string.Format("phone={0}&name={1}&adid={2}{3}", info.receiptorPhone, "优惠券领取", adid, register_where);
marketresult = WebClient_RegisterPost(CRM_API_DOMAIN + "/api/Lead/create_lead", register_parameters);
}
else if (businessId == "happygoal")
{
if (string.IsNullOrEmpty(adid))
{
adid = " ";
}
//开心豆
var register_where = "&add_time=" + DateTime.Now.ToShortDateString();
if (!string.IsNullOrEmpty(cityname))
{
register_where += "&cityName=" + cityname;
}
var register_parameters = string.Format("parent_phone={0}&child_name={1}&adid={2}{3}", info.receiptorPhone, "优惠券领取", adid, register_where);
marketresult = WebClient_RegisterPost(CRM_API_DOMAIN +"/api/kidlead/create_lead", register_parameters);
}
if (!marketresult.state)
{
context.Response.Write(JsonConvert.SerializeObject(result));
}
else
{
//调用接口领取优惠券
var temp = GetCouponInfo(info);
var json = JsonConvert.SerializeObject(temp);
//领取成功后调用发送模板消息的接口
if (temp.code == 200)
{
var type = businessId == "webi" ? "WEBIENGLISH" : "HAPPYGOAL";
var push_parameters = string.Format("mobile={0}&product_type={1}&title={2}", info.receiptorPhone, type, couponName);
var push_result = WebClient_RegisterPost('XXX", push_parameters);
}
context.Response.Write(json);
}
}
else
{
context.Response.Write("领取失败");
}
}
//调用post方法。。。
public result<object> WebClient_Post(string url, string postData, Dictionary<string, string> headers = null)
{
try
{
var client = new WebClient();
//设置请求头信息
if (headers != null)
{
foreach (var item in headers)
{
client.Headers.Add(item.Key, item.Value);
}
}
// 构造POST参数
byte[] sendData = Encoding.UTF8.GetBytes(postData.ToString());
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", sendData.Length.ToString());
client.Headers.Add("Authorization", "XXX"); //验证码加密
var result = client.UploadData(url, "POST", sendData);
var json = Encoding.UTF8.GetString(result);
return JsonConvert.DeserializeObject<result<object>>(json);
}
catch (Exception ex)
{
return new result<object> { code = 500, msg = ex.Message };
}
}
//注册
public marketresult<object> WebClient_RegisterPost(string url, string postData, Dictionary<string, string> headers = null)
{
try
{
var client = new WebClient();
//设置请求头信息
if (headers != null)
{
foreach (var item in headers)
{
client.Headers.Add(item.Key, item.Value);
}
}
// 构造POST参数
byte[] sendData = Encoding.UTF8.GetBytes(postData.ToString());
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", sendData.Length.ToString());
var result = client.UploadData(url, "POST", sendData);
var json = Encoding.UTF8.GetString(result);
return JsonConvert.DeserializeObject<marketresult<object>>(json);
}
catch (Exception ex)
{
return new marketresult<object> { state = false, error = ex.Message };
}
}
/// <summary>
/// 归属地查询
/// </summary>
/// <param name="mobile"></param>
/// <returns></returns>
public AliAscriptionResult GetAscription(string mobile)
{
//文档 https://market.aliyun.com/products/56928004/cmapi022704.html?spm=5176.730005.productlist.d_cmapi022704.QNgUFE#sku=yuncode1670400004
var url = "http://plocn.market.alicloudapi.com/plocn?n=" + mobile;
var client = new WebClient
{
Encoding = Encoding.UTF8
};
client.Headers.Add("Authorization", " 7XXX");
var data = client.DownloadString(url);
var result = JsonConvert.DeserializeObject<AliAscriptionResult>(data);
return result;
}
//调用接口
public result<string> GetCouponInfo(coupon_info input)
{
try
{
//请求地址
var url = WELiveAPIDomain + "/XXX";
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json, text/javascript, */*";
request.ContentType = "application/json; charset=utf-8";
//请求Header
request.Headers.Add("ApiKey", WELiveApiKey);
byte[] buffer = encoding.GetBytes(JsonConvert.SerializeObject(input));
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
var json = reader.ReadToEnd();
return JsonConvert.DeserializeObject<result<string>>(json);
}
}
catch (Exception ex)
{
return new result<string> { code = 500, msg = ex.Message };
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
//优惠券类
public class coupon_info
{
/// <summary>
/// 优惠券ID
/// </summary>
public string couponId { get; set; }
/// <summary>
/// 领取人手机号
/// </summary>
public string receiptorPhone { get; set; }
}
/// <summary>
/// 返回结果
/// </summary>
/// <typeparam name="T"></typeparam>
public class result<T>
{
public int code { get; set; }
public T data { get; set; }
public string msg { get; set; }
}
/// <summary>
/// 返回结果
/// </summary>
/// <typeparam name="T"></typeparam>
public class marketresult<T>
{
public bool state { get; set; }
public T data { get; set; }
public string error { get; set; }
public string error_key { get; set; }
}
public class AliAscriptionResult
{
public string province { get; set; }
/// <summary>
/// 上海
/// </summary>
public string city { get; set; }
/// <summary>
/// true
/// </summary>
public bool success { get; set; }
/// <summary>
/// 手机号码
/// </summary>
public string phone { get; set; }
/// <summary>
/// 中国联通
/// </summary>
public string company { get; set; }
/// <summary>
/// 中国联通
/// </summary>
public string type { get; set; }
/// <summary>
/// 021
/// </summary>
public string area { get; set; }
/// <summary>
/// 1
/// </summary>
public string code { get; set; }
/// <summary>
/// 查询成功
/// </summary>
public string tips { get; set; }
}