7.8.drf实现短信验证码发送

工具类

apps/utils/yunpian.py

import json
import requests


class YunPian(object):

    def __init__(self, api_key):
        self.api_key = api_key
        self.single_send_url = "https://sms.yunpian.com/v2/sms/single_send.json"

    def send_sms(self, code, mobile):
        params = {
            "apikey": self.api_key,
            "mobile": mobile,
            "text": "【Mo Shop】您的验证码是{code}。如非本人操作,请忽略本短信".format(code=code)
        }

        response = requests.post(self.single_send_url, data=params)
        re_dict = json.loads(response.text)
        return re_dict


if __name__ == "__main__":
    yun_pian = YunPian("")
    yun_pian.send_sms("2548", "")

serializer

apps/users/serializers.py

viewSet

apps/users/views.py

url配置

mo_shop/urls.py

settings

mo_shop/settings.py

Last updated

Was this helpful?