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('验证码错误')

相关内容

热门资讯

“双标”换卡背后,银行还需多些... 新华社记者 颜之宏、杨深深 持到期银行卡和身份证去银行网点换新卡,却被要求“必须交回旧卡才能取新卡”...
“离境退税2.0”带动“中国购... 【环球时报综合报道】编者的话:5月18日,商务部等6部门联合发布《关于加力优化离境退税措施扩大入境消...
一年烧掉2000亿、市值蒸发3... 商业润点 |Biz Run Review 三国归晋,用了六十年。即时零售的"三国杀",才刚刚开局...
原创 金... 2026年5月22日,国内黄金市场呈现出令人咋舌的价格鸿沟。基础金价徘徊在每克995.3元,而回收价...
原创 人... SpaceX的星舰V3终于在全球瞩目中成功升空。北京时间5月23日清晨,这颗高达124米的巨型火箭顺...
原创 被... 5月19日,欧洲议会掀起了一场引人注目的风暴,以压倒性的票数通过了最新的钢铁进口规定。 这套规则...
光纤量价齐升,烽火通信加快布局... 烽火通信(600498)5月22日披露的投资者关系活动记录表显示,公司于5月21日参加了中国信息通信...
原创 突... 今天5月24日一大早,打开行情一看,国际现货黄金报4508.25美元/盎司,单日跌了26.68美元,...
企业快讯 | 携手联通!狄耐克... 狄耐克 厦门总商会副会长企业 厦门狄耐克智能科技股份有限公司 与中国联通厦门分公司 将5G智慧“嵌入...
美银策略师警告:SpaceX与... 环球网 据彭博社报道,美国银行首席投资策略师迈克尔·哈特奈特(Michael Hartnett)最新...
卸任55天后,知名基金经理任相... 【导读】卸任55天后,知名基金经理任相栋“奔私”谜底揭晓 见习记者 闫军 知名基金经理任相栋“奔私”...
原创 大... “免签+手机刷一切”就能让老外连夜订机票?2026年一季度,阿根廷人来华暴涨九倍,北京三源里菜市场三...
从泰山顶峰掉落!“大佬背后的大... 文/刘工昌 他曾是柳传志的“大哥”,助力联想完成混合所有制改革;是史玉柱眼中的“贵人”,帮他东山再起...
原创 2... 最近网上流传出一份2030年GDP10强预测榜单,其中一些城市位次的变化也挺有趣的。上海排在第一,深...
原创 全... 2026年3月的全球美债市场迎来剧烈变动,彻底打破了长期稳定的持仓格局。 根据美国财政部发布的国际资...
全球都在给这几只“疯牛”烧钱 近段时间,AI行情再次成为全球资本市场主线,但舞台中央的“主角”发生了变化:投资者不再只偏好云厂商和...
【财闻联播】“硬刚监管”?老虎... ★ 宏观动态 ★ 商务部:1—4月全国吸收外资2876.9亿元人民币 据商务部网站,2026年1—4...
燕京啤酒营收净利双增:U8增速... 蓝鲸新闻5月22日讯(记者 朱欣悦)燕京啤酒(000729.SZ)打了一个翻身仗。 2025年燕京啤...
原创 帮... 老铁们,这周有个事儿挺有意思,估计不少基民都看懵了:都说科技是主线,芯片是未来,可数据显示,年内火爆...
4家银行AIC现身存储巨头股东... 近日,资本市场热度颇高的两家存储巨头长鑫科技集团股份有限公司(以下简称“长鑫科技”)、长江存储控股股...