def to_login(request: HttpRequest): if request.method == "GET": return render(request, 'login.html') elif request.method == "POST": logname = request.POST.get('logname') logpwd = request.POST.get('logpwd') print("用户名:", logname) try: if logname == "admin" and logpwd == "admin": request.session['login_user'] = logname return redirect("/", locals()) else: log = Superuser.objects.get(username=logname) if log.username == logname and log.password == make_pwd( logpwd): if logname != "admin": response = redirect('/', locals()) request.session['login_user'] = logname response.set_cookie('login_status', 'success') return response else: request.session['login_user'] = logname return redirect("/", locals()) else: print("用户名或密码错误!", print(make_pwd(log.password), logpwd)) return render(request, 'login.html') except: print("该用户不存在,请重新输入!") return render(request, 'login.html')
def to_login(request): """ 跳转到登录页 :param request: :return: """ if request.method == 'POST': # 获取用户名和口令 name = request.POST.get('username', '') pwd = request.POST.get('password', '') if any((not name, not pwd, len(name) == 0, len(pwd) == 0)): error = '用户名或口令不能为空!' else: ret = SysUser.objects.filter(name=name, auth_str=make_pwd(pwd)) if ret.exists(): login_user = ret.first() # 将登陆的用户信息存在session中 request.session['login_sys_user'] = { 'id': login_user.id, 'name': login_user.name } return redirect('/sys/') error = '用户名或口令错误!' return render(request, 'sys_user/login.html', locals())
def to_login(request: HttpRequest): if request.method == "POST": # 获取用户名和口令 name = request.POST.get('name', '') pwd = request.POST.get('pwd', '') if any((not name, not pwd, len(name) == 0, len(pwd) == 0)): error = '用户名或口令不能为空!' else: ret = SysUser.objects.filter(name=name, auth_string=make_pwd(pwd)) if ret.exists(): login_user = ret.first() # 将登录的用户信息存在session中 request.session['login_user'] = { 'id': login_user.id, 'name': login_user.name, 'role_name': login_user.role.name, 'role_code': login_user.role.code } return redirect('/') error = "用户名或口令错误!" return render(request, 'login.html', locals())
def change_pwd(request): """ 修改密码 :param request: :return: """ if request.method == 'POST': mem_id = request.POST.get('mem_id', '') old_pwd = request.POST.get('old_pwd', '') new_pwd1 = request.POST.get('new_pwd1', '') new_pwd2 = request.POST.get('new_pwd2', '') if any((not mem_id, not old_pwd, not new_pwd1, not new_pwd2, len(mem_id) == 0, len(new_pwd1) == 0, len(new_pwd2) == 0)): log.error('密码修改失败:无效的参数') msg = '操作失败!' status = 400 else: if new_pwd1 != new_pwd2: log.error('密码修改失败:两次密码输入不一致!') msg = '两次输入的密码不一致!' status = 400 else: try: member = Members.objects.get(pk=mem_id) member.auth_str = make_pwd(new_pwd1) member.save() msg = '操作成功!' status = 200 except Exception as e: log.error('密码修改失败:' + str(e)) msg = '操作失败!' status = 400 return JsonResponse({'status': status, 'msg': msg})
def register(request): """ 会员申请 :param request: :return: """ if request.method == 'POST': name = request.POST.get('name', '') phone = request.POST.get('phone', '') if any((not name, not phone, len(name) == 0, len(phone) == 0)): log.error('申请会员失败:名字或电话号码不能为空') msg = '用户名或手机号不能为空' status = 400 else: ret = re.match(r"^1[3456789]\d{9}$", phone) if not ret: msg = '手机号格式错误' status = 400 else: try: if Members.objects.filter(phone=phone).exists(): return JsonResponse({ 'status': 400, 'msg': '该手机号已绑定会员账号,请更换手机号或直接登录!' }) Members.objects.create(name=name, phone=phone, auth_str=(make_pwd(phone[-6:]))) status = 200 msg = '申请成功,您的初始密码为手机号后6位' except Exception as e: log.error('申请会员失败:' + str(e)) status = 400 msg = '申请失败!' return JsonResponse({'status': status, 'msg': msg})
def to_regist(request: HttpRequest): if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") user = SysUser.objects.create(name=username,auth_string=make_pwd(password)) SysUserRole.objects.create(id=user.id,user_id=user.id,role_id=2) return redirect('/login/') return render(request, 'register.html')
def to_p1(request: HttpRequest): if request.GET.get('id'): role_id = request.GET.get('id') role = Superuser.objects.get(pk=role_id) role.delete() return JsonResponse({'status': 0, 'msg': '删除成功!'}) if request.method == "GET": users = Superuser.objects.all() return render(request, 'p1.html', locals()) elif request.method == "POST": addid = request.POST.get("role_id") addusername = request.POST.get("addname") addpwd = request.POST.get("addpwd") addrealname = request.POST.get("addrealname") addpwd = request.POST.get("addpwd") addsex = request.POST.get("addsex") addcard = request.POST.get("addcard") addposition = request.POST.get("addposition") addemail = request.POST.get("addemail") addphone = request.POST.get("addphone") #如果添加的用户名已经存在就进行修改信息,否则直接添加新用户 if addid: print("正在修改~~~~") Superuser.objects.update(username=addusername, realname=addrealname, password=make_pwd(addpwd), sex=addsex, card=addcard, position=addposition, email=addemail, phone=addphone) return redirect('/p1/', locals()) else: print("正在添加~~~~~") Superuser.objects.create(username=addusername, realname=addrealname, password=make_pwd(addpwd), sex=addsex, card=addcard, position=addposition, email=addemail, phone=addphone) return redirect('/p1/')
def login(request): """ 登录页 :param request: :return: """ if request.method == 'POST': # 获取用户名和口令 name = request.POST.get('username', '') pwd = request.POST.get('password', '') if any((not name, not pwd, len(name) == 0, len(pwd) == 0)): msg = '用户名或口令不能为空!' status = 400 else: try: ret = Members.objects.filter(name=name, auth_str=make_pwd(pwd)) if ret.exists(): login_user = ret.first() if login_user.activate == 1: # 将登陆的用户信息存在session中 request.session['login_mem_user'] = { 'id': login_user.id, 'name': login_user.name } return JsonResponse({ 'status': 200, 'msg': '登录成功!', 'data': { 'id': login_user.id } }) else: msg = '您的会员申请尚未通过,请与后台管理员联系!' status = 400 else: msg = '用户名或密码错误' status = 400 except Exception as e: log.error('用户登录失败:' + str(e)) msg = '登录失败!' status = 400 return JsonResponse({'status': status, 'msg': msg})