PushSmsMon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\monsms\SmsSendConn;
  4. use App\common\model\PushLog;
  5. class PushSmsMon {
  6. // private $url ='http://61.145.229.28:8803/sms/v2/std/';
  7. // private $user_id ='J94975';
  8. // private $pwd = '100777';
  9. //梦网短信消息
  10. public function send($pushLog) {
  11. $ftype = $pushLog->ftype;
  12. $title = $pushLog->title;
  13. $content = $pushLog->content;
  14. $description = $pushLog->description;
  15. $isAll = $pushLog->is_all;
  16. $mobileArray = [];
  17. $account_type = 'normal';
  18. $mobileArray = explode(",", $description);
  19. if (count($mobileArray) == 0) {
  20. $pushLog->status = 2; // 发送失败
  21. $pushLog->err_msg = "手机号为空";
  22. $pushLog->save();
  23. return;
  24. }
  25. $num = 999; //每次群发不超过999个号码 多于200 将进行分组
  26. $group = [];
  27. for ($i = 0; $i < count($mobileArray); $i++) {
  28. $group[$i / $num][] = $mobileArray[$i];
  29. }
  30. $content = $pushLog->content;
  31. $errMsg = [];
  32. //分组发送
  33. $status = 1;
  34. $success_num=0;
  35. // Log::stack(['job'])->debug('send sms:' . json_encode($group));
  36. foreach ($group as $key => $mobileArray) {
  37. $ret = $this->pushSmsData($content, $mobileArray,[],$account_type);
  38. if (is_array($ret) && $ret['result'] != 0) {
  39. $status = 2;
  40. }
  41. else{
  42. $success_num+= count($mobileArray);
  43. }
  44. $errMsg[] = $ret;
  45. }
  46. if($success_num==0){
  47. $status = 2;
  48. }
  49. $bs = $this->calWordsBs($content);
  50. $sms_fee_count = $success_num*$bs;
  51. // Log::stack(['job'])->debug('send sms mon finish:' . json_encode($errMsg).' bs:'.$bs." sms_fee_count:".$sms_fee_count);
  52. $pushLog->status = $status; // 发送状态: 0,发送中 1,发送完成 2,发送失败
  53. $pushLog->success_users = $success_num;//发送成功的短信数
  54. $pushLog->sms_fee_count = $sms_fee_count;
  55. $pushLog->err_msg = json_encode($errMsg);
  56. $pushLog->save();
  57. }
  58. //批量 推送参数
  59. public function pushSmsData($content , $mobileArray = [], $params=[],$account_type) {
  60. list($url,$user_id,$pwd) = $this->getAccountAndUrl($account_type);
  61. $sms = new SmsSendConn($url);
  62. $data['userid'] =$user_id;
  63. $data['pwd'] = $pwd;
  64. $data['content'] = $content;
  65. $data['mobile']=implode(',', $mobileArray);
  66. $outdata =[];
  67. $result = $sms->batchSend($data,$outdata);
  68. // Log::stack(['job'])->debug('pushSmsDataMon:' . json_encode($data).' request encryptdata:'.json_encode($outdata));
  69. return $result;
  70. }
  71. //查询余额
  72. public function getBalance($type = 'normal'){
  73. list($url,$user_id,$pwd) = $this->getAccountAndUrl($type);
  74. $sms = new SmsSendConn($url);
  75. $data['userid'] =$user_id;
  76. $data['pwd'] = $pwd;
  77. $result = $sms->getBalance($data);
  78. return $result;
  79. }
  80. //剩余短信条数 balance ,money 剩余金额
  81. public function getMessNum(){
  82. $result1 = $this->getBalance('normal');
  83. $res1 = 0;
  84. $res2 = 0;
  85. if($result1['result']==0){
  86. $res1 = $result1['balance'];
  87. }
  88. $result2 = $this->getBalance('yinxiao');
  89. if($result2['result']==0){
  90. $res2 = $result2['balance'];
  91. }
  92. return "普通账号:".$res1.",营销账号:".$res2;
  93. }
  94. //区分生产账号和营销账号
  95. public function getAccountAndUrl($type = 'normal'){
  96. if($type=='normal'){
  97. $url ='http://61.145.229.28:8803/sms/v2/std/';
  98. $user_id ='JU1942';
  99. $pwd = '659021';
  100. return [$url,$user_id,$pwd];
  101. }else{
  102. $url ='http://112.91.147.38:8803/sms/v2/std/';
  103. $user_id ='JU1943';
  104. $pwd = '659021';
  105. return [$url,$user_id,$pwd];
  106. }
  107. }
  108. //通过模板id获取 短信类型
  109. public function getAccountType($tmp_id){
  110. return 'normal';
  111. // $tmp = Template::where("tmp_id",$tmp_id)->first();
  112. // if(empty($tmp)||$tmp->type==0){
  113. // return 'normal';
  114. // }
  115. // else{
  116. // return 'wbs';
  117. // }
  118. }
  119. //计算单词为70的N倍数
  120. public function calWordsBs($content){
  121. $len = mb_strlen($content);
  122. $b = intval($len/70)+1;
  123. return $b;
  124. }
  125. }