ECJia到家V1.37.0新增短信宝短信接口

ECJia移动商城系统(EC+)是一款基于移动互联网的商城应用服务产品,拥有执行效率高、上手轻松、管理便捷等一系列优点。今天小编为大家讲解一下ECJIA1.37.0这个版本的短信插件该如何开发,短信接口使用的是我们短信宝短信群发平台,我们短信宝短信群发平台极其稳定,而且短信发送速度相当快捷,验证码和订单通知在3~5秒就能收到,用户体验非常好,注册就送测试短信。

1:打开项目:\vendor\royalcms\sms\config\sms.php 修改短信配置方面

<?php
return [
    'default' => env('SMS_DEFAULT', 'smsbao'),
    'fallback' => env('SMS_FALLBACK'),
    'signName' => env('SMS_SIGNNAME'),
    'agents' => [
        'ihuyi' => [
            'credentials' => [
                'appKey' => env('IHUYI_APPKEY'),
                'appSecret' => env('IHUIYI_APPSECRET')
            ],
            'executableFile' => 'IHuYiAgent',
        ],
         'smsbao' => [
            'credentials' => [
                'appKey' => env('SMSBAO_APPKEY'),
                'appSecret' => env('SMSBAO_APPSECRET'),
                'appsign' => env('SMSBAO_APPSIGN')
            ],
            'executableFile' => 'SMSbao',
        ],
    ],
];

2:打开项目\vendor\royalcms\sms\Royalcms\Component\Sms\Agents\ 创建SMSbao.php

<?php
namespace Royalcms\Component\Sms\Agents;
use Royalcms\Component\Support\Arr;
use Royalcms\Component\Sms\Sms;
use Royalcms\Component\Sms\Contracts\SmsAgent;
use RC_Xml;
use RC_Error;
use Royalcms\Component\Sms\SendResponse;
use Royalcms\Component\Sms\BalanceResponse;
class SMSbao extends Sms implements SmsAgent
{
const HOST= 'http://api.smsbao.com/sms?';
    private $appKey;
    private $appSecret;
    private $appSign;
    private $statusStr = array(
        "0" => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );
    
    public function __construct($config)
    {
        $this->config = $config;
        $this->transformConfig();
    }
    
    public function transformConfig()
    {
        $credentials = Arr::pull($this->config, 'credentials');
        $this->appKey = Arr::pull($credentials, 'appKey');
        $this->appSecret = Arr::pull($credentials, 'appSecret');
        $this->appSign = Arr::pull($credentials, 'appsign');
    }
    
    protected function authParams()
    {
        return [
            'u'   => $this->appKey,
            'p'  => $this->appSecret,
            'k' =>  $this->appSign
        ];
    }
    
    /**
     * 发送信息
     * 
     * @see \Royalcms\Component\Sms\Contracts\SmsAgent::send()
     */
    public function send($mobile)
    {
        $url = self::HOST.'u='.$this->appKey.'&p='.md5($this->appSecret).'&m='.$mobile.'&c=【'.$this->appSign.'】'.$this->content;
        $ret = file_get_contents($url);
        return $this->transformerResponse('send',$ret);
    }
    
    /**
     * 查询账户余额
     */
    public function balance()
    {
        $url = 'http://api.smsbao.com/query?u='.$this->appKey.'&p='.md5($this->appSecret);
        $ret = file_get_contents($url);
        $rest = explode(",",$ret);
        $res['data']['num'] = $rest['1'];
        return $this->transformerResponse('balance',$res);
    }
    
    /**
     * 转换返回的信息处理
     * @param array $response
     * @return array $result
     * @return int $result[].code 返回0则成功,返回其它则错误
     * @return string $result[].msg 返回消息
     * @return string $result[].raw 接口返回的原生信息
     * @return array $result[].data 数据信息
     */
    public function transformerResponse($type,$response)
    {
        $result_arr = $this->statusStr;
        if($type=='send'){
            $result=new SendResponse();
            $result->setMsgid($response);
            $result->setCode($response);
            $result->setDescription($result_arr[$response]);
            $result->getDescription($result_arr[$response]);
        }else{
            $result=new BalanceResponse();
            $result->setBalance($response['data']['num']);
            $result->setCode($response);
            $result->setDescription($result_arr[$response]);
            $result->getDescription($result_arr[$response]);
        }
        
        return $result;
    }

3:接着在项目\content\plugins\创建文件:sms_smsbao\config.php

<?php
return array(
    'sms_code'      => 'sms_smsbao',
    
    'check_balance' => true,
    
    'forms' => array(
       array('name' => 'app_key',           'type' => 'text',       'value' => ''),
       array('name' => 'app_secret',        'type' => 'text',       'value' => ''),
        array('name' => 'app_sign',        'type' => 'text',       'value' => '')
    ),
);

4:接着在项目\content\plugins\sms_smsbao\ 创建sms_smsbao.class.php文件

<?php
 
 
defined('IN_ECJIA') or exit('No permission resources.');
 
 
use Ecjia\App\Sms\SmsAbstract;
 
 
class sms_smsbao extends SmsAbstract
{
    
