luffy-(7)
admin
2024-04-07 11:20:50
0

内容概览

  • 登录注册模态框分析
  • 登录注册前端页面
  • 腾讯短信功能二次封装
  • 短信验证码接口
  • 短信登录接口

登录注册模态框分析

Login.vue

登录框直接显示在主页上,所以在components中写一格登录组件


Header.vue

登录|注册

登录注册前端页面

Header.vue


Login.vue


Register.vue


腾讯短信功能二次封装

封装v3版本

sdk:https://cloud.tencent.com/document/product/382/43196#

# 1. 下载模块:pip install tencentcloud-sdk-python# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.sms.v20210111 import sms_client, models# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfiletry:cred = credential.Credential("AKIDTXcdXG6u5C9xxxxxxxxxD5VwPp", "LZgaKxTOI0VowxxxxxxvDKDtLcfWCiqm")httpProfile = HttpProfile()httpProfile.reqMethod = "POST"  # post请求(默认为post请求)httpProfile.reqTimeout = 30  # 请求超时时间,单位为秒(默认60秒)httpProfile.endpoint = "sms.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)# 非必要步骤:# 实例化一个客户端配置对象,可以指定超时时间等配置clientProfile = ClientProfile()clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法clientProfile.language = "en-US"clientProfile.httpProfile = httpProfileclient = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)req = models.SendSmsRequest()req.SmsSdkAppId = "1400763090" # 腾讯短信创建app把app的id号复制过来https://console.cloud.tencent.com/smsv2/app-manage# 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名# 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看req.SignName = "XX公众号"# 模板 ID: 必须填写已审核通过的模板 ID# 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看req.TemplateId = "1603526"# 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空req.TemplateParamSet = ["8888",'100']# 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]# 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号req.PhoneNumberSet = ["8613711112222"]# 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回req.SessionContext = ""req.ExtendCode = ""req.SenderId = ""resp = client.SendSms(req)# 输出json格式的字符串回包print(resp.to_json_string(indent=2))except TencentCloudSDKException as err:print(err)

把发送短信封装成包

"""后期别的项目可能也需要发送短信;将发送短信的功能封装成包后,后续只需要调用包到项目中使用即可""""""
包的目录结构send_tx_sms  # 包名__init__.pysettings.py  # 配置文件sms.py  # 核心代码
"""
  • _init_.py
    from .sms import get_code,send_sms_by_phone
    
  • settiongs.py
    SECRET_ID = ''
    SECRET_KEY = ''
    APP_ID = ''
    SIGN_NAME=''
    TEMPLATE_ID=''
    
  • sms.py
    import randomfrom tencentcloud.common import credential
    from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
    # 导入对应产品模块的client models。
    from tencentcloud.sms.v20210111 import sms_client, models# 导入可选配置类
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from . import settings# 获取n位随机数组验证码的函数
    def get_code(num=4):code = ''for i in range(num):random_num = random.randint(0, 9)code += str(random_num)return code# 发送短信函数
    def send_sms_by_phone(mobile, code):try:cred = credential.Credential(settings.SECRET_ID, settings.SECRET_KEY)httpProfile = HttpProfile()httpProfile.reqMethod = "POST"  # post请求(默认为post请求)httpProfile.reqTimeout = 30  # 请求超时时间,单位为秒(默认60秒)httpProfile.endpoint = "sms.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)# 非必要步骤:# 实例化一个客户端配置对象,可以指定超时时间等配置clientProfile = ClientProfile()clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法clientProfile.language = "en-US"clientProfile.httpProfile = httpProfileclient = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)req = models.SendSmsRequest()req.SmsSdkAppId = settings.APP_ID  # 腾讯短信创建app把app的id号复制过来https://console.cloud.tencent.com/smsv2/app-manage# 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名# 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看req.SignName = settings.SIGN_NAME# 模板 ID: 必须填写已审核通过的模板 ID# 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看req.TemplateId = settings.TEMPLATE_ID# 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空req.TemplateParamSet = [code, '1']# 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]# 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号req.PhoneNumberSet = ["+86" + mobile, ]# 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回req.SessionContext = ""req.ExtendCode = ""req.SenderId = ""resp = client.SendSms(req)# 输出json格式的字符串回包# 字符串类型print(type(resp.to_json_string(indent=2)))return Trueexcept TencentCloudSDKException as err:return False
    

