from django.conf import settings
import requests
import re
import json
import urllib


def isValidPhone(phone):
    return re.match(r'^((((\+86)|(86))?(1)\d{10})|000\d+)$', phone) and True or False


def isTestPhone(phone):
    return re.match(r'^000\d+$', phone) and True or False


def isValidCode(code):
    return re.match(r'^\d+$', code)


class SendSMSError(Exception):
    pass


def sendSms(phone, msg):
    # 现在会检查返回结果,对于出错情况,将抛出一个SendSMSError异常
    apikey = settings.YUNPIAN_API_KEY # get apikey by global settings
    params = {'apikey': apikey, 'mobile': phone, 'text': msg}
    url = "https://sms.yunpian.com/v1/sms/send.json"
    headers = {"Accept": "text/plain;charset=utf-8;", "Content-Type":"application/x-www-form-urlencoded;charset=utf-8;"}
    response = requests.post(url, headers=headers, data=params)
    if response.status_code != 200:
        raise SendSMSError("sms server status code is {status_code}".format(status_code=response.status_code))
    else:
        content = response.content.decode()
        data = json.loads(content)
        if data["code"] != 0:
            raise SendSMSError("sms server error. {error_msg}".format(error_msg=content))
    return response


def sendCheckcode(phone, checkcode):
    msg = "【麻辣老师】您的验证码是{check_code}".format(check_code=str(checkcode))
    return sendSms(phone, msg)


def tpl_send_sms(phone, tpl_id, tpl_value):
    """
    模板接口发短信
    tpl_value = {'#code#':'1234','#company#':'云片网'}
    """
    apikey = settings.YUNPIAN_API_KEY # get apikey by global settings
    params = {'apikey': apikey, 'tpl_id': tpl_id, 'tpl_value': tpl_value, 'mobile': phone}
    url = "https://sms.yunpian.com/v1/sms/tpl_send.json"
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    response = requests.post(url, headers=headers, data=params)
    if response.status_code != 200:
        raise SendSMSError("sms server status code is {status_code}".format(status_code=response.status_code))
    else:
        content = response.content.decode()
        data = json.loads(content)
        if data["code"] != 0:
            raise SendSMSError("sms server error. {error_msg}".format(error_msg=content))
    return response

@@ -1221,21 +1221,16 @@
 (phone, 
-check
 code):%0A 
@@ -1231,16 +1231,18 @@
 de):%0A   
+ #
  msg = %22
@@ -1254,22 +1254,16 @@
 %E3%80%91%E6%82%A8%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E6%98%AF%7B
-check_
 code%7D%22.f
@@ -1283,21 +1283,16 @@
 ode=str(
-check
 code))%0A 
@@ -1289,24 +1289,26 @@
 r(code))%0A   
+ #
  return send
@@ -1323,16 +1323,147 @@
 e, msg)%0A
+    # %E7%94%A8%E6%A8%A1%E6%9D%BF%091273789%E3%80%90%E9%BA%BB%E8%BE%A3%E8%80%81%E5%B8%88%E3%80%91%E6%82%A8%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E4%B8%BA#code#%EF%BC%8C%E8%AF%B7%E5%9C%A81%E5%88%86%E9%92%9F%E5%86%85%E6%AD%A3%E7%A1%AE%E5%A1%AB%E5%86%99%EF%BC%8C%E8%AF%B7%E5%8B%BF%E5%90%91%E4%BB%BB%E4%BD%95%E4%BA%BA%E6%B3%84%E9%9C%B2%E3%80%82%0A    data = %7B'#code#': code%7D%0A    return tpl_send_sms(phone, 1273789, data)%0A
 %0A%0Adef tp

send checkcode by tpl