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

相关内容

热门资讯

原创 高... 你有没有发现,几年前人人都在拼命买房,而现在,越来越多人开始思考,房子,到底还是不是财富? 这几年,...
这个春节,中国经济热力值拉满 2026年的春节,注定要在中国消费市场上留下浓墨重彩的一笔。 当9天的超长假期遇上持续加码的政策红利...
2026年中国汽车产业十大趋势... 2025年,中国汽车产业在连续17年产销量稳居全球第一的基础上,再次交出了一份充满变革与挑战的答卷。...
2022年天猫烘焙厨电行业趋势... 今天分享的是:2022年天猫烘焙厨电行业趋势白皮书 报告共计:7页 烘焙厨电迎来新变革:从“功能单一...
春节假期县城网吧人气旺,网吧又... 作者 | 豹变 张经纬 春节假期到来,如果你问回到老家的中青年男性假期玩什么,网吧可能是一个答案。...
上海“小巨人”要敲钟了!商米科... 马年伊始,港股市场就再度迎来一家上海本土科技企业。 据港交所消息,近日,上海商米科技集团股份有限公司...
原创 川... 当特朗普的关税武器让美国最高法院“缴械”时,中国、巴西反而从与美国关税战最大的受害者,变成了“最大的...
原创 刚... 白宫新闻办公室刚向媒体证实,特朗普3月底访华,最高法院转头就砸下一记重锤。 九位大法官裁定,特朗普的...
美国出手 5 亿美元委国石油,... 美国方面透露,已完成首批价值 5 亿美元的委内瑞拉石油出售,后续还将继续推进更多相关交易。这批原油大...
筑强基金集群 精准“滴灌”重点... 当下,产业投资基金已成为发展新质生产力的重要抓手,正发挥着日益重要的作用,如何引来金融活水浇灌“产业...
关于“十五五”期间支持科技创新... 财政部 中央宣传部 国家发展改革委 教育部 科技部 工业和信息化部 民政部 商务部 文化和旅游部 国...
雄安综合保税区全域封关运营 2月24日,海关总署批复同意雄安综合保税区(二期)通过验收,标志着规划面积0.63平方公里的雄安综合...
中加敲定重磅合同,特朗普对华能... 加拿大总理卡尼访华成果丰硕,不仅推动中加经贸合作迈上新台阶,更向其他西方国家释放出积极信号。在美加关...
成都和鸿科技IPO辅导备案,获... 2026年2月14日,证监会官网披露,长江证券已提交《关于成都和鸿科技股份有限公司首次公开发行股票并...
原创 马... 当诺奖得主Demis Hassabis把“推导出广义相对论”设为AGI的及格线时,整个科技圈炸了——...
滴滴春节出行数据:“反向过年”... 流动中国年味浓,人们“马”不停蹄奔向团圆。滴滴出行数据显示,“双向奔赴”成2026年春节出行新看点,...
去年韩国上市公司派息达48万亿... 来源:环球市场播报 周二公布的行业数据显示,受韩国股市前所未有的上涨行情推动,2025年韩国上市公司...
九识智能再获3亿美元融资,估值... 图为九识无人车 36氪获悉,九识智能近日完成新一轮超3亿美元融资,估值突破百亿人民币。这也意味着,就...
央行明日开展6000亿元MLF... 中国人民银行持续加码中长期资金投放,中期借贷便利(MLF)将连续12个月加量续做。 中国人民银行2月...