您好,登錄后才能下訂單哦!
本文使用聚合數據的短信接口,需要先獲取到申請接口的appkey和模板id
項目目錄下創建ubtils文件夾,定義返回隨機驗證碼和調取短信接口的函數
function.py文件
import random import re # 隨機數 def range_num(num): # 定義一個種子,從這里面隨機拿出一個值,可以是字母 seeds = "1234567890" # 定義一個空列表,每次循環,將拿到的值,加入列表 random_num = [] # choice函數:每次從seeds拿一個值,加入列表 for i in range(num): random_num.append(random.choice(seeds)) # 將列表里的值,變成四位字符串 return "" . join(random_num)#5454
sendMsg.py文件
import json, urllib from urllib.parse import urlencode # 發送短信 def request2(mobile,num, m="GET"): appkey = '6bba3e7dffa71b79483002e1d92f4d00' url = "http://v.juhe.cn/sms/send" params = { "mobile": mobile, # 接收短信的手機號碼 "tpl_id": "167106", # 短信模板ID,請參考個人中心短信模板設置 "tpl_value": "#code#=%s"%num, # 變量名和變量值對。如果你的變量名或者變量值中帶有#&=中的任意一個特殊符號,請先分別進行urlencode編碼后再傳遞,<a href="http://www.juhe.cn/news/index/id/50" target="_blank">詳細說明></a> "key": appkey, # 應用APPKEY(應用詳細頁查詢) "dtype": "", # 返回數據的格式,xml或json,默認json } params = urlencode(params)#mobile=15038062130&tpl_id=166467&tpl_value=%23code%23%3d431515&key=dabf6ecaebfa9554395dad7dcc6be7c8 if m == "GET": f = urllib.request.urlopen("%s?%s" % (url, params)) else: f = urllib.request.urlopen(url, params) content = f.read()#{"reason":"操作成功","result":{"sid":"201906200911371223162juhe6hy","fee":1,"count":1},"error_code":0} res = json.loads(content)#json轉字典 if res: error_code = res["error_code"] if error_code == 0: # 成功請求 return 'ok' # print(res["result"]) else: return "%s:%s" % (res["error_code"], res["reason"]) # print("%s:%s" % (res["error_code"], res["reason"])) else: return "request api error"
路由
# 手機驗證碼登錄 path('loginTel/', login.loginTel,name='loginTel'),
方法
from blog.utils import sendMsg from blog.utils import function # 引入自定義的驗證碼 # 手機驗證登陸 def loginTel(request): res = {'status': None, 'info': None} # 點擊發送短信執行以下程序 if request.POST.get('sendSms') == '1': tel = request.POST.get('user_tel') print(tel) range_num = function.range_num(4); # 5454 request.session['validcode'] = range_num result = sendMsg.request2(tel, range_num, "GET") # result='ok' if result == 'ok': res['status'] = 1 # res['info']='發送成功%s'%range_num res['info'] = '發送成功' print(res) return HttpResponse(json.dumps(res)) # 把這個結果告訴給前臺,ajax else: res['status'] = 0 res['info'] = '發送失敗' return HttpResponse(json.dumps(res)) # 把這個結果告訴給前臺,ajax # 點擊登錄按鈕執行以下程序: if request.POST.get('dosubmit') == '1': validcode_form = request.POST.get('validcode') validcode_session = request.session.get('validcode') # 獲取session if validcode_form: if validcode_form != validcode_session: res['status'] = 0 res['info'] = '短信驗證碼不正確' return HttpResponse(json.dumps(res)) # 把這個結果告訴給前臺,ajax else: res['status'] = 0 res['info'] = '短信驗證碼不存在' return HttpResponse(json.dumps(res)) # 把這個結果告訴給前臺,ajax # 寫判斷手機號是否在會員表中 member_obj = Member.objects.filter(member_tel=request.POST.get('user_tel')).first() if member_obj: request.session['member_id'] = member_obj.member_id request.session['member_name'] = member_obj.member_name res['status'] = 1 res['info'] = '登錄成功' return HttpResponse(json.dumps(res)) # 把這個結果告訴給前臺,ajax return render(request, 'blog/loginTel.html', locals())
前端登錄的表單
{#手機登陸的表單#} <form method="post"> <input type="text" name="user_tel" placeholder="請輸入手機號"> <input type="text" name="validcode" placeholder="請輸入短信驗證碼"> <div class="am-g"> <input id="yanzhengma" type="button" value="獲取驗證碼"/> <input id="nowYanzhengma" type="button" value=""/> <input class="submit " type="button" id="btn_login" value="登錄"/> </div> </form> {#手機登陸的表單#}
前端登錄的jquery
{#手機號登陸的jquery#} //校驗用戶名 $("#btn_login").click(function () { var user_tel = $.trim($("input[name='user_tel']").val()); var validcode = $.trim($("input[name='validcode']").val()); $.post("/blog/loginTel/", { "user_tel": user_tel, "validcode": validcode, "dosubmit": 1, "csrfmiddlewaretoken": "{{ csrf_token }}" }, function (data) { if (data['status'] == 1) { layer.msg("登錄成功!即將跳轉至首頁~"); location.href = "{% url 'index' %}"; } else { layer.msg(data['info']); } return false; }, "json"); }); //點擊獲取驗證碼按鈕 $("#yanzhengma").click(function () { getSmsCode(); }); //獲取短信驗證碼 function getSmsCode() { var user_tel = $.trim($("input[name='user_tel']").val());//得到頁面里的手機號碼 var reguser = /^[1][3-8]\d{9}$/; if (user_tel == "") { layer.msg("請填寫手機號碼"); return false; } if (!reguser.test(user_tel)) { layer.msg("手機號碼格式不正確"); return false; } {#$("#yanzhengma").off('click');#} $.post("/blog/loginTel/", { "user_tel": user_tel, 'sendSms': 1, "csrfmiddlewaretoken": "{{ csrf_token }}" }, function (data) { if (data['status'] == 1) { timeout(); layer.msg(data['info']); } else { layer.msg(data['content']); } }, 'json'); } var sec = 60; var interval = null; //開啟定時器 function timeout() { interval = setInterval(run, 1000); } function run() { if (sec >= 1) { sec--; $("#yanzhengma").hide(); $("#nowYanzhengma").show(); $("#nowYanzhengma").val("倒計時(" + sec + ")s"); } else { $("#yanzhengma").off('click'); $('#yanzhengma').on("click", function (event) { getSmsCode(); }); $("#yanzhengma").show(); $("#nowYanzhengma").hide(); clearInterval(interval);//關閉定時器 sec = 60; } } {#手機號登陸的jquery結束#}
前端效果
總結
以上所述是小編給大家介紹的django項目中使用手機號登錄的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。