关于注册的一些心得

  1. 邮件发送

    1.1 settings.py 配置

# 邮件
EMAIL_HOST = 'smtp.qq.com'
# 设置端口号,为数字
EMAIL_PORT = 25
#设置发件人邮箱
EMAIL_HOST_USER = '1825241301@qq.com'
# 设置发件人 授权码: 进入QQ邮箱->设置->账户->开启POP3/SMTP服务,发短信获得授权码
EMAIL_HOST_PASSWORD = 'dlmqeqvayfsbfbdj'
# 设置是否启用安全链接
EMAIL_USER_TLS = True
# EMAIL_USER_TLS = False
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

# redis 的配置文件
CACHES = {
   "default": {
       "BACKEND": "django_redis.cache.RedisCache",
       "LOCATION": "redis://127.0.0.1:6379/0",
       "OPTIONS": {
           "CLIENT_CLASS": "django_redis.client.DefaultClient",
           # "CONNECTION_POOL_KWARGS": {"max_connections": 100}
           # "PASSWORD": "密码",
      }
  }
}

1.2 发邮件+存redis

class Email_code_APIView( APIView ):
   def post(self, request):
       try:
           email = request.data.get( 'email' )
           # 生成随机的验证码
           code = Email_Code()
           ret = '您的验证码是{}'.format(code)
           # 给邮箱发送验证码
           my_email = send_mail( '激活验证', ret, settings.DEFAULT_FROM_EMAIL, [email] )
           if not my_email == 1:
               return Response( {
                   'code': 0,
                   'msg': "邮件发送失败",
              } )
           key = email
           # 存redis
           redis_client = get_redis_connection('default')
           redis_client.setex(key, 60*5, code)  # 5分钟的有效时间
           ret = {
               'code': 200,
               'msg': "邮件发送成功"
          }
           return Response( ret )
           # 返回错误信息
       except Exception as e:
           return Response( {'code': 0, 'msg': '网络有些问题,请等一下再试'} )

1.3 验证邮箱验证码

code = request.POST.get( 'email_code' )
if not code:
   return Response( {'code': 4003, 'msg': '请输入验证码'} )
# 获取验证码,并判断是否失效了
redis_client = get_redis_connection('default')
redis_image_code = redis_client.get(email)
redis_image_code = redis_image_code.decode()# bytes
# 比较用户提供的图片内容是否和redis中保存的一致
if code != redis_image_code:
   print(code)
   print(redis_image_code)
   return Response( {'code': 999, 'msg': '验证码不正确'} )
if not redis_image_code:
   return Response( {'code': 4006, 'msg': '验证码已失效'} )
if code == redis_image_code:
  ......
  1. 用户名唯一性

# 利用mysql的唯一索引,将表中某一字段设置为唯一索引(unique),
每次注册时,验证完手机号,密码等信息,直接插入数据,如果用户名设置唯一索引,
那么相同用户名,只有第一次注册能成功,第二次注册由于用户名是唯一索引,
所以在次插入数据会失败,通过这种方法可以减少数据库访问量,减少数据库负担。
  1. ant-design

    文档连接:https://www.antdv.com/docs/vue/introduce-cn/

# 特性 
1. 提炼自企业级中后台产品的交互语言和视觉风格。
2. 开箱即用的高质量 Vue 组件。
3. 共享Ant Design of React设计工具体系。
# 支持环境
1. 现代浏览器和 IE9 及以上(需要 polyfills)。
2. 支持服务端渲染。
3. Electron


实例:    
<a-form-item label="用户名" v-bind="formlayout" style="width: 500px;">
<a-input v-model="username" />
</a-form-item>
<a-form-item label="密码" v-bind="formlayout" style="width: 500px;">
<a-input v-model="password1" />
</a-form-item>

ant-design的标签以a-开头,label为标签名,v-bind绑定样式,样式在data()里。
//表单样式
formlayout:{
//标签
labelCol:{
xs:{span:24},
sm:{span:8}
},
//文本框
wrapperCol:{
xs:{span:24},
sm:{span:16}
},
},
//按钮样式
buttonlayout:{
wrapperCol:{
xs:{span:24,
offset:0
},
sm:{span:26,
offset:8
}
},
}

 #  当点击按钮展示返回信息时使用

this.$message.info(res.data.msg)
posted @ 2020-10-27 20:40  CefiLing  阅读(172)  评论(0)    收藏  举报