    public function setConfig(array $config)
    {
        parent::setConfig($config);
        
        $this->setAgentConfig();
        
        $this->agent = royalcms('sms')->driver('smsbao');
    }
    
    public function setAgentConfig()
    {
        RC_Config::set('sms::sms.agents.smsbao.credentials', [
            'appKey' => $this->config['app_key'],
            'appSecret' => $this->config['app_secret'],
            'appsign' => $this->config['app_sign']
        ]);
    }
    
    
    /**
     * 获取插件代号
     *  
     * @see \Ecjia\System\Plugin\PluginInterface::getCode()
     */
    public function getCode()
    {
        return $this->loadConfig('sms_code');
    }
 
 
    /** 
     * 加载配置文件
     * 
     * @see \Ecjia\System\Plugin\PluginInterface::loadConfig()
     */
    public function loadConfig($key = null, $default = null)
    {        
        return $this->loadPluginData(RC_Plugin::plugin_dir_path(__FILE__) . 'config.php', $key, $default);
    }
 
 
    /** 
     * 加载语言包
     * 
     * @see \Ecjia\System\Plugin\PluginInterface::loadLanguage()
     */
    public function loadLanguage($key = null, $default = null)
    {
        $locale = RC_Config::get('system.locale');
        return $this->loadPluginData(RC_Plugin::plugin_dir_path(__FILE__) . '/languages/'.$locale.'/plugin.lang.php', $key, $default);
    }
}

5:接着在项目\content\plugins\sms_smsbao\创建sms_smsbao.php文件

<?php
 
 
/*
Plugin Name: 短信宝短信
Plugin URI: http://www.smsbao.com
Description: 使用短信宝短信渠道,发送验证码短信、订单通知等。
Author: ECJIA TEAM
Version: 1.0.0
Author URI: http://www.ecjia.com/
Plugin App: sms
*/
defined('IN_ECJIA') or exit('No permission resources.');
class plugin_sms_smsbao {
 
 
    public static function install() {
        $config = include(RC_Plugin::plugin_dir_path(__FILE__) . 'config.php');
        $param = array('file' => __FILE__, 'config' => $config);
        return RC_Api::api('sms', 'plugin_install', $param);
    }
 
 
 
 
    public static function uninstall() {
        $config = include(RC_Plugin::plugin_dir_path(__FILE__) . 'config.php');
        $param = array('file' => __FILE__, 'config' => $config);
        return RC_Api::api('sms', 'plugin_uninstall', $param);
    }
 
 
    public static function royalcms_sms_agent_filter($factories) {
        require_once RC_Plugin::plugin_dir_path(__FILE__) . 'SMSbao.php';
        
        $factories['smsbao'] = 'SMSbao';
        return $factories;
    }
 
 
}
 
 
Ecjia_PluginManager::extend('sms_smsbao', function() {
    require_once RC_Plugin::plugin_dir_path(__FILE__) . 'sms_smsbao.class.php';
    return new sms_smsbao();
});
 
 
RC_Plugin::register_activation_hook(__FILE__, array('plugin_sms_smsbao', 'install'));
RC_Plugin::register_deactivation_hook(__FILE__, array('plugin_sms_smsbao', 'uninstall'));
RC_Hook::add_filter('royalcms_sms_agent_filter', array( 'plugin_sms_smsbao', 'royalcms_sms_agent_filter' ));

6:接着在项目\content\plugins\sms_smsbao\创建SMSbao.php文件

<?php
 
 
use Royalcms\Component\Support\Arr;
use Royalcms\Component\Sms\Sms;
use Royalcms\Component\Sms\Contracts\SmsAgent;
use RC_Xml;
use RC_Error;
use Royalcms\Component\Sms\SendResponse;
use Royalcms\Component\Sms\BalanceResponse;
 
 
class SMSbao extends Sms implements SmsAgent
{
    
