def _get_send_params(self): """装载请求参数""" super(AliSmsService, self)._get_send_params() params = {} if self.send_type != self.type_list[2]: try: single_template_param = json.dumps(self.params[0]) except Exception as e: raise SmsException(_(u'参数转化失败')) params['PhoneNumbers'] = ','.join(self.mobiles) params['SignName'] = self.sms_prefix params['TemplateParam'] = single_template_param else: sign_names = [self.sms_prefix for _ in range(len(self.mobiles))] try: mobiles_json = json.dumps(self.mobiles) sign_names_json = json.dumps(self.sign_names) template_params_json = json.dumps(self.params) except Exception as e: raise SmsException(_(u'参数转化失败')) params['PhoneNumberJson'] = mobiles_json params['SignNameJson'] = sign_names_json params['TemplateParamJson'] = template_params_json return params
def _check_params(self): """扩展父类参数检查""" super(YunPianSmsService, self)._check_params() if not self.client: raise SmsException(_(u'服务执行对象不存在')) if self.send_type == self.type_list[2]: if len(self.mobiles) != len(self.params): raise SmsException(_(u'电话数与内容数不一致'))
def _init_tpl(self): """加载本地模版配置""" config_tpl = SysSetting.query.\ filter(SysSetting.key == 'config_sms_template').first() if config_tpl is None: raise SmsException(_(u'短信模版配置不存在')) try: self.tpl_config = json.loads(config_tpl.value) except Exception as e: raise SmsException(e)
def _init(self): """加载短信服务配置信息""" config = SysSetting.query.\ filter(SysSetting.key == self.config_key).first() if not config: raise SmsException(_(u'短信配置不存在')) try: self.sms_config = json.loads(config.value) except Exception as e: raise SmsException(e) self.sms_prefix = self.sms_config['app_name']
def _init(self): """扩展父类配置加载""" super(AliSmsService, self)._init() self.access_key_id = self.sms_config['access_key_id'] self.access_key_secret = self.sms_config['access_key_secret'] if self.access_key_id is None: raise SmsException(_(u'应用公钥不存在')) if self.access_key_secret is None: raise SmsException(_(u'应用私钥不存在')) self.client = AcsClient(self.access_key_id, self.access_key_secret, 'default')
def _get_tpl_content(self): """获取短信模版内容""" tpl_res = self.client.tpl().get({ 'apikey': self.api_key, 'tpl_id': self.template_id }) if tpl_res.code() != 0 and tpl_res.data() is None: raise SmsException(_(u'模版获取失败')) self.template_content = tpl_res.data()['tpl_content'] if not self.template_content: raise SmsException(_(u'模版获取失败'))
def _init(self): """扩展父类配置加载""" super(YunPianSmsService, self)._init() self.api_key = self.sms_config['ak'] if not self.api_key: raise SmsException(_(u'接口验证序号不存在')) self.client = YunpianClient(self.api_key)
def get_smsservice(): """获取短信服务对象""" # 加载短信服务支持配置 vendor = SysSetting.query.\ filter(SysSetting.key == 'sms_vendor').first() if vendor is None: raise SmsException(_(u'短信服务配置不存在')) mod = importlib.import_module(__name__) return getattr(mod, vendor.value)()
def _do_sms_service(self): """发送短信""" request_params = self._get_send_params() request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') # https | http request.set_version('2017-05-25') request.add_query_param('TemplateCode', self.template_id) if self.send_type != self.type_list[2]: request.set_action_name('SendSms') request.add_query_param('PhoneNumbers', request_params['PhoneNumbers']) request.add_query_param('SignName', request_params['SignName']) request.add_query_param('TemplateParam', request_params['TemplateParam']) else: request.set_action_name('SendBatchSms') request.add_query_param('PhoneNumberJson', request_params['PhoneNumberJson']) request.add_query_param('SignNameJson', request_params['SignNameJson']) request.add_query_param('TemplateParamJson', request_params['TemplateParamJson']) res = self.client.do_action(request) try: data = json.loads(res) except Exception as e: raise SmsException(_(u"发送失败")) if not data or data['Code'] != 'OK': log_info( '[AliSmsService]send_type:%s,code:%s,msg:%s,data:%s,prefix:%s' % (self.send_type, data['Code'], data['Message'], data, self.sms_prefix)) raise SmsException(_(u"发送失败"))
def _do_sms_service(self): """发送短信""" reqest_param = self._get_send_params() log_info(reqest_param) if self.send_type == self.type_list[0]: res = self.client.sms().tpl_single_send(reqest_param) if self.send_type == self.type_list[1]: res = self.client.sms().tpl_batch_send(reqest_param) if self.send_type == self.type_list[2]: res = self.client.sms().multi_send(reqest_param) if res is None: raise SmsException(_(u'发送失败')) if res.code() != 0: log_info( '[YunPianSmsService]send_type:%s,code:%s,msg:%s,data:%s,prefix:%s' % (self.send_type, str(res.code()), res.msg(), (res.data() if res.data() else u'[]'), self.sms_prefix)) raise SmsException(_(u'发送失败'))
def send_sms_code(self, mobile, code): """发送短信验证码""" self._init_tpl() if not mobile and not code: raise SmsException(_(u'缺少电话或验证码')) try: self.send_single_sms([mobile], self.tpl_config['code_tpl'], [{ 'code': code }]) except SmsException as e: raise e return True
def _get_send_params(self): """转载请求参数""" super(YunPianSmsService, self)._get_send_params() # 参数处理,键需要是#key#的形式 if self.send_type == self.type_list[2]: self._get_tpl_content() new_params = [] if self.send_type != self.type_list[2]: for _param in self.params: temp = {} for key_name in _param.keys(): temp['#%s#' % key_name] = _param[key_name] new_params.append(urlencode(temp)) else: for _param in self.params: tpl_content = self.template_content for key_name in _param.keys(): tpl_content = tpl_content.replace("#code#", _param[key_name]) new_params.append(tpl_content) self.params = new_params if self.send_type == self.type_list[0]: return { YC.MOBILE: self.mobiles[0], YC.TPL_ID: self.template_id, YC.TPL_VALUE: self.params[0] } if self.send_type == self.type_list[1]: return { YC.MOBILE: ','.join(self.mobiles), YC.TPL_ID: self.template_id, YC.TPL_VALUE: self.params[0] } if self.send_type == self.type_list[2]: return { YC.MOBILE: ','.join(self.mobiles), YC.TEXT: ','.join(self.params) } raise SmsException(_(u'短息类型不存在'))
def _check_params(self): """检查公共参数, 之类扩展各自参数""" if not self.template_id: raise SmsException(_(u'模版ID不存在')) if not self.mobiles: raise SmsException(_(u'短信接收电话不存在')) if not self.params: raise SmsException(_(u'短信内容不存在')) if len(self.mobiles) > 1000: raise SmsException(_(u'最多同时发送1000个')) if not isinstance(self.mobiles, list): raise SmsException(_(u'电话参数格式不正确')) if not isinstance(self.params, list): raise SmsException(_(u'内容参数格式不正确'))
def _check_params(self): """扩展父类参数检查""" super(AliSmsService, self)._check_params() if not self.sms_prefix: raise SmsException(_(u'短信签名不存在1'))