html
<template> <div id="home"> <Header/> <Banner/> <Footer/> </div> </template> <script> import Header from "./common/Header" import Banner from "./common/Banner" import Footer from "./common/Footer" export default { name:"Home", data(){ return { } }, components:{ Header, Banner, Footer, } } </script> <style scoped> </style>
前端
<template> <div class="header"> <el-container> <el-header> <el-row> <el-col class="logo" :span="3"> <a href="/"> <img src="@/assets/head-logo.svg" alt=""> </a> </el-col> <el-col class="nav" :span="16"> <el-row> <el-col :span="3"><router-link class="current" to="/course">免費課</router-link></el-col> <el-col :span="3"><router-link to="/">輕課</router-link></el-col> <el-col :span="3"><router-link to="/">學位課</router-link></el-col> <el-col :span="3"><router-link to="/">題庫</router-link></el-col> <el-col :span="3"><router-link to="/">教育</router-link></el-col> </el-row> </el-col> <el-col class="login-bar" :span="5"> <el-row v-if="token"> <el-col class="cart-ico" :span="9"> <router-link to=""> <b class="goods-number">0</b> <img class="cart-icon" src="@/assets/cart.svg" alt=""> <span><router-link to="/cart">購物車</router-link></span> </router-link> </el-col> <el-col class="study" :span="8" :offset="2"><router-link to="">學習中心</router-link></el-col> <el-col class="member" :span="5"> <el-menu class="el-menu-demo" mode="horizontal"> <el-submenu index="2"> <template slot="title"><router-link to=""><img src="@/assets/logo@2x.png" alt=""></router-link></template> <el-menu-item index="2-1">個人帳戶</el-menu-item> <el-menu-item index="2-2">個人訂單</el-menu-item> <el-menu-item index="2-3">個人優惠卷</el-menu-item> <el-menu-item index="2-3">退出登陸</el-menu-item> </el-submenu> </el-menu> </el-col> </el-row> <el-row v-else> <el-col class="cart-ico" :span="9"> <router-link to=""> <img class="cart-icon" src="@/assets/cart.svg" alt=""> <span><router-link to="/cart">購物車</router-link></span> </router-link> </el-col> <el-col :span="10" :offset="5"> <span class="register"> <router-link to="/login">登陸</router-link> | <router-link to="/register">註冊</router-link> </span> </el-col> </el-row> </el-col> </el-row> </el-header> </el-container> </div> </template> <script> export default { name: "Header", data(){ return { // 設置一個登陸標識,表示是否登陸 token: false, }; } } </script> <style scoped> .header{ top:0; left:0; right:0; margin: auto; background-color: #fff; height: 80px; z-index: 1000; position: fixed; box-shadow: 0 0.5px 0.5px 0 #c9c9c9; } .header .el-container{ width: 1200px; margin: 0 auto; } .el-header{ height: 80px!important; padding:0; } .logo{ } .logo img{ padding-top: 22px; } .nav{ margin-top: 22px; } .nav .el-col a{ display: inline-block; text-align: center; padding-bottom: 16px; padding-left: 5px; padding-right: 5px; position: relative; font-size: 16px; margin-left: 20px; } .nav .el-col .current{ color: #4a4a4a; border-bottom: 4px solid #ffc210; } .login-bar{ margin-top: 22px; } .cart-ico{ position: relative; border-radius: 17px; } .cart-ico:hover{ background: #f0f0f0; } .goods-number{ width: 16px; height: 16px; line-height: 17px; font-size: 12px; color: #fff; text-align: center; background: #fa6240; border-radius: 50%; transform: scale(.8); position: absolute; left: 16px; top: -1px; } .cart-icon{ width: 15px; height: auto; margin-left: 6px; } .cart-ico span{ margin-left: 12px; } .member img{ width: 26px; height: 26px; border-radius: 50%; display: inline-block; } .member img:hover{ border: 1px solid yellow; } </style>
Footer.vuevue
<template> <div class="footer"> <el-container> <el-row> <el-col :span="4"><router-link to="">關於咱們</router-link></el-col> <el-col :span="4"><router-link to="">聯繫咱們</router-link></el-col> <el-col :span="4"><router-link to="">商務合做</router-link></el-col> <el-col :span="4"><router-link to="">幫助中心</router-link></el-col> <el-col :span="4"><router-link to="">意見反饋</router-link></el-col> <el-col :span="4"><router-link to="">新手指南</router-link></el-col> <el-col :span="24"><p class="copyright">Copyright © luffycity.com版權全部 | 京ICP備17072161號-1</p></el-col> </el-row> </el-container> </div> </template> <script> export default { name:"Footer", data(){ return {} } } </script> <style scoped> .footer{ width: 100%; height: 128px; background: #25292e; } .footer .el-container{ width: 1200px; margin: auto; } .footer .el-row { align-items: center; padding: 0 200px; padding-bottom: 15px; width: 100%; margin-top: 38px; } .footer .el-row a{ color: #fff; font-size: 14px; } .footer .el-row .copyright{ text-align: center; color: #fff; font-size: 14px; } </style>
python
<template> <div class="banner"> <el-carousel trigger="click" height="473px"> <el-carousel-item v-for="banner in banner_list"> <a :href="banner.link"><img width="100%" :src="banner.img" alt=""></a> </el-carousel-item> </el-carousel> </div> </template> <script> export default { name:"Banner", data(){ return { banner_list:[ {link:"http://www.baidu.com",img:"/static/banner/1.png"}, {link:"http://www.baidu.com",img:"/static/banner/2.png"}, {link:"http://www.baidu.com",img:"/static/banner/3.png"}, ] }; } } </script> <style> .el-carousel__arrow{ width: 100px!important; height: 100px!important; } .el-icon-arrow-left{ font-size: 35px; margin-left: 50px; } .el-carousel__arrow--left{ left: -50px; } </style>
ios
import Vue from "vue" import Router from "vue-router" // 導入須要註冊路由的組件 import Home from "../components/Home" Vue.use(Router); // 配置路由列表 export default new Router({ mode:"history", routes:[ // 路由列表 { name:"Home", path: "/home", component:Home, }, { name:"Home", path: "/", component:Home, } ] })
前面已經安裝了,若是沒有安裝則須要安裝git
pip install pillow
由於以前修改了後端的排版格式,因此如今的settings都在dev.py 文件中設置github
# 設置django的靜態文件目錄 STATICFILES_DIRS = [ os.path.join(BASE_DIR,"luffy/statics") ] # 項目中存儲上傳文件的根目錄[暫時配置],注意,static目錄須要手動建立不然上傳文件時報錯 MEDIA_ROOT=os.path.join(BASE_DIR,"luffy/statics") # 訪問上傳文件的url地址前綴 MEDIA_URL ="/media/"
總路由urls.py新增代碼:web
from django.urls import re_path from django.conf import settings from django.views.static import serve urlpatterns = [ ... re_path(r'media/(?P<path>.*)', serve, {"document_root": settings.MEDIA_ROOT}), ]
redis
## cd luffy/apps python ../../manage.py startapp home
算法
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 新增一個系統導包路徑 import sys sys.path.insert(0,os.path.join(BASE_DIR,"apps")) INSTALLED_APPS = [ # 注意,加上drf框架的註冊 'rest_framework', # 子應用 'home', ]
在home/models.py建立模型
from django.db import models # Create your models here. class BannerInfo(models.Model): """ 輪播圖 """ # upload_to 存儲子目錄,真實存放地址會使用配置中的MADIE_ROOT+upload_to image = models.ImageField(upload_to='banner', verbose_name='輪播圖', null=True,blank=True) name = models.CharField(max_length=150, verbose_name='輪播圖名稱') note = models.CharField(max_length=150, verbose_name='備註信息') link = models.CharField(max_length=150, verbose_name='輪播圖廣告地址') orders = models.IntegerField(verbose_name='顯示順序') is_show=models.BooleanField(verbose_name="是否上架",default=False) is_delete=models.BooleanField(verbose_name="邏輯刪除",default=False) class Meta: db_table = 'ly_banner' verbose_name = '輪播圖' verbose_name_plural = verbose_name def __str__(self): return self.name
數據遷移
python manage.py makemigrations
python manage.py migrate
from rest_framework.serializers import ModelSerializer from .models import BannerInfo class BannerInfoSerializer(ModelSerializer): """輪播圖序列化器""" class Meta: model=BannerInfo fields = ("image","link")
from django.db.models import Q from rest_framework.generics import ListAPIView from .models import BannerInfo from .serializers import BannerInfoSerializer class BannerInfoListAPIView(ListAPIView): """ 輪播圖列表 """ queryset = BannerInfo.objects.filter( Q(is_show=True) & Q(is_delete=False) ).order_by("-orders") serializer_class = BannerInfoSerializer
from django.urls import path,re_path from . import views urlpatterns = [ path(r"banner/",views.BannerInfoListAPIView.as_view()), ]
把home的路由urls.py註冊到總路由
from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('', include("home.urls")), ]
因此咱們須要有一個後臺提供數據.
# 使用官方文檔上面的安裝方法,在ubantu上會安裝失敗,須要用下面的方法 pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
在配置文件中settings/dev.py 註冊以下應用
INSTALLED_APPS = [ ... 'xadmin', 'crispy_forms', 'reversion', ... ] # 修改使用中文界面 LANGUAGE_CODE = 'zh-Hans' # 修改時區 TIME_ZONE = 'Asia/Shanghai'
xadmin有創建本身的數據庫模型類,須要進行數據庫遷移
python manage.py makemigrations
python manage.py migrate
在總路由中添加xadmin的路由信息
import xadmin xadmin.autodiscover() # version模塊自動註冊須要版本控制的 Model from xadmin.plugins import xversion xversion.register_models() urlpatterns = [ path(r'xadmin/', xadmin.site.urls), ]
若是以前沒有建立超級用戶,須要建立,若是有了,則能夠直接使用以前的。
python manage.py createsuperuser
import xadmin from xadmin import views class BaseSetting(object): """xadmin的基本配置""" enable_themes = True # 開啓主題切換功能 use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting) class GlobalSettings(object): """xadmin的全局配置""" site_title = "路飛學城" # 設置站點標題 site_footer = "路飛學城有限公司" # 設置站點的頁腳 menu_style = "accordion" # 設置菜單摺疊 xadmin.site.register(views.CommAdminView, GlobalSettings)
在當前子應用中adminx.py,添加以下代碼
# 輪播圖 from .models import BannerInfo class BannerInfoModelAdmin(object): list_display=["name","orders","is_show"] xadmin.site.register(BannerInfo, BannerInfoModelAdmin)
class HomeConfig(AppConfig): name = 'home' verbose_name = '個人首頁'
在home.py/ __init_.py 加上
default_app_config = "home.apps.HomeConfig"
<template> <div class="banner"> <el-carousel trigger="click" height="506px"> <el-carousel-item v-for="item in banner_list"> <a :href="item.link"><img :src="item.image"></a> </el-carousel-item> </el-carousel> </div> </template> <script> export default { name:"Banner", data(){ return { banner_list:[], }; }, created: function(){ // 獲取輪播圖 this.$axios.get(this.$settings.Host+"/banner/").then(response => { console.log(response.data) this.banner_list = response.data }).catch(error => { console.log(error.response); }); } } </script>
引入一個公共模型【抽象模型,不會在數據遷移的時候爲它建立表】
公共模型,保存項目的公共代碼庫目錄下luffy/utils.py文件中。
from django.db import models class BaseModel(models.Model): """公共字段模型""" orders = models.IntegerField(verbose_name='顯示順序') is_show=models.BooleanField(verbose_name="是否上架",default=False) is_delete=models.BooleanField(verbose_name="邏輯刪除",default=False) create_time = models.DateTimeField(auto_now_add=True,verbose_name="添加時間") update_time = models.DateTimeField(auto_now=True,verbose_name="更新時間") class Meta: # 設置當前模型在數據遷移的時候不要爲它建立表 abstract = True
那麼這是在model中有一樣的字段的狀況就不會產生重複的字段
from django.db import models from luffy.utils.models import BaseModel # Create your models here. class BannerInfo(BaseModel): """ 輪播圖 """ # upload_to 存儲子目錄,真實存放地址會使用配置中的MADIE_ROOT+upload_to image = models.ImageField(upload_to='banner', verbose_name='輪播圖', null=True,blank=True) name = models.CharField(max_length=150, verbose_name='輪播圖名稱') note = models.CharField(max_length=150, verbose_name='備註信息') link = models.CharField(max_length=150, verbose_name='輪播圖廣告地址') class Meta: db_table = 'ly_banner' verbose_name = '輪播圖' verbose_name_plural = verbose_name def __str__(self): return self.name class NavInfo(BaseModel): """ 導航 """ NAV_POSITION = ( (0, 'top'), (1, 'footer') ) name = models.CharField(max_length=50, verbose_name='導航名稱') link = models.CharField(max_length=250, verbose_name='導航地址') opt = models.SmallIntegerField(choices=NAV_POSITION, default=0, verbose_name='位置') class Meta: db_table = 'ly_nav' verbose_name = '導航' verbose_name_plural = verbose_name def __str__(self): return self.name
數據遷移
python manage.py makemigrations
python manage.py migrate
from rest_framework.serializers import ModelSerializer from .models import NavInfo class NavInfoSerializer(ModelSerializer): """導航序列化器""" class Meta: model=NavInfo fields = ("name","link")
from .models import NavInfo from .serializers import NavInfoSerializer class NavInfoAPIView(ListAPIView): """ 導航列表 """ queryset = NavInfo.objects.filter( Q(is_show=True) & Q(is_delete=False) & Q(opt=0) ).order_by("-orders") serializer_class = NavInfoSerializer
from django.urls import path,re_path from . import views urlpatterns = [ ... path(r"nav/",views.NavInfoAPIView.as_view()), ]
在當前子應用adminx.py,添加以下代碼
# 導航 from home.models import NavInfo class NavInfoInfoModelAdmin(object): list_display=["name","link","is_show"] xadmin.site.register(NavInfo, NavInfoInfoModelAdmin)
<template> <div class="header"> <el-container> <el-header> <el-row> <el-col class="logo" :span="3"> <a href="/"> <img src="@/assets/head-logo.svg" alt=""> </a> </el-col> <el-col class="nav" :span="16"> <el-row> <el-col v-for="nav in nav_list" :span="3"><a :class="check(nav.link)?'current':''" :href="nav.link">{{nav.name}}</a></el-col> </el-row> </el-col> <el-col class="login-bar" :span="5"> <el-row v-if="token"> <el-col class="cart-ico" :span="9"> <router-link to=""> <b class="goods-number">0</b> <img class="cart-icon" src="@/assets/cart.svg" alt=""> <span><router-link to="/cart">購物車</router-link></span> </router-link> </el-col> <el-col class="study" :span="8" :offset="2"><router-link to="">學習中心</router-link></el-col> <el-col class="member" :span="5"> <el-menu class="el-menu-demo" mode="horizontal"> <el-submenu index="2"> <template slot="title"><router-link to=""><img src="@/assets/logo@2x.png" alt=""></router-link></template> <el-menu-item index="2-1">個人帳戶</el-menu-item> <el-menu-item index="2-2">個人訂單</el-menu-item> <el-menu-item index="2-3">個人優惠卷</el-menu-item> <el-menu-item index="2-3">退出登陸</el-menu-item> </el-submenu> </el-menu> </el-col> </el-row> <el-row v-else> <el-col class="cart-ico" :span="9"> <router-link to=""> <img class="cart-icon" src="@/assets/cart.svg" alt=""> <span><router-link to="/cart">購物車</router-link></span> </router-link> </el-col> <el-col :span="10" :offset="5"> <span class="register"> <router-link to="/login">登陸</router-link> | <router-link to="/register">註冊</router-link> </span> </el-col> </el-row> </el-col> </el-row> </el-header> </el-container> </div> </template> <script> export default { name: "Header", data(){ return { // 設置一個登陸標識,表示是否登陸 token: false, nav_list:[], }; }, created() { // 獲取導航 this.$axios.get(this.$settings.Host+"/nav/").then(response=>{ this.nav_list = response.data console.log(this.nav_list) }).catch(error=>{ console.log(error.response) }) }, methods:{ check(link){ return link==window.location.pathname } } } </script> <style scoped> .header{ top:0; left:0; right:0; margin: auto; background-color: #fff; height: 80px; z-index: 1000; position: fixed; box-shadow: 0 0.5px 0.5px 0 #c9c9c9; } .header .el-container{ width: 1200px; margin: 0 auto; } .el-header{ height: 80px!important; padding:0; } .logo{ } .logo img{ padding-top: 22px; } .nav{ margin-top: 22px; } .nav .el-col a{ display: inline-block; text-align: center; padding-bottom: 16px; padding-left: 5px; padding-right: 5px; position: relative; font-size: 16px; margin-left: 20px; } .nav .el-col .current{ color: #4a4a4a; border-bottom: 4px solid #ffc210; } .login-bar{ margin-top: 22px; } .cart-ico{ position: relative; border-radius: 17px; } .cart-ico:hover{ background: #f0f0f0; } .goods-number{ width: 16px; height: 16px; line-height: 17px; font-size: 12px; color: #fff; text-align: center; background: #fa6240; border-radius: 50%; transform: scale(.8); position: absolute; left: 16px; top: -1px; } .cart-icon{ width: 15px; height: auto; margin-left: 6px; } .cart-ico span{ margin-left: 12px; } .member img{ width: 26px; height: 26px; border-radius: 50%; display: inline-block; } .member img:hover{ border: 1px solid yellow; } </style>
<template> <div class="login box"> <img src="https://www.luffycity.com/static/img/Loginbg.3377d0c.jpg" alt=""> <div class="login"> <div class="login-title"> <img src="https://www.luffycity.com/static/img/Logotitle.1ba5466.png" alt=""> <p>幫助有志向的年輕人經過努力學習得到體面的工做和生活!</p> </div> <div class="login_box"> <div class="title"> <span @click="login_type=0">密碼登陸</span> <span @click="login_type=1">短信登陸</span> </div> <div class="inp" v-if="login_type==0"> <input v-model = "username" type="text" placeholder="用戶名 / 手機號碼" class="user"> <input v-model = "password" type="password" name="" class="pwd" placeholder="密碼"> <div id="geetest1"></div> <div class="rember"> <p> <input type="checkbox" class="no" name="a"/> <span>記住密碼</span> </p> <p>忘記密碼</p> </div> <button class="login_btn">登陸</button> <p class="go_login" >沒有帳號 <span>當即註冊</span></p> </div> <div class="inp" v-show="login_type==1"> <input v-model = "username" type="text" placeholder="手機號碼" class="user"> <input v-model = "password" type="text" class="pwd" placeholder="短信驗證碼"> <button id="get_code">獲取驗證碼</button> <button class="login_btn">登陸</button> <p class="go_login" >沒有帳號 <span>當即註冊</span></p> </div> </div> </div> </div> </template> <script> export default { name: 'Login', data(){ return { login_type: 0, username:"", password:"", } }, methods:{ }, }; </script> <style scoped> .box{ width: 100%; height: 100%; position: relative; overflow: hidden; } .box img{ width: 100%; min-height: 100%; } .box .login { position: absolute; width: 500px; height: 400px; top: 0; left: 0; margin: auto; right: 0; bottom: 0; top: -338px; } .login .login-title{ width: 100%; text-align: center; } .login-title img{ width: 190px; height: auto; } .login-title p{ font-family: PingFangSC-Regular; font-size: 18px; color: #fff; letter-spacing: .29px; padding-top: 10px; padding-bottom: 50px; } .login_box{ width: 400px; height: auto; background: #fff; box-shadow: 0 2px 4px 0 rgba(0,0,0,.5); border-radius: 4px; margin: 0 auto; padding-bottom: 40px; } .login_box .title{ font-size: 20px; color: #9b9b9b; letter-spacing: .32px; border-bottom: 1px solid #e6e6e6; display: flex; justify-content: space-around; padding: 50px 60px 0 60px; margin-bottom: 20px; cursor: pointer; } .login_box .title span:nth-of-type(1){ color: #4a4a4a; border-bottom: 2px solid #84cc39; } .inp{ width: 350px; margin: 0 auto; } .inp input{ border: 0; outline: 0; width: 100%; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp input.user{ margin-bottom: 16px; } .inp .rember{ display: flex; justify-content: space-between; align-items: center; position: relative; margin-top: 10px; } .inp .rember p:first-of-type{ font-size: 12px; color: #4a4a4a; letter-spacing: .19px; margin-left: 22px; display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; /*position: relative;*/ } .inp .rember p:nth-of-type(2){ font-size: 14px; color: #9b9b9b; letter-spacing: .19px; cursor: pointer; } .inp .rember input{ outline: 0; width: 30px; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp .rember p span{ display: inline-block; font-size: 12px; width: 100px; /*position: absolute;*/ /*left: 20px;*/ } #geetest{ margin-top: 20px; } .login_btn{ width: 100%; height: 45px; background: #84cc39; border-radius: 5px; font-size: 16px; color: #fff; letter-spacing: .26px; margin-top: 30px; } .inp .go_login{ text-align: center; font-size: 14px; color: #9b9b9b; letter-spacing: .26px; padding-top: 20px; } .inp .go_login span{ color: #84cc39; cursor: pointer; } </style>
import Vue from "vue" import Router from "vue-router" // 導入須要註冊路由的組件 import Home from "../components/Home" import Login from "../components/Login" Vue.use(Router); // 配置路由列表 export default new Router({ mode:"history", routes:[ // 路由列表 ... { name:"Login", path: "/login", component:Login, } ] })
Header.vue
<router-link to="/login">登陸</router-link>
Django默認已經提供了認證系統。認證系統包含:
用戶管理
權限
用戶組
密碼哈希系統
用戶登陸或內容顯示的表單和視圖
一個可插拔的後臺系統
Django默認用戶的認證機制依賴Session機制,咱們在項目中將引入JWT認證機制,將用戶的身份憑據存放在Token中,而後對接Django的認證系統,幫助咱們來實現:
用戶的數據模型
用戶密碼的加密與驗證
用戶的權限系統
Django認證系統中提供了用戶模型類User保存用戶的數據,默認的User包含如下常見的基本字段:
字段描述 | |
---|---|
username |
必選。150個字符之內。 用戶名可能包含字母數字,_ ,@ ,+ . 和- 個字符。 |
first_name |
可選(blank=True )。 少於等於30個字符。 |
last_name |
可選(blank=True )。 少於等於30個字符。 |
email |
可選(blank=True )。 郵箱地址。 |
password |
必選。 密碼的哈希加密串。 (Django 不保存原始密碼)。 原始密碼能夠無限長並且能夠包含任意字符。 |
groups |
與Group 之間的多對多關係。 |
user_permissions |
與Permission 之間的多對多關係。 |
is_staff |
布爾值。 設置用戶是否能夠訪問Admin 站點。 |
is_active |
布爾值。 指示用戶的帳號是否激活。 它不是用來控制用戶是否可以登陸,而是描述一種賬號的使用狀態。 |
is_superuser |
是不是超級用戶。超級用戶具備全部權限。 |
last_login |
用戶最後一次登陸的時間。 |
date_joined |
帳戶建立的時間。 當帳號建立時,默認設置爲當前的date/time。 |
set_password
(raw_password)
設置用戶的密碼爲給定的原始字符串,並負責密碼的。 不會保存User
對象。當None
爲raw_password
時,密碼將設置爲一個不可用的密碼。
check_password
(raw_password)
若是給定的raw_password是用戶的真實密碼,則返回True,能夠在校驗用戶密碼時使用。
管理器方法便可以經過User.objects.
進行調用的方法。
create_user
(username, email=None, password=None, **extra_fields)
建立、保存並返回一個User
對象。
create_superuser
(username, email, password, **extra_fields)
與create_user()
相同,可是設置is_staff
和is_superuser
爲True
。
python ../../manage.py startapp users
在settings.py文件中註冊子應用。
INSTALLED_APPS = [ ... 'users', ]
Django認證系統中提供的用戶模型類及方法很方便,咱們可使用這個模型類,可是字段有些沒法知足項目需求,如本項目中須要保存用戶的手機號,須要給模型類添加額外的字段。
Django提供了django.contrib.auth.models.AbstractUser
用戶抽象模型類容許咱們繼承,擴展字段來使用Django認證系統的用戶模型類。
咱們能夠在apps中建立Django應用users,並在配置文件中註冊users應用。
在建立好的應用users.py/models.py中定義用戶的用戶模型類。
class User(AbstractUser): """用戶模型類""" mobile = models.CharField(max_length=11, unique=True, verbose_name='手機號') class Meta: db_table = 'ly_users' verbose_name = '用戶' verbose_name_plural = verbose_name
咱們自定義的用戶模型類還不能直接被Django的認證系統所識別,須要在配置文件中告知Django認證系統使用咱們自定義的模型類。
AUTH_USER_MODEL = 'users.User'
AUTH_USER_MODEL點.應用名.模型類名
注意:Django建議咱們對於AUTH_USER_MODEL參數的設置必定要在第一次數據庫遷移以前就設置好,不然後續使用可能出現未知錯誤。
python manage.py makemigrations
python manage.py migrate
在用戶註冊或登陸後,咱們想記錄用戶的登陸狀態,或者爲用戶建立身份認證的憑證。咱們再也不使用Session認證機制,而使用Json Web Token認證機制。
Json web token (JWT), 是爲了在網絡應用環境間傳遞聲明而執行的一種基於JSON的開放標準((RFC 7519).該token被設計爲緊湊且安全的,特別適用於分佈式站點的單點登陸(SSO)場景。JWT的聲明通常被用來在身份提供者和服務提供者間傳遞被認證的用戶身份信息,以便於從資源服務器獲取資源,也能夠增長一些額外的其它業務邏輯所必須的聲明信息,該token也可直接被用於認證,也可被加密。
jwt的頭部承載兩部分信息:
聲明類型,這裏是jwt
完整的頭部就像下面這樣的JSON:
{
'typ': 'JWT',
'alg': 'HS256'
}
而後將頭部進行base64加密(該加密是能夠對稱解密的),構成了第一部分.
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
標準中註冊的聲明
公共的聲明
私有的聲明
標準中註冊的聲明 (建議但不強制使用) :
iss: jwt簽發者
sub: jwt所面向的用戶
aud: 接收jwt的一方
exp: jwt的過時時間,這個過時時間必需要大於簽發時間
nbf: 定義在什麼時間以前,該jwt都是不可用的.
iat: jwt的簽發時間
jti: jwt的惟一身份標識,主要用來做爲一次性token,從而回避重放攻擊。
公共的聲明 : 公共的聲明能夠添加任何的信息,通常添加用戶的相關信息或其餘業務須要的必要信息.但不建議添加敏感信息,由於該部分在客戶端可解密.
私有的聲明 : 私有聲明是提供者和消費者所共同定義的聲明,通常不建議存放敏感信息,由於base64是對稱解密的,意味着該部分信息能夠歸類爲明文信息。
定義一個payload:
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
而後將其進行base64加密,獲得JWT的第二部分。
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9
JWT的第三部分是一個簽證信息,這個簽證信息由三部分組成:
header (base64後的)
payload (base64後的)
secret
這個部分須要base64加密後的header和base64加密後的payload使用.
鏈接組成的字符串,而後經過header中聲明的加密方式進行加鹽secret
組合加密,而後就構成了jwt的第三部分。
secret就是用來進行jwt的簽發和jwt的驗證,因此,它就是你服務端的私鑰,在任何場景都不該該流露出去。一旦客戶端得知這個secret, 那就意味着客戶端是能夠自我簽發jwt了。
關於簽發和核驗JWT,咱們可使用Django REST framework JWT擴展來完成。
文檔網站http://getblimp.github.io/django-rest-framework-jwt/
安裝
pip install djangorestframework-jwt
配置
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } import datetime JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), }
from django_redis import get_redis_connection from rest_framework import serializers from .models import User import re class UserModelSerializer(serializers.ModelSerializer): sms_code = serializers.CharField(write_only=True, max_length=6, min_length=6, required=True, help_text="短信驗證碼") password2 = serializers.CharField(write_only=True, help_text="確認密碼") token = serializers.CharField(read_only=True, help_text="jwt token值") class Meta: model = User # fields = ["mobile","sms_code","id","token"] fields = ["mobile", "id", "token", "password", "password2", "username", "sms_code"] extra_kwargs = { "id": {"read_only": True}, "username": {"read_only": True}, "password": {"write_only": True}, "mobile": {"write_only": True} } def validate_mobile(self, mobile): # 驗證格式 result = re.match('^1[3-9]\d{9}$', mobile) if not result: raise serializers.ValidationError("手機號碼格式有誤!") # 驗證惟一性 try: user = User.objects.get(mobile=mobile) if user: raise serializers.ValidationError("當前手機號碼已經被註冊!") except User.DoesNotExist: pass return mobile def validate(self, attrs): # 判斷密碼長度 password = attrs.get("password") if not re.match('^.{6,16}$', password): raise serializers.ValidationError("密碼長度必須在6-16位之間!") # 判斷密碼和確認密碼是否一致 password2 = attrs.get("password2") if password != password2: raise serializers.ValidationError("密碼和確認密碼不一致!") # 驗證短信驗證碼 mobile = attrs.get("mobile") redis = get_redis_connection("sms_code") try: real_sms_code = redis.get("%s_sms_code" % mobile).decode() except: raise serializers.ValidationError("驗證碼已存在,或已通過期") if real_sms_code != attrs.get("sms_code"): raise serializers.ValidationError("驗證碼不存在,或錯誤") # 刪除本次使用的驗證碼 try: redis.delete("%s_sms_code" % mobile) except: pass return attrs def create(self, validated_data): """保存用戶""" mobile = validated_data.get("mobile") password = validated_data.get("password") try: user = User.objects.create( mobile=mobile, username=mobile, password=password, ) # 密碼加密 user.set_password(user.password) user.save() except: raise serializers.ValidationError("註冊用戶失敗!") # 生成一個jwt from rest_framework_jwt.settings import api_settings jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) user.token = jwt_encode_handler(payload) return user
在用戶註冊或登陸成功後,在序列化器中返回用戶信息之後同時返回token便可。
Django REST framework JWT提供了登陸獲取token的視圖,能夠直接使用
from rest_framework_jwt.views import obtain_jwt_token urlpatterns = [ path(r'authorizations/', obtain_jwt_token, name='authorizations'), ]
在主路由中,引入當前子應用的路由文件
urlpatterns = [ ... path('users/', include("users.urls")), # include 的值必須是 模塊名.urls 格式,字符串中間只能出現一個圓點 ]
在登錄組件中找到登錄按鈕,綁定點擊事件
<button class="login_btn" @click="loginhander">登陸</button>
在methods中請求後端
export default { name: 'Login', data(){ return { login_type: 0, remember:false, // 記住密碼 username:"", password:"", } }, methods:{ // 登陸 loginhander(){ this.$axios.post("http://127.0.0.1:8000/users/authorizations/",{"username":this.username,"password":this.password}).then(response=>{ console.log(response.data) }).catch(error=>{ console.log(error) }) } }, };
瀏覽器的本地存儲提供了sessionStorage 和 localStorage 兩種:
sessionStorage 瀏覽器關閉即失效
localStorage 長期有效
使用方法
sessionStorage.變量名 = 變量值 // 保存數據 sessionStorage.變量名 // 讀取數據 sessionStorage.clear() // 清除全部sessionStorage保存的數據 localStorage.變量名 = 變量值 // 保存數據 localStorage.變量名 // 讀取數據 localStorage.clear() // 清除全部localStorage保存的數據
登錄組件代碼Login.vue
.... loginhander() { // 判斷用戶是否已經經過了極驗驗證 if (!this.is_geek) { return false; } this.$axios.post(this.$settings.Host + "/users/login/", { username: this.username, password: this.password, }).then(response => { let data = response.data; // 根據用戶是否勾選了記住密碼來保存用戶認證信息 if (this.remember) { // 記住密碼 localStorage.token = data.token; localStorage.user_id = data.id; localStorage.user_name = data.username; } else { // 不須要記住密碼 sessionStorage.token = data.token; sessionStorage.user_id = data.id; sessionStorage.user_name = data.username; } // 登陸成功之後,跳轉會上一個頁面 this.$router.push('/'); }).catch(error => { console.log(error.response) }) }, ....
默認的返回值僅有token,咱們還需在返回值中增長username和id,方便在客戶端頁面中顯示當前登錄用戶
經過修改該視圖的返回值能夠完成咱們的需求。
def jwt_response_payload_handler(token, user=None, request=None): """ 自定義jwt認證成功返回數據 """ return { 'token': token, 'id': user.id, 'username': user.username }
修改settings/dev.py配置文件
# JWT JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), 'JWT_RESPONSE_PAYLOAD_HANDLER': 'users.utils.jwt_response_payload_handler', }
登錄組件代碼Login.vue
// 使用瀏覽器本地存儲保存token if (this.remember) { // 記住登陸 sessionStorage.clear(); localStorage.token = response.data.token; localStorage.id = response.data.id; localStorage.username = response.data.username; } else { // 未記住登陸 localStorage.clear(); sessionStorage.token = response.data.token; sessionStorage.id = response.data.id; sessionStorage.username = response.data.username; }
JWT擴展的登陸視圖,在收到用戶名與密碼時,也是調用Django的認證系統中提供的authenticate()來檢查用戶名與密碼是否正確。
咱們能夠經過修改Django認證系統的認證後端(主要是authenticate方法)來支持登陸帳號既能夠是用戶名也能夠是手機號。
修改Django認證系統的認證後端須要繼承django.contrib.auth.backends.ModelBackend,並重寫authenticate方法。
authenticate(self, request, username=None, password=None, **kwargs)
方法的參數說明:
request 本次認證的請求對象
username 本次認證提供的用戶帳號
password 本次認證提供的密碼
咱們想要讓用戶既能夠以用戶名登陸,也能夠以手機號登陸,那麼對於authenticate方法而言,username參數即表示用戶名或者手機號。
重寫authenticate方法的思路:
根據username參數查找用戶User對象,username參數多是用戶名,也多是手機號
若查找到User對象,調用User對象的check_password方法檢查密碼是否正確
在users/utils.py中編寫:
def get_user_by_account(account): """ 根據賬號獲取user對象 :param account: 帳號,能夠是用戶名,也能夠是手機號 :return: User對象 或者 None """ try: if re.match('^1[3-9]\d{9}$', account): # 賬號爲手機號 user = User.objects.get(mobile=account) else: # 賬號爲用戶名 user = User.objects.get(username=account) except User.DoesNotExist: return None else: return user import re from .models import User from django.contrib.auth.backends import ModelBackend class UsernameMobileAuthBackend(ModelBackend): """ 自定義用戶名或手機號認證 """ def authenticate(self, request, username=None, password=None, **kwargs): user = get_user_by_account(username) if user is not None and user.check_password(password): return user
在配置文件dev.py中告知Django使用咱們自定義的認證後端
AUTHENTICATION_BACKENDS = [ 'users.utils.UsernameMobileAuthBackend', ]
<template> <div class="home"> <Header :is_login="is_login" @logout="logout" :current_page="current_page"/> <Banner/> <Footer/> </div> </template> <script> import Header from "./common/Header" import Banner from "./common/Banner" import Footer from "./common/Footer" export default{ name:"Home", data(){ return { id: sessionStorage.id || localStorage.id, username: sessionStorage.username || localStorage.username, token: sessionStorage.token || localStorage.token, current_page:0, is_login:false, }; }, components:{ Header, Banner, Footer, }, mounted(){ if(this.id && this.token){ this.is_login=true; } }, methods:{ logout(){ this.is_login=false; } } } </script>
Header子組件顯示登錄狀態
Header.vue
<script> export default { name:"Header", props:["current_page","is_login"], data(){ return { nav_list: [], } }, created: function(){ // 獲取輪播圖 this.$axios.get("http://api.luffycity.cn:8000/home/nav/").then(res => { this.nav_list = res.data }).catch(error => { console.log(error); }); }, methods:{ logout(){ localStorage.clear(); sessionStorage.clear(); this.$emit("logout",{"is_login":false}) } } } </script>
官網: https://www.geetest.com/first_page/
接下來,就能夠根據官方文檔,把驗證碼集成到項目中了
文檔地址:
下載和安裝驗證碼模塊包。
git clone https://github.com/GeeTeam/gt3-python-sdk.git
安裝依賴模塊
pip install requests
把驗證碼模塊放置在utils中
並在users子應用下建立驗證碼視圖類,並提供驗證碼和校驗驗證碼的視圖方法。
from luffy.utils.geetest import GeetestLib from rest_framework.views import APIView from rest_framework.response import Response # Create your views here. class VerifyCode(APIView): def get(self,request): user_id = 'test' pc_geetest_id = "a9feefab99c8d4bbcf0d9e3021048312" pc_geetest_key = "aa467ab83be3c44929bc7da76eb88028" gt = GeetestLib(pc_geetest_id, pc_geetest_key) status = gt.pre_process(user_id, JSON_FORMAT=0, ip_address="127.0.0.1") if not status: status = 2 request.session[gt.GT_STATUS_SESSION_KEY] = status request.session["user_id"] = user_id response_str = gt.get_response_str() print( response_str ) return Response(response_str) def post(self,request): pc_geetest_id = "a9feefab99c8d4bbcf0d9e3021048312" pc_geetest_key = "aa467ab83be3c44929bc7da76eb88028" gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.data.get(gt.FN_CHALLENGE, "") validate = request.data.get(gt.FN_VALIDATE, "") seccode = request.data.get(gt.FN_SECCODE, "") status = request.session[gt.GT_STATUS_SESSION_KEY] user_id = request.session["user_id"] if status == 1: result = gt.success_validate(challenge, validate, seccode, user_id, JSON_FORMAT=0) else: result = gt.failback_validate(challenge, validate, seccode) request.session["user_id"] = user_id return Response({{"message":result}})
路由註冊:
path(r'code/', views.VerifyCode.as_view()),
// 導入gt極驗 import '../static/globals/gt.js'
<template> <div class="login box"> <img src="https://www.luffycity.com/static/img/Loginbg.3377d0c.jpg" alt=""> <div class="login"> <div class="login-title"> <img src="https://www.luffycity.com/static/img/Logotitle.1ba5466.png" alt=""> <p>幫助有志向的年輕人經過努力學習得到體面的工做和生活!</p> </div> <div class="login_box"> <div class="title"> <span @click="login_type=0">密碼登陸</span> <span @click="login_type=1">短信登陸</span> </div> <div class="inp" v-if="login_type==0"> <input v-model = "username" type="text" placeholder="用戶名 / 手機號碼" class="user"> <input v-model = "password" type="password" name="" class="pwd" placeholder="密碼"> <div id="geetest"></div> <div class="rember"> <p> <input type="checkbox" class="no" name="a"/> <span>記住密碼</span> </p> <p>忘記密碼</p> </div> <button class="login_btn" @click="loginhander">登陸</button> <p class="go_login" >沒有帳號 <span>當即註冊</span></p> </div> <div class="inp" v-show="login_type==1"> <input v-model = "username" type="text" placeholder="手機號碼" class="user"> <input v-model = "password" type="text" class="pwd" placeholder="短信驗證碼"> <button id="get_code">獲取驗證碼</button> <button class="login_btn">登陸</button> <p class="go_login" >沒有帳號 <span>當即註冊</span></p> </div> </div> </div> </div> </template> <script> export default { name: 'Login', data(){ return { login_type: 0, username:"", password:"", validateResult:false, } }, created(){ this.$axios.get("http://127.0.0.1:8000/users/code",{responseType: 'json',}).then(response=>{ let data = response.data; console.log(data); var _this = this; //請檢測data的數據結構, 保證data.gt, data.challenge, data.success有值 initGeetest({ // 如下配置參數來自服務端 SDK gt: data.gt, challenge: data.challenge, offline: !data.success, new_captcha: true, product: 'popup', width:'100%' }, (captchaObj)=>{ var result = captchaObj.getValidate(); // 這裏能夠調用驗證明例 captchaObj 的實例方法 captchaObj.appendTo("#geetest"); //將驗證按鈕插入到宿主頁面中captchaBox元素內 captchaObj.onReady(()=>{ //your code }).onSuccess(()=>{ var result = captchaObj.getValidate(); this.validateResult = result; }).onError(()=>{ _this.validateResult=false; }) }) }) }, methods:{ // 登陸 loginhander(){ // 提交數據前判斷用戶是否經過了驗證碼校驗 if(!this.validateResult){ return false; } this.$axios.post("http://127.0.0.1:8000/users/authorizations/",{"username":this.username,"password":this.password}).then(response=>{ // 使用瀏覽器本地存儲保存token if (this.remember) { // 記住登陸 sessionStorage.clear(); localStorage.token = response.data.token; localStorage.id = response.data.id; localStorage.username = response.data.username; } else { // 未記住登陸 localStorage.clear(); sessionStorage.token = response.data.token; sessionStorage.id = response.data.id; sessionStorage.username = response.data.username; } this.$router.push("/") }).catch(error=>{ console.log(error) }) } }, }; </script>
註冊頁面Register,主要是經過登陸頁面進行改爲而成.
<template> <div class="box"> <img src="https://www.luffycity.com/static/img/Loginbg.3377d0c.jpg" alt=""> <div class="register"> <div class="register_box"> <div class="register-title">註冊路飛學城</div> <div class="inp"> <input v-model = "mobile" type="text" placeholder="手機號碼" class="user"> <div id="geetest"></div> <input v-model = "sms" type="text" placeholder="輸入驗證碼" class="user"> <button class="register_btn" >註冊</button> <p class="go_login" >已有帳號 <router-link to="/login">直接登陸</router-link></p> </div> </div> </div> </div> </template> <script> export default { name: 'Register', data(){ return { sms:"", mobile:"", validateResult:false, } }, created(){ }, methods:{}, }; </script> <style scoped> .box{ width: 100%; height: 100%; position: relative; overflow: hidden; } .box img{ width: 100%; min-height: 100%; } .box .register { position: absolute; width: 500px; height: 400px; top: 0; left: 0; margin: auto; right: 0; bottom: 0; top: -338px; } .register .register-title{ width: 100%; font-size: 24px; text-align: center; padding-top: 30px; padding-bottom: 30px; color: #4a4a4a; letter-spacing: .39px; } .register-title img{ width: 190px; height: auto; } .register-title p{ font-family: PingFangSC-Regular; font-size: 18px; color: #fff; letter-spacing: .29px; padding-top: 10px; padding-bottom: 50px; } .register_box{ width: 400px; height: auto; background: #fff; box-shadow: 0 2px 4px 0 rgba(0,0,0,.5); border-radius: 4px; margin: 0 auto; padding-bottom: 40px; } .register_box .title{ font-size: 20px; color: #9b9b9b; letter-spacing: .32px; border-bottom: 1px solid #e6e6e6; display: flex; justify-content: space-around; padding: 50px 60px 0 60px; margin-bottom: 20px; cursor: pointer; } .register_box .title span:nth-of-type(1){ color: #4a4a4a; border-bottom: 2px solid #84cc39; } .inp{ width: 350px; margin: 0 auto; } .inp input{ border: 0; outline: 0; width: 100%; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp input.user{ margin-bottom: 16px; } .inp .rember{ display: flex; justify-content: space-between; align-items: center; position: relative; margin-top: 10px; } .inp .rember p:first-of-type{ font-size: 12px; color: #4a4a4a; letter-spacing: .19px; margin-left: 22px; display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; /*position: relative;*/ } .inp .rember p:nth-of-type(2){ font-size: 14px; color: #9b9b9b; letter-spacing: .19px; cursor: pointer; } .inp .rember input{ outline: 0; width: 30px; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp .rember p span{ display: inline-block; font-size: 12px; width: 100px; /*position: absolute;*/ /*left: 20px;*/ } #geetest{ margin-top: 20px; } .register_btn{ width: 100%; height: 45px; background: #84cc39; border-radius: 5px; font-size: 16px; color: #fff; letter-spacing: .26px; margin-top: 30px; } .inp .go_login{ text-align: center; font-size: 14px; color: #9b9b9b; letter-spacing: .26px; padding-top: 20px; } .inp .go_login span{ color: #84cc39; cursor: pointer; } </style>
前端註冊路由:
import Register from "../components/Register" // 配置路由列表 export default new Router({ mode:"history", routes:[ // 路由列表 ... { name:"Register", path: "/register", component:Register, } ] })
接下來,咱們把註冊過程當中一些註冊信息(例如:短信驗證碼)和session緩存到redis數據庫中。
pip install django-redis
在dev.py配置中添加一下代碼:
# 設置redis緩存 CACHES = { # 默認緩存 "default": { "BACKEND": "django_redis.cache.RedisCache", # 項目上線時,須要調整這裏的路徑 "LOCATION": "redis://127.0.0.1:6379/0", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, # 提供給xadmin或者admin的session存儲 "session": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, # 提供存儲短信驗證碼 "sms_code":{ "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/2", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } } # 設置xadmin用戶登陸時,登陸信息session保存到redis SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "session"
django-redis提供了get_redis_connection的方法,經過調用get_redis_connection方法傳遞redis的配置名稱可獲取到redis的鏈接對象,經過redis鏈接對象能夠執行redis命令
https://redis-py.readthedocs.io/en/latest/
在登陸後的平臺上面獲取一下信息:
ACCOUNT SID:8aaf0708697b6beb01699f4442911776
AUTH TOKEN : b4dea244f43a4e0f90e557f0a99c70fa
AppID(默認):8aaf0708697b6beb01699f4442e3177c
Rest URL(生產): app.cloopen.com:8883 [項目上線時使用真實短信發送服務器]
Rest URL(開發): sandboxapp.cloopen.com:8883 [項目開發時使用沙箱短信發送服務器]
在開發過程當中,爲了節約發送短信的成本,能夠把本身的或者同事的手機加入到測試號碼中.
from .yuntongxun.sms import CCP class SMSCodeAPIView(APIView): """ 短信驗證碼 """ def get(self, request, mobile): """ 短信驗證碼 """ # 生成短信驗證碼 sms_code = "%06d" % random.randint(0, 999999) # 保存短信驗證碼與發送記錄 redis_conn = get_redis_connection('verify_codes') # 使用redis提供的管道操做能夠一次性執行多條redis命令 pl = redis_conn.pipeline() pl.multi() pl.setex("sms_%s" % mobile, 300, sms_code) # 設置短信有效期爲300s pl.setex("sms_time_%s" % mobile, 60, 1) # 設置發送短信間隔爲60s pl.execute() # 發送短信驗證碼 ccp = CCP() ccp.send_template_sms(mobile, [code, "5"], 1) return Response({"message": "OK"}, status.HTTP_200_OK)
建立序列化器對象[暫時不涉及到手機驗證碼功能
from rest_framework import serializers from .models import User import re class UserModelSerializer(serializers.ModelSerializer): """用戶信息序列化器""" sms_code = serializers.CharField(label='手機驗證碼', required=True, allow_null=False, allow_blank=False, write_only=True) password2 = serializers.CharField(label='確認密碼', required=True, allow_null=False, allow_blank=False, write_only=True) class Meta: model=User fields = ('sms_code', 'mobile', 'password','password2') extra_kwargs={ "password":{ "write_only":True } } def validate_mobile(self, value): """驗證手機號""" if not re.match(r'^1[345789]\d{9}$', value): raise serializers.ValidationError('手機號格式錯誤') # 驗證手機號是否已經被註冊了 # try: # user = User.objects.get(mobile=value) # except: # user = None # # if user: # raise serializers.ValidationError('當前手機號已經被註冊') # 上面驗證手機號是否存在的代碼[優化版] try: User.objects.get(mobile=value) # 若是有獲取到用戶信息,則下面的代碼不會被執行,若是沒有獲取到用戶信息,則表示手機號沒有註冊過,能夠直接pass raise serializers.ValidationError('當前手機號已經被註冊') except: pass return value def validate(self,data): # 驗證密碼 password = data.get("password") password2 = data.get("password2") if len(password)<6: raise serializers.ValidationError('密碼過短不安全~') if password !=password2: raise serializers.ValidationError('密碼和確認必須一致~') return data def create(self, validated_data): # 刪除一些不須要保存到數據庫裏面的字段 del validated_data['password2'] del validated_data['sms_code'] # 由於數據庫中默認用戶名是惟一的,因此咱們把用戶手機號碼做爲用戶名 validated_data["username"] = validated_data["mobile"] # 繼續調用ModelSerializer內置的添加數據功能 user = super().create(validated_data) # 針對密碼要加密 user.set_password(user.password) # 修改密碼等用於更新了密碼,因此須要保存 user.save() return user
用戶管理視圖代碼:
# users/views.py from rest_framework.generics import CreateAPIView from .models import User from .serializers import UserModelSerializer class UserAPIView(CreateAPIView): """用戶管理""" queryset = User.objects.all() serializer_class = UserModelSerializer
# 子應用路由 urls.py urlpatterns=[ ... path(r"user", views.UserAPIView.as_view()), ]
客戶端發送註冊信息和發送短信 (前端代碼)
<template> <div class="box"> <img src="https://www.luffycity.com/static/img/Loginbg.3377d0c.jpg" alt=""> <div class="register"> <div class="register_box"> <div class="register-title">註冊路飛學城</div> <div class="inp"> <!--<el-select v-model="region">--> <!--<el-option v-for="item in region_list" :label="item.nation_name+'('+item.nation_code+')'" :value="item.nation_code"></el-option>--> <!--</el-select>--> <input v-model = "mobile" type="text" placeholder="手機號碼" class="user"> <input v-model = "password" type="password" placeholder="密碼" class="user"> <input v-model = "password2" type="password" placeholder="確認密碼" class="user"> <div id="geetest"></div> <div class="sms"> <input v-model="sms_code" maxlength="16" type="text" placeholder="輸入驗證碼" class="user"> <span class="get_sms" @click="send_sms">{{get_sms_text}}</span> </div> <button class="register_btn" @click="registerHander">註冊</button> <p class="go_login" >已有帳號 <router-link to="/login">直接登陸</router-link></p> </div> </div> </div> </div> </template> <script> export default { name: 'Register', data(){ return { region:"+86", sms_code:"", password:"", password2:"", mobile:"", validateResult:false, get_sms_text:"獲取驗證碼", } }, created(){ // 頁面初始化的時候設置號碼的地區號 // this.region_list = this.$nation; // 顯示圖片驗證碼 this.$axios.get("http://127.0.0.1:8000/users/verify").then(response=>{ // 請求成功 let data = response.data; // 使用initGeetest接口 // 參數1:配置參數 // 參數2:回調,回調的第一個參數驗證碼對象,以後可使用它作appendTo之類的事件 console.log(response.data); data = JSON.parse(data); initGeetest({ gt: data.gt, challenge: data.challenge, width: "350px", product: "embed", // 產品形式,包括:float,embed,popup。注意只對PC版驗證碼有效 offline: !data.success // 表示用戶後臺檢測極驗服務器是否宕機,通常不須要關注 // 更多配置參數請參見:http://www.geetest.com/install/sections/idx-client-sdk.html#config }, this.handlerPopup); }).catch(error=>{ console.log(error) }) }, methods:{ send_sms(){ let reg = /1[1-9]{2}\d{8}/; if( !reg.test(this.mobile) ){ return false; } // 若是get_sms_text 不是文本,而是數字,則表示當前手機號碼還在60秒的發送短信間隔內 if(this.get_sms_text != "獲取驗證碼"){ return false; } // 發送短信 let _this = this; this.$axios.get("http://127.0.0.1:8000/users/sms?mobile="+this.mobile).then(response=>{ console.log(response); // 顯示發送短信之後的文本倒計時 let time = 60; let timer = setInterval(()=>{ --time; if(time <=1){ // 若是倒計時爲0,則關閉當前定時器 _this.get_sms_text = "獲取驗證碼"; clearInterval(timer); }else{ _this.get_sms_text = time; } },1000) }).catch(error=>{ console.log(error); }) }, registerHander(){ // 註冊信息提交 // 提交數據前判斷用戶是否經過了驗證碼校驗 if(!this.validateResult){ alert("驗證碼驗證有誤"); return false; } this.$axios.post("http://127.0.0.1:8000/users/user",{ "mobile":this.mobile, "password":this.password, "password2":this.password2, "sms_code":this.sms_code, },{ responseType:"json", }). then(response=>{ // 請求成功,保存登錄狀態 localStorage.removeItem("token"); let data = response.data; sessionStorage.token = data.token; sessionStorage.id = data.id; sessionStorage.username = data.mobile; // 註冊成功之後默認表示已經登陸了,跳轉用戶中心的頁面 // this.$router.push("/user"); alert("註冊成功!"); }).catch(error=>{ console.log(error); }) }, handlerPopup(captchaObj){ // 驗證碼成功的回調 let _this = this; captchaObj.onSuccess(function () { var validate = captchaObj.getValidate(); _this.$axios.post("http://127.0.0.1:8000/users/verify",{ geetest_challenge: validate.geetest_challenge, geetest_validate: validate.geetest_validate, geetest_seccode: validate.geetest_seccode },{ responseType:"json", }).then(response=>{ // 請求成功 console.log(response.data); if(response.data.status == "success") { _this.validateResult = true; // 獲取驗證結果 } }).catch(error=>{ // 請求失敗 console.log(error) }) }); // 將驗證碼加到id爲captcha的元素裏 captchaObj.appendTo("#geetest"); } }, }; </script> <style scoped> .box{ width: 100%; height: 100%; position: relative; overflow: hidden; } .el-select{ width:100%; margin-bottom: 15px; } .box img{ width: 100%; min-height: 100%; } .box .register { position: absolute; width: 500px; height: 400px; top: 0; left: 0; margin: auto; right: 0; bottom: 0; top: -338px; } .register .register-title{ width: 100%; font-size: 24px; text-align: center; padding-top: 30px; padding-bottom: 30px; color: #4a4a4a; letter-spacing: .39px; } .register-title img{ width: 190px; height: auto; } .register-title p{ font-family: PingFangSC-Regular; font-size: 18px; color: #fff; letter-spacing: .29px; padding-top: 10px; padding-bottom: 50px; } .sms{ margin-top: 15px; position: relative; } .sms .get_sms{ position: absolute; right: 15px; top: 14px; font-size: 14px; color: #ffc210; cursor: pointer; border-left: 1px solid #979797; padding-left: 20px; } .register_box{ width: 400px; height: auto; background: #fff; box-shadow: 0 2px 4px 0 rgba(0,0,0,.5); border-radius: 4px; margin: 0 auto; padding-bottom: 40px; } .register_box .title{ font-size: 20px; color: #9b9b9b; letter-spacing: .32px; border-bottom: 1px solid #e6e6e6; display: flex; justify-content: space-around; padding: 50px 60px 0 60px; margin-bottom: 20px; cursor: pointer; } .register_box .title span:nth-of-type(1){ color: #4a4a4a; border-bottom: 2px solid #84cc39; } .inp{ width: 350px; margin: 0 auto; } .inp input{ border: 0; outline: 0; width: 100%; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp input.user{ margin-bottom: 16px; } .inp .rember{ display: flex; justify-content: space-between; align-items: center; position: relative; margin-top: 10px; } .inp .rember p:first-of-type{ font-size: 12px; color: #4a4a4a; letter-spacing: .19px; margin-left: 22px; display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; /*position: relative;*/ } .inp .rember p:nth-of-type(2){ font-size: 14px; color: #9b9b9b; letter-spacing: .19px; cursor: pointer; } .inp .rember input{ outline: 0; width: 30px; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp .rember p span{ display: inline-block; font-size: 12px; width: 100px; /*position: absolute;*/ /*left: 20px;*/ } #geetest{ margin-top: 20px; } .register_btn{ width: 100%; height: 45px; background: #84cc39; border-radius: 5px; font-size: 16px; color: #fff; letter-spacing: .26px; margin-top: 30px; } .inp .go_login{ text-align: center; font-size: 14px; color: #9b9b9b; letter-spacing: .26px; padding-top: 20px; } .inp .go_login span{ color: #84cc39; cursor: pointer; } </style>
Header.vue組件代碼:
<script> export default { name: "Header", data(){ return { // 設置一個登陸標識,表示是否登陸 token: sessionStorage.token || localStorage.token, user_name: sessionStorage.user_name || localStorage.user_name, user_id: sessionStorage.user_id || localStorage.user_id, nav_list:[], }; }, 。。。 } </script>
實現的思路:頭部子組件是經過token值進行判斷登陸狀態,因此當用戶點擊"退出登陸",則須要移出token的值,並使用elementUI裏面的彈窗組件進行提示。
在登陸認證中接入極驗驗證
官網: https://www.geetest.com/first_page/
註冊登陸之後,即進入登陸後臺,選擇行爲驗證。
<template> <div class="login-box"> <img src="../../static/img/login1.jpg" alt=""> <div class="login"> <div class="login-title"> <img src="../../static/img/Logotitle.png" alt=""> <p>幫助有志向的年輕人經過努力學習得到體面的工做和生活!</p> </div> <div class="login_box"> <div class="title"> <span @click="login_type=0">密碼登陸</span> <span @click="login_type=1">短信登陸</span> </div> <div class="inp" v-show="login_type===0"> <input v-model="username" type="text" placeholder="用戶名 / 手機號碼" class="user"> <input v-model="password" type="password" name="" class="pwd" placeholder="密碼"> <div id="geetest1"></div> <div class="rember"> <p> <input type="checkbox" class="no" v-model="remember"/> <span>記住密碼</span> </p> <p>忘記密碼</p> </div> <button class="login_btn" @click="loginhander">登陸</button> <p class="go_login">沒有帳號 <router-link to="/register">當即註冊</router-link> </p> </div> <div class="inp" v-show="login_type===1"> <input v-model="username" type="text" placeholder="手機號碼" class="user"> <input v-model="sms" type="text" class="pwd" placeholder="短信驗證碼"> <button id="get_code" @click="smsHandle">{{sms_text}}</button> <button class="login_btn" @click="login2Hander">登陸</button> <p class="go_login">沒有帳號 <router-link to="/register">當即註冊</router-link> </p> </div> </div> </div> </div> </template> <script> export default { name: 'Login', data() { return { login_type: 0, username: "", sms: "", is_send: false, // 是否已經發送短信的狀態 password: "", remember: "", is_geek: false, send_interval: 60, // 發送短信的間隔 sms_text: "點擊發送短信", // 發送短信的提示 } }, created() { // 請求後端獲取生成驗證碼的流水號 this.$axios.get(this.$settings.Host + "/users/captcha/", { responseType: 'json', // 但願返回json數據 }).then(response => { let data = response.data; // 驗證初始化配置 initGeetest({ gt: data.gt, challenge: data.challenge, product: "popup", // 產品形式,包括:float,embed,popup。注意只對PC版驗證碼有效 offline: !data.success }, this.handlerPopup) }).catch(error => { console.log(error.response); }); }, methods: { //發送短信 smsHandle() { // 判斷是否填寫了手機 if (!/^\d{11}$/.test(this.username)) { this.$alert('手機號碼格式有誤!', '警告'); return false; } // 判斷是否在60s內有發送太短信,若是有則,不能點擊發送 if (this.is_send) { this.$alert('60s內不能頻繁發送短信!', '警告'); return false; } // 判斷是否在60s內有發送太短信,若是有則,不能點擊發送 if (this.is_send) { this.$alert('60s內不能頻繁發送短信!', '警告'); return false; } let _this = this; _this.$axios.get(_this.$settings.Host + `/users/sms/${_this.username}/`).then(response => { let data = response.data; console.log(data); if (data.result === '-1') { _this.$alert("發送短信失敗!", "錯誤"); } else { _this.is_send = true; _this.$alert("發送短信成功了!", "成功", { callback() { let num = _this.send_interval; let timer = setInterval(() => { if (num < 1) { clearInterval(timer); _this.sms_text = "點擊發送短信"; _this.is_send = false; } else { num--; _this.sms_text = num + "後可繼續點擊發送"; } }, 1000) } }); } }).catch(error => { console.log(error.response) }) }, //短信驗證碼登陸 login2Hander(){ // 驗證手機號碼 if (!/^\d{11}$/.test(this.username)) { this.$alert('手機號碼格式有誤!', '警告'); return false; } this.$axios.post(this.$settings.Host+"/users/login/",{ username:this.username, password:this.sms, }) }, // 用戶登陸 loginhander() { // 判斷用戶是否已經經過了極驗驗證 if (!this.is_geek) { return false; } this.$axios.post(this.$settings.Host + "/users/login/", { username: this.username, password: this.password, }).then(response => { let data = response.data; // 根據用戶是否勾選了記住密碼來保存用戶認證信息 if (this.remember) { // 記住密碼 localStorage.token = data.token; localStorage.user_id = data.id; localStorage.user_name = data.username; } else { // 不須要記住密碼 sessionStorage.token = data.token; sessionStorage.user_id = data.id; sessionStorage.user_name = data.username; } // 登陸成功之後,跳轉會上一個頁面 this.$router.push('/'); }).catch(error => { console.log(error.response) }) }, // 驗證碼的成功驗證事件方法 handlerPopup(captchaObj) { // 把驗證碼添加到模板中制定的頁面 captchaObj.appendTo("#geetest1"); // 記錄vue對象 let _this = this; // 監聽用戶對於驗證碼的操做是否成功了 captchaObj.onSuccess(() => { var validate = captchaObj.getValidate(); _this.$axios.post(_this.$settings.Host + "/users/captcha/", { geetest_challenge: validate.geetest_challenge, geetest_validate: validate.geetest_validate, geetest_seccode: validate.geetest_seccode }).then(response => { // 在用戶成功添加數據之後,能夠容許點擊登陸按鈕 _this.is_geek = true; }).catch(error => { console.log(error.response) }) }); }, }, }; </script> <style scoped> .login-box { width: 100%; height: 100%; position: relative; overflow: hidden; margin-top: -80px; } .login-box img { width: 100%; min-height: 100%; } .login-box .login { position: absolute; width: 500px; height: 400px; left: 0; margin: auto; right: 0; bottom: 0; top: -220px; } .login .login-title { width: 100%; text-align: center; } .login-title img { width: 190px; height: auto; } .login-title p { font-size: 18px; color: #fff; letter-spacing: .29px; padding-top: 10px; padding-bottom: 50px; } .login_box { width: 400px; height: auto; background: #fff; box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5); border-radius: 4px; margin: 0 auto; padding-bottom: 40px; } .login_box .title { font-size: 20px; color: #9b9b9b; letter-spacing: .32px; border-bottom: 1px solid #e6e6e6; display: flex; justify-content: space-around; padding: 50px 60px 0 60px; margin-bottom: 20px; cursor: pointer; } .login_box .title span:nth-of-type(1) { color: #4a4a4a; border-bottom: 2px solid #84cc39; } .inp { width: 350px; margin: 0 auto; } .inp input { outline: 0; width: 100%; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp input.user { margin-bottom: 16px; } .inp .rember { display: flex; justify-content: space-between; align-items: center; position: relative; margin-top: 5px; } .inp .rember p:first-of-type { font-size: 12px; color: #4a4a4a; letter-spacing: .19px; margin-left: 22px; display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; /*position: relative;*/ } .inp .rember p:nth-of-type(2) { font-size: 14px; color: #9b9b9b; letter-spacing: .19px; cursor: pointer; } .inp .rember input { outline: 0; width: 30px; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp .rember p span { display: inline-block; font-size: 12px; width: 100px; /*position: absolute;*/ /*left: 20px;*/ } #geetest1 { margin-top: 20px; } .login_btn { width: 100%; height: 45px; background: #84cc39; border-radius: 5px; font-size: 16px; color: #fff; letter-spacing: .26px; margin-top: 30px; } .inp .go_login { text-align: center; font-size: 14px; color: #9b9b9b; letter-spacing: .26px; padding-top: 20px; } .inp .go_login span { color: #84cc39; cursor: pointer; } </style>
接下來,咱們把註冊過程當中一些註冊信息(例如:短信驗證碼)和session緩存到redis數據庫中。
安裝django-redis。
關於django-redis 的使用,說明文檔可見http://django-redis-chs.readthedocs.io/zh_CN/latest/
django-redis提供了get_redis_connection的方法,經過調用get_redis_connection方法傳遞redis的配置名稱可獲取到redis的鏈接對象,經過redis鏈接對象能夠執行redis命令
https://redis-py.readthedocs.io/en/latest/
在登陸後的平臺上面獲取一下信息:
註冊的前端
1 <template> 2 <div class="box"> 3 <img src="../../static/img/login1.jpg" alt=""> 4 <div class="register"> 5 <div class="register_box"> 6 <div class="register-title">註冊路飛學城</div> 7 <div class="inp"> 8 <input v-model="mobile" type="text" placeholder="手機號碼" class="user"> 9 <input v-model="password" type="password" placeholder="登陸密碼" class="user"> 10 <input v-model="password2" type="password" placeholder="確認密碼" class="user"> 11 <div id="geetest"></div> 12 <div class="sms-box"> 13 <input v-model="sms" type="text" placeholder="輸入驗證碼" class="user"> 14 <div class="sms-btn" @click="smsHandle">{{sms_text}}</div> 15 </div> 16 <button class="register_btn" @click="registerHander">註冊</button> 17 <p class="go_login">已有帳號 18 <router-link to="/login">直接登陸</router-link> 19 </p> 20 </div> 21 </div> 22 </div> 23 </div> 24 </template> 25 26 <script> 27 export default { 28 name: 'Register', 29 data() { 30 return { 31 sms: "", 32 mobile: "", 33 password: "", 34 password2: "", 35 validateResult: false, 36 is_send: false, // 是否已經發送短信的狀態 37 send_interval: 60, // 發送短信的間隔 38 sms_text: "點擊發送短信", // 發送短信的提示 39 } 40 }, 41 methods: { 42 // 發送短信 43 smsHandle() { 44 // 判斷是否填寫了手機 45 if (!/^\d{11}$/.test(this.mobile)) { 46 this.$alert('手機號碼格式有誤!', '警告'); 47 return false; 48 } 49 50 // 判斷是否在60s內有發送太短信,若是有則,不能點擊發送 51 if (this.is_send) { 52 this.$alert('60s內不能頻繁發送短信!', '警告'); 53 return false; 54 } 55 56 57 let _this = this; 58 59 _this.$axios.get(_this.$settings.Host + `/users/sms/${_this.mobile}/`).then(response => { 60 let data = response.data; 61 console.log(data); 62 if (data.result === '-1') { 63 _this.$alert("發送短信失敗!", "錯誤"); 64 } else { 65 _this.is_send = true; 66 _this.$alert("發送短信成功了!", "成功", { 67 callback() { 68 let num = _this.send_interval; 69 let timer = setInterval(() => { 70 if (num < 1) { 71 clearInterval(timer); 72 _this.sms_text = "點擊發送短信"; 73 _this.is_send = false; 74 } else { 75 num--; 76 _this.sms_text = num + "後可繼續點擊發送"; 77 } 78 }, 1000) 79 } 80 }); 81 } 82 }).catch(error => { 83 console.log(error.response) 84 }) 85 86 }, 87 // 提交註冊信息 88 registerHander() { 89 // 驗證手機號碼 90 if (!/^\d{11}$/.test(this.mobile)) { 91 this.$alert('手機號碼格式有誤!', '警告'); 92 return false; 93 } 94 95 // 密碼長度 96 if (!/^.{6,16}$/.test(this.password)) { 97 this.$alert('密碼長度必須在6-16位字符之間!', '警告'); 98 return false; 99 } 100 101 // 密碼和確認密碼 102 if (this.password !== this.password2) { 103 this.$alert('確認密碼必須和密碼保持一致!', '警告'); 104 return false; 105 } 106 107 // 發送請求註冊用戶 108 this.$axios.post(this.$settings.Host + "/users/register/", { 109 mobile: this.mobile, 110 password: this.password, 111 password2: this.password2, 112 sms_code: this.sms, 113 }).then(response => { 114 115 let _this = this; 116 117 _this.$alert("註冊成功!", { 118 callback() { 119 let data = response.data; 120 console.log(data); 121 // 保存登陸狀態 122 sessionStorage.token = data.token; 123 sessionStorage.user_id = data.id; 124 sessionStorage.user_name = data.username; 125 126 // 跳轉到首頁 127 _this.$router.push("/"); 128 } 129 }); 130 131 }).catch(error => { 132 console.log(error.response) 133 }) 134 135 } 136 }, 137 138 }; 139 </script> 140 141 <style scoped> 142 .box { 143 width: 100%; 144 height: 100%; 145 position: relative; 146 overflow: hidden; 147 margin-top: -80px; 148 } 149 150 .box img { 151 width: 100%; 152 min-height: 100%; 153 } 154 155 .box .register { 156 position: absolute; 157 width: 500px; 158 height: 400px; 159 top: 0; 160 left: 0; 161 margin: auto; 162 right: 0; 163 bottom: 0; 164 top: -220px; 165 } 166 167 .register .register-title { 168 width: 100%; 169 font-size: 24px; 170 text-align: center; 171 padding-top: 30px; 172 padding-bottom: 30px; 173 color: #4a4a4a; 174 letter-spacing: .39px; 175 } 176 177 .register-title img { 178 width: 190px; 179 height: auto; 180 } 181 182 .register-title p { 183 font-family: PingFangSC-Regular; 184 font-size: 18px; 185 color: #fff; 186 letter-spacing: .29px; 187 padding-top: 10px; 188 padding-bottom: 50px; 189 } 190 191 .register_box { 192 width: 400px; 193 height: auto; 194 background: #fff; 195 box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5); 196 border-radius: 4px; 197 margin: 0 auto; 198 padding-bottom: 40px; 199 } 200 201 .register_box .title { 202 font-size: 20px; 203 color: #9b9b9b; 204 letter-spacing: .32px; 205 border-bottom: 1px solid #e6e6e6; 206 display: flex; 207 justify-content: space-around; 208 padding: 50px 60px 0 60px; 209 margin-bottom: 20px; 210 cursor: pointer; 211 } 212 213 .register_box .title span:nth-of-type(1) { 214 color: #4a4a4a; 215 border-bottom: 2px solid #84cc39; 216 } 217 218 .inp { 219 width: 350px; 220 margin: 0 auto; 221 } 222 223 .inp input { 224 border: 0; 225 outline: 0; 226 width: 100%; 227 height: 45px; 228 border-radius: 4px; 229 border: 1px solid #d9d9d9; 230 text-indent: 20px; 231 font-size: 14px; 232 background: #fff !important; 233 } 234 235 .inp input.user { 236 margin-bottom: 16px; 237 } 238 239 .inp .rember { 240 display: flex; 241 justify-content: space-between; 242 align-items: center; 243 position: relative; 244 margin-top: 10px; 245 } 246 247 .inp .rember p:first-of-type { 248 font-size: 12px; 249 color: #4a4a4a; 250 letter-spacing: .19px; 251 margin-left: 22px; 252 display: -ms-flexbox; 253 display: flex; 254 -ms-flex-align: center; 255 align-items: center; 256 /*position: relative;*/ 257 } 258 259 .inp .rember p:nth-of-type(2) { 260 font-size: 14px; 261 color: #9b9b9b; 262 letter-spacing: .19px; 263 cursor: pointer; 264 } 265 266 .inp .rember input { 267 outline: 0; 268 width: 30px; 269 height: 45px; 270 border-radius: 4px; 271 border: 1px solid #d9d9d9; 272 text-indent: 20px; 273 font-size: 14px; 274 background: #fff !important; 275 } 276 277 .inp .rember p span { 278 display: inline-block; 279 font-size: 12px; 280 width: 100px; 281 /*position: absolute;*/ 282 /*left: 20px;*/ 283 284 } 285 286 #geetest { 287 margin-top: 20px; 288 } 289 290 .register_btn { 291 width: 100%; 292 height: 45px; 293 background: #84cc39; 294 border-radius: 5px; 295 font-size: 16px; 296 color: #fff; 297 letter-spacing: .26px; 298 margin-top: 30px; 299 } 300 301 .inp .go_login { 302 text-align: center; 303 font-size: 14px; 304 color: #9b9b9b; 305 letter-spacing: .26px; 306 padding-top: 20px; 307 } 308 309 .inp .go_login span { 310 color: #84cc39; 311 cursor: pointer; 312 } 313 314 .sms-box { 315 position: relative; 316 } 317 318 .sms-btn { 319 font-size: 14px; 320 color: #ffc210; 321 letter-spacing: .26px; 322 position: absolute; 323 right: 16px; 324 top: 10px; 325 cursor: pointer; 326 overflow: hidden; 327 background: #fff; 328 border-left: 1px solid #484848; 329 padding-left: 16px; 330 padding-bottom: 4px; 331 } 332 </style>