Exemple #1
0
def set():
    default_pwd = '******'
    # 展示账户详情
    if request.method == 'GET':
        resp_data = {}
        req = request.args
        uid = int(req.get('id',0))
        user_info = None
        if uid :
            user_info = User.query.filter_by(uid=uid).first()
        resp_data['user_info'] = user_info
        return ops_render( "account/set.html",resp_data )
    
    # 新建账户
    req = request.values
    resp = {'code':200,'msg':'操作成功','data':{}}

    id = req['id'] if 'id' in req else ''
    nickname = req['nickname'] if 'nickname' in req else ''
    mobile = req['mobile'] if 'mobile' in req else ''
    email = req['email'] if 'email' in req else ''
    login_name = req['login_name'] if 'login_name' in req else ''
    login_pwd = req['login_pwd'] if 'login_pwd' in req else ''

    if nickname is None or len(nickname) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的姓名~~'
        return jsonify(resp)

    if mobile is None or len(mobile) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的手机号码~~'
        return jsonify(resp)

    if email is None or len(email) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的邮箱~~'
        return jsonify(resp)

    if login_name is None or len(login_name) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的登录用户名~~'
        return jsonify(resp)

    if login_pwd is None or len(login_pwd) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入符合规范的登录密码~~'
        return jsonify(resp)
    # 如果新增用户,只需判断新增的用户名和数据库用户名是否相等即可
    # 如果是修改用户,需要判断除当前用户外还有没有相同的用户名
    # filter方法可以传入多个参数进行判断
    has_in = User.query.filter(User.login_name == login_name,User.uid != id).first()
    
    if has_in:
        resp['code'] = -1
        resp['msg'] = '该登录名已经存在,请换一个试试~~'
        return jsonify(resp)

    user_info = User.query.filter_by(uid = id).first()

    # 修改用户信息
    if user_info:
        model_user = user_info
    # 新增加用户
    else:
        model_user = User()
        model_user.login_salt = UserService.geneSalt()
        model_user.created_time = getCurrentDate()

    model_user.nickname = nickname
    model_user.mobile = mobile
    model_user.email = email
    model_user.login_name = login_name
    model_user.update_time = getCurrentDate()
    if login_pwd != default_pwd:
        model_user.login_pwd = UserService.genePwd(login_pwd,model_user.login_salt)
    # else:
    #     model_user.login_pwd = UserService.genePwd(default_pwd,model_user.login_salt)
        

    db.session.add(model_user)
    db.session.commit()

    return jsonify(resp)
def set():
    # 这里用来区别是添加用户还是修改用户资料
    default_pwd = '******'
    request_method = request.method
    if request_method == 'GET':
        resp_data= {}
        req = request.args
        uid = int(req.get('id', 0))
        info = None
        if uid:
            info = User.query.filter_by(uid=uid).first()
        resp_data['info'] = info
        return ops_render('/account/set.html', resp_data)

    resp_data = {'cade': 200, 'msg': '操作成功', 'data': {}}
    request_data = request.values
    uid = request_data['id'] if 'id' in request_data else 0
    nickname = request_data['nickname'] if 'nickname' in request_data else ''
    mobile = request_data['mobile'] if 'mobile' in request_data else ''
    email = request_data['email'] if 'email' in request_data else ''
    login_name = request_data['login_name'] if 'login_name' in request_data else ''
    login_pwd = request_data['login_pwd'] if 'login_pwd' in request_data else ''

    if not nickname or len(nickname) < 1:
        resp_data['code'] = -1
        resp_data['msg'] = "请输入符合规范的昵称"
        return jsonify(resp_data)

    if not email or len(email) < 1:
        resp_data['code'] = -1
        resp_data['msg'] = "请输入符合规范的邮箱"
        return jsonify(resp_data)

    if not login_name or len(login_name) < 1:
        resp_data['code'] = -1
        resp_data['msg'] = "请输入符合规范的用户名"
        return jsonify(resp_data)

    if not login_pwd or len(login_pwd) < 1:
        resp_data['code'] = -1
        resp_data['msg'] = "请输入符合规范的登录密码"
        return jsonify(resp_data)

    # 判断新增用户名是否存在
    has_in = User.query.filter(User.login_name == login_name, User.uid != uid).first()
    if has_in:
        resp_data['code'] = -1
        resp_data['msg'] = '登录名已存在, 请换一个试试'
        return jsonify(resp_data)

    user_info = User.query.filter_by(uid=uid).first()
    if user_info:
        model_user = user_info
    else:
        model_user = User()
        model_user.create_time = getCurrentDate()
        model_user.login_salt = UserService.get_salt()

    model_user.nickname = nickname
    model_user.email = email
    model_user.mobile = mobile
    # TODO:用户头像以及用户状态
    model_user.sex = model_user.status =1
    model_user.avatar = 'www'
    model_user.login_name = login_name
    if login_pwd != default_pwd:
        model_user.login_pwd = UserService.genePwd(login_pwd, model_user.login_salt)
    
    model_user.update_time = getCurrentDate()
    db.session.add(model_user)
    db.session.commit()
    return jsonify(resp_data)