短信验证码接口

from libs.send_tx_sms import get_code, send_sms_by_phone
from django.core.cache import cacheclass UserView(ViewSet):@action(methods=['GET'], detail=False)def send_sms(self, request):mobile = request.query_params.get('mobile')if re.match(r'^1[3-9]\d{9}$', mobile):  # 通过正则校验手机号code = get_code()  # 获取随机验证码cache.set(f'sms_code_{mobile}', code)  # 保存验证码到内存中res = send_sms_by_phone(mobile, code)if res:return APIResponse(msg='发送短信成功')else:# raise APIException('发送短信失败')return APIResponse(msg='发送短信失败', code=101)else:return APIResponse(msg='手机号不合法', code=102)

短信登录接口

view.py

class UserView(ViewSet):def get_serializer(self, data):# 判断如果请求的action是:mul_login,返回UserMulLoginSerializer# 判断如果请求的action是:mobile_login,返回UserMobileLoginSerializerif self.action == 'mul_login':return UserMulLoginSerializer(data=data)else:return UserMobileLoginSerializer(data=data)def common_login(self, request):ser = self.get_serializer(data=request.data)ser.is_valid(raise_exception=True)token = ser.context.get('token')username = ser.context.get('username')icon = ser.context.get('icon')return APIResponse(token=token, username=username, icon=icon)@action(methods=['GET'], detail=False)def send_sms(self, request):mobile = request.query_params.get('mobile')if re.match(r'^1[3-9]\d{9}$', mobile):  # 通过正则校验手机号code = get_code()  # 获取随机验证码cache.set(f'sms_code_{mobile}', code)  # 保存验证码到内存中res = send_sms_by_phone(mobile, code)if res:return APIResponse(msg='发送短信成功')else:# raise APIException('发送短信失败')return APIResponse(msg='发送短信失败', code=101)else:return APIResponse(msg='手机号不合法', code=102)@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('手机号不存在')"""多用户登录与手机号登录内部代码只有序列化类不同,抽取成一个方法"""@action(methods=['POST'], detail=False)def mul_login(self, request):return self.common_login(request)@action(methods=['POST'], detail=False)def mobile_login(self, request):return self.common_login(request)

serializers.py

# 多个序列化类使用了相同的代码,抽取出一个父类
class ModelUserSerializer(serializers.ModelSerializer):def _get_token(self, user):try:payload = jwt_payload_handler(user)token = jwt_encode_handler(payload)return tokenexcept Exception as e:raise APIException(str(e))def validate(self, attrs):# 1  手机号和codeuser = self._get_user(attrs)# 2 签发tokentoken = self._get_token(user)# 3 把token放到序列化类对象中self.context['token'] = tokenself.context['username'] = user.usernameself.context['icon'] = 'http://127.0.0.1:8000/media/' + str(user.icon)return attrsclass UserMulLoginSerializer(ModelUserSerializer):username = serializers.CharField()class Meta:model = UserInfofields = ['username', 'password']def _get_user(self, attrs):# attrs是校验后的数据:通过了字段自己的校验和局部钩子才会执行到这里username = attrs.get('username')password = attrs.get('password')# username是手机号/邮箱/用户名其中之一,我们使用正则判断if re.match(r'^1[3-9]\d{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 userelse:raise ValidationError('用户名或密码错误')class UserMobileLoginSerializer(ModelUserSerializer):code = serializers.CharField()mobile = serializers.CharField()class Meta:model = UserInfofields = ['mobile', 'code']def _get_user(self, attrs):mobile = attrs.get('mobile')code = attrs.get('code')# 校验code是否正确old_code = cache.get('sms_code_%s' % mobile)cache.set('sms_code_%s' % mobile, '')  # 验证码用过了要清除if code == old_code:  # 万能验证码,在测试阶段,测试用的user = UserInfo.objects.filter(mobile=mobile).first()return userraise APIException('验证码错误')

