<?php
class smsAction extends baseAction
{
	/**
	  * @method 发送短信
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  sms_type           enum   [必填] 发送速度：'普通','紧急','延时'
	  * @param  sms_from           enum   [必填] 来源：'B2C','主站','客户网','CRM'
	  * @param  sms_to_mobile      string [必填] 接收人手机号码，多个逗号分隔
	  * @param  sms_text           string [必填] 旧平台： 验证码为724847（手机绑定功能），请在页面中输入以完成验证。
												 阿里：{"code":"724847","act":"手机绑定功能"}
	  * @param  sms_style          string [可选] default|bind|unbind|password|paypassword|notice
	  * @param  again              bool   [可选] 是否为重发 flse|true， 如果是重发会优先阿里平台
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data":{"sms_id":"32","sms_status":"ok","sms_error_msg":"","sms_last_time":1492165666},"code":0}
	  {"status":false,"data":'失败原因',"code":0}
	  */
	function send()
	{
		$MustArr = ['sms_type', 'sms_from', 'sms_to_mobile', 'sms_text'];
		foreach($MustArr as $V)
		{
			if(empty($_POST[$V]))
			{
				LibFc::ReturnJson(false, $V.'参数必填');
				exit;
			}
		}
		$MobileArr = [];
		if(preg_match_all('/(1\d{10})\S?/', $_POST['sms_to_mobile'], $Match))
		{
			$MobileArr = $Match[1];
		}
		if(empty($MobileArr))
		{
			LibFc::ReturnJson(false, '手机号码不正确');
			exit;
		}

		$SendBase = new SendBase();

		$DataArr = [
			'sms_type'=>trim($_POST['sms_type']),	
			'sms_from'=>trim($_POST['sms_from']),
			'sms_to_mobile'=>implode(',', $MobileArr),
			'sms_text'=>$_POST['sms_text'],
			'sms_add_time'=>time(),
			'again'=>$_POST['again']
		];
		$DataArr['sms_sign'] = empty($_POST['sms_sign'])? '宜配网': trim($_POST['sms_sign']);
		if(isset($_POST['sms_expire_time'])) $DataArr['sms_expire_time'] = (int) $_POST['sms_expire_time'];
		if(isset($_POST['sms_style'])) $DataArr['sms_style'] = $_POST['sms_style'];

		$Res = $SendBase->GetEnum('sc_sms', 'sms_type');
		if(!in_array($DataArr['sms_type'], $Res['data']))
		{
			LibFc::ReturnJson(false, '提供的sms_type参数值不存在');
			exit;
		}
		$SendSms = new SendSms();
		$Res = $SendSms->send($DataArr);
		echo json_encode($Res);
	}
	
