Beispiel #1
0
def login():
    resp = validate_json()
    if resp: return resp
    resp = validate_params('type', 'pwd', 'username')
    if resp: return resp
    data = request.get_json()
    pwd = hash_encode(data['pwd'])
    if data['type'] == '房东':
        user = TLandlord.query.filter(
            or_(TLandlord.phone == data['username'],
                TLandlord.l_uname == data['username']),
            TLandlord.l_pwd == pwd).first()
        if not user:
            return jsonify({'state': 0, 'msg': '口令错误了'})
        return y_login(user, user.ld_id)
    elif data['type'] == '经纪人':
        user = TBroker.query.filter(
            or_(TBroker.phone == data['username'],
                TBroker.b_name == data['username']),
            TBroker.b_pwd == pwd).first()
        if not user:
            return jsonify({'state': 0, 'msg': '口令错误了'})
        return y_login(user, user.broker_id)
    elif data['type'] == '用户':
        user = TUser.query.filter(
            or_(TUser.phone == data['username'],
                TUser.u_name == data['username']), TUser.u_pwd == pwd).first()
        if not user:
            return jsonify({'state': 3, 'msg': '口令错误了'})
        return y_login(user, user.user_id)
Beispiel #2
0
def login(request):
    if request.method=='GET':
        return render(request,'login.html')
    elif request.method=="POST":
        error = None
        username = request.POST.get('username')
        password = request.POST.get('password')
        password = hash_encode(password)
        sup_users = TSup.objects.filter(name=username,pwd=password)#先查询超级管理员
        if sup_users.exists():#如果查到了
            user=sup_users.first()
        else:
            pt_users = TPtAdmin.objects.filter(name=username,pwd=password)#查询普通管理员
            if pt_users.exists():#如果查到了
                user=pt_users.first()
            else:
                kf_users = TServ.objects.filter(name=username,pwd=password)#查询客服
                if kf_users.exists():
                    user=kf_users.first()
                    user.login_state=1
                    user.save()
                else:
                    ad_users = TAdBm.objects.filter(name=username,pwd=password)#查询广告商
                    if ad_users.exists():
                        user=ad_users.first()
                    else:
                        error = '账号密码有误,请重新输入'
                        return render(request, 'login.html', locals())
        request.session['name'] = user.name
        request.session['type'] = user.type
        return redirect("/")
Beispiel #3
0
def user_register():  #用户注册接口
    resp = validate_json()  #判断是否提供了json数据
    if resp: return resp  #如果resp有数据,说明没有提供json数据
    resp = validate_params('username', 'pwd', 'phone', 'code',
                           'sex')  #验证提供数据的完整性
    if resp: return resp
    data = request.get_json()  #获取接前端json数据
    if not valid_code(data['phone'],
                      data['code']):  #如果短信验证码和手机号在redis查不到则验证码错误
        return jsonify({'state': 2, 'msg': '验证码错误,请重新输入正确的验证码,2分钟后重新获取'})
    pwd = hash_encode(data['pwd'])  #将密码加密

    new_user = TUser(u_name=data['username'],
                     u_pwd=pwd,
                     phone=data['phone'],
                     sex=data['sex'],
                     regi_date=datetime.now(),
                     last_date=datetime.now(),
                     status=1,
                     balance=0)

    db.session.add(new_user)
    db.session.commit()

    token = gen_token(new_user.user_id)  #将用户id传入,生成token
    add_token(token, new_user.user_id)  # 以key,value的形式存入redis 有效天数一周

    return jsonify({  #将token给前端一份
        'state': 0,
        'msg': '注册并登录成功',
        'token': token
    })
Beispiel #4
0
def fdregister():  #房东注册
    resp = validate_json()  #判断是否提供了json数据
    if resp: return resp  #如果resp有数据,说明没有提供json数据
    resp = validate_params('name', 'sex', 'phone', 'username', 'pwd',
                           'code')  #验证提供数据的完整性
    if resp: return resp
    data = request.get_json()  #获取接受的数据
    if not valid_code(data['phone'], data['code']):  #验证短信验证码和手机号
        return jsonify({'state': 2, 'msg': '验证码错误,请重新输入正确的验证码,2分钟后重新获取'})
    pwd = hash_encode(data['pwd'])  #密码加密
    fuser = TLandlord(l_name=data['name'],
                      sex=data['sex'],
                      phone=data['phone'],
                      l_uname=data['username'],
                      l_pwd=pwd,
                      regi_date=datetime.now(),
                      last_date=datetime.now(),
                      status=1,
                      sou_num=0)

    db.session.add(fuser)
    db.session.commit()

    token = gen_token(fuser.ld_id)  #生成token
    add_token(token, fuser.ld_id)  #以key,value的形式存入redis

    return jsonify({'state': 0, 'msg': '注册并登录成功', 'token': token})
