REAPI/app/kami/controller/suoka/Zhongzhao.php
2024-09-29 15:43:18 +08:00

390 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\kami\controller\suoka;
use app\kami\controller\getcookie\ZhongZhao as ZhongZhaoCookie;
use dever\Log;
use think\admin\Controller;
#中兆
class Zhongzhao extends Core
{
# 登录获取
public function get_card_kind($pid,$cash)
{
$info = $this->getProductInfo($pid);
if ($info) {
if (isset($info['gid_rule']) && $info['gid_rule']) {
$rule = json_decode($info['gid_rule'], true);
if (isset($rule[$cash]) && $rule[$cash]) {
$gid = $rule[$cash];
return $gid;
}
}
if (isset($info['gid']) && $info['gid']) {
return $info['gid'];
}
}
}
public function lockcard($cid,$cardno,$cardpwd,$pid,$cash,$num=0,$type=1){
$suokaRequest = $this->getChanelSuoKaData($cid);
if(!$suokaRequest == 'error'){
return 'error';
}
$url = $suokaRequest['suoka_url'];
$cookie_path = $this->getcCookiePatch($cid);
// /www/sites/reapi/index/extend/data/cookie/5//cookie.txt
$cookie_path .= 'cookie.txt';
// $cookies = file_get_contents($cookie_path);
// var_dump($cookies);die;
if (is_readable($cookie_path)) {
$cookies = file_get_contents($cookie_path);
// 处理文件内容
} else {
// 文件不存在或不可读
$ZhongZhaoCookie = new ZhongZhaoCookie($this->app);
$request = [
'login_info' => [
'username' => $suokaRequest['username'],
'password' => $suokaRequest['password'],
'type' => ''
],
'login_url' => $suokaRequest['login_url'],
'cid' => $cid
];
$cookie_path = $ZhongZhaoCookie->login($request);
if($cookie_path == 'error'){
return $cookie_path;
}else{
$cookies = file_get_contents($cookie_path);
}
}
// var_dump(1);die;
$suoka_id = $this->get_suoka_id($suokaRequest['suokaid_url'], $cookie_path, $cardno,$type);
if($suoka_id == '未找到卡号'){
if (is_readable($cookie_path)) {
unlink($cookie_path);
// 处理文件内容
}
if($num == 0){
$this->UnlockCard($cid, $cardno, $cardpwd, $pid, $cash,1);
}
return 'error';
}
if($type == 1 && $suoka_id == '已锁定'){
return 'old_ok';
}
if($type == 2 && $suoka_id == '正常'){
return 'ok';
}
if($suoka_id == '已使用'){
return 'use_error';
}
if(is_numeric($suoka_id)){
$headers = array(
'X-Requested-With: XMLHttpRequest',
'Content-Type: application/x-www-form-urlencoded',
'User-Agent: Apifox/1.0.0 (https://apifox.com)',
'Accept: */*',
'Host: 101.200.165.85',
'Connection: keep-alive',
);
$msg = $this->send_lock_card($headers, $cookie_path, $suoka_id, $url, $suokaRequest, $cardno,0,$type);
if($msg == 'http_error'){
if (is_readable($cookie_path)) {
unlink($cookie_path);
// 处理文件内容
}
if($type == 2 && $num == 0){
$this->UnlockCard($cid, $cardno, $cardpwd, $pid, $cash,1);
}elseif($type == 2 && $num == 0){
$this->suoka($cid, $cardno, $cardpwd, $pid, $cash,1);
}
return 'error';
}elseif($msg != 'ok'){
Log::write('kami', 'zhongzhao', $cardno);
return 'error';
}
return 'ok';
}else{
return 'error';
}
// var_dump($suoka_id);die;
}
#获取锁卡id
public function get_suoka_id($url,$cookie_path,$cardno,$type= 1){
$postData = [
'rekey' => $cardno
];
$options['cookie_file'] = $cookie_path;
$result = http_get( $url, $postData, $options);
$table= $this->get_between($result, '<table class="table table-custom selldetail-records">', '</table>');
$table= $this->get_between($table, '<tbody>', '</tbody>');
$need_value = $this->get_between($table, '<input type="checkbox" value="', '" class="order-cancel-checkbox">');
if(!strpos($table, $cardno)){
return "未找到卡号";
}
if(strpos($table, "已锁定")){
if($type==1){
return "已锁定";
}
}
if(strpos($table, "正常")){
if($type==2){
return "正常";
}
}
// if(strpos($table, "正常")){
// return "正常";
// }
if(strpos($table, "该卡已使用,不能锁定")){
return "已使用";
}
if(is_numeric($need_value)){
return $need_value;
}
return "未找到可用锁卡id";
}
/**
* php截取指定两个字符之间字符串方式二
* @param string $str 需要截取的字符串
* @param string $start 开始字符串
* @param string $end 结束字符串
* @return string
*/
public function get_between($str, $start, $end) {
$substr = substr($str, strlen($start)+strpos($str, $start),(strlen($str) - strpos($str, $end))*(-1));
return $substr;
}
public function UnlockCard($cid,$cardno,$cardpwd,$pid,$cash,$num = 0)
{
$unlockcard = $this->lockcard($cid, $cardno, $cardpwd, $pid, $cash,$num,2);
return $unlockcard;
}
public function send_lock_card($headers,$cookie_path,$suoka_id,$url,$suokaRequest,$cardno,$num=0,$type=1){
$options['headers'] = $headers;
$options['cookie_file'] = $cookie_path;
$postData = [
'ids' => $suoka_id,
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'ids='.$suoka_id,
CURLOPT_COOKIEFILE=>$cookie_path,
CURLOPT_HTTPHEADER => array(
'X-Requested-With: XMLHttpRequest',
// 'Cookie: PHPSESSID=f843c2d3947b218df078ced2afb2933e ; PHPSESSID=f843c2d3947b218df078ced2afb2933e',
'User-Agent: Apifox/1.0.0 (https://apifox.com)',
'Accept: */*',
'Host: 101.200.165.85',
'Connection: keep-alive',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
// $result = http_post( $url, $postData, $options);
$result = json_decode($response,true);
// var_dump($response);die;
if(isset($result['msg']) && $result['msg'] == '总共操作1笔设置成功 1笔') {
// var_dump($result['msg']);die;
$suoka_ids = $this->get_suoka_id($suokaRequest['suokaid_url'], $cookie_path, $cardno,$type);
while ($type == 1 && $suoka_ids != '已锁定'&&$num<=3) {
$num++;
return $this->send_lock_card($headers,$cookie_path,$suoka_id,$url,$suokaRequest,$cardno,$num);
}
while ($type == 2 && $suoka_ids != '正常'&&$num<=3) {
$num++;
return $this->send_lock_card($headers,$cookie_path,$suoka_id,$url,$suokaRequest,$cardno,$num,2);
}
if ($type==1 && $suoka_ids == '已锁定') {
return 'ok';
}
if ($type==2 && $suoka_ids == '正常') {
return 'ok';
}
if($num>3) return 'error';
}else{
$suoka_ids = $this->get_suoka_id($suokaRequest['suokaid_url'], $cookie_path, $cardno,$type);
if ($type==1 && $suoka_ids == '已锁定') {
return 'ok';
}elseif($suoka_ids == '已使用'){
return 'use_error';
}elseif($type==2 && $suoka_ids == '正常') {
return 'ok';
}
}
return 'http_error';//请求失败或者错误
}
public function suoka($cid,$cardno,$cardpwd,$pid,$cash,$num = 0)
{
$suokaRequest = $this->getChanelSuoKaData($cid);
if(!$suokaRequest == 'error'){
return 'error';
}
$url = $suokaRequest['suoka_url'];
$cookie_path = $this->getcCookiePatch($cid);
// /www/sites/reapi/index/extend/data/cookie/5//cookie.txt
$cookie_path .= 'cookie.txt';
// $cookies = file_get_contents($cookie_path);
// var_dump($cookies);die;
if (is_readable($cookie_path)) {
$cookies = file_get_contents($cookie_path);
// 处理文件内容
} else {
// 文件不存在或不可读
$ZhongZhaoCookie = new ZhongZhaoCookie($this->app);
$request = [
'login_info' => [
'username' => $suokaRequest['username'],
'password' => $suokaRequest['password'],
'type' => ''
],
'login_url' => $suokaRequest['login_url'],
'cid' => $cid
];
$cookie_path = $ZhongZhaoCookie->login($request);
if($cookie_path == 'error'){
return $cookie_path;
}else{
$cookies = file_get_contents($cookie_path);
}
}
// var_dump(1);die;
$suoka_id = $this->get_suoka_id($suokaRequest['suokaid_url'], $cookie_path, $cardno);
// var_dump($suoka_id);die;
if($suoka_id == '未找到卡号'){
unlink($cookie_path);
if($num == 0){
$this->suoka($cid, $cardno, $cardpwd, $pid, $cash,1);
}
return 'error';
}
if($suoka_id == '已锁定'){
return 'old_ok';
}
if($suoka_id == '已使用'){
return 'use_error';
}
if(is_numeric($suoka_id)){
$headers = array(
'X-Requested-With: XMLHttpRequest',
'Content-Type: application/x-www-form-urlencoded',
'User-Agent: Apifox/1.0.0 (https://apifox.com)',
'Accept: */*',
'Host: 101.200.165.85',
'Connection: keep-alive',
);
$msg = $this->send_lock_card($headers, $cookie_path, $suoka_id, $url, $suokaRequest, $cardno);
if($msg == 'http_error'){
unlink($cookie_path);
if($num == 0){
$this->suoka($cid, $cardno, $cardpwd, $pid, $cash,1);
}
return 'error';
}elseif($msg != 'ok'){
Log::write('kami', 'zhongzhao', $cardno);
return 'error';
}
return 'ok';
}else{
return 'error';
}
}
}