Browse Source

订单短信提醒

sunhao 5 years ago
parent
commit
e70b653e54

+ 42 - 1
app/Http/Controllers/Admin/OrderController.php

@@ -18,6 +18,7 @@ use PHPExcel_Reader_Excel2007;
18 18
 use PHPExcel_Reader_Excel5;
19 19
 use PHPExcel;
20 20
 use PHPExcel_Writer_Excel2007;
21
+use YPSMS;
21 22
 
22 23
 class OrderController extends Controller
23 24
 {
@@ -553,6 +554,9 @@ class OrderController extends Controller
553 554
                 $tables = 'order';
554 555
                 $data_id = $res;
555 556
                 Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
557
+
558
+                #如果提交审核,发短信
559
+                if( $order['status'] == 1 ) $this->sendRemind($self_id, $res, 1);
556 560
             }
557 561
             
558 562
             $last_url = $request->input('last_url');
@@ -748,7 +752,7 @@ class OrderController extends Controller
748 752
         if($res){
749 753
             #记录操作日志
750 754
             $status_arr = array(
751
-                0 => '已录入(驳回)',
755
+                0 => '已录入',
752 756
                 1 => '待审核',
753 757
                 2 => '已审核',
754 758
                 3 => '已发货',
@@ -760,6 +764,9 @@ class OrderController extends Controller
760 764
             $tables = 'order';
761 765
             $data_id = $id;
762 766
             Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
767
+
768
+            #发提醒短信
769
+            $this->sendRemind($self_id, $id, $order->status);
763 770
         } 
764 771
 
765 772
         exit('<script>window.location.replace(document.referrer)</script>');
@@ -1260,6 +1267,40 @@ class OrderController extends Controller
1260 1267
         return $result;
1261 1268
     }
1262 1269
 
1270
+    /**
1271
+     * 根据订单状态发短信提醒
1272
+     */
1273
+    public function sendRemind($user_id, $id, $status){
1274
+        if($status == 0 || $status == 3){
1275
+            //被驳回,获取销售手机号
1276
+            $saler_id = DB::table('order')->where('id', $id)->pluck('admin_id');
1277
+            $phone = DB::table('admin')->where('id, $saler_id')->pluck('phone');
1278
+            $type = $status == 0 ? 5 : 3;
1279
+        }elseif($status == 1){
1280
+            //待审核,获取团队管理员手机号
1281
+            $admin_ids = DB::table('admin_role')->where('role_name','管理员')->lists('user_id');
1282
+            $team_id = DB::table('admin')->where('id', $user_id)->pluck('team_id');
1283
+            $admin_info = DB::table('admin')->where('team_id', $team_id)->whereIn('id', $admin_ids)->first();
1284
+            $phone = $admin_info->phone;
1285
+            $type = 1;
1286
+        }elseif($status == 2){
1287
+            //审核通过,通知仓管发货
1288
+            $warehouse = DB::table('order')->where('id', $id)->pluck('warehouse');
1289
+            if($warehouse == 1){
1290
+                $warehouse_admin = DB::table('admin_role')->select('user_id')->where('role_name','A仓库管理员')->orderBy('id', 'desc')->first();
1291
+                $admin_info = DB::table('admin')->where('id', $warehouse_admin->user_id)->first();
1292
+                $phone = $admin_info->phone;
1293
+                $type = 2;
1294
+            }
1295
+        }
1296
+
1297
+        if(!$phone){
1298
+            return false;
1299
+        }
1300
+        $res = YPSMS::sendMsg($phone,$type,$id);
1301
+        return $res;
1302
+    }
1303
+
1263 1304
 
1264 1305
 }
1265 1306
 

+ 223 - 0
app/libs/sms/AliSMS.php

