# -*- coding: utf-8 -*- # !/usr/bin/env python import logging import time from hashlib import md5 import simplejson as json import requests from requests import ConnectionError from simplejson import JSONDecodeError logger = logging.getLogger(__name__) from library.sms import SmsSender class Sender(SmsSender): BASE_URL = 'https://api.mix2.zthysms.com' USERNAME_INDUSTRY = 'whdyhy' PASSWORD_INDUSTRY = 'T7HvCv0J' USERNAME_MARKET = 'whdyyx' PASSWORD_MARKET = 'D0HOFFn6' TEMPLATE = { 'CAPTCHA_TEMPLATE_ID': u'尊敬的用户,您本次的验证码为:{},2分钟内有效。如非本人操作,请忽略本信息。', 'SMS_NOTIFY_EXPIRED_DEVICE_TEMPLATEID': u'尊敬的用户, {}, 届时将无法使用线上支付功能, 请您尽快续费,已经续费请忽略本消息。祝您生活愉快。', 'EDIT_MONITOR_ID': u'正在编辑审核人。您本次的验证码为:{},2分钟有效。', 'DEALER_MONITOR_WITHDRAW_ID': u'尊敬的用户,您监管的账号需要验证码,本次的验证码为:{},2分钟有效。', 'MERCHANT_NOTIFY': u'尊敬的用户,根据央行相关政策,使用银联、微信支付、支付宝等支付机构收款需商家提交身份证、银行卡等相关证件,' u'请于{}之前登录后台系统完成提交,逾期会影响您的收款和提现,如已开通请忽略。' } def __init__(self): pass def send(self, phoneNumber, templateName, msg, productName, verifyCode = False): """ :param phoneNumber: :param templateName: :param msg: :param productName: :return: dict """ url = '{}/v2/sendSms'.format(self.BASE_URL) logger.debug('productName = %s; templateName = %s; msg = %s' % (productName, templateName, msg)) msg_template = self.TEMPLATE[templateName] send_msg = u'【{}】{}'.format(productName, msg_template.format(msg)) headers = { 'Accept': 'application/json', 'Content-Type': 'application/json;charset=utf-8' } tKey = int(time.time()) password = md5('{}{}'.format(md5(self.PASSWORD_INDUSTRY).hexdigest().lower(), tKey)).hexdigest().lower() body = { 'username': self.USERNAME_INDUSTRY, 'password': password, 'tKey': tKey, 'mobile': phoneNumber, 'content': send_msg, 'time': '', 'ext': 0, 'extend': '' } logger.debug(body) try: result = requests.post(url, data = json.dumps(body), headers = headers, timeout = 15, verify = False) response = result.json() logger.debug('send sms response=(code={} msg={} msgId={} contNum={}), mobile={}'.format( response.get("code", ""), response.get("msg", "").encode('utf-8'), response.get('msgId'), response.get('contNum'), phoneNumber)) if response.get("code") != 200: if response.get("code") == 4004: return {'result': False, 'msg': u'手机号码错误'} else: return {'result': False, 'msg': u'请求验证码失败, 请稍后再试'} else: return {'result': True, 'msg': 'success'} except JSONDecodeError as e: logger.exception(e) return {'result': False, 'msg': u'短信服务器繁忙,请稍后重试'} except ConnectionError as e: logger.exception(e) return {'result': False, 'msg': u'短信服务器不可用,请稍后重试'}