天天看點

niushop商城系統短信接口替換

Niushop是一款基于thinkphp5.0 MVC+service架構的開源軟體,友善二次開發與內建,今天我就來分享一下,如何進行二次開發。我以替換短信接口為例,一步一步的手把手教大家開發過程,我們做示範的短信平台是短信寶(http://www.smsbao.com)短信平台,我們公司和短信寶平台合作好幾年了,他們的平台非常穩定,而且快速,注冊還送免費測試短信,大家可以在短信寶平台注冊一個賬号,進行測試。

首先,我們先更換背景的顯示界面檔案。打開模闆檔案,替換一下模闆檔案。打開項目/template/admin/Config/messageConfig.html,替換的代碼從138行~160行,代碼如下圖所示:

<div class="set-style">
    <dl>
        <dt>是否開啟:</dt>
        <dd style="width:5%;">

                <input id="sms_is_use" type="checkbox" class="checkbox" {if condition="$mobile_message.is_use=='1'"}checked="checked"{/if}/>
        </dd>
        <dd style="width:20%;color:#666;" class="showmessage">目前使用短信寶短信配置</dd>
    </dl>
    <dl>
        <dt>短信寶帳号:</dt>
        <dd>
            <input id="app_key" type="text" value="{$mobile_message['value']['appKey']}" /> 立即<a href="http://www.smsbao.com" target="_blank">免費注冊</a>短信寶賬号
        </dd>
    </dl>
    <dl>
        <dt>短信寶密碼:</dt>
        <dd>
            <input id="secret_key" type="password" value="{$mobile_message['value']['secretKey']}" />
        </dd>
    </dl>
    <button class="edit_button" onclick="setConfigAjax();">送出</button>
</div>
           

打開項目/template/admin/Config/notifySmsTemplate.html,替換的代碼從191行~203行,代碼如下圖所示:

<div class="right_main">
    <div class="main-top">您正在編輯<a href="javascript:;" id="update_nameid"></a>通知模闆</div>
    <div class="qianming">
          <div style="width:75px;float:left;">模闆:
    </div>
    <input type="text" name="" id="template_titleid" value="{$template_select.template_title}" style="margin-right: 15px;"/></div>
    <div class="qianming">
<input type="text" name="" id="signNameid" style="margin-right: 15px;" value="{$template_select.sign_name  }"/>
</div>
    <div class="bl">
    <div style="width:75px;float:left;">可用變量:</div>
    <div style="width:88%;float: left;font-size:13px;" id="user_variable">
    {foreach name="template_item_list" item="item_obj"}
    {$item_obj.replace_name}({$item_obj.item_name}),
        {/foreach}
    </div>
    </div>
</div>
           

經過替換後,所有的顯示都變成短信寶短信平台的了。第一步完成。接下來替換發送短信的業務代碼。修改項目/data/extend/hook/Notify.php檔案,代碼如下:

<?php
namespace data\extend\hook;

use data\model\WebSiteModel;
use data\model\UserModel;
use data\model\ConfigModel;
use data\model\NoticeTemplateModel;
use data\model\NsOrderGoodsModel;
use data\model\NsOrderModel;
use phpDocumentor\Reflection\Types\This;
use data\model\NsOrderGoodsExpressModel;
class Notify
{
    public $result=array(
        "code"=>,
        "message"=>"success",
        "param"=>""
    );
    /**
     * 郵件的配置資訊
     * @var unknown
    */
    public $email_is_open=;
    public $email_host="";
    public $email_port="";
    public $email_addr="";
    public $email_id="";
    public $email_pass="";
    /**
     * 短信的配置資訊
     * @var unknown
     */
    public $mobile_is_open;
    public $appKey="";
    public $secretKey="";
    public $freeSignName="";

    public $shop_name;

    public $ali_use_type=;
    /**
     * 得到系統通知的配置資訊
     * @param unknown $shop_id
     */
    private function getShopNotifyInfo($shop_id){

        $website_model=new WebSiteModel();
        $website_obj=$website_model->getInfo("1=1", "title");
        if(empty($website_obj)){
            $this->shop_name="NiuShop開源商城";
        }else{
            $this->shop_name=$website_obj["title"];
        }

        $config_model=new ConfigModel();
        #檢視郵箱是否開啟
        $email_info=$config_model->getInfo(["instance_id"=>$shop_id, "`key`"=>"EMAILMESSAGE"], "*");
        if(!empty($email_info)){
            $this->email_is_open=$email_info["is_use"];
            $value=$email_info["value"];
            if(!empty($value)){
                $email_array=json_decode($value, true);
                $this->email_host=$email_array["email_host"];
                $this->email_port=$email_array["email_port"];
                $this->email_addr=$email_array["email_addr"];
                $this->email_id=$email_array["email_id"];
                $this->email_pass=$email_array["email_pass"];
            }
        }
        $mobile_info=$config_model->getInfo(["instance_id"=>$shop_id, "`key`"=>"MOBILEMESSAGE"], "*");
        if(!empty($mobile_info)){
            $this->mobile_is_open=$mobile_info["is_use"];
            $value=$mobile_info["value"];
            if(!empty($value)){
                $mobile_array=json_decode($value, true);
                $this->appKey=$mobile_array["appKey"];
                $this->secretKey=$mobile_array["secretKey"];
                $this->freeSignName=$mobile_array["freeSignName"];
                $this->ali_use_type=$mobile_array["user_type"];
                if(empty($this->ali_use_type)){
                    $this->ali_use_type=;
                }
            }
        }
    }


    public function sendSMS($appkey, $secret, $signName, $smsParam, $send_mobile, $template_code)
    {
       $content = strtr($template_code,$smsParam);//替換模闆中的變量
       $contents = '【'.$signName.'】'.$content;
       $url = 'http://api.smsbao.com/sms?u='.$appkey.'&p='.md5($secret).'&m='.$send_mobile.'&c='.$contents;
       $result = file_get_contents($url);
       return $result;

    }

    /**
     * 查詢模闆的資訊
     * @param unknown $shop_id
     * @param unknown $template_code
     * @param unknown $type
     * @return unknown
     */
    private function getTemplateDetail($shop_id, $template_code, $type){
       $template_model=new NoticeTemplateModel();
       $template_obj=$template_model->getInfo(["instance_id"=>$shop_id, "template_type"=>$type, "template_code"=>$template_code]);
       return $template_obj;
    }
    /**
     * 處理阿裡大于 的傳回資料
     * @param unknown $result
     */
    private function dealAliSmsResult($result){
        $deal_result=array();
        try {
            if($this->ali_use_type==){
                #舊使用者發送
                if(!empty($result)){
                    if(!isset($result->result)){
                        $result=json_decode(json_encode($result), true);
                        #發送失敗
                        $deal_result["code"]=$result["code"];
                        $deal_result["message"]=$result["msg"];
                    }else{
                        #發送成功
                        $deal_result["code"]=;
                        $deal_result["message"]="發送成功";
                    }
                }
            }else{
                #新使用者發送
                if(!empty($result)){
                    if($result->Code=="OK"){
                        #發送成功
                        $deal_result["code"]=;
                        $deal_result["message"]="發送成功";
                    }else{
                        #發送失敗
                        $deal_result["code"]=-;
                        $deal_result["message"]=$result->Message;
                    }
                }
          }
        } catch (\Exception $e) {
            $deal_result["code"]=-;
            $deal_result["message"]="發送失敗!";
        }

        return $deal_result;
    }
    /**
     * 使用者注冊成功後
     * @param string $params
     */
    public function registAfter($params=null){
        /**
         * 店鋪id
         */
        $shop_id=$params["shop_id"];
        #查詢系統配置資訊
        $this->getShopNotifyInfo();
        /**
         * 使用者id
         */
        $user_id=$params["user_id"];

        $user_model=new UserModel();
        $user_obj=$user_model->get($user_id);
        $mobile="";
        $user_name="";
        $email="";
        if(empty($user_obj)){
            $user_name="使用者";
        }else{
            $user_name=$user_obj["user_name"];
            $mobile=$user_obj["user_tel"];
            $email=$user_obj["user_email"];
        }
        #短信驗證
        if(!empty($mobile) && $this->mobile_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "after_register", "sms");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $sms_params=array(
                    "shop_name"=>$this->shop_name,
                    "user_name"=>$user_name
                );
                $result=$this->sendSMS($this->appKey, $this->secretKey,
                    $template_obj["sign_name"], $sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
                $this->result["code"]=$result->code;
                $this->result["message"]=$result->msg;
            }else{
                $this->result["code"]=-;
                $this->result["message"]="商家沒開啟短信模闆!";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="商家沒開啟短信模闆!";
        }
        #郵箱驗證
        if(!empty($email) && $this->email_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "after_register", "email");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $content=$template_obj["template_content"];
                $content=str_replace("{商場名稱}", $this->shop_name, $content);
                $content=str_replace("{使用者名稱}", $user_name, $content);
                $result=emailSend($this->email_host, $this->email_id, $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                if($result == '0'){
                    $this->result["code"]=;
                    $this->result["message"]="發送成功!";
                }else{
                    $this->result["code"]=$result;
                    $this->result["message"]="發送失敗!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="商家沒開啟郵箱驗證";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="商家沒開啟郵箱驗證";
        }
        return $this->result;
    }
    /**
     * 注冊短信驗證
     * @param string $params
     */
    public function registBefor($params=null){
        $rand = rand(,);
        $mobile=$params["mobile"];
        $shop_id=$params["shop_id"];
        #查詢系統配置資訊
        $this->getShopNotifyInfo($shop_id);
        $result="";
        if(!empty($mobile) && $this->mobile_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "register_validate", "sms");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $sms_params=array(

            );
                $this->result["param"]=$rand;
                if(!empty($this->appKey) && !empty($this->secretKey) && !empty($template_obj["sign_name"]) && !empty($template_obj["template_title"])){
                      $sms_params=array(
                    "number"=>$rand."",
                );

                    $result=$this->sendSMS($this->appKey, $this->secretKey,
                        $template_obj["sign_name"], $sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
                    if ($result == '0') {
                    $this->result["code"]=;
                    $this->result["message"]="發送成功!";
                     $this->result["param"]=$rand;
                    }else{
                    $this->result["code"]= $result;
                    $this->result["message"]="發送失敗!";
                     $this->result["param"]=$rand;

                    }

                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="短信配置資訊有誤!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="短信通知模闆有誤!";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="店家沒有開啟短信驗證";
        }
        return $this->result;
    }
    /**
     * 注冊郵箱驗證
     * 已測試
     * @param string $params
     */
    public function registEmailValidation($params=null){
        $rand = rand(,);
        $email=$params["email"];
        $shop_id=$params["shop_id"];
        #查詢系統配置資訊
        $this->getShopNotifyInfo($shop_id);
        if(!empty($email) && $this->email_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "register_validate", "email");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $content=$template_obj["template_content"];
                $content=str_replace("{驗證碼}", $rand, $content);
                if(!empty($this->email_host) && !empty($this->email_id) && !empty($this->email_pass) && !empty($this->email_addr)){
                    $result=emailSend($this->email_host, $this->email_id, $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                    $this->result["param"]=$rand;
                    if($result){
                        $this->result["code"]=;
                        $this->result["message"]="發送成功!";
                    }else{
                        $this->result["code"]=-;
                        $this->result["message"]="發送失敗!";
                    }
                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="郵箱配置資訊有誤!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="配置郵箱注冊驗證模闆有誤!";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="店家沒有開啟郵箱驗證";
        }
        return $this->result;

    }
    /**
     * 訂單發貨
     * @param string $params
     */
    public function orderDelivery($params=null){
        #查詢系統配置資訊
        $this->getShopNotifyInfo();
        $order_goods_ids=$params["order_goods_ids"];
        $order_goods_str=explode(",", $order_goods_ids);
        $result="";
        $user_name="";
        if(count($order_goods_str)>){
            $order_goods_id=$order_goods_str[];
            $order_goods_model=new NsOrderGoodsModel();
            $order_goods_obj=$order_goods_model->get($order_goods_id);
            $shop_id=$order_goods_obj["shop_id"];
            $order_id=$order_goods_obj["order_id"];
            $order_model=new NsOrderModel();
            $order_obj=$order_model->get($order_id);
            $user_name=$order_obj["receiver_name"];
            $buyer_id=$order_obj["buyer_id"];
            $goods_name=$order_goods_obj["goods_name"];
            $goods_sku=$order_goods_obj["sku_name"];
            $order_no=$order_obj["out_trade_no"];
            $order_money=$order_obj["pay_money"];
            $goods_money=$order_goods_obj["goods_money"];
            $mobile=$order_obj["receiver_mobile"];
            $goods_express_model=new NsOrderGoodsExpressModel();
            $express_obj=$goods_express_model->getInfo(["order_id"=>$order_id, "order_goods_id_array"=>$order_goods_ids], "*");
            $sms_params=array(
                "shop_name"=>$this->shop_name,
                "user_name"=>$user_name,
                "goods_name"=>$goods_name,
                "goods_sku"=>$goods_sku,
                "order_no"=>$order_no,
                "order_money"=>$order_money,
                "goods_money"=>$goods_money,
                "express_company"=>$express_obj["express_name"],
                "express_no"=>$express_obj["express_no"]
            );
            #短信發送
            if(!empty($mobile) && $this->mobile_is_open==){
                $template_obj=$this->getTemplateDetail($shop_id, "order_deliver", "sms");
                if(!empty($template_obj) && $template_obj["is_enable"]==){
                    $result=$this->sendSMS($this->appKey, $this->secretKey, $template_obj["sign_name"], $sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
                }
            }
            // 郵件發送
            $user_model = new UserModel();
            $user_obj = $user_model->get($buyer_id);
            if (! empty($user_obj)) {
                $email = $user_obj["user_email"];
                if (! empty($email) && $this->email_is_open == ) {
                    $template_obj = $this->getTemplateDetail($shop_id, "order_deliver", "email");
                    if (! empty($template_obj) && $template_obj["is_enable"] == ) {
                        $content = $template_obj["template_content"];
                        $content = str_replace("{商場名稱}", $this->shop_name, $content);
                        $content = str_replace("{使用者名稱}", $user_name, $content);
                        $content = str_replace("{商品名稱}", $goods_name, $content);
                        $content = str_replace("{商品規格}", $goods_sku, $content);
                        $content = str_replace("{主訂單号}", $order_no, $content);
                        $content = str_replace("{訂單金額}", $order_money, $content);
                        $content = str_replace("{商品金額}", $goods_money, $content);
                        $content = str_replace("{物流公司}", $express_obj["express_name"], $content);
                        $content = str_replace("{快遞編号}", $express_obj["express_no"], $content);
                        $result=emailSend($this->email_host, $this->email_id,
                            $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                    }
                }
            }
        }
    }
    /**
     * 訂單确認
     * @param string $params
     */
    public function orderComplete($params=null){
        #查詢系統配置資訊
        $this->getShopNotifyInfo();

        $order_id=$params["order_id"];
        $order_model=new NsOrderModel();
        $order_obj=$order_model->get($order_id);
        $shop_id=$order_obj["shop_id"];
        $buyer_id=$order_obj["buyer_id"];
        $user_name=$order_obj["receiver_name"];
        $order_no=$order_obj["out_trade_no"];
        $order_money=$order_obj["pay_money"];
        $mobile=$order_obj["receiver_mobile"];
        $sms_params=array(
            "shop_name"=>$this->shop_name,
            "user_name"=>$user_name,
            "order_no"=>$order_no,
            "order_money"=>$order_money
        );
            // 短信發送
        if (! empty($mobile) && $this->mobile_is_open == ) {
            $template_obj = $this->getTemplateDetail($shop_id, "confirm_order", "sms");
            if (! empty($template_obj) && $template_obj["is_enable"] == ) {
                $result = $this->sendSMS($this->appKey, $this->secretKey, $template_obj["sign_name"],$sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
            }
        }
        // 郵件發送
        $user_model = new UserModel();
        $user_obj = $user_model->get($buyer_id);
        if (! empty($user_obj)) {
            $email = $user_obj["user_email"];
            if (! empty($email) && $this->email_is_open == ) {
                $template_obj = $this->getTemplateDetail($shop_id, "confirm_order", "email");
                if (! empty($template_obj) && $template_obj["is_enable"] == ) {
                    $content = $template_obj["template_content"];
                    $content = str_replace("{商場名稱}", $this->shop_name, $content);
                    $content=str_replace("{使用者名稱}", $user_name, $content);
                    $content=str_replace("{主訂單号}", $order_no, $content);
                    $content=str_replace("{訂單金額}", $order_money, $content);
                    $result=emailSend($this->email_host, $this->email_id,
                        $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                }
            }
        }
    }
    /**
     * 訂單付款成功
     * @param string $params
     */
    public function orderPay($params=null){
        #查詢系統配置資訊
        $this->getShopNotifyInfo();

        $order_id=$params["order_id"];
        $order_model=new NsOrderModel();
        $order_obj=$order_model->get($order_id);
        $shop_id=$order_obj["shop_id"];
        $buyer_id=$order_obj["buyer_id"];
        $user_name=$order_obj["receiver_name"];
        $order_no=$order_obj["out_trade_no"];
        $order_money=$order_obj["pay_money"];
        $mobile=$order_obj["receiver_mobile"];
        $goods_money=$order_obj["goods_money"];
        $sms_params=array(
            "shop_name"=>$this->shop_name,
            "user_name"=>$user_name,
            "order_no"=>$order_no,
            "order_money"=>$order_money,
            "goods_money"=>$goods_money
        );
        #短信發送
        if(!empty($mobile) && $this->mobile_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "pay_success", "sms");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $result=$this->sendSMS($this->appKey, $this->secretKey,
                    $template_obj["sign_name"],$sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
            }
        }
        #郵件發送
        $user_model=new UserModel();
        $user_obj=$user_model->get($buyer_id);
        if(!empty($user_obj)){
            $email=$user_obj["user_email"];
            if(!empty($email) && $this->email_is_open==){
                $template_obj=$this->getTemplateDetail($shop_id, "pay_success", "email");
                if(!empty($template_obj) && $template_obj["is_enable"]==){
                    $content=$template_obj["template_content"];
                    $content=str_replace("{商場名稱}", $this->shop_name, $content);
                    $content=str_replace("{使用者名稱}", $user_name, $content);
                    $content=str_replace("{主訂單号}", $order_no, $content);
                    $content=str_replace("{訂單金額}", $order_money, $content);
                    $content=str_replace("{商品金額}", $goods_money, $content);
                    $result=emailSend($this->email_host, $this->email_id,
                                $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                }
            }
        }
    }
    /**
     * 訂單建立成功
     * @param string $params
     */
    public function orderCreate($params=null){
        #查詢系統配置資訊
        $this->getShopNotifyInfo();
        $order_id=$params["order_id"];
        $order_model=new NsOrderModel();
        $order_obj=$order_model->get($order_id);
        $shop_id=$order_obj["shop_id"];
        $buyer_id=$order_obj["buyer_id"];
        $user_name=$order_obj["receiver_name"];
        $order_no=$order_obj["out_trade_no"];
        $order_money=$order_obj["pay_money"];
        $mobile=$order_obj["receiver_mobile"];
        $goods_money=$order_obj["goods_money"];
        $sms_params= array(
            "shop_name"=>$this->shop_name,
            "user_name"=>$user_name,
            "order_no"=>$order_no,
            "order_money"=>$order_money,
            "goods_money"=>$goods_money
        );
        #短信發送
        if(!empty($mobile) && $this->mobile_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "create_order", "sms");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $result=$this->sendSMS($this->appKey, $this->secretKey,
                    $template_obj["sign_name"],$sms_params, $mobile, $template_obj["template_title"], $this->ali_use_type);
            }
        }
            // 郵件發送
        $user_model = new UserModel();
        $user_obj = $user_model->get($buyer_id);
        if (! empty($user_obj)) {
            $email = $user_obj["user_email"];
            if (! empty($email) && $this->email_is_open == ) {
                $template_obj = $this->getTemplateDetail($shop_id, "create_order", "email");
                if (! empty($template_obj) && $template_obj["is_enable"] == ) {
                    $content = $template_obj["template_content"];
                    $content = str_replace("{商場名稱}", $this->shop_name, $content);
                    $content = str_replace("{使用者名稱}", $user_name, $content);
                    $content = str_replace("{主訂單号}", $order_no, $content);
                    $content = str_replace("{訂單金額}", $order_money, $content);
                    $content = str_replace("{商品金額}", $goods_money, $content);
                    $result = emailSend($this->email_host, $this->email_id, $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                }
            }
        }
    }
    /**
     * 找回密碼
     * @param string $params
     * @return multitype:number string
     */
    public function forgotPassword($params=null){
        $send_type=$params["send_type"];
        $send_param=$params["send_param"];
        $shop_id=$params["shop_id"];
        $this->getShopNotifyInfo($shop_id);
        $rand = rand(,);
        $template_obj=$this->getTemplateDetail($shop_id, "forgot_password", $send_type);
        if($send_type=="email"){
            #郵箱驗證
            if($this->email_is_open==){
                if(!empty($template_obj) && $template_obj["is_enable"]==){
                    #發送
                    $content=$template_obj["template_content"];
                    $content=str_replace("{驗證碼}", $rand, $content);
                    $result=emailSend($this->email_host, $this->email_id, $this->email_pass, $this->email_addr, $send_param, $template_obj["template_title"], $content);
                    $this->result["param"]=$rand;
                    if($result){
                        $this->result["code"]=;
                        $this->result["message"]="發送成功!";
                    }else{
                        $this->result["code"]=-;
                        $this->result["message"]="發送失敗!";
                    }
                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="商家沒有設定找回密碼的模闆!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="商家沒開啟郵箱驗證!";
            }
        }else{
            #短信驗證
            if($this->mobile_is_open==){
                if(!empty($template_obj) && $template_obj["is_enable"]==){
                    #發送
                    $sms_params=array(
                    "number"=>$rand.""
                        );
                        $result=$this->sendSMS($this->appKey, $this->secretKey,
                            $template_obj["sign_name"],$sms_params, $send_param, $template_obj["template_title"], $this->ali_use_type);

                         if ($result == '0') {
                    $this->result["code"]=;
                    $this->result["message"]="發送成功!";
                     $this->result["param"]=$rand;
                    }else{
                    $this->result["code"]= $result;
                    $this->result["message"]="發送失敗!";
                     $this->result["param"]=$rand;

                    }
                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="商家沒有設定找回密碼的短信模闆!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="商家沒開啟短信驗證!";
            }
        }
        return $this->result;
    }
    /**
     * 使用者綁定手機号
     * @param string $params
     */
    public function bindMobile($params=null){
        $rand = rand(,);
        $mobile=$params["mobile"];
        $shop_id=$params["shop_id"];
        $user_id=$params["user_id"];
        #查詢系統配置資訊
        $this->getShopNotifyInfo($shop_id);
        if(!empty($mobile) && $this->mobile_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "bind_mobile", "sms");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $user_model=new UserModel();
                $user_obj=$user_model->get($user_id);
                $sms_params=array(
                    "number"=>$rand."",
                    "user_name"=>$user_obj["user_name"]
                );
                $this->result["param"]=$rand;
                if(!empty($this->appKey) && !empty($this->secretKey) && !empty($template_obj["sign_name"]) && !empty($template_obj["template_title"])){
                    $result=$this->sendSMS($this->appKey, $this->secretKey,
                                    $template_obj["sign_name"], json_encode($sms_params), $mobile, $template_obj["template_title"], $this->ali_use_type);

                     if ($result == '0') {
                    $this->result["code"]=;
                    $this->result["message"]="發送成功!";
                     $this->result["param"]=$rand;
                    }else{
                    $this->result["code"]= $result;
                    $this->result["message"]="發送失敗!";
                     $this->result["param"]=$rand;

                    }
                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="短信配置資訊有誤!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="短信通知模闆有誤!";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="店家沒有開啟短信驗證";
        }
        return $this->result;
    }
    /**
     * 使用者綁定郵箱
     * @param string $params
     */
    public function bindEmail($params=null){
        $rand = rand(,);
        $email=$params["email"];
        $shop_id=$params["shop_id"];
        $user_id=$params["user_id"];
        #查詢系統配置資訊
        $this->getShopNotifyInfo($shop_id);
        if(!empty($email) && $this->email_is_open==){
            $template_obj=$this->getTemplateDetail($shop_id, "bind_email", "email");
            if(!empty($template_obj) && $template_obj["is_enable"]==){
                $user_model=new UserModel();
                $user_obj=$user_model->get($user_id);
                $content=$template_obj["template_content"];
                $content=str_replace("{驗證碼}", $rand, $content);
                $content=str_replace("{使用者名稱}", $user_obj["user_name"], $content);
                $this->result["param"]=$rand;
                if(!empty($this->email_host) && !empty($this->email_id) && !empty($this->email_pass) && !empty($this->email_addr)){
                    $result=emailSend($this->email_host, $this->email_id, $this->email_pass, $this->email_addr, $email, $template_obj["template_title"], $content);
                    if($result){
                        $this->result["code"]=;
                        $this->result["message"]="發送成功!";
                    }else{
                        $this->result["code"]=-;
                        $this->result["message"]="發送失敗!";
                    }
                }else{
                    $this->result["code"]=-;
                    $this->result["message"]="郵箱配置資訊有誤!";
                }
            }else{
                $this->result["code"]=-;
                $this->result["message"]="郵箱通知模闆有誤!";
            }
        }else{
            $this->result["code"]=-;
            $this->result["message"]="店家沒有開啟郵箱驗證";
        }
        return $this->result;
    }
}

?>
           

修改完成之後,短信寶的接口替換完成了,但是niushop1.05這個版本使用者注冊成功後發送短信失敗這個BUG在此次示範中也修複了,隻需要修改項目/data/service/Member.php中的registerMember方法,代碼如下:

public function registerMember($user_name, $password, $email, $mobile, $user_qq_id, $qq_info, $wx_openid, $wx_info, $wx_unionid)
    {
        if(trim($user_name) != ""){
            $is_false = $this->verifyValue($user_name, $password, "plain");
        }else{
            if($mobile != "" && $email ==""){
                 $is_false = $this->verifyValue($user_name, $password, "mobile");
            }else{
                 $is_false = $this->verifyValue($user_name, $password, "email");
            }
        }
        if(!$is_false){
            return USER_WORDS_ERROR;
        }
        $res = parent::add($user_name, $password, $email, $mobile, , $user_qq_id, $qq_info, $wx_openid, $wx_info, $wx_unionid, );
        if ($res > ) {

            if ($mobile) {
                $params['shop_id'] = ;
                $params['user_id'] = $res;
                $result = runhook('Notify', 'registAfter', $params);
            }

            //擷取預設會員等級id
             $member_level = new NsMemberLevelModel();
            $level_info = $member_level->getInfo(['is_default' => ], 'level_id');
            $member_level = $level_info['level_id'];
            $member = new NsMemberModel();
            $data = array(
                'uid' => $res,
                'member_name' => $user_name,
                'member_level'=>$member_level,
                'reg_time' => date("Y-m-d H:i:s", time())
            );
            $retval = $member->save($data);
            // 注冊會員送積分
            $promote_reward_rule = new PromoteRewardRule();
            // 添加關注
            switch (NS_VERSION) {
                case NS_VER_B2C:
                    break;
                case NS_VER_B2C_FX:
                    if (! empty($_SESSION['source_uid'])) {
                        // 判斷目前版本
                        $nfx_user = new NfxUser();
                        $nfx_user->userAssociateShop($res, , $_SESSION['source_uid']);
                    } else {
                        // 判斷目前版本
                        $nfx_user = new NfxUser();
                        $nfx_user->userAssociateShop($res, , );
                    }
                    break;

            }
            // 平台贈送積分
            $promote_reward_rule->RegisterMemberSendPoint(, $res);
            // 直接登入
            if (! empty($user_name)) {
                $this->login($user_name, $password);
            } elseif (! empty($mobile)) {
                $this->login($mobile, $password);
            } elseif (! empty($user_qq_id)) {
                $this->qqLogin($user_qq_id);
            } elseif (! empty($wx_openid)) {
                $this->wchatLogin($wx_openid);
            } elseif (! empty($wx_unionid)) {
                $this->wchatUnionLogin($wx_unionid);
            }
        }
        return $res;
    }
           

這樣使用者注冊成功後也可以發送短信通知了,我們在添加短信模版的時候,會發現超過50個字元後的文字是沒有的,這是因為在資料庫中限制了模版的長度為50,我們隻需要修改一下字段長度就可以了。在項目/application/shop/controller建立一個Smsbao.php檔案,代碼如下:

<?php
/**
 * Smsbao.php
 * 短信寶插件安裝,修改模闆字段長度
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
class Smsbao extends Controller
{

    /**
     * 修改長度為255
     */
    public function index()
    {
        Db::query("alter table sys_notice_template  modify column template_title varchar(255) not null");
        echo '短信寶插件安裝成功';

    }
}
           

我們隻需要通路 域名/shop/Smsbao.php看到短信寶插件安裝成功就證明字段修改成功。

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

給大家幾個示例模版:

注冊成功:恭喜你成功注冊為shop_name的會員,會員名稱為:user_tel。

注:注冊成功發送短信僅限手機注冊,手機注冊會員名稱為手機号,不能填寫user_name.

注冊驗證:您的驗證碼為:number,請不要告訴他人。

充值成功:user_name你好,恭喜你在shop_name充值成功,充值金額為:recharge_money。

确認訂單:user_name你好,恭喜你在shop_name購物成功,訂單号為:order_no,訂單金額為:order_money,有任何疑問請咨詢客服。

付款成功:user_name你好,恭喜你在shop_name付款成功,訂單号為:order_no,訂單金額為:order_money,有任何疑問請咨詢客服。

下單成功:user_name你好,恭喜你在shop_name下單成功,訂單号為:order_no,訂單金額為:order_money,有任何疑問請咨詢客服。