@@ -0,0 +1,223 @@
1
+<?php
2
+
3
+/**
4
+ * 阿里云短信验证码发送类
5
+ * @author Administrator
6
+ *
7
+ */
8
+ 
9
+class AliSMS {
10
+ 
11
+    // 保存错误信息
12
+ 
13
+    public $error;
14
+ 
15
+    // Access Key ID
16
+ 
17
+    private $accessKeyId = '';
18
+ 
19
+    // Access Access Key Secret
20
+ 
21
+    private $accessKeySecret = '';
22
+ 
23
+    // 签名
24
+ 
25
+    private $signName = '';
26
+ 
27
+    // 模版ID
28
+ 
29
+    private $templateCode = '';
30
+ 
31
+    public function __construct($cofig = array()) {
32
+ 
33
+        $cofig = array (
34
+ 
35
+                'accessKeyId' => 'LTAI6RVSZCr1QfQs',
36
+ 
37
+                'accessKeySecret' => 'A2fkajFYfaGnL5eiqnrYXrAgQPAHBf',
38
+ 
39
+                'signName' => '酷炫营销',
40
+ 
41
+                'templateCode' => 'SMS_151992296'
42
+ 
43
+        );
44
+ 
45
+        // 配置参数
46
+ 
47
+        $this->accessKeyId = $cofig ['accessKeyId'];
48
+ 
49
+        $this->accessKeySecret = $cofig ['accessKeySecret'];
50
+ 
51
+        $this->signName = $cofig ['signName'];
52
+ 
53
+        $this->templateCode = $cofig ['templateCode'];
54
+ 
55
+    }
56
+ 
57
+    private function percentEncode($string) {
58
+ 
59
+        $string = urlencode ( $string );
60
+ 
61
+        $string = preg_replace ( '/\+/', '%20', $string );
62
+ 
63
+        $string = preg_replace ( '/\*/', '%2A', $string );
64
+ 
65
+        $string = preg_replace ( '/%7E/', '~', $string );
66
+ 
67
+        return $string;
68
+ 
69
+    }
70
+ 
71
+    /**
72
+     * 签名
73
+     *
74
+     * @param unknown $parameters            
75
+     * @param unknown $accessKeySecret            
76
+     * @return string
77
+     */
78
+ 
79
+    private function computeSignature($parameters, $accessKeySecret) {
80
+ 
81
+        ksort ( $parameters );
82
+ 
83
+        $canonicalizedQueryString = '';
84
+ 
85
+        foreach ( $parameters as $key => $value ) {
86
+ 
87
+            $canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
88
+ 
89
+        }
90
+ 
91
+        $stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
92
+ 
93
+        $signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
94
+ 
95
+        return $signature;
96
+ 
97
+    }
98
+ 
99
+    /**
100
+     * @param unknown $mobile            
101
+     * @param unknown $verify_code            
102
+     *
103
+     */
104
+ 
105
+    public function send_verify($mobile, $verify_code) {
106
+ 
107
+        $params = array (   //此处作了修改
108
+ 
109
+                'SignName' => $this->signName,
110
+ 
111
+                'Format' => 'JSON',
112
+ 
113
+                'Version' => '2017-05-25',
114
+ 
115
+                'AccessKeyId' => $this->accessKeyId,
116
+ 
117
+                'SignatureVersion' => '1.0',
118
+ 
119
+                'SignatureMethod' => 'HMAC-SHA1',
120
+ 
121
+                'SignatureNonce' => uniqid (),
122
+ 
123
+                'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
124
+ 
125
+                'Action' => 'SendSms',
126
+ 
127
+                'TemplateCode' => $this->templateCode,
128
+ 
129
+                'PhoneNumbers' => $mobile,
130
+ 
131
+                'TemplateParam' => '{"code":"' . $verify_code . '"}' 
132
+ 
133
+                //'TemplateParam' => '{"time":"1234"}'   //更换为自己的实际模版
134
+ 
135
+        );
136
+ 
137
+        //var_dump($params);die;
138
+ 
139
+        // 计算签名并把签名结果加入请求参数
140
+ 
141
+        $params ['Signature'] = $this->computeSignature ( $params, $this->accessKeySecret );
142
+ 
143
+        // 发送请求(此处作了修改)
144
+ 
145
+        //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );
146
+ 
147
+        $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );
148
+ 
149
+         
150
+ 
151
+        $ch = curl_init ();
152
+ 
153
+        curl_setopt ( $ch, CURLOPT_URL, $url );
154
+ 
155
+        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
156
+ 
157
+        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
158
+ 
159
+        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
160
+ 
161
+        curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
162
+ 
163
+        $result = curl_exec ( $ch );
164
+ 
165
+        curl_close ( $ch );
166
+ 
167
+        $result = json_decode ( $result, true );
168
+ 
169
+ 		$ret = ['code'=>1];
170
+        if (isset ( $result ['Code'] ) && $result['Code'] == 'OK') {
171
+          
172
+          	$ret['code'] = 0;
173
+            return $ret;
174
+        }
175
+ 
176
+ 		$this->error = $this->getErrorMessage ( $result ['Code'] );
177
+        return $ret;
178
+ 
179
+    }
180
+ 
181
+    /**
182
+     * 获取详细错误信息
183
+     *
184
+     * @param unknown $status            
185
+     */
186
+ 
187
+    public function getErrorMessage($status) {
188
+ 
189
+        // 阿里云的短信 乱八七糟的(其实是用的阿里大于)
190
+ 
191
+        // https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450
192
+ 
193
+        $message = array (
194
+ 
195
+                'InvalidDayuStatus.Malformed' => '账户短信开通状态不正确',
196
+ 
197
+                'InvalidSignName.Malformed' => '短信签名不正确或签名状态不正确',
198
+ 
199
+                'InvalidTemplateCode.MalFormed' => '短信模板Code不正确或者模板状态不正确',
200
+ 
201
+                'InvalidRecNum.Malformed' => '目标手机号不正确,单次发送数量不能超过100',
202
+ 
203
+                'InvalidParamString.MalFormed' => '短信模板中变量不是json格式',
204
+ 
205
+                'InvalidParamStringTemplate.Malformed' => '短信模板中变量与模板内容不匹配',
206
+ 
207
+                'InvalidSendSms' => '触发业务流控',
208
+ 
209
+                'InvalidDayu.Malformed' => '变量不能是url,可以将变量固化在模板中'
210
+ 
211
+        );
212
+ 
213
+        if (isset ( $message [$status] )) {
214
+ 
215
+            return $message [$status];
216
+ 
217
+        }
218
+ 
219
+        return $status;
220
+ 
221
+    }
222
+ 
223
+}