相关内容

热门资讯

利润最高预计增长914%!光谷... 今年以来,光通信赛道持续景气。截至7月20日,光谷多家上市公司发布2026年半年度业绩快报显示,营收...
八年IPO长跑重启,南海农商行... 《星岛》见习记者 洪雨欣 深圳报道 等待八年,南海农商银行的A股上市之路迎新进展。 2026年6月...
公募基金2026年二季报全面解... 本篇看点 截至2026年二季度末,公募基金市场总规模约38.6万亿元,较上季度增长5.69%;基金数...
房地产市场出现积极变化 近年来,各地持续优化调整房地产政策,控增量、去库存、优供给,有序放宽购房限制,深化住房公积金制度改革...
【IPO追踪】年内港股最大IP... 近年来光通信企业在资本市场爆火,港股的剑桥科技(06166.HK)、海光芯正(01191.HK)等概...
收购2艘二手箱船,这家“新船东... 由厦门建发集团全资控股的集装箱航运企业Greta完成2艘支线集装箱船收购,持续扩张船队与航线布局。 ...
于东来深夜发文,称胖东来禁止员... 极目新闻记者 王柳钦 7月21日晚,胖东来创始人于东来在社交平台发文,分享胖东来保障幸福生活的制度,...
原创 关... 2026年7月以来,多组城市街景对比视频在各大社交平台持续发酵刷屏,不少网友实拍的城市老商业街、沿街...
万亿市值背后的资金洪流——A股... 7月21日,沪指重新站上3800点,科创50指数飙涨10.73%,AI概念股大幅回升,半导体、存储芯...
原创 别... 最近不少退休的粉丝私信问,手里攒了点养老钱,存银行定期跑不赢通胀,又怕炒股亏钱,该买点啥保值?今天就...
潍柴雷沃智慧农业三度递表港交所... 近日,潍柴雷沃智慧农业科技股份有限公司(以下简称“潍柴雷沃智慧农业”)向香港交易所递交主板上市申请,...
能源早报|中国石化氢能装备产业... 中国石化氢能装备产业集群建设正式启动 7月21日,据中国石化消息,石化机械氢能产业技术交流会暨数智氢...
港股收盘 | 三大指数集体走弱... 财联社7月22日讯(编辑胡家荣)港股三大指数震荡下行。截至收盘,恒生指数跌0.95%,报收24892...
财经调查|老铺黄金深圳门店金价... 深圳商报·读创客户端记者 周良成 7月21日,老铺黄金(HK06181)股价报374.4港元/股,上...
财政部:上半年证券交易印花税1... 来源:财政部网站 2026年上半年财政收支情况 一、全国一般公共预算收支情况 (一)一般公共预算收...
2026CBME,小熊电器正在... 随着00后家长全面接棒生育主力,母婴行业正经历从“粗放增长”到“高质量发展”的关键转型。在这个拐点上...
跨越山海的金融温度——兴业证券... 闽宁协作三十载,山海同心向共富。近日,记者跟随采访团走进兴业证券,探寻这家福建省属国有金融企业如何以...
WAIC大咖说|指尖上的“内卷... “瑞典科学家曾做过一个非常经典的实验,让人去做划火柴的操作,正常情况下只需要几秒钟,但当在他的手指尖...
4747万!大族激光董事长老婆... 来源:市场资讯 来源:财通社 香港豪宅市场回暖之际,一笔来自A股上市公司创始人家族的交易引发市场关注...