关于畅卓/聚合 短信发送

1、定义短信相关配置
<?php

/*
 * config file for PhpSms
 */
return [

    /*
     * agent use scheme
     * -------------------------------------------------------------------
     * Format: 'name' => scheme
     *
     * The scheme value include:
     * 1. weight (must be a positive integer)
     * 2. 'backup' (ignore upper/lower case)
     *
     * supported agents:
     * 'Log', 'SmsBao', 'Luosimao', 'YunTongXun', 'YunPian', 'SubMail', 'Ucpaas', 'JuHe', 'Alidayu', 'SendCloud', 'ChanzorSMS'
     */
    'scheme' => [

        //被使用的概率,30为3/3,20为2/3
        'Chanzor' => [  //【注意】:如果有了agentClass 其他配置都需要關閉
            'agentClass' => 'App\Jobs\Wechat\Chanzor',
        ]
    ],

    /*
     * agents config
     * -------------------------------------------------------------------
     * Note: agent name must be string.
     *
     */
    'agents' => [

        /*
         * -----------------------------------
         * Chanzor
         * 畅卓
         * -----------------------------------
         * website:http://www.chanzor.com/
         * support template sms.
         */
        'Chanzor' => [

            //APP_ID
            'appId' => env('SMS_APP_ID', 'your app id'),

            // APP_KEY
            'appKey' => env('SMS_APP_KEY', 'your app key'),

            //请求地址
            'sendUrl' => env('SMS_APP_URL', 'your app key'),
        ],

        /*
         * -----------------------------------
         * YunPian
         * 云片代理器
         * -----------------------------------
         * website:http://www.yunpian.com
         * support content sms.
         */
        'YunPian' => [
            //用户唯一标识,必须
            'apikey' => 'your api key',
        ],

        /*
         * -----------------------------------
         * YunTongXun
         * 云通讯代理器
         * -----------------------------------
         * website:http://www.yuntongxun.com/
         * support template sms.
         */
        'YunTongXun' => [
            //主帐号,对应开官网发者主账号下的 ACCOUNT SID
            'accountSid' => 'your account sid',

            //主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN
            'accountToken' => 'your account token',

            //应用Id,在官网应用列表中点击应用,对应应用详情中的APP ID
            //在开发调试的时候,可以使用官网自动为您分配的测试Demo的APP ID
            'appId' => 'your app id',

            //请求地址
            //沙盒环境(用于应用开发调试):sandboxapp.cloopen.com
            //生产环境(用户应用上线使用):app.cloopen.com
            'serverIP' => 'app.cloopen.com',

            //请求端口,生产环境和沙盒环境一致
            'serverPort' => '8883',

            //被叫号显
            'displayNum' => null,

            //语音验证码使用的语言类型
            'voiceLang' => 'zh',

            //语音验证码播放次数
            'playTimes' => 3,
        ],

        /*
         * -----------------------------------
         * SubMail
         * -----------------------------------
         * website:http://submail.cn/
         * support template sms.
         */
        'SubMail' => [

            'appid' => 'your app id',

            'signature' => 'your app key',
        ],

        /*
         * -----------------------------------
         * luosimao
         * -----------------------------------
         * website:http://luosimao.com
         * support content sms.
         */
        'Luosimao' => [
            //短信 API key
            //在管理中心->短信->触发发送下查看
            'apikey' => 'your api key',

            //语言验证 API key
            //在管理中心->语音->语音验证下查看
            'voiceApikey' => 'your voice api key',
        ],

        /*
         * -----------------------------------
         * ucpaas
         * -----------------------------------
         * website:http://ucpaas.com
         * support template sms.
         */
        'Ucpaas' => [
            //主帐号,对应开官网发者主账号下的 ACCOUNT SID
            'accountSid' => 'your account sid',

            //主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN
            'accountToken' => 'your account token',

            //应用Id,在官网应用列表中点击应用,对应应用详情中的APP ID
            //在开发调试的时候,可以使用官网自动为您分配的测试Demo的APP ID
            'appId' => 'your app id',
        ],

        /*
         * -----------------------------------
         * JuHe
         * 聚合数据
         * -----------------------------------
         * website:https://www.juhe.cn
         * support template sms.
         */
        'JuHe' => [
            //应用App Key
            'key' => 'your key',

            //语音验证码播放次数
            'times' => 3,
        ],

        /*
         * -----------------------------------
         * Alidayu
         * 阿里大鱼代理器
         * -----------------------------------
         * website:http://www.alidayu.com
         * support template sms.
         */
        'Alidayu' => [
            //请求地址
            'sendUrl' => 'http://gw.api.taobao.com/router/rest',

            //淘宝开放平台中,对应阿里大鱼短信应用的App Key
            'appKey' => 'your app key',

            //淘宝开放平台中,对应阿里大鱼短信应用的App Secret
            'secretKey' => 'your secret key',

            //短信签名,传入的短信签名必须是在阿里大鱼“管理中心-短信签名管理”中的可用签名
            'smsFreeSignName' => 'your sms free sign name',

            //被叫号显(用于语音通知),传入的显示号码必须是阿里大鱼“管理中心-号码管理”中申请或购买的号码
            'calledShowNum' => null,
        ],

        /*
         * -----------------------------------
         * SendCloud
         * -----------------------------------
         * website: http://sendcloud.sohu.com/sms/
         * support template sms.
         */
        'SendCloud' => [
            //SMS_USER
            'smsUser' => 'your SMS_USER',

            //SMS_KEY
            'smsKey' => 'your SMS_KEY',
        ],

        /*
         * -----------------------------------
         * SmsBao
         * -----------------------------------
         * website: http://www.smsbao.com
         * not support template sms.
         * 不支持模板变量短信
         * support content sms.
         * 支持内容短信
         * support voice sms.
         * 支持语音验证码
         * 支持自定义短信签名
         * 最低消费5元(50条) 最低消费单价0.04元(100万条)
         */
        'SmsBao' => [
            //username
            'username' => 'your username',

            //password
            'password' => 'your password',
        ],
        //短信通知
        'ChangTZ' => [
            'sendUrl' => env('SMS_NOTICE_APP_URL', 'http://api.chanzor.com/'),
            'appId' => env('SMS_NOTICE_APP_ID', '设置分配的appId'),
            'appKey' => env('SMS_NOTICE_APP_KEY', '设置分配的appKey'),
        ]
    ],
];

