day8_接口开发_add_stu
@server.route('/add_stu',methods = ['post'])
def add_stu():
sign = request.json.get('sign','') # 必填参数
username = request.json.get('username','') # 必填参数
real_name = request.json.get('real_name','') # 必填参数
sex = request.json.get('sex','0') # 可不填,0代表性别男
phone = request.json.get('phone','') # 必填参数
u_type = request.json.get('type','1') # 可不填,0代表现场班
u_class = request.json.get('class','') # 必填参数
if sign.strip() and username.strip() and real_name.strip() and phone.strip() and u_class.strip():
if sex != '1' and sex != '0': # 处理性别是否传入正确
return my_json(SEX_ERR)
elif u_type != '1' and u_type != '0': # 处理学习类型是否传入正确
return my_json(TYPE_ERR)
elif not (phone.isdigit() and len(phone) == 11): # 判断手机号格式是否正确
return my_json(PHONE_ERR)
else:
redis_sign = op_redis(username) # 从redis里取到sign
if redis_sign: # 从redis里取到sign,在判断sign是否正确
if redis_sign.decode() == sign: # redis_sign是bytes类型,从redis里取出的都是bytes类型,转成字符串
select_sql = 'select id from bt_stu where phone = "%s"' % phone
if conn_mysql(select_sql):
return my_json(PHONE_EXIST) # 判断手机号是否已存在
sql = "insert into `jxz`.`bt_stu`(`real_name`,`sex`,`phone`,`class`,`type`) " \
"values('%s','%s','%s','%s','%s');" % (real_name,sex,phone,u_class,u_type)
conn_mysql(sql)
return my_json(SUCCESS) # 添加用户成功
else:
return my_json(SIGN_ERR) # 签名失败
else:
return my_json(NOT_LOGIN) # 用户未登录
else:
return my_json(NOT_NULL)
如果add_stu接口的入参是request.json.get类型的就用下面的方式
如果add_stu接口入参的格式是request.values.get这种形式的话就用下面的方式