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 send_email(request): data = request.data if not data.get("email"): return Response({"status": 400, "msg": "参数错误"}) # 验证邮箱有效性 if not Validate.email_validate(data.get("email")): return Response({"status": 400, "msg": "参数错误"}) validate_code = VerificationCode.num_random_list(6) send_mail('萌宠屋', '注册验证码:{0}'.format(validate_code), settings.EMAIL_HOST_USER, [data["email"]], fail_silently=False) # email作为key缓存code 注册时候比较 cache.set(data["email"], validate_code, timeout=30 * 60) return Response({"status": 200, "msg": "注册码发送成功"})
def send_mobile(request): data = request.data # 判断验证码是否正确 if not data.get("uid"): return Response({"status": 400, "msg": "参数错误"}) if not data.get("code"): return Response({"status": 400, "msg": "参数错误"}) key = data.get("uid") cache_data = cache.get(key) if not data: return Response({"status": 400, "msg": "图片验证码过期"}) if cache_data.lower() != data.get("code"): return Response({"status": 400, "msg": "图片验证码错误"}) if not data.get("mobile"): return Response({"status": 400, "msg": "参数错误"}) # 验证手机号有效性 if not Validate.mobile_validate(data.get("mobile")): return Response({"status": 400, "msg": "手机号不正确"}) mobile = data["mobile"] apikey = user_settings.smsApikey sms_host = user_settings.smsHost sms_port = user_settings.smsPort sms_version = user_settings.smsVersion sms_send_uri = "/" + sms_version + "/sms/single_send.json" validate_code = VerificationCode.num_random_list(6) text = "【易宠享】您的验证码是%s" % (validate_code) params = urllib.parse.urlencode( {'apikey': apikey, 'text': text, 'mobile': mobile}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = http.client.HTTPSConnection(sms_host, port=sms_port, timeout=60) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() try: response_data = json.loads(response_str.decode("utf-8")) if response_data.get("code") == 0: # 缓存 cache.set(mobile, validate_code, timeout=30 * 60) return Response({"status": 200, "data": response_data}) else: return Response({"status": 500, "msg": "发送失败"}) except Exception as e: return Response({"status": 500, "msg": "发送失败"})
def send_mobile(request): data = request.data # 判断验证码是否正确 if not data.get("uid"): return Response({"status": 400, "msg": "参数错误"}) if not data.get("code"): return Response({"status": 400, "msg": "参数错误"}) key = data.get("uid") cache_data = cache.get(key) if not data: return Response({"status": 400, "msg": "图片验证码过期"}) if cache_data.get("code") != data.get("code"): return Response({"status": 400, "msg": "图片验证码错误"}) if not data.get("mobile"): return Response({"status": 400, "msg": "参数错误"}) # 验证手机号有效性 if not Validate.mobile_validate(data.get("mobile")): return Response({"status": 400, "msg": "手机号不正确"})
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 = passport[0] passport.password = password passport.save() return Response({"status": 200, "msg": "密码更新成功"})