LMMessage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2015/12/24
  6. * Time: 16:56
  7. */
  8. namespace common\library;
  9. use common\models\Shortmsg;
  10. /**
  11. * 短信接口
  12. * Class LMMessage
  13. * @package common\libary
  14. */
  15. class LMMessage {
  16. const LIMIT_SECOND = 60; //同个号码发送频率
  17. // static function SendMessage($mobiles,$content, $limit= true){
  18. //
  19. // if($limit && !self::limitTime($mobiles)){
  20. // return false;
  21. // }
  22. // if(empty($mobiles) && !self::mobileFormat($mobiles)){
  23. // return false;
  24. // }
  25. //
  26. // $cust_code = \Yii::$app->params['message']['id'];
  27. // $password =\Yii::$app->params['message']['pwd'];
  28. //// $sp_code = '';
  29. // $destMobiles = $mobiles;
  30. //
  31. // $url = "http://".\Yii::$app->params['message']['ip'].":8860/";
  32. //
  33. // $post_data = array();
  34. // $post_data['cust_code'] = $cust_code;
  35. // $post_data['destMobiles'] = $destMobiles;
  36. // $post_data['content'] = $content;
  37. // $post_data['sign'] = md5(urlencode($content.$password)); //签名
  38. //
  39. // $o="";
  40. // foreach ($post_data as $k=>$v)
  41. // {
  42. // $o.= "$k=".urlencode($v)."&";
  43. // }
  44. // $post_data=substr($o,0,-1);
  45. // $ch = curl_init();
  46. // curl_setopt($ch, CURLOPT_POST, 1);
  47. // curl_setopt($ch, CURLOPT_HEADER, 0);
  48. // curl_setopt($ch, CURLOPT_URL,$url);
  49. // //为了支持cookie
  50. // curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  51. // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  52. // curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  53. // $result = curl_exec($ch);
  54. //
  55. // $resultarr = self::resultHandle($result);
  56. // self::saveShortMessage($destMobiles,$content,$resultarr);
  57. // return $resultarr;
  58. // }
  59. /**
  60. * 新短信接口接入
  61. * @param $mobiles
  62. * @param $content
  63. * @param bool $limit
  64. * @return bool
  65. */
  66. static function SendMessage($mobiles,$content, $limit= true){
  67. if($limit && !self::limitTime($mobiles)){
  68. return false;
  69. }
  70. if(empty($mobiles) && !self::mobileFormat($mobiles)){
  71. return false;
  72. }
  73. $cust_code = \Yii::$app->params['message']['id'];
  74. $password =\Yii::$app->params['message']['pwd'];
  75. // $sp_code = '';
  76. $destMobiles = $mobiles;
  77. $url = "http://".\Yii::$app->params['message']['ip'].":8860/";
  78. $post_data = array();
  79. $post_data['cust_code'] = $cust_code;
  80. $post_data['content'] = $content;
  81. $post_data['destMobiles'] = $destMobiles;
  82. $post_data['sign'] =md5(urlencode($content.$password)); //签名
  83. $o="";
  84. foreach ($post_data as $k=>$v)
  85. {
  86. $o.= "$k=".urlencode($v)."&";
  87. }
  88. $post_data=substr($o,0,-1);
  89. ;
  90. $ch = curl_init();
  91. curl_setopt($ch, CURLOPT_POST, 1);
  92. curl_setopt($ch, CURLOPT_HEADER, 0);
  93. curl_setopt($ch, CURLOPT_URL,$url);
  94. //为了支持cookie
  95. // curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  96. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  98. $result = curl_exec($ch);
  99. $resultarr = self::resultHandle($result);
  100. self::saveShortMessage($destMobiles,$content,$resultarr);
  101. return $resultarr;
  102. }
  103. static function saveShortMessage($destMobiles,$content,$resultarr){
  104. $short = new Shortmsg();
  105. $short->msgid = isset($resultarr[0]['msgid'])?$resultarr[0]['msgid']:"";
  106. $short->recipient = $destMobiles;
  107. $short->content = $content;
  108. $short->receipt = isset($resultarr[0]['state'])?$resultarr[0]['state']:"失败";
  109. $short->created_at = time();
  110. $short->sendtime = time();
  111. if($short->validate() && $short->save()){
  112. }
  113. }
  114. /**
  115. * 限制请求频率
  116. * @param $mobile
  117. * @return bool
  118. */
  119. static function limitTime($mobile){
  120. $shortmsg = Shortmsg::find()->where('recipient = :mobile',[':mobile'=>$mobile])->orderBy('created_at DESC')->one();
  121. if(empty($shortmsg)){
  122. return true;
  123. }else{
  124. if($shortmsg->created_at < (time()-self::LIMIT_SECOND)){
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * 验证手机格式
  132. * @param $mobile
  133. * @return int
  134. */
  135. static function mobileFormat($mobile){
  136. return preg_match('/^1[34578]\d{9}$/',$mobile);
  137. }
  138. /**
  139. * 返回数据处理
  140. * @return bool
  141. */
  142. static function resultHandle($result){
  143. $reg = '/[\s]+/s';
  144. $result = preg_split($reg,$result);
  145. $resultarr = [];
  146. foreach($result as $k => $v){
  147. if($k != 0){
  148. $ar = mb_split(':',$v);
  149. if(count($ar) == 3){
  150. $resultarr[] = ['mobile'=>$ar[0],'msgid'=>$ar[1],'state'=>$ar[2]];
  151. }
  152. }
  153. }
  154. return $resultarr;
  155. }
  156. /**
  157. *接收下行的回执
  158. * @param $include
  159. * @return bool
  160. */
  161. static function receiveState($include){
  162. return false;
  163. }
  164. /**
  165. * 接收上行消息
  166. * @param $include
  167. * @return bool
  168. */
  169. static function receiveMessage($include){
  170. return false;
  171. }
  172. /**
  173. * 获取账号余额
  174. */
  175. static function getMoney(){
  176. $result= '';
  177. return $result;
  178. }
  179. }