大商创,是一款功能强大的B2B2C多用户商城系统,二次开发进行功能扩展很方便,代码清晰简洁,通俗易懂。我以替换接口为例为大家详细讲解如何进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信平台非常稳定,短信发送速度快,注册就送测试短信,推大家使用。
首先我们要更换后台的显示界面文件。打开模版文件,替换一下模版文件。打开项目/admin/templates/huyi_configure_list.dwt、/admin/templates/alidayu_configure_list.dwt、/admin/templates/alitongxin_configure_list.dwt这三个文件,都修改代码11~28行,代码如下:
<div class="explanation" id="explanation">
<div class="ex_tit">
<i class="sc_icon"></i><h4>操作提示</h4><span id="explanationZoom" title="收起提示"></span>
{if $open eq 1}
<div class="view-case">
<div class="view-case-tit"><i></i>查看教程</div>
<div class="view-case-info">
<a href="http://help.ecmoban.com/article-5211.html" target="_blank">短信宝短信配置使用说明</a>
</div>
</div>
{/if}
</div>
<ul>
<li class="li_color">注意:目前短信宝和阿里大鱼的短信模板是一致的,请慎重操作添加、编辑和删除.</li>
<li>列表页展示所有短信配置模板的信息列表。</li>
<li>每条信息可以进行编辑和删除操作。</li>
</ul>
</div>
经过替换后所有的页面都变成我们短信宝的了,第一步完成。接下来替换短信发送的接口代码,打开项目\includes\cls_sms.php文件,修改sms、send这两个方法,修改代码为:
function sms() {
/* 由于要包含init.php,所以这两个对象一定是存在的,因此直接赋值 */
$this->sms_name = $GLOBALS['_CFG']['sms_ecmoban_user'];
$this->sms_password = $GLOBALS['_CFG']['sms_ecmoban_password'];
$this->sms_sign = $GLOBALS['_CFG']['shop_name'];
}
// 发送短消息
function send($phones, $msg = '', $send_date = '', $send_num = 1, $sms_type='', $version='1.0', &$sms_error = '', $mobile_code = '') {
if($GLOBALS['_CFG']['sms_type'] == 0)
{
/* 检查发送信息的合法性 */
$contents = $this->get_contents($phones, $msg);
if (!$contents) {
return false;
}
/* 获取API URL */
$sms_url = "http://api.smsbao.com/sms?";
if (count($contents) > 1) {
foreach ($contents as $key => $val) {
$post_data = 'u=' . $this->sms_name . '&p=' . $this->sms_password . '&m=' . $val['phones'] . '&c=【' .$this->sms_sign.'】' . rawurlencode($val['content']); //密码可以使用明文密码或使用32位MD5加密
$get = file_get_contents($sms_url.$post_data);
$gets['code']=$get;
$gets['msg']='发送成功';
sleep(1);
}
} else {
$post_data = 'u=' . $this->sms_name . '&p=' . $this->sms_password . '&m=' . $contents[0]['phones'] . '&c=【' .$this->sms_sign.'】' . rawurlencode($contents[0]['content']);
$get = file_get_contents($sms_url.$post_data);
$gets['code'] = $get;
$gets['msg'] = '发送成功';
}
//print_r($gets);exit; //开启调试模式
if ($get == '0') {
return true;
}
else {
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
);
$sms_error = $phones .$statusStr["$get"];
$this->logResult($sms_error);
return false;
}
}
else if($GLOBALS['_CFG']['sms_type'] >=1)
{
$msg = $this->get_usser_sms_msg($msg);
//阿里大鱼短信接口
if(!empty($msg['sms_value'])){
$smsParams = array(
'mobile_phone' => $phones,
'code' => $msg['code']
);
$send_time = $msg['sms_value'];
}else{
$smsParams = array(
'mobile_phone' => $phones,
'code' => $msg['code'],
'product' => $msg['product']
);
$send_time = 'sms_signin';
}
$result = sms_ali($smsParams, $send_time); //阿里大鱼短信变量传值,发送时机传值
$resp = $GLOBALS['ecs']->ali_yu($result);
if($resp->code == 0){
return true;
}else{
if($resp->sub_msg){
$sms_error = $phones . $resp->sub_msg;
}else{
$sms_error = $phones .":". $resp->msg;
}
$this->logResult($sms_error);
return false;
}
}
}
修改配置文件,打开项目/languages/zh_cn/admin/common.php文件,修改470行左右,修改代码如下:
$_LANG['huyi_configure'] = '短信宝短信';
打开项目/languages/zh_cn/admin/shop_config.php文件,修改85、206~208行左右,修改代码如下:
85 $_LANG['cfg_range']['sms_type']['0'] = '短信宝';
206 $_LANG['cfg_name']['sms_ecmoban_user'] = '短信宝短信接口用户名';
207 $_LANG['cfg_name']['sms_ecmoban_password'] = '短信宝短信接口密码';
208 $_LANG['cfg_name']['sms_sign'] = '短信签名';
pc端的我们已经替换成功了,我们进行手机端的替换,打开项目\mobile\app\helpers\base_helper.php文件,修改send_sms方法,修改代码为:
function send_sms($mobile, $send_time = '', $content)
{
$sms_type = ['ihuyi', 'alidayu', 'aliyun'];
$config = array(
'driver' => 'sms',
'driverConfig' => array(
'sms_type' => $sms_type[$GLOBALS['_CFG']['sms_type']], // 短信类型对应 $sms_type 数组索引
'ihuyi' => array(
'sms_name' => $GLOBALS['_CFG']['sms_ecmoban_user'],
'sms_password' => $GLOBALS['_CFG']['sms_ecmoban_password'],
'sms_sign' => $GLOBALS['_CFG']['shop_name']
),
'alidayu' => array(
'ali_appkey' => $GLOBALS['_CFG']['ali_appkey'],
'ali_secretkey' => $GLOBALS['_CFG']['ali_secretkey']
),
'aliyun' => array(
'access_key_id' => $GLOBALS['_CFG']['access_key_id'],
'access_key_secret' => $GLOBALS['_CFG']['access_key_secret']
)
)
);
// 发送消息
$sms = new \App\Notifications\Send($config);
if ($sms->push($mobile, $send_time, $content) === true) {
return true;
} else {
return $sms->getError();
}
}
接下来修改发送的接口类,打开项目\mobile\app\modules\notifications\sms\driver\Ihuyi.php文件,修改以下代码:
protected $sms_api = "http://api.smsbao.com/sms?";
private function send()
{
foreach ($this->phones as $mobile) {
$post_data = array(
'u' => $this->config['sms_name'],
'p' => $this->config['sms_password'],
'm' => $mobile,
'c' => '【'.$this->config['sms_sign'].'】'.$this->content
);
$res = Http::doPost($this->sms_api, $post_data);
//print_r($data);exit; //开启调试模式 TODO 此处暂时只能发送一次
if ($data['SubmitResult']['code'] == 0) {
return true;
} else {
$this->errorInfo = $data['SubmitResult']['msg'];
logResult($this->errorInfo, 'sms');
return false;
}
}
}
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:
报备一下短信宝的VIP模版,这样就可以走短信宝的优质通道,并且免审核了,短信内容3~5秒就可送达。
来源:CSDN
作者:mindev
链接:https://blog.csdn.net/mindev/article/details/78438275