2、进行方法封装 在 App\Jobs\Wechat;

<?php
/**
 * Created by PhpStorm.
 * @author ifehrim@gmail.com
 */

namespace App\Jobs\Wechat;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Config;
use Toplan\PhpSms\Agent;
use Toplan\PhpSms\ContentSms;
use Log;

class Chanzor extends Agent implements ContentSms
{

    public $url;

    public $account;

    public $password;

    protected $resultArr = [
        '0' => '发送成功',
        '-101' => '账户为空',
        '-102' => '密码为空',
        '-103' => '发送时间参数错误',
        '-113' => '账户余额不足',
        '-118' => '短信内容有误,一般是签名有问题',
        '-121' => '限制发送,一般是应用被冻结的情况',
    ];

    public function __construct(array $config = [])
    {
        if(empty($config)) $config = config('phpsms.agents.ChangTZ');
        parent::__construct($config);
        $this->url = $config['sendUrl'];
        $this->account = $config['appId'];
        $this->password = $config['appKey'];
    }

    public function sendContentSms($to, $content, $sendTime = NULL)
    {
        $url = $this->url . 'send';
        $params = [
            'account' => $this->account,
            'password' => md5($this->password),
            'mobile' => $to,
            'content' => $content,
        ];
        if ($sendTime) {
            $params['sendTime'] = $sendTime;
        }
        Log::debug($url, $params);
        $result = $this->curlGet($url, $params);
        $this->setResult($result);
        return $result;
    }

    public function getDrSms($to, $content)
    {
        $url = $this->url . 'dr';
        $params = [
            'account' => $this->username,
            'password' => md5($this->password),
            'mobile' => $to,
            'content' => $content,
        ];
        $result = $this->curlPost($url, $params);
        $this->setResult($result);
    }

    public function getDrSmsTask($taskId = null, $statusNum = null)
    {
        $url = $this->url . 'dr';
        $params = [
            'account' => $this->account,
            'password' => md5($this->password),
        ];
        $taskId && $params['taskId'] = $taskId;
        $statusNum && $params['statusNum'] = $statusNum;
        $result = $this->curlPost($url, $params);
        $this->setResult($result);
        return $result;
    }

    public function getMoSms($to, $content)
    {
        $url = $this->url . 'mo';
        $params = [
            'account' => $this->username,
            'password' => md5($this->password),
            'mobile' => $to,
            'content' => $content,
        ];
        $result = $this->curlPost($url, $params);
        $this->setResult($result);
    }

    public function voiceVerify($to, $code, $tempId, array $tempData)
    {
        $url = $this->url . 'voice';
        $params = [
            'account' => $this->username,
            'password' => md5($this->password),
            'mobile' => $to,
            'content' => $code,
        ];
        $result = $this->curlPost($url, $params);
        $this->setResult($result);
    }

    protected function setResult($result)
    {
        if ($result['request']) {
            Log::info(json_encode($result));
            $result = json_decode($result['response'], true);
            $msg = array_key_exists($result['status'], $this->resultArr) ? $this->resultArr[$result['status']] : 'unknown error';
            if ($result['status'] === 0) {
                $this->result(Agent::SUCCESS, true);
            } else {
                $this->result(Agent::SUCCESS, false);
            }
            $this->result(Agent::INFO, $result['desc']);
            $this->result(Agent::CODE, $result['status']);
        } else {
            $this->result(Agent::INFO, 'request failed');
        }
    }
}

