// +---------------------------------------------------------------------- // | 修改者: anuo (本权限类在原3.2.3的基础上修改过来的) // +---------------------------------------------------------------------- namespace car; use think\Db; use think\Config; use think\Session; use think\Request; use fast\Http; use app\common\model\user4s\User; /** * 企信通短信 */ class Sms { /** * @var object 对象实例 */ protected static $instance; protected $rules = []; /** * http实例 * @var Http */ protected $http; //默认配置 protected $config = [ 'url' => 'http://47.106.229.82:8888/', // 接口地址 'userid' => 1, // 企业ID 'account' => '', // 账号 'password' => '', // 密码 ]; public function __construct($options = []) { if ($auth = Config::get('sms')) { $this->config = array_merge($this->config, $auth); } if(!empty($options)){ $this->config = array_merge($this->config, $options); } $this->http = new Http; return self::$instance; } /** * 给用户发送短信 支持变量模板 [ID] [姓名] 张三 [姓氏] 张 [联系方式] 1526425507 [余额] 1.00 [积分] 100 [性别] 男 女 [称呼] 先生 女士 [身份证] 3713261xxxxxxxxx [生日] 2022-05-08 [购车日期] 2022-05-08 [车牌号码] 鲁Q12345 [车架号] LGXC14DF6L0127066 [车型] 宋经典手动 [颜色] 水晶白 [保险类型] 交强险 [保险公司] 平安保险 人保 [保险日期] 2022-05-08 [备注] * @return array */ public function send($uid,$content,$sendTime='') { if(!is_array($uid)){ $user_ids[] = $uid; }else{ $user_ids = $uid; } if(empty($user_ids) || empty($content)){ $res['code'] = 0; $res['msg'] = '用户ID或内容为空'; return $res; } $return = array('code' => 1, 'total' => 0, 'success' => 0); $user = new User; $list = User::where('id', 'in', $user_ids)->select(); $list = collection($list)->toarray(); $user_list = array_column($list, null, 'id'); $prefix = Config::get('database.prefix'); $fieldArr = []; $table = $prefix . 'user4s_user'; $database = Config::get('database.database'); $pk = Db::getTableInfo($table, 'pk'); $list = db()->query( "SELECT COLUMN_NAME,COLUMN_COMMENT,COLUMN_TYPE,IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database] ); foreach ($list as $k => $v) { if ($v['COLUMN_NAME'] !== $pk) { $fieldArr[$v['COLUMN_COMMENT']] = $v; //['COLUMN_NAME'] } } foreach ($user_ids as $u) { if (!empty($user_list[$u]['tel'])) { foreach ($fieldArr as $name => $field) { if (stripos($name, ':') != false) { $name = explode(':', $name, 2)[0]; } if (stripos($content, '[' . $name . ']') != false && !empty($user_list[$u][$field['COLUMN_NAME']])) { $content = str_replace('[' . $name . ']', $user_list[$u][$field['COLUMN_NAME']], $content); } if(stripos($content, '[称呼]') != false){ if ($user_list[$u]['genderdata'] == 'female') { $content = str_replace('[称呼]', '女士', $content); }else{ $content = str_replace('[称呼]', '先生', $content); } } if(stripos($content, '[姓氏]') != false){ $xingshi = mb_substr($user_list[$u]['name'], 0, 1, 'utf-8'); $content = str_replace('[姓氏]', $xingshi, $content); } if (stripos($content, 'male') != false) { $content = str_replace('male', '男', $content); } if (stripos($content, 'female') != false) { $content = str_replace('female', '女', $content); } } //todo:测试 $res = $this->sendsms($user_list[$u]['tel'], $content, $sendTime,$user_list[$u]['id']); //$res = array('status'=>false,'message'=>'手机号为空'); if ($res['status'] == true) { $return['status'][] = array('code' => 1,'name'=>$user_list[$u]['name'],'tel'=>$user_list[$u]['tel'], 'msg' => '发送成功'); $return['total'] = $return['total'] + 1; $return['success'] = $return['success'] + 1; } else { $return['status'][] = array('code' => 0,'name'=>$user_list[$u]['name'],'tel'=>$user_list[$u]['tel'], 'msg' => $res['message']); $return['total'] = $return['total'] + 1; } } else { $return['total'] = $return['total'] + 1; $return['status'][] = array('code' => 0,'name'=>$user_list[$u]['name'],'tel'=>$user_list[$u]['tel'], 'msg' => '手机号为空'); } } return $return; } /** * 根据手机号发送短信 * @return array */ public function sendsms($mobile,$content,$sendTime='',$user_id=0) { if(empty($mobile) || empty($content)){ $res['status'] = false; $res['message'] = '手机号或内容为空'; return $res; } $url = $this->config['url'].'sms.aspx'; $params = $this->config; unset($params['url']); $params['action'] = 'send'; $params['extno'] = ''; $params['mobile'] = is_array($mobile)?implode(',', $mobile):$mobile; $params['content'] = $content; $params['sendTime'] = !empty($sendTime)?date('Y-m-d H:i:s',strtotime($sendTime)):''; $xml = $this->http->post($url,$params); //var_dump($url,$params,$xml); $res = (array)simplexml_load_string($xml); $log = array( 'user_id'=>$user_id, 'mobile'=>$mobile, 'content'=>$content, 'createtime'=>time(), 'res'=>json_encode($res,true), 'status'=>0, ); if($res['returnstatus'] == 'Faild'){ $res['status'] = false; }else{ $res['status'] = true; $log['status'] = 1; } //$table = Config::get('database.prefix') . 'car_smslog'; Db::name('smslog')->insert($log); //var_dump(Db::getlastsql()); return $res; } /** * 上行接口 * @return array */ public function getCall() { $url = $this->config['url'].'callApi.aspx'; $params = $this->config; unset($params['url']); $params['action'] = 'query'; $xml = $this->http->post($url,$params); //var_dump($url,$params,$xml); $res = (array)simplexml_load_string($xml); if(isset($res['errorstatus'])){ $res['status'] = false; $res['count'] = 0; return $res; }else{ $res['status'] = true; $res['count'] = isset($res['returnsms'])?count($res['returnsms']):0; return $res; } } /** * 状态报告 * @return array */ public function getStatus() { $url = $this->config['url'].'statusApi.aspx'; $params = $this->config; unset($params['url']); $params['action'] = 'query'; $xml = $this->http->post($url,$params); //var_dump($url,$params,$xml); $res = (array)simplexml_load_string($xml); if(isset($res['errorstatus'])){ $res['status'] = false; return $res; }else{ $res['status'] = true; return $res; } } /** * 非法关键词查询 * @return mixed */ public function check($content) { if(empty($content)){ return '短信内容为空'; } $url = $this->config['url'].'sms.aspx'; $params = $this->config; unset($params['url']); $params['action'] = 'checkkeyword'; $params['content'] = $content; $xml = $this->http->post($url,$params); //var_dump($url,$params,$xml); $res = (array)simplexml_load_string($xml); if($res['returnstatus'] == 'Faild'){ return $res['message']; }else{ return true; } } /** * 余额及已发送量查询 * @return mixed */ public function getOverage($params=[]) { $url = $this->config['url'].'sms.aspx'; $params = array_merge($this->config, $params); unset($params['url']); $params['action'] = 'overage'; $xml = $this->http->post($url,$params); $res = (array)simplexml_load_string($xml); if($res['returnstatus'] == 'Faild'){ $res['status'] = false; return $res; }else{ $res['status'] = true; return $res; } } }