    const HOST      = 'http://api.smsbao.com/sms?';
   
    
    private $appKey;
    private $appSecret;
    private $appSign;
 
 
    private $statusStr = array(
        "0" => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );
    
    public function __construct($config)
    {
        $this->config = $config;
        $this->transformConfig();
    }
    
    public function transformConfig()
    {
        $credentials = Arr::pull($this->config, 'credentials');
        $this->appKey = Arr::pull($credentials, 'appKey');
        $this->appSecret = Arr::pull($credentials, 'appSecret');
        $this->appSign = Arr::pull($credentials, 'appsign');
    }
    
    protected function authParams()
    {
        return [
            'u'   => $this->appKey,
            'p'  => $this->appSecret,
            'k' =>  $this->appSign
        ];
    }
    
    /**
     * 发送信息
     * 
     * @see \Royalcms\Component\Sms\Contracts\SmsAgent::send()
     */
    public function send($mobile)
    {
        $url = self::HOST.'u='.$this->appKey.'&p='.md5($this->appSecret).'&m='.$mobile.'&c=【'.$this->appSign.'】'.$this->content;
        $ret = file_get_contents($url);
        return $this->transformerResponse('send',$ret);
 
 
    }
    
    /**
     * 查询账户余额
     */
    public function balance()
    {
        $url = 'http://api.smsbao.com/query?u='.$this->appKey.'&p='.md5($this->appSecret);
 
 
 
 
        $ret = file_get_contents($url);
        $rest = explode(",",$ret);
        $res['data']['num'] = $rest['1'];
        return $this->transformerResponse('balance',$res);
        
        
        
    }
    
    /**
     * 转换返回的信息处理
     * @param array $response
     * @return array $result
     * @return int $result[].code 返回0则成功,返回其它则错误
     * @return string $result[].msg 返回消息
     * @return string $result[].raw 接口返回的原生信息
     * @return array $result[].data 数据信息
     */
    public function transformerResponse($type,$response)
    {
        $result_arr = $this->statusStr;
        if($type=='send'){
            $result=new SendResponse();
            $result->setMsgid($response);
            $result->setCode($response);
            $result->setDescription($result_arr[$response]);
            $result->getDescription($result_arr[$response]);
        }else{
            $result=new BalanceResponse();
            $result->setBalance($response['data']['num']);
            $result->setCode($response);
            $result->setDescription($result_arr[$response]);
            $result->getDescription($result_arr[$response]);
        }
        
        return $result;
    }

7:最后在项目\content\plugins\sms_smsbao\创建新的文件夹languages\zh_CN\,名为:plugin.lang.php文件

<?php
 
 
defined('IN_ECJIA') or exit('No permission resources.');
 
 
/**
 * ECJIA 程序语言包
 */
 
 
return array(
    'app_key'  => '短信宝帐号:',
    'app_secret'   => '短信宝密码:',
    'app_sign'     => '短信签名:'
);

经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的ECJIA1.37.0商城系统短信宝插件,点击此链接 下载及查看安装流程。

注意事项(必读):
1、本站所展示的一切软件、教程和内容信息等资源均仅限用于学习和研究目的,请在下载后24小时内自觉删除;不保证其完整性及可用性,本平台不提供任何技术支持,若作商业用,请到原网站购买,由于未获授权而发生的侵权行为与本站无关。如有侵权请联系vip#mmeasy.cn(将#替换成@),我们将及时处理。
2、一切网盘资源请勿在线解压!在线解压会提示文件损坏或密码错误,特别注意若压缩包名带part1或z01这样的标识,则均为分卷压缩包,需要下载每个文件夹下的所有压缩包后,用WinRAR软件解压part1或zip即可释放当前文件夹下所有压缩包的内容!
3、如果链接失效,遇到资源失效可提交工单处理。
4、强烈建议在本站注册成为会员后再购买,游客购买只能短期保留记录,如超期或购买后自行清空浏览器缓存,将恢复购买前状态!
本文链接:https://www.mmeasy.cn/3404.html

0
分享海报
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码