Exemplo n.º 1
0
def login_view(request):
    print('---', request.method)
    if request.method == 'GET':
        return render(request, 'login.html')
    # 分两种用户,一种是系统管理员,一种是用户
    if request.method == 'POST':
        print(request.POST)

        error = None
        username = request.POST['username'].strip()
        password = request.POST['password'].strip()
        remember = request.POST.get('remember', '')  #checkbox
        # 对密码进行加密处理
        password_ = md5_.hash_encode(password)  # 转成md5加密后的图文
        # 验证用户名和密码口令是否为空
        if not all((username, password)):
            error = f'用户名或口令不能为空!'
        try:
            login_user = SysManagerUser.objects.filter(
                username=username, auth_string=password_).first()

            # 登录成功后,验证该用户的身份
            if login_user:
                # 系统管理员
                role_ = login_user.role
                login_info = {
                    '_id': login_user.id,
                    'name': role_.username,
                    'code': role_.code,
                    'nike_name': role_.nick_name,
                    'head': role_.head,
                    'mail': role_.mail
                }

                print(login_info)
            else:
                login_user = User.objects.filter(
                    (Q(username=username) or Q(user_phone=username))
                    and Q(auth_string=password)).first()
                if login_user:
                    # 用户
                    login_info = {
                        '_id': login_user.id,
                        'name': login_user.username,
                        'nick_name': login_user.nick_name,
                        'mail': login_user.mail,
                        'head': login_user.head
                    }
                    print(login_info)
                else:
                    error = f'{username} 用户名或口令错误!'
        except:
            error = '查询数据库出错,请与管理员联系'

        if not error:
            # 如果用户名和密码都没有错误,将当前登录用户的信息存入session中
            request.session['login_user'] = login_info
            return redirect(reverse('y:i'), locals())

    return render(request, 'login.html', locals())
Exemplo n.º 2
0
    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):

        if len(self.auth_string) != 32:
            self.auth_string = md5_.hash_encode(self.auth_string)

        super(SysManagerUser, self).save()
Exemplo n.º 3
0
 def save(self,
          force_insert=False,
          force_update=False,
          using=None,
          update_fields=None):
     if len(self.auth_string) != 32:
         self.auth_string = md5_.hash_encode(self.auth_string)
Exemplo n.º 4
0
    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None):

        if len(self.password) != 32:
            self.password = md5_.hash_encode(self.password)

        super(TSysUser, self).save()
Exemplo n.º 5
0
def login(request):
    # 分两种用户,一个是会员,一个管理员(系统)
    print('--->', request.method)
    if request.method == 'POST':
        print(request.POST)

        error = None

        username = request.POST['username'].strip()
        password = request.POST['password'].strip()
        remeber = request.POST.get('remeber', '')  # checkbox

        password_ = md5_.hash_encode(password)  # 转成md5后的密文

        # 验证用户名和口令是否为空
        if not all((username, password)):
            error = f'用户名或口令不能为空!'

        login_user = TSysUser.objects.filter(username=username,
                                             auth_string=password_).first()
        if login_user:
            # 系统管理员
            role_ = login_user.role
            login_info = {
                '_id': login_user.id,
                'name': role_.name,
                'code': role_.code
            }

        else:
            login_user = TUser.objects.filter(name=username,
                                              auth_string=password).first()
            if login_user:
                # 会员
                login_info = {
                    '_id': login_user.user_id,
                    'name': login_user.name,
                    'code': '',
                    'head': login_user.head,
                    'email': login_user.mail,
                    'phone': login_user.phone
                }
            else:
                error = f'{username} 用户名或口令错误!'

        if not error:
            request.session['login_user'] = login_info
            return redirect('/')

    return render(request, 'login.html', locals())
Exemplo n.º 6
0
def set_pwd(request):
    token = request.POST['token'].strip()
    password = request.POST['password'].strip()
    password_ = request.POST['password_'].strip()
    if password != password_:
        info = {'state': 1, 'msg': '密码不一致!'}
        return HttpResponse(json.dumps(info, ensure_ascii=False), content_type='application/json', charset='utf-8')
    id_ = cache_.get_user_id(token)
    print(token, id_)
    user = TUser.objects.get(pk=id_)
    pwd = md5_.hash_encode(password)
    user.password = pwd
    user.save()
    info = {'state': 0, 'msg': '密码设置成功!'}
    return HttpResponse(json.dumps(info, ensure_ascii=False), content_type='application/json', charset='utf-8')
Exemplo n.º 7
0
def login(request):
    # 分两种用户,一个是会员,一个管理员(系统)
    # print('--->', request.method)
    if request.method == 'POST':
        # print(request.POST)
        error = None

        username = request.POST['username'].strip()
        password = request.POST['password'].strip()
        remeber = request.POST.get('remeber', '')  # checkbox

        password_ = md5_.hash_encode(password)  # 转成md5后的密文
        print(password_)

        # 验证用户名和口令是否为空
        if not all((username, password)):
            error = f'用户名或口令不能为空!'

        else:
            login_user = TSysUser.objects.filter(username=username,
                                                 password=password_).first()
            if login_user:
                # 超级管理员
                role_ = login_user.role

                login_info = {
                    'user_id': login_user.user_id,
                    'head': str(login_user.head),
                    'email': login_user.email,
                    'nick_name': login_user.nick_name,
                    'role_name': role_.role_name,
                    'role_code': role_.role_code,
                }

            else:
                error = f'{username} 用户名或口令错误!'

        if not error:
            request.session['login_user'] = login_info
            return redirect(reverse('main:dash'))
    return render(request, 'login.html', locals())
Exemplo n.º 8
0
def login_pwd(request):
    email = request.POST['email'].strip()
    password = request.POST['password'].strip()
    if not all((email, password)):
        info = {'state': 1, 'msg': '用户名或口令不能为空!'}
        return HttpResponse(json.dumps(info, ensure_ascii=False), content_type='application/json', charset='utf-8')
    password_ = md5_.hash_encode(password)
    login_user = TUser.objects.filter(email=email, password=password_).first()
    if login_user:
        token = token_.gen_token(login_user.user_id)
        cache_.add_token(token, login_user.user_id)
        head_url = 'http://localhost:8000/static/天空.jpg'
        if login_user.img:
            head_url = login_user.img
        nickname = '昵称'
        if login_user.nickname:
            nickname = login_user.nickname
        info = {'state': 0, 'msg': '登录成功!', 'token': token, 'head_url': head_url, 'nickname': nickname}
        return HttpResponse(json.dumps(info, ensure_ascii=False), content_type='application/json', charset='utf-8')
    else:
        info = {'state': 1, 'msg': '用户名或口令错误!'}
        return HttpResponse(json.dumps(info, ensure_ascii=False), content_type='application/json', charset='utf-8')