+ 117 - 0
app/libs/sms/TruiSMS.php

@@ -0,0 +1,117 @@
1
+<?php
2
+
3
+/**
4
+ * 阿里云短信验证码发送类
5
+ * @author Administrator
6
+ *
7
+ */
8
+ 
9
+class TruiSMS {
10
+ 
11
+    // 保存错误信息
12
+ 
13
+    public $error;
14
+ 
15
+    // Access Key ID
16
+ 
17
+    private $accesskey = '';
18
+ 
19
+    // Access Access Key Secret
20
+ 
21
+    private $secret = '';
22
+ 
23
+    // 签名
24
+ 
25
+    private $signName = '猎豆优选';
26
+ 
27
+    // 模版ID
28
+ 
29
+    private $templateId = '30233';
30
+ 
31
+    public function __construct($cofig = array()) {
32
+ 
33
+        $cofig = array (
34
+ 
35
+                'accesskey' => 'octOap8wLMsXIS38',
36
+ 
37
+                'secret' => 'rqjJg3Hg4jiGsWH4C5wCVzegZUKxHgRU',
38
+ 
39
+                'signName' => '【猎豆优选】',
40
+ 
41
+                'templateId' => 'SMS_151992296'
42
+ 
43
+        );
44
+ 
45
+        // 配置参数
46
+ 
47
+        $this->accesskey = $cofig ['accesskey'];
48
+ 
49
+        $this->secret = $cofig ['secret'];
50
+ 
51
+        $this->signName = $cofig ['signName'];
52
+ 
53
+        $this->templateId = $cofig ['templateId'];
54
+ 
55
+    }
56
+
57
+ 
58
+    /**
59
+     * @param unknown $mobile            
60
+     * @param unknown $verify_code            
61
+     *
62
+     */
63
+ 
64
+    public function send_verify($mobile, $verify_code, $minute) {
65
+ 
66
+        $params = array (   //此处作了修改
67
+                
68
+                'accesskey' => $this->accesskey,
69
+
70
+                'secret' => $this->secret,
71
+
72
+                'sign' => $this->signName,
73
+
74
+                'templateId' => $this->templateId,
75
+ 
76
+                'mobile' => $mobile,
77
+ 
78
+                'content' => $verify_code.'##'.$minute 
79
+ 
80
+        );
81
+
82
+        $url = "http://api.1cloudsp.com/api/v2/single_send";
83
+        $data = http_build_query ( $params );
84
+         
85
+        $ch = curl_init ();
86
+ 
87
+        curl_setopt ( $ch, CURLOPT_URL, $url );
88
+
89
+        curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
90
+ 
91
+        curl_setopt($ch, CURLOPT_HEADER, 0);//
92
+  
93
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
94
+        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
95
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
96
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
97
+ 
98
+        $result = curl_exec ( $ch );
99
+ 
100
+        curl_close ( $ch );
101
+ 
102
+        $result = json_decode ( $result, true );
103
+ 
104
+        if (isset ( $result ['Code'] )) {
105
+ 
106
+            $this->error = $this->getErrorMessage ( $result ['Code'] );
107
+ 
108
+            return false;
109
+ 
110
+        }
111
+ 
112
+        return true;
113
+ 
114
+    }
115
+ 
116
+ 
117
+}

