天天看點

DsMall商城新增短信寶短信接口

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" rel="external nofollow"  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;
 
    }
 
 
}
           

經過上面的替換,短信寶的短信平台已經替換成功了,可以正常使用了。