def login(request): msg = '' team_show_list = InvestmentTeamShow.obs.get_queryset().order_by( '-order_no') if request.method == 'POST': phone = request.POST.get('phone', '') code = request.POST.get('code', '') msg = '' status = check_v_code(request, redis_conn, phone, code, 'v_code_json', 360) if not verify_mobile(phone): msg = '手机号码格式错误' elif status == 3: msg = '未发送验证码' elif status == 1: msg = '验证码已过期' if status: msg = '验证码错误' else: user, _ = User.objects.get_or_create(username=phone, ) user_info = UserInfo.objects.get_or_create(user=user, mobile=phone) request.session['user_id'] = user.id return HttpResponseRedirect(reverse('h5:h5_index')) context = { 'domain': DOMAIN, 'team_show_list': team_show_list, 'msg': msg, } return render(request, "h5/login.html", context)
def confirm_mobile(request): user_info = UserInfo.objects.filter(is_del=False, user__id=request.session.get( 'user_id', 0)).first() msg = '' if request.method == 'POST': phone = request.POST.get('phone', '') code = request.POST.get('code', '') status = check_v_code(request, redis_conn, phone, code, 'v_code_json', 360) if not verify_mobile(phone): msg = '手机号码格式错误' elif status == 3: msg = '未发送验证码' elif status == 1: msg = '验证码已过期' if status: msg = '验证码错误' else: user_info.mobile = phone user_info.save() user_info.user.username = phone user_info.user.save() return HttpResponseRedirect(reverse('h5:person_info')) context = { 'user_info': user_info, 'msg': msg, } return render(request, "h5/edit_phone2.html", context)
def send_code(request): """ Send SMS Code """ if request.method == 'POST': req_data = request.POST mobile = req_data.get('mobile', None) res = verify_mobile(mobile) if not res: msg = "请填写正确手机号!" return JsonResponse(return_data(msg=msg)) # 发送短信验证码 sms = SMS(mobile) result = sms.send() if not result: msg = "短信验证码发送失败!" return JsonResponse(return_data(msg=msg)) # 避免设置session过期时间造成登录总是超时的问题 session_data = { "expired_at": time() + result[1], "code": result[0] } request.session[mobile] = session_data # request.session[mobile] = result[0] # request.session.set_expiry(result[1]) msg = "短信验证码发送成功!" return JsonResponse(return_data(status="OK", code=0, msg=msg)) return JsonResponse(return_data())
def send_v_code(request): mobile = request.GET.get('mobile', '') if not verify_mobile(mobile): return JsonResponse({'error_code': 2, 'error_msg': '手机号格式错误'}) v_code = str(random.randint(1000, 9999)) data = send_yanzheng_code(mobile, v_code, AL_VALIDATION_CODE, 2) if data: redis_conn.set( 'v_code_json', json.dumps({ data['mobile']: v_code, 'send_time': data['send_time'] }, ensure_ascii=False)) return JsonResponse({'error_code': 0, 'v_code': v_code}) else: return JsonResponse({'error_code': 1, 'error_msg': '发送失败'})
def post(self, request): mobile = request.data.get('mobile', '') if not mobile: return JsonResponse({'error_code': 3, 'error_msg': '手机号为空'}) if not verify_mobile(mobile): return JsonResponse({'error_code': 2, 'error_msg': '手机号格式错误'}) v_code = str(random.randint(1000, 9999)) data = send_v_code2(mobile, v_code, 2) if data: redis_conn.set( 'v_code_json', json.dumps( { data['mobile']: data['v_code'], 'send_time': data['send_time'] }, ensure_ascii=False)) return JsonResponse({'error_code': 0, 'error_msg': '请求成功'}) else: return JsonResponse({'error_code': 1, 'error_msg': '发送失败'})