# -*- encoding: utf-8 -*-
"""
@File    : AliyunSmsObject.py
@Time    : 2022/7/19 8:50
@Author  : stephen
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""
import json
# -*- coding: utf-8 -*-
import uuid

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.profile import region_provider
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest

"""
短信业务调用接口示例，版本号：v20170525
Created on 2017-06-12
"""


class AliyunSmsObject(object):

    def __init__(self):
        # 注意：不要更改
        REGION = "cn-hangzhou"
        PRODUCT_NAME = "Dysmsapi"
        DOMAIN = "dysmsapi.aliyuncs.com"
        ali_sms = {
            'ACCESS_KEY_ID': 'LTAIDFtlwLdttWiX',
            'ACCESS_KEY_SECRET': 'Siu6SolsTN5A3evXUkFLyyr0QZ3iiT',
        }
        self.acs_client = AcsClient(ali_sms['ACCESS_KEY_ID'], ali_sms['ACCESS_KEY_SECRET'], REGION)
        region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)

    def do_send_sms(self, business_id, phone_numbers, sign_name, template_code, template_param=None):
        smsRequest = SendSmsRequest.SendSmsRequest()
        # 申请的短信模板编码,必填
        smsRequest.set_TemplateCode(template_code)

        # 短信模板变量参数
        if template_param is not None:
            smsRequest.set_TemplateParam(template_param)

        # 设置业务请求流水号，必填。
        smsRequest.set_OutId(business_id)

        # 短信签名
        smsRequest.set_SignName(sign_name)

        # 数据提交方式
        # smsRequest.set_method(MT.POST)

        # 数据提交格式
        # smsRequest.set_accept_format(FT.JSON)

        # 短信发送的号码列表，必填。
        smsRequest.set_PhoneNumbers(phone_numbers)

        # 调用短信发送接口，返回json
        smsResponse = self.acs_client.do_action_with_exception(smsRequest)

        # TODO 业务处理

        return smsResponse

    # 发送验证码
    def send_code_sms(self, phone, code, sign_name, temp_msg):
        __business_id = uuid.uuid1()
        params = "{\"code\":\"" + str(code) + "\"}"
        res = self.do_send_sms(__business_id, phone, sign_name, temp_msg, params)
        res_json = json.loads(res.decode('utf-8'))
        return res_json

        # 发送验证码

    def send_code_sms_cloud(self, phone, params, sign_name, temp_msg):
        __business_id = uuid.uuid1()
        params = params
        res = self.do_send_sms(__business_id, phone, sign_name, temp_msg, params)
        res_json = json.loads(res.decode('utf-8'))
        return res_json


if __name__ == '__main__':
    pass
    # sign = '周视'
    # ali_sms = AliyunSmsObject()
    # params = u'{"devname":"DKOGER11A","usage":"流量365M","usable":"流量12M"}'
    # res = ali_sms.send_code_sms_cloud('15768812162', params, sign, 'SMS_246090429')
    # print(ali_sms)
