zthy.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. import time
  5. from hashlib import md5
  6. import simplejson as json
  7. import requests
  8. from requests import ConnectionError
  9. from simplejson import JSONDecodeError
  10. logger = logging.getLogger(__name__)
  11. from library.sms import SmsSender
  12. class Sender(SmsSender):
  13. BASE_URL = 'https://api.mix2.zthysms.com'
  14. USERNAME_INDUSTRY = 'whdyhy'
  15. PASSWORD_INDUSTRY = 'T7HvCv0J'
  16. USERNAME_MARKET = 'whdyyx'
  17. PASSWORD_MARKET = 'D0HOFFn6'
  18. TEMPLATE = {
  19. 'CAPTCHA_TEMPLATE_ID': u'尊敬的用户,您本次的验证码为:{},2分钟内有效。如非本人操作,请忽略本信息。',
  20. 'SMS_NOTIFY_EXPIRED_DEVICE_TEMPLATEID': u'尊敬的用户, {}, 届时将无法使用线上支付功能, 请您尽快续费,已经续费请忽略本消息。祝您生活愉快。',
  21. 'EDIT_MONITOR_ID': u'正在编辑审核人。您本次的验证码为:{},2分钟有效。',
  22. 'DEALER_MONITOR_WITHDRAW_ID': u'尊敬的用户,您监管的账号需要验证码,本次的验证码为:{},2分钟有效。',
  23. 'MERCHANT_NOTIFY': u'尊敬的用户,根据央行相关政策,使用银联、微信支付、支付宝等支付机构收款需商家提交身份证、银行卡等相关证件,'
  24. u'请于{}之前登录后台系统完成提交,逾期会影响您的收款和提现,如已开通请忽略。'
  25. }
  26. def __init__(self):
  27. pass
  28. def send(self, phoneNumber, templateName, msg, productName, verifyCode = False):
  29. """
  30. :param phoneNumber:
  31. :param templateName:
  32. :param msg:
  33. :param productName:
  34. :return: dict
  35. """
  36. url = '{}/v2/sendSms'.format(self.BASE_URL)
  37. logger.debug('productName = %s; templateName = %s; msg = %s' % (productName, templateName, msg))
  38. msg_template = self.TEMPLATE[templateName]
  39. send_msg = u'【{}】{}'.format(productName, msg_template.format(msg))
  40. headers = {
  41. 'Accept': 'application/json',
  42. 'Content-Type': 'application/json;charset=utf-8'
  43. }
  44. tKey = int(time.time())
  45. password = md5('{}{}'.format(md5(self.PASSWORD_INDUSTRY).hexdigest().lower(), tKey)).hexdigest().lower()
  46. body = {
  47. 'username': self.USERNAME_INDUSTRY,
  48. 'password': password,
  49. 'tKey': tKey,
  50. 'mobile': phoneNumber,
  51. 'content': send_msg,
  52. 'time': '',
  53. 'ext': 0,
  54. 'extend': ''
  55. }
  56. logger.debug(body)
  57. try:
  58. result = requests.post(url, data = json.dumps(body), headers = headers, timeout = 15, verify = False)
  59. response = result.json()
  60. logger.debug('send sms response=(code={} msg={} msgId={} contNum={}), mobile={}'.format(
  61. response.get("code", ""), response.get("msg", "").encode('utf-8'),
  62. response.get('msgId'), response.get('contNum'), phoneNumber))
  63. if response.get("code") != 200:
  64. if response.get("code") == 4004:
  65. return {'result': False, 'msg': u'手机号码错误'}
  66. else:
  67. return {'result': False, 'msg': u'请求验证码失败, 请稍后再试'}
  68. else:
  69. return {'result': True, 'msg': 'success'}
  70. except JSONDecodeError as e:
  71. logger.exception(e)
  72. return {'result': False, 'msg': u'短信服务器繁忙,请稍后重试'}
  73. except ConnectionError as e:
  74. logger.exception(e)
  75. return {'result': False, 'msg': u'短信服务器不可用,请稍后重试'}