123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace app\common\model;
- use app\common\library\monsms\SmsSendConn;
- use App\common\model\PushLog;
- class PushSmsMon {
- // private $url ='http://61.145.229.28:8803/sms/v2/std/';
- // private $user_id ='J94975';
- // private $pwd = '100777';
- //梦网短信消息
- public function send($pushLog) {
- $ftype = $pushLog->ftype;
- $title = $pushLog->title;
- $content = $pushLog->content;
- $description = $pushLog->description;
- $isAll = $pushLog->is_all;
- $mobileArray = [];
- $account_type = 'normal';
- $mobileArray = explode(",", $description);
-
- if (count($mobileArray) == 0) {
- $pushLog->status = 2; // 发送失败
- $pushLog->err_msg = "手机号为空";
- $pushLog->save();
- return;
- }
- $num = 999; //每次群发不超过999个号码 多于200 将进行分组
- $group = [];
- for ($i = 0; $i < count($mobileArray); $i++) {
- $group[$i / $num][] = $mobileArray[$i];
- }
- $content = $pushLog->content;
- $errMsg = [];
- //分组发送
- $status = 1;
- $success_num=0;
- // Log::stack(['job'])->debug('send sms:' . json_encode($group));
- foreach ($group as $key => $mobileArray) {
- $ret = $this->pushSmsData($content, $mobileArray,[],$account_type);
- if (is_array($ret) && $ret['result'] != 0) {
- $status = 2;
- }
- else{
- $success_num+= count($mobileArray);
- }
- $errMsg[] = $ret;
- }
- if($success_num==0){
- $status = 2;
- }
- $bs = $this->calWordsBs($content);
- $sms_fee_count = $success_num*$bs;
- // Log::stack(['job'])->debug('send sms mon finish:' . json_encode($errMsg).' bs:'.$bs." sms_fee_count:".$sms_fee_count);
- $pushLog->status = $status; // 发送状态: 0,发送中 1,发送完成 2,发送失败
- $pushLog->success_users = $success_num;//发送成功的短信数
- $pushLog->sms_fee_count = $sms_fee_count;
- $pushLog->err_msg = json_encode($errMsg);
- $pushLog->save();
- }
- //批量 推送参数
- public function pushSmsData($content , $mobileArray = [], $params=[],$account_type) {
- list($url,$user_id,$pwd) = $this->getAccountAndUrl($account_type);
- $sms = new SmsSendConn($url);
- $data['userid'] =$user_id;
- $data['pwd'] = $pwd;
- $data['content'] = $content;
- $data['mobile']=implode(',', $mobileArray);
- $outdata =[];
-
- $result = $sms->batchSend($data,$outdata);
- // Log::stack(['job'])->debug('pushSmsDataMon:' . json_encode($data).' request encryptdata:'.json_encode($outdata));
- return $result;
- }
- //查询余额
- public function getBalance($type = 'normal'){
- list($url,$user_id,$pwd) = $this->getAccountAndUrl($type);
- $sms = new SmsSendConn($url);
- $data['userid'] =$user_id;
- $data['pwd'] = $pwd;
- $result = $sms->getBalance($data);
- return $result;
- }
- //剩余短信条数 balance ,money 剩余金额
- public function getMessNum(){
- $result1 = $this->getBalance('normal');
- $res1 = 0;
- $res2 = 0;
- if($result1['result']==0){
- $res1 = $result1['balance'];
- }
- $result2 = $this->getBalance('yinxiao');
- if($result2['result']==0){
- $res2 = $result2['balance'];
- }
- return "普通账号:".$res1.",营销账号:".$res2;
- }
- //区分生产账号和营销账号
- public function getAccountAndUrl($type = 'normal'){
- if($type=='normal'){
- $url ='http://61.145.229.28:8803/sms/v2/std/';
- $user_id ='JU1942';
- $pwd = '659021';
- return [$url,$user_id,$pwd];
- }else{
- $url ='http://112.91.147.38:8803/sms/v2/std/';
- $user_id ='JU1943';
- $pwd = '659021';
- return [$url,$user_id,$pwd];
- }
- }
- //通过模板id获取 短信类型
- public function getAccountType($tmp_id){
- return 'normal';
- // $tmp = Template::where("tmp_id",$tmp_id)->first();
- // if(empty($tmp)||$tmp->type==0){
- // return 'normal';
- // }
- // else{
- // return 'wbs';
- // }
- }
- //计算单词为70的N倍数
- public function calWordsBs($content){
- $len = mb_strlen($content);
- $b = intval($len/70)+1;
- return $b;
- }
- }
|