stanley-king пре 4 година
родитељ
комит
88084eab18
5 измењених фајлова са 172 додато и 61 уклоњено
  1. 39 30
      core/framework/libraries/sms.php
  2. 3 3
      data/api/smsapi/yuntx/SendSMS.php
  3. 18 0
      data/logic/queue.logic.php
  4. 91 26
      helper/sms_helper.php
  5. 21 2
      test/TestSms.php

+ 39 - 30
core/framework/libraries/sms.php

@@ -21,7 +21,7 @@ class Sms
     const getfcode_code = 9;
     const after_comment = 10;
 
-    const tpl_reg_user_bonus = '219562';        //亲爱的用户,您已获得{1}元新人首单红包,请登陆APP查收!退订回复N
+    const tpl_reg_user_bonus = '756830';        //亲爱的用户,您已获得{1}元新人首单红包,请登陆APP查收!退订回复N
     const tpl_reg_inviter = '219559';           //亲爱的会员,{1}成为您的{2}度粉丝,您获得{3}元全抵红包。退订回复N
     const tpl_reg_invitee = '219558';           //亲爱的会员,您的好友{1}成为{2}的{3}度粉丝,您获得{4}元雨露均沾红包。退订回复N
     const tpl_order_complete_all = '219557';    //亲爱的会员,{1}下单成功,引发红包风暴,{2}元全抵红包砸中了你。快去查看红包余额!退订回复N
@@ -37,16 +37,18 @@ class Sms
     */
     public function send($mobile, $content)
     {
-        $mobile_host_type = C('mobile_host_type');
-        if ($mobile_host_type == 1) {
-            return $this->mysend_smsbao($mobile, $content);
-        } elseif ($mobile_host_type == 2) {
-            return $this->mysend_yunpian($mobile, $content);
-        } elseif ($mobile_host_type == 3) {
-            return $this->mysend_yuntx($mobile, $content);
-        } elseif ($mobile_host_type == 4) {
-            return $this->send_yunpian($mobile, $content);
-        }
+        return $this->mysend_yuntx($mobile, $content);
+
+//        $mobile_host_type = C('mobile_host_type');
+//        if ($mobile_host_type == 1) {
+//            return $this->mysend_smsbao($mobile, $content);
+//        } elseif ($mobile_host_type == 2) {
+//            return $this->mysend_yunpian($mobile, $content);
+//        } elseif ($mobile_host_type == 3) {
+//            return $this->mysend_yuntx($mobile, $content);
+//        } elseif ($mobile_host_type == 4) {
+//            return $this->send_yunpian($mobile, $content);
+//        }
     }
 
     private function send_msg($mobile, $datas, $type)
@@ -59,7 +61,7 @@ class Sms
             require_once(PLUGIN_ROOT . DS . $plugin . DS . 'CCPRestSDK.php');
             require_once(PLUGIN_ROOT . DS . $plugin . DS . 'SendSMS.php');
 
