待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-18:00

禾匠小程序新增短信宝短信接口

禾匠商城小程序是一个很好用的微擎禾匠小程序商城!小编对他还是比较了解的,今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,我们今天讲解的是2.5版本,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用。

首先打开项目:\zjhj_mall\zjhj_mall\core\modules\mch\views\store\sms.php文件,替换为以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
 
defined('YII_ENV') or exit('Access Denied');
/* @var $sms \app\models\SmsSetting */
$urlManager = Yii::$app->urlManager;
$this->title = '短信通知';
$this->params['active_nav_group'] = 1;
?>
 
 
<div class="panel">
<div class="panel-header"><?= $this->title ?></div>
<div class="panel-body">
    <form method="post" class="auto-form">
        <p>短信设置用于用户下单时,给指定手机号发送短信通知。</p>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">开启短信提醒</label>
            </div>
            <div class="col-sm-6">
                <label class="radio-label">
                    <input id="radio2" <?= $sms->status == 0 ? 'checked' : null ?>
                           value="0"
                           name="status" type="radio" class="custom-control-input">
                    <span class="label-icon"></span>
                    <span class="label-text">关闭</span>
                </label>
                <label class="radio-label">
                    <input id="radio1" <?= $sms->status == 1 ? 'checked' : null ?>
                           value="1"
                           name="status" type="radio" class="custom-control-input">
                    <span class="label-icon"></span>
                    <span class="label-text">开启</span>
                </label>
            </div>
        </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">短信宝账号:</label>
            </div>
            <div class="col-sm-6">
                <?php if ($sms->AccessKeyId): ?>
                    <div class="input-hide">
                        <input class="form-control" type="text" name="AccessKeyId"
                               value="<?= $sms->AccessKeyId ?>">
                        <div class="tip-block">已隐藏短信宝账号,点击查看或编辑</div>
                    </div>
                <?php else: ?>
                    <input class="form-control" type="text" name="AccessKeyId"
                           value="<?= $sms->AccessKeyId ?>">
                <?php endif; ?>
            </div>
        </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">短信宝密码:</label>
            </div>
            <div class="col-sm-6">
                <?php if ($sms->AccessKeyId): ?>
                    <div class="input-hide">
                        <input class="form-control" type="text" name="AccessKeySecret"
                               value="<?= $sms->AccessKeySecret ?>">
                        <div class="tip-block">已隐藏短信宝密码,点击查看或编辑</div>
                    </div>
                <?php else: ?>
                    <input class="form-control" type="text" name="AccessKeySecret"
                           value="<?= $sms->AccessKeySecret ?>">
                <?php endif; ?>
            </div>
        </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">短信签名</label>
            </div>
            <div class="col-sm-6">
                <input autocomplete="off" class="form-control" type="text" name="sign"
                       value="<?= $sms->sign ?>">
            </div>
        </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">接收短信通知手机号</label>
            </div>
            <div class="col-sm-6">
                <input autocomplete="off" class="form-control" type="text" name="mobile"
                       value="<?= $sms->mobile ?>">
                <div class="fs-sm text-muted">多个请使用英文逗号<code>,</code>分隔</div>
            </div>
        </div>
 
 
        <div>订单下单提醒</div>
        <div class="text-muted fs-sm">例如:模板内容:您有一条新的订单,订单号:89757,请登录商城后台查看。 </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">模板变量</label>
            </div>
            <div class="col-sm-6">
                <input autocomplete="off" class="form-control" type="text" name="msg"
                       value="<?= $sms->msg ?>">
                <div class="text-muted fs-sm">例如:模板内容: 您有一个新的订单,订单号:${order},则填写order</div>
                <div class="text-danger fs-sm">注意:目前只支持设置订单号</div>
            </div>
        </div>
 
 
        <div>订单退款提醒</div>
        <div class="text-muted fs-sm">例如:模板内容:您有一条新的退款订单,请登录商城后台查看。 </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">模板变量</label>
            </div>
            <div class="col-sm-6">
                <input autocomplete="off" class="form-control" type="text" name="refund[msg]"
                       value="<?= $refund['msg'] ?>">
                <div class="text-muted fs-sm">例如:模板内容: 您有一个新的退款订单,订单号:${order},则填写order</div>
                <div class="text-danger fs-sm">注意:目前只支持设置订单号</div>
            </div>
        </div>
 
 
        <div>发送手机验证码</div>
        <div class="text-muted fs-sm">例如:模板内容:您的验证码为89757,请勿告知他人。</div>
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
                <label class="col-form-label">模板变量</label>
            </div>
            <div class="col-sm-6">
                <input autocomplete="off" class="form-control" type="text" name="code[msg]"
                       value="<?= $code['msg'] ?>">
                <div class="text-muted fs-sm">例如:模板内容: 您的验证码为${code},请勿告知他人。,则填写“code</div>
            </div>
        </div>
 
 
        <div class="form-group row">
            <div class="form-group-label col-sm-2 text-right">
            </div>
            <div class="col-sm-6">
                <a class="btn btn-primary auto-form-btn" href="javascript:">保存</a>
            </div>
        </div>
    </form>
</div>
</div>
 
 
<script>
var app = new Vue({
    el: "#app",
    data: {
        mobile_list: [],
        mobile: ""
    }
});
</script>