	/**
	  * @method 获取对应ID的信息
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  sms_ids            string   [必填] 短信ID,使用逗号（,）分隔
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
		{"status":true,"data":{"32":{"sms_id":"32","sms_type":"\u666e\u901a","sms_from":"B2C","sms_status":"ok","sms_to_mobile":"13535309690","sms_text":"\u3010\u5b9c\u914d\u7f51\u3011\u77ed\u4fe1\u9a8c\u8bc1\u7801888888","sms_add_time":"1492165666","sms_error_count":"0","sms_error_msg":"","sms_last_time":"1492165666","sms_style":"","sms_expire_time":"0"}},"code":0}
		{"status":false,"data":'失败原因',"code":0}
	  */
	function get()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['sms_ids']))
		{
			LibFc::ReturnJson(false, '短信ID必填');
			exit;
		}
		$SmsIdArr = [];
		foreach(explode(',', $Params['sms_ids']) as $V)
		{
			$SmsIdArr[] = (int) $V;
		}
		$SendBase = new SendBase();
		$Res = $SendBase->Get('sc_sms', ['sms_id, sms_status,sms_error_msg,sms_last_time'], sprintf(" AND sms_id IN (%s)", implode(',', $SmsIdArr)));
		$Arr = [];
		foreach($Res['data'] as $V)
		{
			$Arr[$V['sms_id']] = $V;
		}
		LibFc::ReturnJson(true, $Arr);
	}

	/**
	  * @method 发送指定ID的短信
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  sms_ids            array   [必填] 短信ID
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
	  {"status":true,"data":{"32":{"sms_id":"32","sms_status":"ok","sms_error_msg":"","sms_last_time":1492165666}},"code":0}
	  {"status":false,"data":'失败原因',"code":0}
	  */
	function send_by_ids()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['sms_ids']))
		{
			LibFc::ReturnJson(false, '短信ID必填');
			exit;
		}
		$SendSms = new SendSms();
		$resArr = [];
		foreach(explode(',', $Params['sms_ids']) as $smsId)
		{
			$res = $SendSms->send(['sms_id'=>$smsId]);
			$resArr[$smsId] = $res;
		}
		LibFc::ReturnJson(true, $resArr);
	}


	/**
	  * @method 删除对应ID的短信记录
	  * @http  GET
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  sms_ids            string   [必填] 短信ID,使用逗号（,）分隔
	  * @author soul
	  * @copyright 2017/4/13
	  * @return {"status":true|false,"data":'',"code":0}
	  */
	function del()
	{
		$Params = mvc::$URL_PARAMS;
		if(empty($Params['sms_ids']))
		{
			LibFc::ReturnJson(false, '短信ID必填');
			exit;
		}
		$SmsIdArr = [];
		foreach(explode(',', $Params['sms_ids']) as $V)
		{
			$SmsIdArr[] = (int) $V;
		}
		$SendBase = new SendBase();
		$SendBase->del('sc_sms', sprintf(" AND sms_id IN (%s)", implode(',', $SmsIdArr)));
		LibFc::ReturnJson(true);
	}


	/**
	  * @method 发送短信验证码
	  * @http  get
	  * @param  token            string [必填] 调用接口凭证 (post|get)
	  * @param  mobile           string [必填] 手机号码
	  * @param  style            string [必填] 类型  
												通用模板:default
												手机绑定（含注册）:bind
												手机解绑：unbind
												找回密码：password
												手机登录：login
												设置支付密码：paypassword
												绑定提现账号：baccount
												解绑提现账号：ubaccount
												注销账号：delaccount
	  * @param  from             string [必填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data": 发送短信的ID ,"code":""}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	function sendMobileCode()
	{
		$Params = mvc::$URL_PARAMS;
		$SendSms = new SendSms();
		$dataArr = [
			'mobile'=>$Params['mobile'],
			'style'=>$Params['style'],
			'from'=>$Params['from']
		];
		$res = $SendSms->sendMobileCode($dataArr);
		echo json_encode($res);
	}

	/**
	  * @method 校验手机验证码
	  * @http  get
	  * @param  token            string [必填] 调用接口凭证 (post|get)
	  * @param  mobile           string [必填] 手机号码
	  * @param  style            string [必填] 类型 
												通用模板:default
												手机绑定（含注册）:bind
												手机解绑：unbind
												找回密码：password
												手机登录：login
												设置支付密码：paypassword
												绑定提现账号：baccount
												解绑提现账号：ubaccount
	  * @param  code             string [必填] 验证码
	  * @param  from             string [必填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data": 发送短信的ID ,"code":""}
			{"status":false,"data":"手机验证码不正确","code":"code_error"}
	  */
	function checkMobileCode()
	{
		$Params = mvc::$URL_PARAMS;
		$SendSms = new SendSms();
		$dataArr = [
			'mobile'=>$Params['mobile'],
			'style'=>$Params['style'],
			'from'=>$Params['from'],
			'code'=>$Params['code']
		];
		$res = $SendSms->checkMobileCode($dataArr);
		echo json_encode($res);
	}
	
	/**
	  * @method 将手机验证码失效
	  * @http  get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  mobile             string [必填] 手机号码
	  * @param  stype              string [必填] 
	  * @param  from               string [必填] 
	  * @author soul
	  * @copyright 2018/1/25
	  * @return 
		{"status":true,"data": id, "code":0}
		{"status":false,"data":'失败原因',"code":0}
	  */
	function outMobileCode()
	{
		$Params = mvc::$URL_PARAMS;
		if(!empty($Params['mobile']) && !empty($Params['style']) && !empty($Params['from']))
		{
			$SendBase = new SendBase();
			$where = sprintf(" and smc_mobile='%s' and smc_style='%s' and smc_from='%s'", LibFc::Escape($Params['mobile']), LibFc::Escape($Params['style']), LibFc::Escape($Params['from']));
			$SendBase->Set('sc_mobile_code', ['smc_expire_time'=>0], $where);
		}
		LibFc::ReturnJson(true);
	}
}