-            $datas_param = array();
+            $datas_param = [];
             foreach($datas as $key=>$val)
             {
                 $datas_param[] = $val;
@@ -86,27 +88,34 @@ class Sms
      */
     private function mysend_yuntx($mobile, $content)
     {
-        $type = intval($content['type']);
-
-        static $templates = [
-            self::register_code => '59006',
-            self::resetpass_code => '63477',
-            self::getbonus_code => '63479',
-            self::getbind_code => '59006',
-            self::newcomer_code => '92893',
-            self::newcomer_bonus_code => '146196',
-            self::order_sended => '355420',     //【熊猫美妆】尊敬的会员,您的订单已发货,{1}运单号为{2},详细订单信息请登陆APP查看。
-            self::order_received => '355421',   //【熊猫美妆】您已确认收货,订单奖励红包已到账。评价商品还能领更多红包噢~
-            self::getfcode_code => '166281',
-            self::after_comment => '355423',    //【熊猫美妆】感谢您对商品的评价,{1}元红包已到账!您的意见对我们至关重要~
-        ];
-
-        if (!array_key_exists($type, $templates)) {
-            Log::record("mysend_yuntx type error}",Log::ERR);
+        $type = $this->getYuntxTpl(intval($content['type']));
+        if(empty($type)) {
             return -1;
         }
 
-        return $this->send_msg($mobile, $content['datas'], $templates[$type]);
+        return $this->send_msg($mobile, $content['datas'], $type);
+    }
+
+    private function getYuntxTpl($type)
+    {
+        static $sms_tpls = [
+            'register'  => '756830',
+            'resetpass' => '63477'
+//            self::getbonus_code => '63479',
+//            self::getbind_code => '59006',
+//            self::newcomer_code => '92893',
+//            self::newcomer_bonus_code => '146196',
+//            self::order_sended => '355420',     //【熊猫美妆】尊敬的会员,您的订单已发货,{1}运单号为{2},详细订单信息请登陆APP查看。
+//            self::order_received => '355421',   //【熊猫美妆】您已确认收货,订单奖励红包已到账。评价商品还能领更多红包噢~
+//            self::getfcode_code => '166281',
+//            self::after_comment => '355423',    //【熊猫美妆】感谢您对商品的评价,{1}元红包已到账!您的意见对我们至关重要~
+        ];
+        if($type == sms_helper::register) {
+            return $sms_tpls['register'];
+        }
+        else {
+            return false;
+        }
     }
 
     public function send_oper($mobile,$type,$params = null)

+ 3 - 3
data/api/smsapi/yuntx/SendSMS.php

@@ -21,13 +21,13 @@
 function sendSMS($to,$datas,$tempId)
 {
     //主帐号
-    $accountSid = '8a48b5514ff923b4014ffe4f7d570d2c';
+    $accountSid = '8a216da873cec132017457d4c8e337f8';
 
     //主帐号Token
-    $accountToken = '28953ff4d46c492f90329cda3ce473ad';
+    $accountToken = 'a1f3d43326b14d96a798628bc2c9f31e';
 
     //应用Id
-    $appId = '8a48b5515018a0f4015059a74f0f7565';
+    $appId = '8a216da873cec132017457d4c9d637fe';
 
     //请求地址,格式如下,不需要写https://
     $serverIP = 'app.cloopen.com';

+ 18 - 0
data/logic/queue.logic.php

@@ -552,6 +552,24 @@ class queueLogic
         return callback(true);
     }
 
+    public function send_sms($param)
+    {
+        if (empty($param) || empty($param['mobile']) || empty($param['type'])) {
+            Log::record("sms info: 数据有误! param:" . json_encode($param), Log::ERR);
+            return callback(false);
+        }
+
+        try {
+            $sms = new Sms();
+            $status = $sms->send($param['mobile'], $param);
+            Log::record("sms info: status:" . json_encode($status) . "\t param:" . json_encode($param), Log::DEBUG);
+        } catch (Exception $e) {
+            Log::record("sms info: fall error\treturn:" . $e->getMessage() . "\t param:" . json_encode($param), Log::ERR);
+        }
+
+        return callback(true);
+    }
+
     public function sendOperSMS($params)
     {
         if (empty($params) || empty($params['mobile']) || empty($params['type'])) {

+ 91 - 26
helper/sms_helper.php

@@ -14,27 +14,91 @@ class sms_helper
     const repeat_mintime = 60;
     const code_expire = 5 * 60;
 
-    static public function send($mobile,$type)
+    const register  = 1;
+    const resetpass = 2;
+    const getbonus = 3;
+    const getwxbind = 4;
+    const getfcode = 5;
+    const type_max = 6;
+
+    public function send_code($mobile,$type,$fcheck = true)
     {
-        static $types = array('register', 'resetpass', 'getbonus','getwxbind','getfcode');
-        if (!in_array($type, $types)) {
-            return array('code' => errcode::ErrParamter, 'msg' => "错误的类型参数: type={$type}");
+        if($type < 0 || $type >= self::type_max) {
+            return ['code' => errcode::ErrParamter, 'msg' => "错误的类型参数: type={$type}"];
         }
 
-        if ($type == 'register') {
-            $itype = Sms::register_code;
+        if($fcheck)
+        {
+            $cur_time = time();
+            if(isset($_SESSION['sms_code'][$type]))
+            {
+                $last_time = $_SESSION['sms_code'][$type]['last_time'];
+                if($cur_time - $last_time < self::repeat_mintime) {
+                    return ['code' => errcode::ErrSmsLessRepeateMinTime, 'msg' => errcode::msg(errcode::ErrSmsLessRepeateMinTime)];
+                }
+            }
         }
-        elseif ($type == 'resetpass') { //重置密码需要在登录的状态下才可以执行。
-            $itype = Sms::resetpass_code;
+
+
+        $code = makeSmscode();
+        Log::record("sms code = {$code}",Log::DEBUG);
+//        QueueClient::push('sendSMS', ['mobile'=>$mobile,'type'=>$type,'datas' => ['code'=>$code,'time'=>'5']]);
+
+        $logic = Logic('queue');
+        $logic->send_sms( ['mobile'=>$mobile,'type'=>$type,'datas' => ['code'=>$code,'time'=>'5']]);
+        $status = 0;
+        if ($status == 0)
+        {
+            if($fcheck)
+            {
+                if(isset($_SESSION['sms_code'][$type])) {
+                    $count = $_SESSION['sms_code'][$type]['count'];
+                } else {
+                    $count = 0;
+                }
+
+                $_SESSION['sms_code'][$type] = ['count' => $count + 1, 'code' => $code, 'last_time' => time(),'mobile' => $mobile];
+            }
+
+            return ['code' => 200];
+        } else {
+            $msg = self::get_errmsg($status,$type);
+            return ['code' => errcode::ErrSms, 'msg' => $msg];
+        }
+    }
+    public function cast_type($stype)
+    {
+        static $types = ['register', 'resetpass', 'getbonus','getwxbind','getfcode'];
+        if (!in_array($stype, $types)) {
+            return false;
+        }
+
+        if ($stype == 'register') {
+            $itype = sms_helper::register;
+        }
+        elseif ($stype == 'resetpass') { //重置密码需要在登录的状态下才可以执行。
+            $itype = sms_helper::resetpass;
+        }
+        elseif ($stype == 'getbonus') {
+            $itype = sms_helper::getbonus;
+        }
+        elseif($stype == 'getwxbind') {
+            $itype = sms_helper::getwxbind;
         }
-        elseif ($type == 'getbonus') {
-            $itype = Sms::getbonus_code;
+        elseif($stype == 'getfcode') {
+            $itype = sms_helper::getfcode;
         }
-        elseif($type == 'getwxbind') {
-            $itype = Sms::getbind_code;
+        else {
+            return false;
         }
-        elseif($type == 'getfcode') {
-            $itype = Sms::getfcode_code;
+        return $itype;
+    }
+
+    public function send($mobile,$stype)
+    {
+        $itype = $this->cast_type($stype);
+        if($itype == false) {
+            return ['code' => errcode::ErrParamter, 'msg' => "错误的类型参数: type={$stype}"];
         }
 
         $cur_time = time();
@@ -42,7 +106,7 @@ class sms_helper
         {
             $last_time = $_SESSION['sms_code'][$itype]['last_time'];
             if($cur_time - $last_time < self::repeat_mintime) {
-                return array('code' => errcode::ErrSmsLessRepeateMinTime, 'msg' => errcode::msg(errcode::ErrSmsLessRepeateMinTime));
+                return ['code' => errcode::ErrSmsLessRepeateMinTime, 'msg' => errcode::msg(errcode::ErrSmsLessRepeateMinTime)];
             }
         }
 
@@ -50,7 +114,7 @@ class sms_helper
         $code = makeSmscode();
         Log::record("sms code = {$code}",Log::DEBUG);
         //Logic('queue')->sendSMS(array('mobile'=>$mobile,'type'=>$itype,'datas' => array('code'=>$code,'time'=>'5')));
-        QueueClient::push('sendSMS',array('mobile'=>$mobile,'type'=>$itype,'datas' => array('code'=>$code,'time'=>'5')));
+        QueueClient::push('sendSMS', ['mobile'=>$mobile,'type'=>$itype,'datas' => ['code'=>$code,'time'=>'5']]);
         $status = 0;
         if ($status == 0)
         {
@@ -64,17 +128,16 @@ class sms_helper
             $_SESSION['sms_code'][$itype]['last_time'] = time();
             $_SESSION['sms_code'][$itype]['mobile'] = $mobile;
 
-            return array('code' => 200);
+            return ['code' => 200];
         } else {
             $msg = self::get_errmsg($status,$itype);
-            return array('code' => errcode::ErrSms, 'msg' => $msg);
+            return ['code' => errcode::ErrSms, 'msg' => $msg];
         }
-        return array('code' => 200);
     }
 
     static public function send_nostore_message($mobile,$type,$datas)
     {
-        QueueClient::push('sendSMS', array('mobile' => $mobile,'type' => $type, 'datas' => $datas));
+        QueueClient::push('sendSMS', ['mobile' => $mobile,'type' => $type, 'datas' => $datas]);
     }
 
     static public function check_code($itype,$code,$mobile)
@@ -82,7 +145,7 @@ class sms_helper
         if(isset($_SESSION['sms_code'][$itype]))
         {
             if($_SESSION['sms_code'][$itype]['mobile'] != $mobile) {
-                return array('code' => errcode::ErrSmscode, 'msg' => '验证手机号和发送验证码手机号不一致.');
+                return ['code' => errcode::ErrSmscode, 'msg' => '验证手机号和发送验证码手机号不一致.'];
             }
 
             $tmout = time() - $_SESSION['sms_code'][$itype]['last_time'];
@@ -91,13 +154,15 @@ class sms_helper
                 if($code == $_SESSION['sms_code'][$itype]['code']) {
                     return true;
                 } else {
-                    return array('code' => errcode::ErrSmscode, 'msg' => '验证码错误.');
+                    return ['code' => errcode::ErrSmscode, 'msg' => '验证码错误.'];
                 }
-            } else {
-                return array('code' => errcode::ErrSmscodeExpire, 'msg' => errcode::msg(errcode::ErrSmscodeExpire));
             }
-        } else {
-            return array('code' => errcode::ErrSmscode, 'msg' => '验证码错误.');
+            else {
+                return ['code' => errcode::ErrSmscodeExpire, 'msg' => errcode::msg(errcode::ErrSmscodeExpire)];
+            }
+        }
+        else {
+            return ['code' => errcode::ErrSmscode, 'msg' => '验证码错误.'];
         }
     }
 

+ 21 - 2
test/TestSms.php

@@ -6,20 +6,39 @@
  * Time: 下午7:44
  */
 
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/27
+ * Time: 下午10:27
+ */
+
 define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/global.php');
+require_once(BASE_CORE_PATH . '/lrlz.php');
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/stat_helper.php');
 require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');
 
-class TestSms extends PHPUnit_Framework_TestCase
+class TestSms extends TestCase
 {
-    public static function setUpBeforeClass()
+    public static function setUpBeforeClass() : void
     {
         Base::run_util();
     }
 
     public function testRegister()
     {
+        $mobile = '13911129867';
+        $sms = new sms_helper();
+        $ret = $sms->send_code($mobile, sms_helper::register,false);
+
+    }
+    public function testInvitee()
+    {
         $sms = new Sms();
 
         $mobile = '13911129867';