DSMall基于国内优秀开源框架THinkPHP、打造出的一套开源的B2B2C电子商务系统。小编带着大家一起开发DSMall商城2.5.3的短信宝插件接口。我们使用的短信接口是短信宝短信群发平台的短信接口,短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
首先我们打开项目application\admin\view\message\mobile.html文件,替换17~38行代码:
<tr class="noborder">
<td class="required w120">短信宝账号</td>
<td class="vatop rowform">
<input type="text" name="smsbao_user" id="smsbao_user" value="{$list_config.smsbao_user}" class="w200"/>
<a href="http://www.smsbao.com/reg" target="_blank" class="btn btn-blue btn-mini">立即注册</a>
</td>
<td class="vatop tips"></td>
</tr>
<tr class="noborder">
<td class="required w120">短信宝密码</td>
<td class="vatop rowform">
<input type="text" name="smsbao_pass" id="smsbao_pass" value="{$list_config.smsbao_pass}" class="w200"/>
</td>
<td class="vatop tips"></td>
</tr>
<tr class="noborder">
<td class="required w120">短信宝签名</td>
<td class="vatop rowform">
<input type="text" name="smsbao_sign" id="smsbao_sign" value="{$list_config.smsbao_sign}" class="w200"/>
</td>
<td class="vatop tips"></td>
</tr>
接着我们打开项目application\admin\controller\Message.php文件,替换mobile方法:
public function mobile() {
$config_model = model('config');
if (!(request()->isPost())) {
$list_config = $config_model->getConfigList();
$smscf_wj_num = '';
if(!empty($list_config['smsbao_user'])&&!empty($list_config['smsbao_pass'])){
$arr = http_request('http://www.smsbao.com/query?u='.$list_config['smsbao_user'].'&p='.md5($list_config['smsbao_pass']),'get');
$arr = explode(',', $arr);
$smscf_wj_num = $arr[1];
}
$this->assign('smscf_wj_num', $smscf_wj_num);
$this->assign('list_config', $list_config);
$this->setAdminCurItem('mobile');
return $this->fetch('mobile');
} else {
$update_array = array();
$update_array['smsbao_user'] = input('post.smsbao_user');
$update_array['smsbao_pass'] = input('post.smsbao_pass');
$update_array['smsbao_sign'] = input('post.smsbao_sign');
$update_array['smscf_wj_username'] = input('post.smscf_wj_username');
$update_array['smscf_wj_key'] = input('post.smscf_wj_key');
$update_array['sms_register'] = input('post.sms_register');
$update_array['sms_login'] = input('post.sms_login');
$update_array['sms_password'] = input('post.sms_password');
$result = $config_model->editConfig($update_array);
if ($result === true) {
$this->log(lang('ds_edit') . lang('mobile_set'), 1);
$this->success(lang('ds_common_save_succ'));
} else {
$this->log(lang('ds_edit') . lang('mobile_set'), 0);
$this->error(lang('ds_common_save_fail'));
}
}
}
接着我们打开项目application\common\model\Smslog.php文件,替换sendSms方法:
function sendSms($smslog_phone,$smslog_msg,$smslog_type='',$smslog_captcha='',$member_id='0',$member_name='')
{
$smsbao_sign = config('smsbao_sign');
$cont=explode('】', $smslog_msg);
$smslog_msg = "【".$smsbao_sign."】".($cont[1]);
//通过手机号判断是否允许发送短信
$begin_add_time = strtotime(date('Y-m-d', TIMESTAMP));
$end_add_time = strtotime(date('Y-m-d', TIMESTAMP)) + 24 * 3600;
//同一IP 每天只能发送20条短信
$condition = array();
$condition['smslog_ip'] = request()->ip();
$condition['smslog_smstime'] = array('between', array($begin_add_time, $end_add_time));
if ($this->getSmsCount($condition) > 20) {
return array('state'=>FALSE,'code'=>10001,'message'=>'同一IP地址一天内只能发送20条短信,请勿多次获取动态码!');
}
//同一手机号,60秒才能提交发送一次
$condition = array();
$condition['smslog_phone'] = $smslog_phone;
$condition['smslog_smstime'] = array('between', array(TIMESTAMP-30, TIMESTAMP));
if ($this->getSmsCount($condition) > 0) {
return array('state'=>FALSE,'code'=>10001,'message'=>'同一手机30秒后才能再次发送短信,请勿多次获取动态码!');
}
//同一手机号,每天只能发送5条短信
$condition = array();
$condition['smslog_phone'] = $smslog_phone;
$condition['smslog_smstime'] = array('between', array($begin_add_time, $end_add_time));
if ($this->getSmsCount($condition) > 5) {
return array('state'=>FALSE,'code'=>10001,'message'=>'同一手机一天内只能发送5条短信,请勿多次获取动态码!');
}
//通过手机号获取现绑定的客户信息
if(empty($member_id)||empty($member_name)){
//通过手机号查询用户名
$member = model('member')->getMemberInfo(array('member_mobile' => $smslog_phone));
$member_id = isset($member['member_id'])?$member['member_id']:'0';
$member_name = isset($member['member_name'])?$member['member_name']:'';
}
$sms = new \sendmsg\Sms();
$result = $sms->send($smslog_phone, $smslog_msg);
if ($result) {
$log['smslog_phone'] = $smslog_phone;
$log['smslog_captcha'] = $smslog_captcha;
$log['smslog_ip'] = request()->ip();
$log['smslog_msg'] = $smslog_msg;
$log['smslog_type'] = $smslog_type;
$log['smslog_smstime'] = TIMESTAMP;
$log['member_id'] = $member_id;
$log['member_name'] = $member_name;
$result = $this->addSms($log);
if($result>=0){
return array('state'=>TRUE,'code'=>10000,'message'=>'');
}else{
return array('state'=>FALSE,'code'=>10001,'message'=>'手机短信发送失败');
}
}else{
return array('state'=>FALSE,'code'=>10001,'message'=>'手机短信发送失败');
}
}
最后我们打开项目extend\sendmsg\Sms.php文件,替换mysend_sms方法:
private function mysend_sms($mobile, $content)
{
$smsbao_user = urlencode(config('smsbao_user')); // 户名
$smsbao_pwd = urlencode(config('smsbao_pass')); // 这里填接口安全密钥
$smsbao_sign = urlencode(config('smsbao_sign'));
if (!$mobile || !$content || !$smsbao_user || !$smsbao_pwd || !$smsbao_sign)
return false;
if (is_array($mobile)) {
$mobile = implode(",", $mobile);
}
$mobile=urlencode($mobile);
$content=urlencode($content);
$url = 'http://api.smsbao.com/sms?u='.$smsbao_user.'&p='.md5($smsbao_pwd).'&m='.$mobile.'&c='.$content;
if (function_exists('file_get_contents')) {
$res = file_get_contents($url);
}
else {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$res = curl_exec($ch);
curl_close($ch);
}
if ($res == 0) {
return true;
}
return false;
}
}
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。