Wex5发送短信的两种方法分享

上次发的那个受到了鼓励,打算把我学Wex5的经验都分享给大家
这次是Wex5的短信验证,我给大家分享2种,第一个是移动端的,第二个是云片,希望对大家在学习Wex5有帮助,功能都是一样的发送短信验证码,写法不同,都可以用于用户注册、修改密码时发送验证码并且验证,以此来增加安全性。
移动端
1、这个是移动端的发送短信,我就以这个找回密码为例子(移动端的原理就是把数据保存到数据库里面,一定时间改变数据库数据状态来达到发短信的效果(例如一秒))
Wex5发送短信的两种方法分享_第1张图片
1、)相信大家都知道点击上图的获取验证码之后会有一个60S出来并且获取验证码,代码如下
Model.prototype.button2Click = function(event) {
var jieshourenlist = this.comp(“jieshourenlist”).val();
if ($(‘#’ + this.getIDByXID(‘span2’)).text() !== ‘获取验证码’) {

    } else {
        if (jieshourenlist) {
            timeok = 60;
            this.comp('timer1').set({
                'enabled' : true
            });
            $.ajax({
                "type" : 'post',
                "dataType" : 'json',
                "data" : "{'FaSongUser' : '" + FaSongUser + "','jieshourenlist' : '" + jieshourenlist + "'}",
                "contentType" : 'application/json;charset=utf-8',
                "url" : haikanweburl + "WebServiceInner.asmx/SendSMS",
                "success" : function(data) {
                    if (data.isOk == 1) {
                        justep.Util.addCookie('yanzheng', data.retMsg, '/', 0.05);
                    }
                },
                "error" : function(XMLHttpRequest, textStatus, errorThrown) {

                }
            });
        } else {
            justep.Util.hint('输入正确手机号');
        }
    }
};

/* ** 获取验证码倒计时** */

var timeok = 60;
Model.prototype.timer1Timer = function(event) {
if (timeok == 0) {
this.comp(‘timer1’).set({
‘enabled’ : false
});
(‘#’ + this.getIDByXID(‘span2’)).text(‘获取验证码’);  
        } else {  
            timeok–;
(‘#’ + this.getIDByXID(‘span2’)).text(timeok + ‘s’);
}
};
云片
1、)下面是一个注册用户的例子,主要是接口部分,Wex5部分跟上面的移动端一样,在这里不作过多说明
云片接口写法注意事项(https://www.yunpian.com查您的apikey)
首先随机生成一个四位数
获取手机号码判断是否注册过(否)
调用yunpDX(mobile, RandKey)(下面的代码照写)
text里面编辑你要发送短信的格式(下面照写即可)
成功返回1把所得到的验证码(RandKey)返回给Wex5里面用输入的验证码与返回的验证码作对比即可;
[WebMethod(Description = “短信接口”)]
public void DX_App(string mobile)
{
HttpResponse httpResponse = HttpContext.Current.Response;
httpResponse.ContentType = “application/json;charset=utf-8”;
SystemPage.returnMsg r = new SystemPage.returnMsg();
DataSet ds= DbHelperSQL.Query(“select * from 表名”);
Random ran = new Random();
int RandKey = ran.Next(1000, 9999);
if (ds.Tables[0].Rows[0][“shoujh”].ToString() == mobile)
{
r.isOk = 1;
r.retMsg_zhuanl = “该手机号已被注册”;
r.retMsg_tuandtpy = ds.Tables[0].Rows[0][“id”];

        }
        else { 

                if (yunpDX(mobile, RandKey) ==1)
                {
                    r.isOk = 1;
                    r.retMsg = RandKey;
                }
                else if(yunpDX(mobile, RandKey) == 0)
                {
                    r.isOk = 0;
                    r.retMsg = "发送短信失败";
                }
        }
        // SystemPage.ResponseInfo(r);
        httpResponse.Write(JsonHelper.GetJson<SystemPage.returnMsg>(r));
        httpResponse.Flush();
        httpResponse.End();
    }

    public int yunpDX(string mobile,int RandKey)
    {
        // 设置为您的apikey(https://www.yunpian.com)可查
        string apikey = "**自查**";
        // 发送的手机号
        // mobile = "";
        // 发送模板编号
        //int tpl_id = 1;
        // 发送模板内容
        mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
        string company = "";

        string code = "";
        code = ""+RandKey;
        string tpl_value = HttpUtility.UrlEncode(
        HttpUtility.UrlEncode("#code#", Encoding.UTF8) + "=" +
        HttpUtility.UrlEncode(code, Encoding.UTF8) + "&" +
        HttpUtility.UrlEncode("#company#", Encoding.UTF8) + "=" +
        HttpUtility.UrlEncode(company, Encoding.UTF8), Encoding.UTF8);
        // 发送内容
        string text = "您的验证码是"+ code + ",如果不是您本人申请的验证码,请联系我们相关的工作人员,谢谢! 【"+ company + "】";
        // 获取user信息url
        //string url_get_user = "https://sms.yunpian.com/v1/user/get.json";
        // 智能模板发送短信url
        string url_send_sms = "https://sms.yunpian.com/v1/sms/send.json";
        // 指定模板发送短信url
        //string url_tpl_sms = "https://sms.yunpian.com/v1/sms/tpl_send.json";
        // 发送语音短信url
       // string url_send_voice = "https://voice.yunpian.com/v1/voice/send.json";

       // string data_get_user = "apikey=" + apikey;
        string data_send_sms = "apikey=" + apikey + "&mobile=" + mobile + "&text=" + text;
       //string data_tpl_sms = "apikey=" + apikey + "&mobile=" + mobile + "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
       // string data_send_voice = "apikey=" + apikey + "&mobile=" + mobile + "&code=" + "1234";


       // HttpPost(url_get_user, data_get_user);
       return HttpPost(url_send_sms, data_send_sms);
      //  HttpPost(url_tpl_sms, data_tpl_sms);
        //HttpPost(url_send_voice, data_send_voice);
    }
    public int HttpPost(string Url, string postDataStr)
    {
        byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
        // Console.Write(Encoding.UTF8.GetString(dataArray));

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataArray.Length;
        //request.CookieContainer = cookie;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(dataArray, 0, dataArray.Length);
        dataStream.Close();
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String res = reader.ReadToEnd();
            reader.Close();
            Console.Write("\nResponse Content:\n" + res + "\n");
            return 1;
        }
        catch (Exception e)
        {
            Console.Write(e.Message + e.ToString());
            return 0;
        }
    }

友情提示:上面有各种模版供大家选择

你可能感兴趣的:(移动,验证码,wex5)