def register(request): # 判断参数是否正确 data = request.data code = data.get("code") if not code: return Response({"status": 400, "msg": "验证码不能为空"}) validate_code = None kwargs = {} if data.get("mobile"): kwargs["mobile"] = data["mobile"] passport = passportRepository.load(**kwargs) if passport: return Response({"status": 400, "msg": "用户已存在"}) validate_code = cache.get(data.get("mobile")) elif data.get("email"): kwargs["email"] = data["email"] passport = passportRepository.load(**kwargs) if passport: return Response({"status": 400, "msg": "用户已存在"}) validate_code = cache.get(data.get("email")) else: return Response({"status": 400, "msg": "参数错误"}) kwargs = {} kwargs["name"] = data["name"] passport_list = passportRepository.search(**kwargs) if passport_list: return Response({"status": 400, "msg": "用户名已存在"}) if validate_code != str(code): return Response({"status": 400, "msg": "验证码错误"}) password = data.get("password") if not password: return Response({"status": 400, "msg": "密码不能为空"}) if not Validate.password_validate(password): return Response({"status": 400, "msg": "密码格式不正确"}) # 随机生成盐值加密 salt = CryptogramGenerate.get_salt().decode() password = CryptogramGenerate.md5(password + salt) password = password + '|' + salt created = TimeUtil.get_utc_time() avert = user_settings.defaultAvert passport = Passport(password=password, created=created, email=data.get("email"), mobile=data.get("mobile"), name=data.get("name"), avert=avert) passport.save() return Response({"status": 200, "msg": ""})
def detail(request): data = request.GET if not data.get("id"): return Response({"status": 400, "msg": "参数错误"}) kwargs = {} kwargs["id"] = data["id"] article = articleRepository.load(**kwargs) if not article: return Response({"status": 404, "msg": "文章不存在"}) # 获取 passport kwargs = {} kwargs["id"] = article.passport_id passport = passportRepository.load(**kwargs) article = ArticleSerializers(article).data if (passport): article["passport_name"] = passport.name article["avert"] = passport.avert workspace = WorkspaceUtil.get_workspace(request) article["is_collect"] = False if workspace: passport_id = workspace.passport_id kwargs = {"passport_id": passport_id, "article_id": data["id"]} collect = collectRepository.load(**kwargs) if collect: article["is_collect"] = True return Response({"status": 200, "data": article})
def is_exist(request): data = request.data passport = None kwargs = {} if data.get("email"): kwargs["email"] = data["email"] passport = passportRepository.load(**kwargs) if passport: passport = RegisterSerializers(passport).data elif data.get("mobile"): kwargs["mobile"] = data["mobile"] passport = passportRepository.load(**kwargs) if passport: passport = RegisterSerializers(passport).data else: return Response({"status": 400, "msg": "参数错误"}) return Response({"status": 200, "data": passport})
def modify_password(request): data = request.data code = data.get("code") if not code: return Response({"status": 400, "msg": "参数错误"}) kwargs = {} if data.get("email"): kwargs["email"] = data["email"] passport = passportRepository.load(**kwargs) if not passport: return Response({"status": 404, "msg": "用户不存在"}) validate_code = cache.get(data.get("email")) if not validate_code: return Response({"status": 404, "msg": "验证码过期"}) elif request.get("mobile"): kwargs["mobile"] = data["mobile"] passport = passportRepository.load(**kwargs) if not passport: return Response({"status": 404, "msg": "用户不存在"}) validate_code = cache.get(data.get("mobile")) if not validate_code: return Response({"status": 404, "msg": "验证码过期"}) else: return Response({"status": 400, "msg": "参数错误"}) if str(code) != validate_code: return Response({"status": 400, "msg": "验证码错误"}) password = data.get("password") if not password: return Response({"status": 400, "msg": "密码不能为空"}) if not Validate.password_validate(password): return Response({"status": 400, "msg": "密码格式不正确"}) salt = CryptogramGenerate.get_salt().decode() password = CryptogramGenerate.md5(password + salt) password = password + '|' + salt passport.password = password passport.save() return Response({"status": 200, "msg": "密码更新成功"})
def login(request): data = request.data password = data.get("password") if not password: return Response({"status": 400, "msg": "参数错误"}) kwargs = {} if data.get("email"): kwargs["email"] = data["email"] passport = passportRepository.load(**kwargs) if not passport: return Response({"status": 404, "msg": "用户名或密码错误"}) elif data.get("mobile"): kwargs["mobile"] = data["mobile"] passport = passportRepository.load(**kwargs) if not passport: return Response({"status": 404, "msg": "用户名或密码错误"}) else: return Response({"status": 400, "msg": "参数错误"}) salt = passport.password.split('|')[1] password = CryptogramGenerate.md5(password + salt) if passport.password != password + '|' + salt: return Response({"status": 404, "msg": "用户名或密码错误"}) # 每次登录都会创建一个token信息, 并将token信息写入到redis缓存, key passport, value token login_type = data["login_type"] expire_time = None if login_type == Token.LoginType.app.value: expire_time = user_settings.appExpireTime elif login_type == Token.LoginType.web.value: expire_time = user_settings.webExpireTime kwargs = {} kwargs["passport_id"] = passport.id kwargs["expire_time"] = expire_time kwargs["login_type"] = login_type token_util = TokenUtil() workspace = token_util.create_token(**kwargs) workspace = WorkspaceSerializers(workspace).data return Response({"status": 200, "msg": "登录成功", "data": workspace})
def improve_user_information(request): data = request.data workspace = WorkspaceUtil.get_workspace(request) passport_id = workspace.passport_id data["id"] = passport_id # 判断用户名是否存在 if data.get("name"): kwargs = {} kwargs["name"] = data["name"] passport = passportRepository.load(**kwargs) if passport and passport.id != passport_id: return Response({"status": 400, "msg": "昵称已存在"}) passportRepository.modify(**data) return Response({"status": 200, "msg": "修改成功"})
def get_workspace_by_token(token): passport_id = token.passport_id search = {} search["passport_id"] = passport_id passport = passportRepository.load(**search) workspace = Workspace() workspace.name = passport.name workspace.passport_id = passport_id workspace.token_id = token.token_id workspace.avert = passport.avert workspace.signature = passport.signature workspace.start_time = token.start_time workspace.expire_time = token.expire_time workspace.login_type = token.login_type return workspace
def detail(request): data = request.GET id = "" if data.get("id"): id = data["id"] else: workspace = WorkspaceUtil.get_workspace(request) if not workspace: return Response({"status": 401, "msg": "登录超时,请重新登录"}) id = workspace.passport_id kwargs = {"id": id} passport = passportRepository.load(**kwargs) if not passport: return Response({"status": 404, "msg": "用户不存在"}) passport = RegisterSerializers(passport).data del passport["password"] del passport["mobile"] del passport["email"] return Response({"status": 200, "msg": "", "data": passport})
def create_token(self, **kwargs): # 首先判断Token是否存在 passport_id = kwargs["passport_id"] search = {} search["passport_id"] = passport_id search["login_type"] = kwargs["login_type"] token_list = tokenRepository.search(**search) if len(token_list) > 0: # 先删除已经存在的token exist_token = token_list[0] exist_token.delete() token = Token() token.passport_id = kwargs["passport_id"] token.token_id = self.create_token_id() token.start_time = TimeUtil.get_utc_time() token.expire_time = kwargs["expire_time"] + token.start_time token.login_type = kwargs["login_type"] token.created = TimeUtil.get_utc_time() token.version = 1 token.save() # 获取用户名称. search = {} search["passport_id"] = passport_id passport = passportRepository.load(**search) workspace = Workspace() workspace.name = passport.name workspace.avert = passport.avert workspace.signature = passport.signature workspace.update_time = TimeUtil.get_utc_time() workspace.passport_id = passport_id workspace.token_id = token.token_id workspace.start_time = token.start_time workspace.expire_time = token.expire_time workspace.login_type = token.login_type cache.set(token.token_id, workspace, kwargs["expire_time"]) return workspace