接着打开项目:\zjhj_mall\zjhj_mall\core\modules\mch\models\SmsForm.php文件,替换34行左右的代码:

?
1
[['AccessKeyId','AccessKeySecret','sign','status','mobile'],'required','on'=>'SUCCESS'],

最后打开项目:\zjhj_mall\zjhj_mall\core\extensions\Sms.php文件,替换为以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace app\extensions;
 
 
 
 
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Profile\DefaultProfile;
use app\models\SmsRecord;
use app\models\SmsSetting;
use Hejiang\Sms\Messages\TemplateMessage;
use Hejiang\Sms\Messages\VerificationCodeMessage;
use Hejiang\Sms\Senders\AliyunSender;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
 
 
require_once __DIR__. '/alidayu/TopSdk.php';
 
 
class Sms
{
    /**
     * 发送短信
     *
     * 短信通知
     * @param string $store_id 商铺ID
     * @param string $content 内容,字符串
     * @return array
     */
    public static function sendSms($user,$pwd,$sign,$mobile,$content)
    {
      $con '【'.$sign.'】'.$content;
        $url 'http://api.smsbao.com/sms?u='.$user.'&p='.md5($pwd).'&m='.$mobile.'&c='.UrlEncode($con);
        $ret file_get_contents($url);
        return $ret;
 
    }
    public static function send($store_id$content = null)
    {
        $sms_setting = SmsSetting::findOne(['is_delete' => 0, 'store_id' => $store_id]);
        if ($sms_setting->status == 0) {
            return [
                'code' => 1,
                'msg' => '短信通知服务未开启'
            ];
        }
        $con str_replace('${order}',$content,$sms_setting->msg);
       $res =  self::sendSms($sms_setting->AccessKeyId,$sms_setting->AccessKeySecret,$sms_setting->sign,$sms_setting->mobile,$con);
      if ($res == 0) {
            $smsRecord new SmsRecord();
            $smsRecord->mobile = $sms_setting->mobile;
            $smsRecord->tpl =$sms_setting->msg;
            $smsRecord->content = $con;
            $smsRecord->ip = \Yii::$app->request->userIP;
            $smsRecord->addtime = time();
            $smsRecord->save();
            return [
                'code' => 0,
                'msg' => $content
            ];
        else {
            return [
                'code' => 2,
                'msg' => $res
            ];
        }
    }
    /**
     * 发送短信  退款通知
     * @param string $store_id 商铺ID
     * @param string $content 内容,字符串
     * @return array
     */
    public static function send_refund($store_id$content = null)
    {
        $sms_setting = SmsSetting::findOne(['is_delete' => 0, 'store_id' => $store_id]);
             var_dump($sms_setting);exit;
        if ($sms_setting->status == 0) {
            return [
                'code' => 1,
                'msg' => '短信通知服务未开启'
            ];
        }
         $con str_replace('${order}',$content,$sms_setting->msg);
     
         $res =  self::sendSms($sms_setting->AccessKeyId,$sms_setting->AccessKeySecret,$sms_setting->sign,$sms_setting->mobile,$con);
 
 
      if ($res == 0) { 
            $smsRecord new SmsRecord();
            $smsRecord->mobile = $sms_setting->mobile;
            $smsRecord->tpl = $sms_setting->msg;
            $smsRecord->content = $con;
            $smsRecord->ip = \Yii::$app->request->userIP;
            $smsRecord->addtime = time();
            $smsRecord->save();
            return [
                'code' => 0,
                'msg' => $content
            ];
        else {
            return [
                'code' => 2,
                'msg' => $res
            ];
        }
    }
 
 
 
 
    public static function send_text($store_id$content = null,$mobile)
    {
        $sms_setting = SmsSetting::findOne(['is_delete' => 0, 'store_id' => $store_id]);
        $mobile_cache = \Yii::$app->cache->get('mobile_cache'.$mobile);
 
 
        \Yii::$app->cache->set('mobile_cache'.$mobile,true,60);
 
 
        if(!$mobile){
            return [
                'code'=>1,
                'msg'=>'请输入手机号'
            ];
        }
        $tpl = json_decode($sms_setting->tpl_code,true);
        if(!is_array($tpl) || !$tpl['msg']){
            return [
                'code'=>1,
                'msg'=>'未设置验证码短信'
            ];
        }
        $con str_replace('${code}',$content,$tpl['msg']);
       $res =  self::sendSms($sms_setting->AccessKeyId,$sms_setting->AccessKeySecret,$sms_setting->sign,$mobile,$con);
    \Yii::$app->cache->set('code_cache'.$mobile,$content,600);
      if ($res == 0) {
            $smsRecord new SmsRecord();
            $smsRecord->mobile = $mobile;
            $smsRecord->tpl = $tpl['msg'];
            $smsRecord->content = $con;
            $smsRecord->ip = \Yii::$app->request->userIP;
            $smsRecord->addtime = time();
            $smsRecord->save();
            return [
                'code' => 0,
                'msg' => $content
            ];
        else {
            \Yii::$app->cache->delete('mobile_cache'.$mobile);
            return [
                'code' => 2,
                'msg' => $res
            ];
        }
    }
  
    
}

经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。

另外:我们已经开发好完整的禾匠商城系统短信宝插件,点击此链接 下载及查看安装流程。

 
开源插件
文章标签