public_key = $pub_key; $this->private_key = $pri_key; $pub_id = openssl_get_publickey($this->public_key); $this->key_len = openssl_pkey_get_details($pub_id)['bits']; } public static function createKeys($key_size = 2048) { $config = array( "private_key_bits" => $key_size, "private_key_type" => self::RSA_ALGORITHM_KEY_TYPE, ); $res = openssl_pkey_new($config); openssl_pkey_export($res, $private_key); $public_key_detail = openssl_pkey_get_details($res); $public_key = $public_key_detail["key"]; return [ "public_key" => $public_key, "private_key" => $private_key, ]; } public function publicEncrypt($data) { $encrypted = ''; $part_len = $this->key_len / 8 - 11; $parts = str_split($data, $part_len); foreach ($parts as $part) { $encrypted_temp = ''; openssl_public_encrypt($part, $encrypted_temp, $this->public_key); $encrypted .= $encrypted_temp; } return url_safe_base64_encode($encrypted); } public function privateDecrypt($encrypted) { $decrypted = ""; $part_len = $this->key_len / 8; $base64_decoded = url_safe_base64_decode($encrypted); $parts = str_split($base64_decoded, $part_len); foreach ($parts as $part) { $decrypted_temp = ''; openssl_private_decrypt($part, $decrypted_temp,$this->private_key); $decrypted .= $decrypted_temp; } return $decrypted; } public function privateEncrypt($data) { $encrypted = ''; $part_len = $this->key_len / 8 - 11; $parts = str_split($data, $part_len); foreach ($parts as $part) { $encrypted_temp = ''; openssl_private_encrypt($part, $encrypted_temp, $this->private_key); $encrypted .= $encrypted_temp; } return url_safe_base64_encode($encrypted); } public function publicDecrypt($encrypted) { $decrypted = ""; $part_len = $this->key_len / 8; $base64_decoded = url_safe_base64_decode($encrypted); $parts = str_split($base64_decoded, $part_len); foreach ($parts as $part) { $decrypted_temp = ''; openssl_public_decrypt($part, $decrypted_temp,$this->public_key); $decrypted .= $decrypted_temp; } return $decrypted; } public function sign($data) { openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN); return url_safe_base64_encode($sign); } public function verify($data, $sign) { $pub_id = openssl_get_publickey($this->public_key); $res = openssl_verify($data, url_safe_base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN); return $res; } } if (! function_exists('url_safe_base64_encode')) { function url_safe_base64_encode ($data) { return str_replace(array('+','/', '='),array('-','_', ''), base64_encode($data)); } } if (! function_exists('url_safe_base64_decode')) { function url_safe_base64_decode ($data) { $base_64 = str_replace(array('-','_'),array('+','/'), $data); return base64_decode($base_64); } } if (! function_exists('base64_to_url_safe_base64')) { function base64_to_url_safe_base64 ($data) { return str_replace(array('+', '/', '='),array('-', '_', ''), $data); } } if (! function_exists('url_safe_base64_to_base64')) { function url_safe_base64_to_base64 ($data) { return str_replace(array('-','_'),array('+','/'), $data); } } // 获取套餐剩余周期 function get_package_surplus($db,$rsa){ $netList = $db->query('select simid,id from network where (protocoltype = 1 or protocoltype = 2) AND simid != ""'); foreach ($netList as $v) { if (empty($v['simid'])) continue; $msisdnData = $db->query('select msisdn,type from msisdn where imsi = '.$v['simid']); if (!empty($msisdnData[0]) && !empty($msisdnData[0]['msisdn'])) { if ($msisdnData[0]['type'] == 0) { $msisdn = $msisdnData[0]['msisdn'] + 0; $data = array('_type' => 'list','msisdns' => [$msisdn]); $data = json_encode($data); //加密 $encrypted = $rsa->privateEncrypt($data); $url = "https://aiot.wl1688.net/docking/card/cardinfo.do"; $rt = http_post_data($url,$encrypted); //解密 $data = $rsa->privateDecrypt($rt); $data = json_decode($data,true); if (!empty($data) && $data['code'] == 0) { $cardData = $data['data']['cards'][0]; $db->table('network')->where('`id`='.$v['id'])->update(['packageSurplus'=>$cardData['period_number'],'down_time'=>date('Y-m-d H:i:s',$cardData['down_time']/1000),'is_charge_card'=>1,'monthly_can_usage'=>$cardData['monthly_can_usage'],'monthly_already_usage'=>$cardData['monthly_already_usage']]); } sleep(1); }else{ $url = 'http://api.quectel-service.com/openapi/router'; $msisdn = $msisdnData[0]['msisdn']; $appkey = '7A9137C166B948B747C7F493BFE097EB'; $sercet = '7ECB7521AD5D8239303A6AB7DC61D638'; $method = 'fc.function.card.info'; $time = time(); $str = 'appKey'.$appkey.'method'.$method.'msisdn'.$msisdn.'t'.$time; $sign = sha1($sercet.$str.$sercet); $reqData = array('appKey'=>$appkey,'t'=>$time,'method'=>$method,'sign'=>$sign,'msisdn'=>$msisdn); $data = request_post($url,$reqData); $info = json_decode($data,true); if ($info['resultCode'] == 0) { $flow = $info['flow']; // 已使用流量 $residueFlow = $info['residueFlow']; // 剩余流量 $all = $residueFlow + $flow; $expiryDate = $info['expiryDate']; // 套餐到期时间 if (empty($expiryDate)) { $db->table('network')->where('`id`='.$v['id'])->update(['packageSurplus'=>$residueFlow,'is_charge_card'=>1,'monthly_can_usage'=>$all,'monthly_already_usage'=>$flow]); }else{ $db->table('network')->where('`id`='.$v['id'])->update(['packageSurplus'=>$residueFlow,'down_time'=>$expiryDate,'is_charge_card'=>1,'monthly_can_usage'=>$all,'monthly_already_usage'=>$flow]); } } } } } } // 套餐到期提醒 function send_sms($db){ $netList = $db->query('select down_time,projectid,id from network where is_charge_card=1'); foreach ($netList as $v) { $projectData = $db->query('select company,projectname from project where id = '.$v['projectid']); if (empty($projectData[0]) || empty($projectData[0]['company']) || $projectData[0]['projectname'] == '') continue; $lampData = $db->query('select id,number,section from lampinfo where networkid = '.$v['id']); if (empty($lampData[0]) || empty($lampData[0]['id'])) continue; if (empty($v['down_time'])) continue; $dayCount = ceil((strtotime($v['down_time']) - time())/(24*3600)); if ($dayCount > 30) continue; if (empty($lampData[0]['section'])) { $msg = '【太阳能路灯系统】您位于'.$projectData[0]['projectname'].'项目路灯编号为'.$lampData[0]['number'].'的路灯剩余套餐不足'.$dayCount.'天!'; }else{ $msg = '【太阳能路灯系统】您位于'.$projectData[0]['projectname'].'项目'.$lampData[0]['section'].'路段路灯编号为'.$lampData[0]['number'].'的路灯剩余套餐不足'.$dayCount.'天!'; } // 获取公司管理员 $userList = $db->query('select mobile,email from user where role = 2 AND companyid = '.$projectData[0]['company']); foreach ($userList as $u) { if (!empty($u['mobile'])) { // 发送短信通知 $res = send_sms_msg($u['mobile'],$msg); if ($res != '00') { // 发送短信失败,重发 $db->table('send_sms_info')->insert(['phone'=>$u['mobile'],'msg'=>$msg]); } } if (!empty($u['mobile'])) { // 发送邮件通知 send_email($u['mobile'],$msg); } sleep(1); } } } // 发送短信通知 function send_sms_msg($phone, $message) { $config['epid'] = '124441'; $config['username'] = 'solar'; $config['password'] = 'Weclouds456'; $config['linkid'] = '00'; $config['signature'] = '【LampMind】'; $epid = $config['epid']; $username = $config['username']; $password = md5($config['password']); $linkid = $config['linkid']; $signature = $config['signature']; $subcode = ''; $message = urlencode(iconv("UTF-8", "GBK//IGNORE", $message)); $url = "http://q.hl95.com:8061/?username={$username}&password={$password}&epid={$epid}&linkid={$linkid}&subcode={$subcode}&phone={$phone}&message={$message}"; return http_get($url); } function http_get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1); $result = curl_exec($ch); curl_close($ch); return $result; } // 发送邮件 function send_email($to,$msg,$title = 'SIM卡套餐通知'){ require_once '../application/libraries/Smtp.php'; //******************** 配置信息 ******************************** $config['protocol'] = 'smtp'; $config['smtp_host'] = 'smtpdm.aliyun.com'; $config['user_email'] = 'admin@lampmind.com'; $config['smtp_user'] = 'admin@lampmind.com'; $config['smtp_pass'] = 'wecloudsSMTP2018'; $config['smtp_port'] = 80; $config['smtp_timeout'] = 30; $config['crlf'] = "\r\n"; $config['newline'] = "\r\n"; $config['mailtype'] = "html"; $smtpserver = $config['smtp_host'];//SMTP服务器 $smtpserverport = $config['smtp_port'];//SMTP服务器端口 $smtpusermail = $config['user_email'];//SMTP服务器的用户邮箱 $smtpemailto = $to;//发送给谁 $smtpuser = $config['smtp_user'];//SMTP服务器的用户帐号,注:部分邮箱只需@前面的用户名 $smtppass = $config['smtp_pass'];//SMTP服务器的用户密码 $mailtitle = $title;//邮件主题 $mailcontent = $msg;//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 //************************ 配置信息 **************************** $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp->debug = false;//是否显示发送的调试信息 return $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype); } require_once './DB.php'; date_default_timezone_set('Asia/Shanghai'); $publicKey = file_get_contents("../file/certificate/rsa_public_key.der"); $privateKey = file_get_contents('../file/certificate/private_key.pem'); $rsa = new XRsa($publicKey,$privateKey); $config = [ 'hostname' => 'rm-wz98r5cn33zq4ou980o.mysql.rds.aliyuncs.com', 'username' => 'lampmanager', 'password' => 'lampmanager@2019', 'dbname' => 'lampmanager', ]; $index = 0; while (1) { $db = new Db($config); $t = date('H'); if ($index >= 10) { // 1、获取剩余使用周期 get_package_surplus($db,$rsa); $index = 0; }else{ $index ++; } if ($t == '10') { // 2、发送短信通知 send_sms($db); sleep(3600); } $db->close(); sleep(30); } function request_post($url = '', $data = '') { $tuCurl = curl_init(); //参数 curl_setopt($tuCurl, CURLOPT_URL,$url); curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data); curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($tuCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($tuCurl, CURLOPT_SSLVERSION, 1); curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1); $tuData = curl_exec($tuCurl); curl_close($tuCurl); return $tuData; } function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string), 'customId:741') ); ob_start(); curl_exec($ch); $return_content = ob_get_contents(); // echo $return_content."
"; ob_end_clean(); $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); return $return_content; }