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"=>0,
"message"=>"success",
"param"=>""
);
/**
* 邮件的配置信息
* @var unknown
*/
public $email_is_open=0;
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=0;
/**
* 得到系统通知的配置信息
* @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=0;
}
}
}
}
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==0){
#旧用户发送
if(!empty($result)){
if(!isset($result->result)){
$result=json_decode(json_encode($result), true);
#发送失败
$deal_result["code"]=$result["code"];
$deal_result