소스 검색

发短信限制

shish 6 년 전
부모
커밋
60b7aeeab6
2개의 변경된 파일50개의 추가작업 그리고 0개의 파일을 삭제
  1. 6 0
      common/components/httpUtil.php
  2. 44 0
      common/components/sms.php

+ 6 - 0
common/components/httpUtil.php

@@ -121,4 +121,10 @@ class httpUtil
 		return false;
 	}
 
+	//获取ip shish 2019.12.18
+	public static function ip()
+	{
+		return Yii::$app->request->userIP;
+	}
+	
 }

+ 44 - 0
common/components/sms.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace common\components;
+
+use biz\weixin\services\WxOpenService;
+
+/**
+ * 短信接口
+ */
+class sms
+{
+	
+	//发短信
+	public static function send($mobile, $msg, $merchant = null)
+	{
+		$open = WxOpenService::getById(1);
+		$openName = $open['openName'];
+		$sms = new chuanglanSMS('N2890342', 'abc178c7');
+		$sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
+		$msg .= $sign;
+		
+		//验证是否有权限发短信
+		$uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
+		$ip = httpUtil::ip();
+		
+		$statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
+		$hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
+		if (!empty($hasSend)) {
+			util::fail('请60秒后再操作');
+		}
+		
+		$todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
+		$num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
+		$num = is_numeric($num) ? $num : 0;
+		if ($num > 10) {
+			util::fail('您今天申请的短信条数达到上限');
+		}
+
+		Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
+		Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
+		$sms->send($mobile, $msg);
+	}
+
+}