Beispiel #5
0
def gpwd():  #修改密码
    resp = validate_json()
    if resp: return resp
    resp = validate_params('newpwd', 'pwd')
    if resp: return resp
    data = request.get_json()
    newpwd = hash_encode(data['newpwd'])
    pwd = hash_encode(data['pwd'])
    token = request.cookies.get('token')  #获取token
    user_id = get_user_id(token)  #缓存中获取用户id
    if not user_id:
        return jsonify({'state': 3, 'msg': '登录过期,请重新登录'})
    user = TLandlord.query.get(user_id)  #通过用户id查找用户
    if user.l_pwd == pwd:
        user.l_pwd = newpwd
        user.last_date = datetime.now()
        db.session.add(user)
        db.session.commit()
        return jsonify({'state': 0, 'msg': '修改成功'})
    return jsonify({'state': 4, 'msg': '原密码错误'})
Beispiel #6
0
def alter_user_pwd():  #修改密码接口
    resp = validate_json()  #判断是否提供了json数据
    if resp: return resp  #如果resp有数据,说明没有提供json数据
    resp = validate_params('new_pwd', 'old_pwd', 'token')  #验证提供数据的完整性
    if resp: return resp
    data = request.get_json()  #获取json字符串
    user_id = get_user_id(data['token'])  # 根据token获取用户id
    if not user_id:
        return jsonify({
            'state': 3,
            'msg': '登录已期,需要重新登录并获取新的token',
        })

    user = TUser.query.filter_by(user_id=user_id).first()  #通过token获取的用户id查用户
    if user.u_pwd == hash_encode(data['old_pwd']):  #判断原密码是否正确
        user.u_pwd = hash_encode(data['new_pwd'])  #修改密码
        user.last_date = datetime.now()  #最后修改时间
        db.session.add(user)
        db.session.commit()  #修改后提交
        return jsonify({'state': 0, 'msg': '修改成功'})
    return jsonify({'state': 4, 'msg': '原密码错误'})
Beispiel #7
0
 def post(self, request):
     """
     普通用户页面
     分页器展示给后台
     可在后台修改已有的
     """
     role_id = request.POST.get('id', '')
     if role_id:
         jjr = TBroker.objects.get(broker_id=role_id)
         name = request.POST.get("b_name")
         uname = request.POST.get("b_uname")
         password = request.POST.get("b_pwd")
         password = hash_encode(password)
         sex = request.POST.get("sex")
         phone = request.POST.get("phone")
         common_id = request.POST.get("common_name")
         company = TCompany.objects.filter(company_id=common_id).first()
         jjr.b_name = name
         jjr.b_uname = uname
         jjr.b_pwd = password
         jjr.sex = sex
         jjr.phone = phone
         jjr.status = 0
         jjr.clinch_num = 0
         jjr.sou_num = 0
         jjr.years = 0
         jjr.company = company
         jjr.save()
         print("修改成功")
         return redirect('/broker_manage/')
     else:
         name = request.POST.get("b_name")
         uname = request.POST.get("b_uname")
         password = request.POST.get("b_pwd")
         sex = request.POST.get("sex")
         phone = request.POST.get("phone")
         common_id = request.POST.get("common_name")
         """
         先查出所属公司的id
         """
         company = TCompany.objects.filter(company_id=common_id).first()
         TBroker.objects.create(b_name=name,
                                b_uname=uname,
                                b_pwd=password,
                                sex=sex,
                                phone=phone,
                                status=0,
                                clinch_num=0,
                                sou_num=0,
                                years=0,
                                company=company)
         return redirect('/broker_manage/')
Beispiel #8
0
 def post(self, request):
     l_pwd = request.POST.get('l_pwd', '')
     l_pwd = hash_encode(l_pwd)
     from sup_managerapp.forms import FangDongForm
     ld_id = request.POST.get('fangdong_id', '')  #用来判断是编辑还是创建
     if ld_id:
         form = FangDongForm(request.POST,
                             instance=TLandlord.objects.get(pk=ld_id))
     else:
         form = FangDongForm(request.POST)
     if form.is_valid():
         form.save()
         fd = TLandlord.objects.get(
             l_uname=request.POST.get('l_uname'))  #根据账号查,账号唯一
         fd.l_pwd = l_pwd
         fd.save()
         return redirect('/fd_manage/')
     errors = json.loads(form.errors.as_json())
     return render(request, 'fangdong/edit.html', locals())
Beispiel #9
0
    def post(self, request):
        """
        普通用户页面
        分页器展示给后台
        可在后台修改已有的
        """
        from .forms import PtUserForm
        role_id = request.POST.get('id', '')
        if role_id:
            form = PtUserForm(request.POST,
                              instance=TUser.objects.get(user_id=role_id))
            if form.is_valid():
                print("*" * 30)
                form.save()
                return redirect('/pt_user_manage/')

        else:
            name = request.POST.get("u_name")
            password = request.POST.get("u_pwd")
            password = hash_encode(password)
            sex = request.POST.get("sex")
            phone = request.POST.get("phone")
            # time1 = datetime.datetime.now()
            # time = str(time1)[:-7]
            """
            还没写推荐码  确定推荐吗后添加字段  并在前台展示推荐码添加字段
            """
            TUser.objects.create(u_name=name,
                                 u_pwd=password,
                                 sex=sex,
                                 phone=phone,
                                 status=0,
                                 balance=0)
            return redirect('/pt_user_manage/')

        errors = json.loads(form.errors.as_json())
        return render(request, 'pt_user_role/edit.html', locals())