diff --git a/dashboard/models.py b/dashboard/models.py
index a061a05..049b74a 100644
--- a/dashboard/models.py
+++ b/dashboard/models.py
@@ -610,4 +610,4 @@ class News(models.Model):
class CheckCode(models.Model):
phone_code = models.CharField('手机验证码',max_length=256,null=True,blank=True)
phone = models.CharField('电话号',max_length=256,null=True,blank=True)
- c_time = models.DateField('时间',null=True,blank=True)
+ c_time =models.DateTimeField('时间', auto_now_add=True)
diff --git a/dashboard/templates/dashboard/login-for-phone.html b/dashboard/templates/dashboard/login-for-phone.html
index 1429823..c993c09 100644
--- a/dashboard/templates/dashboard/login-for-phone.html
+++ b/dashboard/templates/dashboard/login-for-phone.html
@@ -57,14 +57,16 @@
class="login loginpage col-lg-offset-4 col-lg-4 col-md-offset-3 col-md-6 col-sm-offset-3 col-sm-6 col-xs-offset-2 col-xs-8">
-
忘记密码
- 账号登录
+ 账号登录
注册
@@ -125,9 +127,58 @@
img = $(".captcha");
id_captcha_0.attr("value", new_cptch_key);
img.attr("src", new_cptch_image);
- });
+ });
});
+ var check = false;
+ $('#phone').blur(function () {
+ var a = $(this).val();
+ e = /^1[34578]\d{9}$/;
+ if (!e.test(a)) {
+ check = false
+ } else {
+ check = true;
+ }
+ });
+
+ $('#code').click(function () {
+ if (check) {
+ $.ajax({
+ type: 'post',
+ url: '/author/code/',
+ data: {
+ csrfmiddlewaretoken: $('[name="csrfmiddlewaretoken"]').val(),
+ mobile: $('#phone').val()
+ },
+ success: function (ad) {
+ alert('验证码已发至您的手机,请查收!!!')
+ },
+ error: function (){
+ alert('验证码已发至您的手机,请查收!!!')
+ }
+ })
+ } else (alert('请输入正确的手机号'))
+ });
+
+
})
+ var countdown = 60
+
+ function settime(obj) {
+ if (countdown == 0) {
+ obj.removeAttribute('disable');
+ obj.value = '免费获取验证码';
+ countdown = 60;
+ return;
+ } else {
+ obj.setAttribute('disable', true);
+ obj.value = '重新发送(' + countdown + ")";
+ countdown--;
+ }
+ setTimeout(function () {
+ settime(obj)
+ }
+ , 1000)
+ }
{% endblock %}
diff --git a/dashboard/views.py b/dashboard/views.py
index 2fec459..6d6594b 100644
--- a/dashboard/views.py
+++ b/dashboard/views.py
@@ -85,22 +85,45 @@ def user_login(request):
messages.error(request, '账号或密码错误,请您确认账号和密码')
return render(request, 'dashboard/login.html', {'hash_key': hash_key, 'image_url': image_url})
+
def user_login_for_phone(request):
if request.method == 'GET':
- return render(request,'dashboard/login-for-phone.html')
+ return render(request, 'dashboard/login-for-phone.html')
elif request.method == 'POST':
p_code = request.POST.get('check_code')
phone = request.POST.get('phone')
- try:
+ print(phone, p_code)
+ results = CheckCode.objects.filter(phone=18119305139).count()
+ print(str(results) + "11111111111111111")
+ # try:
+ if results > 0:
check_code = CheckCode.objects.get(phone_code=p_code, phone=phone)
- if float(time.time()) - float(check_code.c_time) > 60:
- check_code.delete()
- return '验证码超时已被删除'
- print(21333333333)
- check_code.delete()
- return '登录成功'
- except:
+ if User.objects.filter(username=phone).count() > 0:
+ user = User.objects.get(username=phone)
+ login(request, user, backend='django.contrib.auth.backends.ModelBackend')
+ # check_code.delete()
+ return HttpResponseRedirect('/index/')
+ else:
+ user = User(username=phone)
+
+ user.save()
+ userprofile = Userprofile(user_id=user.id, image='default.png')
+ userprofile.save()
+ u = authenticate(username=phone)
+ login(request, u, backend='django.contrib.auth.backends.ModelBackend')
+ return HttpResponseRedirect('/index/')
+ # if float(time.time()) - float(check_code.c_time) > 60:
+ # check_code.delete()
+ # return '验证码超时已被删除'
+ # print(21333333333)
+ #
+ # except:
+ else:
print(1233333333)
+ messages.error(request, '请输入正确的验证码!!!')
+ return render(request, 'dashboard/login-for-phone.html')
+
+
def author_code(request):
sms_host = "sms.yunpian.com"
voice_host = "voice.yunpian.com"
@@ -110,18 +133,21 @@ def author_code(request):
version = "v2"
# 模板短信接口的URI
sms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json"
- # if request.method == 'GET':
- # phone = request.POST.get('phone')
apikey = "304eb08353f7ebf00596737acfc31f53"
- mobile = "18119305139"
- tpl_id = 4041392
+ # mobile = "18119305139"
+ mobile = request.POST.get('mobile')
+ print(str(mobile) + "99999999999999999999999999999999999999999")
+ tpl_id = 4045320
- str = ""
+ code = ""
for i in range(6):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
- str += ch
- print(str)
- tpl_value = {'#dateStart#': '10月1日', '#dateEnd#': '10日', '#type#': '微信公众号', '#name#': str}
+ code += ch
+ print(code)
+ # tpl_value = {'#dateStart#': '10月1日', '#dateEnd#': '10日', '#type#': '微信公众号', '#name#': code}
+ tpl_value = {'#code#':code}
+
+
params = parse.urlencode({
'apikey': apikey,
'tpl_id': tpl_id,
@@ -136,21 +162,18 @@ def author_code(request):
conn.request("POST", sms_tpl_send_uri, params, headers)
response = conn.getresponse()
response_str = response.read()
+ try:
+ check_code = CheckCode.objects.get(phone=mobile)
+ check_code.phone_code = code
+ check_code.save()
+ except:
+ print(code, mobile, 1111111111)
+ check_code = CheckCode(phone_code=code, phone=mobile)
+ check_code.save()
conn.close()
return response_str
- # return HttpResponse('ok')
- # a = YunPian(settings.API_KEY)
- # a.send_sms(phone_code, mobile)
- # try:
- # check_code = CheckCode.objects.get(phone=mobile)
- #
- # check_code.phone_code = phone_code
- # check_code.save()
- # except:
- # print(phone_code, mobile, 1111111111)
- # check_code = CheckCode(phone_code=phone_code, phone=mobile)
- # check_code.save()
- # return JsonResponse({'data': '验证码发送成功'})
+
+
def user_logout(request):
logout(request)
return HttpResponseRedirect('/')
@@ -232,7 +255,8 @@ def register(request):
# user.is_staff = True
# user.first_name = phone
- userprofile = Userprofile(name=name, image=image, user_id=user.id, organization_id=user_organization, sex=sex, status=0)
+ userprofile = Userprofile(name=name, image=image, user_id=user.id, organization_id=user_organization,
+ sex=sex, status=0)
userprofile.save()
user.save()