def en_token(telephone, user_id): iat = time.time() exp = iat + ex_time['token_ex'] payload = { 'telephone': str(telephone), 'user_id': str(user_id), 'iat': iat, 'exp': exp } key_pix = generate_key() token = jwt.encode(payload, signer=jws.HmacSha(bits=256, key=web['token_key'] + key_pix)) redis_service.set(token, key_pix) return token
def login_pretreat(): """ 登录处理 :return:state, token """ params = get_params() state, result = login_service(params['telephone']) if state: if result["password"] == params['password']: token = en_token(user_id=result["id"], telephone=params['telephone']) redis_server.set(redis_pre['token_pix'] + params['telephone'], token) return True, token return False, None
def ver_code(): """ 验证码 :return: """ params = get_params() telephone = params["telephone"] code = dict() code["code"] = generate_code() redis_server.set(redis_pre['register_sms'] + str(telephone), code["code"], 60) # 阿里大鱼发送验证码 result = json.loads(send(code["code"], str(telephone))) print result if result["Code"] == "OK": return True return False
def admin_login_service(username, password): """ 管理员登录 :param username: 用户名 :param password: 密码 :return: """ session = g.session result = session.query(Admin).filter(and_( Admin.username == username, Admin.password == password )).one_or_none() if result: token = en_token(user_id=result.id, telephone=username) redis_server.set(redis_pre['token_pix'] + username, token) return True, token return False, None
def user_register(): """ 注册 :return: """ params = get_params() telephone = params["telephone"] # redis验证码和前端验证码比较 code = redis_server.get(redis_pre['register_sms'] + str(telephone)) if code and int(params["code"]) == int(code): redis_server.delete(redis_pre['register_sms'] + str(telephone)) state, result = register(params["telephone"], params["password"]) if state: token = en_token(user_id=result, telephone=params['telephone']) redis_server.set(redis_pre['token_pix'] + params['telephone'], token) return True, token return state, result return False, msg.A_CODE_TIMEOUT