329 行
13 KiB
PHP
可执行文件
329 行
13 KiB
PHP
可执行文件
<?php
|
|
|
|
//阿里大鱼短信接口
|
|
|
|
namespace Api\Controller;
|
|
|
|
use Think\Controller;
|
|
use think\image\Exception;
|
|
|
|
//[1.7.10]
|
|
|
|
/**
|
|
* 阿里大鱼短信发送
|
|
* Class AlidayuController
|
|
* @package Api\Controller
|
|
*/
|
|
class AlidayuController extends Controller
|
|
{
|
|
/**
|
|
* 发送验证码给商户的用户
|
|
* @param $phone
|
|
* @param $merchant_id
|
|
* @return bool|\stdClass
|
|
* @throws \think\Exception
|
|
*/
|
|
public function Send($phone, $merchant_id)
|
|
{
|
|
//查询商户的短信配置
|
|
$mer_conf = D('MerchantSms')->getFind(['merchantid' => $merchant_id]);
|
|
|
|
//生成验证码
|
|
$code = rand(100000, 999999);
|
|
|
|
// 商户未设置或关闭自定义短信
|
|
if (empty($mer_conf) || $mer_conf['status'] == 2) {
|
|
// 判断合伙人是否让商户使用系统验证码
|
|
$sms_merchant = D('SystemSmsToMer')->getFind(['merchantid' => $merchant_id]);
|
|
|
|
if (empty($sms_merchant) || $sms_merchant['status'] == 2) {
|
|
return callBack(statusCode()['error'], '商家无发送短信权限');
|
|
}
|
|
if (bcsub($sms_merchant['allnum'], $sms_merchant['usenum']) <= 0) {
|
|
return callBack(statusCode()['error'], '商家系统验证码使用次数已达上限');
|
|
}
|
|
|
|
$user_type = 1;
|
|
|
|
//使用合伙人的配置发送验证码
|
|
$result = $this->sendSmsCode($phone, $code);
|
|
} else {
|
|
$field = ['accesskeyid', 'accesskeysecret', 'signname', 'verifitemplatecode'];
|
|
foreach ($field as $val) {
|
|
if (empty($mer_conf[$val])) return callBack(statusCode()['error'], '商户未完善短信配置');
|
|
}
|
|
$data = [
|
|
"accessKeyId" => $mer_conf['accesskeyid'],// accessKeyId
|
|
"accessKeySecret" => $mer_conf['accesskeysecret'],// 秘钥
|
|
"SignName" => $mer_conf['signname'],// 短信签名
|
|
"TemplateCode" => $mer_conf['verifitemplatecode'],// 短信模板号
|
|
"phone" => $phone,// 接收短信的手机号
|
|
"TemplateParam" => Array(
|
|
"code" => $code,//验证码
|
|
),// 短信参数 详细请按照设置的短信模板传入
|
|
];
|
|
$user_type = 2;
|
|
|
|
//发送验证码
|
|
$result = $this->sendSMS($data);
|
|
}
|
|
|
|
if ($result['status'] == 1) {
|
|
//发送成功
|
|
session('identifying_codes', $code, 300); //把验证码存到session
|
|
}
|
|
|
|
//添加商户发送短信记录
|
|
$this->addSendRecord($merchant_id, $phone, '验证码:' . $code, $result['status'] == 1 ? 1 : 2, $user_type, 1);
|
|
|
|
$result['codes'] = $result['data'];
|
|
$result['msg'] = $result['message'];
|
|
$result['Code'] = $result['status'] == 1 ? 'OK' : 'FAIL';
|
|
$result['data']['code'] = $code;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 系统发送验证码
|
|
* @param 接收短信的手机号
|
|
* @param $code 验证码
|
|
* @return bool|mixed|\stdClass 内含验证码
|
|
*/
|
|
function sendSmsCode($phone, $code = null)
|
|
{
|
|
if (empty($phone) || !verifyMobilePhone($phone)) {
|
|
return callBack(statusCode()['error'], '请传入正确的手机号');
|
|
}
|
|
$conf = D('SystemSms')->getFind();
|
|
if (empty($conf)) {
|
|
return callBack(statusCode()['error'], '系统暂不支持发送验证码功能');
|
|
}
|
|
if (empty($conf['accesskeyid']) || empty($conf['accesskeysecret'])
|
|
|| empty($conf['accessname']) || empty($conf['accesscode'])) {
|
|
return callBack(statusCode()['error'], '系统短信配置有误');
|
|
}
|
|
if (empty($code)) {
|
|
$code = rand(100000, 999999); //生成验证码
|
|
}
|
|
$data = [
|
|
"accessKeyId" => $conf['accesskeyid'],// accessKeyId
|
|
"accessKeySecret" => $conf['accesskeysecret'],// 秘钥
|
|
"SignName" => $conf['accessname'],// 短信签名
|
|
"TemplateCode" => $conf['accesscode'],// 短信模板号
|
|
"phone" => $phone,// 接收短信的手机号
|
|
"TemplateParam" => Array(
|
|
"code" => $code,//验证码
|
|
),// 短信参数 详细请按照设置的短信模板传入
|
|
];
|
|
$result = $this->sendSMS($data);
|
|
$result['code'] = $code;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 发送会员注册成功提醒(仅仅提供商家自己配置的短信设置)
|
|
* @param $phone 接收手机短信的手机号
|
|
* @param $merchant_id 商户ID
|
|
* @param $mer_name 商户名称
|
|
* @return array|mixed
|
|
* @throws \think\Exception
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function sendRegisterVipSuccess($phone, $merchant_id, $mer_name)
|
|
{
|
|
//查询商户的短信配置
|
|
$mer_conf = D('MerchantSms')->getFind(['merchantid' => $merchant_id]);
|
|
if (empty($mer_conf) || $mer_conf['status'] == 2) {
|
|
return callBack(statusCode()['error'], '商家短信参数未完善');
|
|
}
|
|
$field = ['accesskeyid', 'accesskeysecret', 'signname', 'registertemplatecode'];
|
|
foreach ($field as $val) {
|
|
if (empty($mer_conf[$val])) return callBack(statusCode()['error'], '商户未完善短信配置');
|
|
}
|
|
|
|
$data = [
|
|
"accessKeyId" => $mer_conf['accesskeyid'],// accessKeyId
|
|
"accessKeySecret" => $mer_conf['accesskeysecret'],// 秘钥
|
|
"SignName" => $mer_conf['signname'],// 短信签名
|
|
"TemplateCode" => $mer_conf['registertemplatecode'],// 短信模板号
|
|
"phone" => $phone,// 接收短信的手机号
|
|
"TemplateParam" => Array(
|
|
"shopname" => $mer_name,
|
|
"phone" => $phone,
|
|
),// 短信参数 详细请按照设置的短信模板传入
|
|
];
|
|
//发送短信
|
|
$result = $this->sendSMS($data);
|
|
|
|
//添加商户短信发送记录
|
|
$this->addSendRecord($merchant_id, $phone, '会员注册成功:' . $phone, $result['status'] == 1 ? 1 : 2, 2, 4);
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 发送赊账统计短信给商家(仅仅提供商家自己配置的短信设置)
|
|
* @param $phones 接收短信的手机号数组
|
|
* @param $item 统计的数据
|
|
*
|
|
* $item['store_count'] 门店数量----
|
|
* 门店名称与门店数量二选一
|
|
* $item['store_name'] 门店名称-----
|
|
*
|
|
* $item['month_wait_still'] 本月待还
|
|
* $item['all_wait_still'] 总待还
|
|
* @param $merchant_id 商户ID
|
|
* @param $type 通知类型 1商户 2门店
|
|
* @return array|mixed
|
|
* @throws \think\Exception
|
|
*/
|
|
public function sendCreditRemind($phones, $item, $merchant_id, $type = 1)
|
|
{
|
|
$mer_conf = D('MerchantSms')->getFind(['merchantid' => $merchant_id]);
|
|
if (empty($mer_conf) || $mer_conf['status'] != 1) {
|
|
return callBack(statusCode()['error'], '商户未开启短信配置');
|
|
}
|
|
$field = ['accesskeyid', 'accesskeysecret', 'signname', $type == 1 ? 'mer_credit_code' : 'store_credit_code'];
|
|
foreach ($field as $val) {
|
|
if (empty($mer_conf[$val])) return callBack(statusCode()['error'], '商户未完善短信配置');
|
|
}
|
|
|
|
$data = [
|
|
"accessKeyId" => $mer_conf['accesskeyid'],// accessKeyId
|
|
"accessKeySecret" => $mer_conf['accesskeysecret'],// 秘钥
|
|
"SignName" => $mer_conf['signname'],// 短信签名
|
|
"TemplateCode" => $mer_conf[$type == 1 ? 'mer_credit_code' : 'store_credit_code'],// 短信模板号
|
|
"TemplateParam" => Array(
|
|
"month_wait_still" => $item['month_wait_still'],
|
|
"all_wait_still" => $item['all_wait_still'],
|
|
),// 短信参数 详细请按照设置的短信模板传入
|
|
];
|
|
if ($type == 1) {
|
|
$data['TemplateParam']['store_count'] = $item['store_count'];
|
|
} else {
|
|
$data['TemplateParam']['store_name'] = $item['store_name'];
|
|
}
|
|
$result = [];
|
|
foreach ($phones as $v) {
|
|
$data['phone'] = $v;
|
|
|
|
//发送短信
|
|
$response = $this->sendSMS($data);
|
|
|
|
//添加商户短信发送记录
|
|
$this->addSendRecord($merchant_id, $v, '赊账提醒', $result['status'] == 1 ? 1 : 2, 2, 5);
|
|
|
|
$result[] = $response;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加短信发送记录
|
|
* @param $merchant_id 商户ID
|
|
* @param $phone 接收短信的手机号
|
|
* @param $info 短信关键内容描述
|
|
* @param $status 发送状态 1成功 2失败
|
|
* @param $scene_type 短信类型 1验证码 2会员充值 3会员消费 4成功激活会员 5赊账统计提醒商家
|
|
* @param $type 使用的短信模板所属 1系统 2商户
|
|
* @throws \think\Exception
|
|
*/
|
|
private function addSendRecord($merchant_id, $phone, $info, $status, $scene_type, $type)
|
|
{
|
|
$item = array(
|
|
'merchantid' => $merchant_id, //商户id
|
|
'phone' => $phone, //手机号码
|
|
'number' => 1, //数量
|
|
'type' => $type, //短信类型 1验证码 2会员充值 3会员消费 4成功激活会员 5赊账统计提醒商家
|
|
'info' => $info, //短信关键内容。
|
|
'isok' => $status, //是否发送成功,1是,2,否
|
|
'ismerchantsms' => $scene_type, //是否是商户自己配置短信,1,系统短信,2,商户短信。
|
|
'addtime' => time(),
|
|
);
|
|
|
|
D('SmsOrder')->addOrder($item);
|
|
if ($type == 1) {
|
|
// 添加商户短信 发送条数增加 1
|
|
D('SystemSmsToMer')->toSetInc(['merchantid' => $merchant_id], 'usenum', 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发送短信
|
|
* @param $param
|
|
* $param["accessKeyId"] accessKeyId
|
|
* $param["accessKeySecret"] 秘钥
|
|
* $param['phone'] 接收短信的手机号
|
|
* $param["SignName"] 短信签名
|
|
* $param["TemplateCode"] 短信模板号
|
|
* $param["TemplateParam"] 短信参数 详细请按照设置的短信模板传入
|
|
* @return bool|\stdClass
|
|
*/
|
|
private function sendSMS($param)
|
|
{
|
|
require_once "./SignatureHelper.php";
|
|
$params = array();
|
|
|
|
// *** 需用户填写部分 ***
|
|
|
|
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
|
|
// fixme 必填: 短信接收号码
|
|
$params["PhoneNumbers"] = $param['phone'];
|
|
|
|
// fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
|
|
$params["SignName"] = $param["SignName"];
|
|
|
|
// fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
|
|
$params["TemplateCode"] = $param['TemplateCode'];
|
|
|
|
// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
|
|
$params['TemplateParam'] = $param['TemplateParam'];
|
|
|
|
// fixme 可选: 设置发送短信流水号
|
|
$params['OutId'] = "12345";
|
|
|
|
// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
|
|
$params['SmsUpExtendCode'] = "1234567";
|
|
|
|
|
|
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
|
|
if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
|
|
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
// 初始化SignatureHelper实例用于设置参数,签名以及发送请求
|
|
$helper = new \SignatureHelper();
|
|
|
|
// 此处可能会抛出异常,注意catch
|
|
try {
|
|
$content = $helper->request(
|
|
$param['accessKeyId'],
|
|
$param['accessKeySecret'],
|
|
"dysmsapi.aliyuncs.com",
|
|
array_merge($params, array(
|
|
"RegionId" => "cn-hangzhou",
|
|
"Action" => "SendSms",
|
|
"Version" => "2017-05-25",
|
|
))
|
|
// fixme 选填: 启用https
|
|
// ,true
|
|
);
|
|
$content = json_decode(json_encode($content), true);
|
|
if ($content) {
|
|
if ($content['Code'] == 'OK') {
|
|
return callBack(statusCode()['success'], '发送成功', $content);
|
|
} else {
|
|
return callBack(statusCode()['error'], '发送失败:' . $content['Message']);
|
|
}
|
|
} else {
|
|
return callBack(statusCode()['error'], '短信发送失败');
|
|
}
|
|
} catch (Exception $e) {
|
|
return callBack(statusCode()['error'], '短信配置异常');
|
|
}
|
|
}
|
|
}
|