3、进行发送验证码  这里在获取验证码时 进行手机号+当前时间的加密,防止验证码盗刷

public function sApplets(Request $request)
{

    $tel = $request->input('phone');//要发信息的手机号码
    $type = $request->input('type');

    $rsa_type = $request->input('rsa_type', 1);
    $rsa = $request->input('rsa', '');
    $rsa_path = config('rsa_path.666')??'';
    if( empty($rsa_path) ) return response_json(333, '参数不正确');
    $rsa_path = storage_path($rsa_path);

    if (!$tel || !$type) return parent::_jsonMsg(4001, Config::get('code.4001'));
    if( !in_array($type, [34]) ) return parent::_jsonMsg(5007, Config::get('code.5007'));
    if ( !self::verifyRsaPri($tel,$rsa, $rsa_path) ) return parent::_jsonMsg(5007, Config::get('code.5007'));

    if(AuthCodeRepository::verify_phone($tel, 34)) return parent::_jsonMsg(5006, Config::get('code.5006'));//有验证码可用的手机号注册时不再重复发送
    $today = $this->authcode->Today($tel, $type);//判断每天发短息条数
    if (!$today) return parent::_jsonMsg(12009, Config::get('code.12009'));

    $num = rand(1000, 9999);
    $overtime = time() + 900;

    Authcode::deleteByPhpone($tel, $type);

    $CreateCode = $this->authcode->CreateCode($tel, $num, $type, $overtime);//验证码插入数据库

    if (!$CreateCode) return parent::_jsonMsg(5001, Config::get('code.5001'));


    (new Chanzor(config('phpsms.agents.ChangTZ')))->sendContentSms($tel,"【好玩 哒哒哒】您本次的验证码为{$num},感谢您的支持!");
    return response_json(200, '发送成功');

}

最后、解密rsa就不贴了,以上就是大概接入验证码的一个流程,注意发送内容的模板,和第三方平台定义的要一致哦  不然可能会报  短信签名错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
组合/聚合复用原则是指在设计类之间的关系时,应该优先考虑使用组合或聚合关系,而不是继承关系,以达到代码复用的目的。下面是一个使用组合/聚合复用原则的Java案例: 假设有一个图形类Shape,其中包含了计算面积的方法calculateArea(),以及一个颜色属性color。现在需要创建一个矩形类Rectangle,它继承自Shape并添加了一个宽度属性width和一个高度属性height,同时重写了calculateArea()方法来计算矩形面积。这样的设计虽然简单,但存在以下问题: 1. Rectangle类和Shape类之间存在继承关系,这会使得Rectangle类紧耦合于Shape类,难以扩展和修改。 2. 如果后续需要创建其他形状的类,比如圆形类Circle,就需要再次重复实现计算面积的方法,造成了代码的冗余。 为了解决这些问题,我们可以使用组合/聚合复用原则来重新设计: 首先,我们不再让Rectangle类继承自Shape类,而是将Shape类作为Rectangle类的一个属性,即将Shape类对象组合到Rectangle类中。这样Rectangle类就能够调用Shape类的calculateArea()方法来计算面积,而不需要重复实现。 其次,我们可以将Shape类设计为一个抽象类,其中包含了一个抽象方法calculateArea(),以及一个颜色属性color。然后,我们可以创建具体的形状类,比如Rectangle和Circle类,它们都继承自Shape类,并根据自己的特点实现calculateArea()方法。这样,我们就避免了代码的冗余,并且能够方便地扩展和修改形状类。 下面是具体的Java代码实现: ``` abstract class Shape { protected String color; public abstract double calculateArea(); // getters and setters for color } class Rectangle { private double width; private double height; private Shape shape; public Rectangle(double width, double height, Shape shape) { this.width = width; this.height = height; this.shape = shape; } public double calculateArea() { return width * height; } // getters and setters for width and height // delegate getters and setters for color to shape object public String getColor() { return shape.getColor(); } public void setColor(String color) { shape.setColor(color); } } class Circle { private double radius; private Shape shape; public Circle(double radius, Shape shape) { this.radius = radius; this.shape = shape; } public double calculateArea() { return Math.PI * radius * radius; } // getters and setters for radius // delegate getters and setters for color to shape object public String getColor() { return shape.getColor(); } public void setColor(String color) { shape.setColor(color); } } ``` 在以上代码中,Rectangle类和Circle类都使用了组合的方式来复用Shape类。同时,Shape类被设计为一个抽象类,以便于创建具体的形状类并重写calculateArea()方法。这样,我们就成功地使用了组合/聚合复用原则来优化设计,并提高了代码的可扩展性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值