+ 115 - 0
app/libs/sms/YPSMS.php

@@ -0,0 +1,115 @@
1
+<?php
2
+use App\libs\sms;
3
+define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
4
+define('YP_SMS_YHQ', '73a74eb72c42b765669acd8e94096b9f');
5
+define("YP_SMS_KEY_FAMLI",'995629e02beaaf47118b84ac19c4b5b9');
6
+define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
7
+define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
8
+define("YP_TPL_ID", "2122814");
9
+
10
+class YPSMS{
11
+
12
+    private static $order_tpl = [
13
+        1 => '3210114', //新订单审核提醒管理员
14
+        2 => '3210120',  //新订单提醒仓管
15
+        3 => '3210134',   //订单发货提醒销售
16
+        4 => '3210146',   //订单审核通过提醒销售
17
+        5 => '3210312',   //订单审核驳回提醒销售
18
+    ];
19
+    private static function init(){
20
+        $ch = curl_init();
21
+        /* 设置验证方式 */
22
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
23
+            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
24
+        /* 设置返回结果为流 */
25
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26
+
27
+        /* 设置超时时间*/
28
+        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
29
+
30
+        /* 设置通信方式 */
31
+        curl_setopt($ch, CURLOPT_POST, 1);
32
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
33
+        return $ch;
34
+
35
+    }
36
+    public static function sendMsg($phone,$type,$order_id = null){
37
+        $tpl = self::$order_tpl;
38
+        $tpl_id = $tpl[$type];
39
+        $ch=self::init();       
40
+        //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);       
41
+        $data = [
42
+                'apikey' => YP_SMS_KEY,
43
+                'mobile' => $phone,
44
+                'tpl_id' => $tpl_id,
45
+                ];
46
+        if( in_array($type, [3,5]) ){
47
+            $data['tpl_value'] = ('#order_id#').'='.$order_id;
48
+        }     
49
+        $json_data = self::tpl_send($ch,$data);
50
+        //print_r($json_data);    ******************************maybe影响验证码发出
51
+         $array = json_decode($json_data,true);
52
+        // echo '<pre>';print_r($array);
53
+        curl_close($ch);
54
+        return $array;
55
+    }
56
+
57
+    public static function sendRedSMS($phone,$text,$id){
58
+        $ch=self::init();
59
+        $data=array('tpl_id' => $id,'text'=>$text,'apikey'=>YP_SMS_YHQ,'mobile'=>$phone);
60
+        $json_data = self::send($ch,$data);
61
+        curl_close($ch);
62
+        return $json_data;
63
+    }
64
+
65
+    public static function sendVoiceCheck($phone,$code){
66
+        $ch=self::init();
67
+        $data=array('code'=>$code,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
68
+        $json_data =self::voice_send($ch,$data);
69
+        // $array = json_decode($json_data,true);
70
+        // echo '<pre>';print_r($array);
71
+        curl_close($ch);
72
+        return $json_data;
73
+
74
+    }
75
+    private static function checkErr($result,$error) {
76
+        if($result === false)
77
+        {
78
+            echo 'Curl error: ' . $error;
79
+        }
80
+//        else
81
+//        {
82
+//            echo '操作完成没有任何错误';
83
+//        }
84
+    }
85
+    private static function send($ch,$data){
86
+        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
87
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
88
+        $result = curl_exec($ch);
89
+        $error = curl_error($ch);
90
+        self::checkErr($result,$error);
91
+        return $result;
92
+    }
93
+    private static function voice_send($ch,$data){
94
+        curl_setopt ($ch, CURLOPT_URL, YP_VOICE_URL);
95
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
96
+        $result = curl_exec($ch);
97
+        $error = curl_error($ch);
98
+        self::checkErr($result,$error);
99
+        return $result;
100
+    }
101
+    private static function tpl_send($ch,$data){
102
+        curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
103
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
104
+        $result = curl_exec($ch);
105
+        $error = curl_error($ch);
106
+        self::checkErr($result,$error);
107
+        return $result;
108
+    }
109
+}
110
+// YPSMS::init();
111
+//$code=100;
112
+//$minutes=3;
113
+// YPSMS::sendSMS('13613665865','【钱多多随手记】您的验证码是' . $code . ',有效期为' . $minutes . '分钟,请尽快验证。');
114
+// YPSMS::sendVoiceCheck('13613665865','123456');
115
+