一、Luffy主页中间课程搭建
<div class="course">
<el-row>
<el-col :span="6" v-for="(o, index) in 8" :key="o" class="course_detail">
<el-card :body-style="{ padding: '0px' }">
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1g0zd133mj20l20a875i.jpg"
class="image">
<div style="padding: 14px;">
<span>推荐课程</span>
<div class="bottom clearfix">
<time class="time">价格:999</time>
<el-button type="text" class="button">查看详情</el-button>
</div>
</div>
</el-card>
</el-col>
</el-row>
</div>
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1g112oiclj224l0u0jxl.jpg" alt="" width="100%" height="500px">
<style scoped>
.time {
font-size: 13px;
color: #999;
}
.bottom {
margin-top: 13px;
line-height: 12px;
}
.button {
padding: 0;
float: right;
}
.image {
width: 100%;
display: block;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both
}
.course_detail {
padding: 50px;
}
</style>
二、多方式登录接口编写
user/views.py
from rest_framework.viewsets import ViewSet, GenericViewSet, ViewSetMixin
from rest_framework.decorators import action
from .serializer import UserMulLoginSerializer
from utils.response import APIResponse
from .models import UserInfo
from rest_framework.exceptions import APIException
class UserView(ViewSet):
@action(methods=['POST'], detail=False)
def mul_login(self, request):
ser = UserMulLoginSerializer(data=request.data)
ser.is_valid(raise_exception=True) # 会执行:序列化类字段自己的校验规则,局部钩子,全局钩子 jwt 模块的登录就是这么写的
# 用户名密码校验通过了,在序列化类中--》签发token
token = ser.context.get('token')
username = ser.context.get('username')
icon = ser.context.get('icon') # icon是个对象 字符串
return APIResponse(token=token, username=username, icon=icon)
user/serializer.py
from rest_framework import serializers
from .models import UserInfo
import re
from django.contrib.auth import authenticate
from rest_framework.exceptions import ValidationError
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
# 这个序列类,只用来做登录校验,不做序列化,不做反序列化
class UserMulLoginSerializer(serializers.ModelSerializer):
username = serializers.CharField() # 重写,优先用现在的,就没有unique的限制了
class Meta:
model = UserInfo
fields = ['username', 'password']
def _get_user(self, attrs):
# attrs 是校验过后的数据:字段自己的规则
username = attrs.get('username')
password = attrs.get('password')
# username可能是用户名,邮箱,手机号---》使用正则判断
if re.match(r'^1[3-9][0-9]{9}$', username):
user = authenticate(mobile=username, password=password)
elif re.match(r'^.+@.+$', username):
user = authenticate(email=username, password=password)
else:
user = authenticate(username=username, password=password)
if user:
return user
else:
raise ValidationError('用户名或密码错误')
def _get_token(self, user):
try:
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return token
except Exception as e:
raise ValidationError(str(e))
def validate(self, attrs):
user = self._get_user(attrs)
token = self._get_token(user)
self.context['token'] = token
self.context['username'] = user.username
self.context['icon'] = 'http://127.0.0.1:8000/media/'+str(user.icon) # 这是个对象,可能会有问题
return attrs
user/urls.py
'''
path('api/v1/user/', include('user.urls')), # 总路由分发
'''
from . import views
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register('user', views.UserView, 'user') # 127.0.0.1:8080/api/v1/user/user/mul_login
urlpatterns = [
]
urlpatterns += router.urls
三、手机号是否存在接口
user/views.py
from rest_framework.viewsets import ViewSet, GenericViewSet, ViewSetMixin
from rest_framework.decorators import action
from .serializer import UserMulLoginSerializer
from utils.response import APIResponse
from .models import UserInfo
from rest_framework.exceptions import APIException
class UserView(ViewSet):
@action(methods=['POST'], detail=False)
def mul_login(self, request):
ser = UserMulLoginSerializer(data=request.data)
ser.is_valid(raise_exception=True) # 会执行:序列化类字段自己的校验规则,局部钩子,全局钩子 jwt 模块的登录就是这么写的
# 用户名密码校验通过了,在序列化类中--》签发token
token = ser.context.get('token')
username = ser.context.get('username')
icon = ser.context.get('icon') # icon是个对象 字符串
return APIResponse(token=token, username=username, icon=icon)
@action(methods=['GET'], detail=False)
def mobile(self, request):
try:
mobile = request.query_params.get('mobile')
UserInfo.objects.get(mobile=mobile)
return APIResponse(msg='手机号已存在!!!')
except Exception as e:
raise APIException('手机号不存在')
四、什么是API?什么是SDK?
API
API(接口)是什么?举个常见的例子,在京东上下单付款之后,商家选用顺丰发货,然后你就可以在京东上实时查看当前的物流信息。京东和顺丰作为两家独立的公司,为什么会在京东上实时看到顺丰的快递信息,这就要用到API,当查看自己的快递信息时,京东利用顺丰提供的API接口,可以实时调取信息呈现在自己的网站上。除此,你也可以在快递100上输入订单号查取到快递信息。只要有合作,或是有允许,别的公司都可以通过顺丰提供的API接口调取到快递信息。既然有多方调用,那提供一个统一的调用规范会方便很多。
借鉴地址:API是什么?https://zhuanlan.zhihu.com/p/30742302
SDK
SDK是Software Development Kit 的缩写,翻译过来——软件开发工具包。这是一个覆盖面相当广泛的名词,可以这么说:辅助开发某一类软件的相关文档、范例和工具的集合都可以叫做SDK。
SDK开发出来是为了减少程序员工作量的。
举例:
有公司开发出某种软件的某一功能,把它封装成SDK(比如数据分析SDK就是能够实现数据分析功能的SDK),出售给其他公司做开发用,其他公司如果想要给软件开发出某种功能,但又不想从头开始搞开发,直接付钱省事。
API和SDK的区别
总的来说,两者没有值得比较的区别,因为是具有关联性的两种东西。你可以把SDK想象成一个虚拟的程序包,在这个程序包中有一份做好的软件功能,这份程序包几乎是全封闭的,只有一个小小接口可以联通外界,这个接口就是API。比如——我们现在要在企业ERP系统中增加某个功能(比如自动备份、数据分析、云存储等),但又不想耗费大量时间、也没那么多研发亲自去做这个功能。这时我们可以选择使用这个“SDK”软件包,把ERP系统连接上API接口,就可以使用SDK软件包里的功能。
SDK借鉴于:https://www.zhihu.com/question/21691705/answer/770586138