def set(): default_pwd = "******" 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 = {'code': 200, 'msg': '操作成功~~', 'data': {}} req = request.values id = req['id'] if 'id' in req else 0 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(email) < 6: resp['code'] = -1 resp['msg'] = "请输入符合规范的登录密码~~" return jsonify(resp) 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.created_time = get_current_time() model_user.login_salt = UserService.geneSalt() model_user.nickname = nickname model_user.mobile = mobile model_user.email = email model_user.login_name = login_name if login_pwd != default_pwd: if user_info and user_info.uid == 1: resp['code'] = -1 resp['msg'] = "该用户是演示账号,不准修改密码和登录用户名~~" return jsonify(resp) model_user.login_pwd = UserService.genePwd(login_pwd, model_user.login_salt) model_user.updated_time = get_current_time() db.session.add(model_user) db.session.commit() return jsonify(resp)
def set(): default_pwd = "******" if request.method == 'GET': resp_data = {} uid = request.args.get('id', 0) user_info = None if uid: user_info = User.query.filter_by(uid=uid).first() resp_data['user_info'] = user_info resp_data['default_pwd'] = default_pwd return ops_render("account/set.html", resp_data) elif request.method == 'POST': resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values id = req['id'] if 'id' in req else None nickname = req['nickname'] if 'nickname' in req else None mobile = req['mobile'] if 'mobile' in req else None email = req['email'] if 'email' in req else None login_name = req['login_name'] if 'login_name' in req else None login_pwd = req['login_pwd'] if 'login_pwd' in req else None # if not all([nickname, mobile, email, login_name, login_pwd]): # resp['code'] = -1 # resp['msg'] = '数据不完整,请输入完整再提交' # return jsonify(resp) if nickname is None or len(nickname) < 1: resp['code'] = -1 resp['msg'] = "请输入符合规范的姓名" return jsonify(resp) if mobile is None or len(mobile) < 11: 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) 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.created_time = getCurrentDate() model_user.login_salt = UserService.geneSalt() model_user.nickname = nickname model_user.mobile = mobile model_user.email = email 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.updated_time = getCurrentDate() db.session.add(model_user) db.session.commit() return jsonify(resp)
def set(): """ 修改个人信息页面、添加账号信息页面 """ default_pwd = "******" if request.method == "GET": resp_data = {} req = request.args uid = int(req.get("id", 0)) info = None # 因为如果是添加信息,那么上个页面,就不会传回id,所以为None,进入添加账号页面。否则点击编辑就传回id,进入修改信息页面 if uid: info = User.query.filter_by( uid=uid).first() # filter_by不用写类,他会自动区分的 resp_data['info'] = info # 统一渲染的 resp_data(json)里面,将user_info放进去 return ops_render("account/set.html", resp_data) resp = {'code': 200, 'msg': u"操作成功", 'data': {}} # 获取登录变量 req = request.values # 所有的请求变量,放到这个数组里 id = req['id'] if 'id' in req else 0 # id 是用来判断是增加用户信息,还是修改用户信息 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) < 2 or len(nickname) > 15: # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的姓名" return jsonify(resp) # json 格式的转换 if mobile is None or not re.match(REGEX_MOBILE, mobile): # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的手机号码" return jsonify(resp) # json 格式的转换 if email is None or not re.match(REGEX_Email, email): # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的邮箱" return jsonify(resp) # json 格式的转换 if login_name is None or not re.match(REGEX_LOGIN_NAME, login_name): # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的登录名" return jsonify(resp) # json 格式的转换 if login_pwd is None or len(login_pwd) < 6 or len( login_pwd) > 15: # 进行参数判断 resp['code'] = -1 resp['msg'] = "请输入符合规范的登录密码" return jsonify(resp) # json 格式的转换 has_in = User.query.filter(User.login_name == login_name, User.uid != id).first() # login_name判断用户是否存在。User.uid != id:这个表明是该用户id不存在,即为增加用户信息。filter支持的方式更多一点。filter_by只能传一个json if has_in: # 如果用户名已经存在了 resp['code'] = -1 resp['msg'] = "该登录名已存在,请重新输入" return jsonify(resp) # json 格式的转换 user_info = User.query.filter_by( uid=id).first() # 判断用户 id是否存在。如果存在,那么 modle_use,就是这个用户的信息。set页面为修改用户信息 if user_info: modle_use = user_info else: # 否则,就是这个uid不存在。那么久为增加用户信息界面 modle_use = User() modle_use.created_time = getCurrentData() # 增加用户信息时,created_time才改变 modle_use.login_salt = UserService.geneSalt( ) # geneSalt即数据库salt字段, 自定义的加密规则。增加用户信息,才会生成salt modle_use.nickname = nickname modle_use.mobile = mobile modle_use.email = email modle_use.login_name = login_name if login_pwd != default_pwd: # 如果传回来的密码value,不是default密码,那么就改密码,反之不改密码。 modle_use.login_pwd = UserService.genePwd( login_pwd, modle_use.login_salt) # 加密后的密码,就是前面定义的,通过密码和 salt进行加密 resp['msg'] = "操作成功,登录用户 %s 的密码为:%s" % (login_name, login_pwd) modle_use.updated_time = getCurrentData() db.session.add(modle_use) # 数据库添加数据,统一提交 db.session.commit() return jsonify(resp) # 返回信息,更改成功
def set(): 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) # POST 更新数据库 resp = {'code': 200, 'msg': "操作成功", 'data': {}} # ajax 发送的数据 req = request.values id = req['id'] if 'id' in req else 0 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) < 6: resp['code'] = -1 resp['msg'] = "请输入符合规范的密码" return jsonify(resp) is_exsits = User.query.filter(User.login_name == login_name, User.uid != id).first() if is_exsits: 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.created_time = getCurrentDate() model_user.login_salt = UserService.generateSalt() model_user.nickname = nickname model_user.mobile = mobile model_user.email = email model_user.login_name = login_name model_user.avatar = '' if user_info and user_info.uid == 1: resp['code'] = -1 resp['msg'] = "该用户为apang,不允许修改" return jsonify(resp) model_user.login_pwd = UserService.generatePwd(login_pwd, model_user.login_salt) model_user.updated_time = getCurrentDate() db.session.add(model_user) db.session.commit() return jsonify(resp)