MobileGetService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\service\mobile_get;
  3. use app\common\library\MobileConstant;
  4. use app\common\model\Area;
  5. use app\common\model\Mobile;
  6. use app\common\model\SysConfig;
  7. use app\common\service\MobileComputer;
  8. use GuzzleHttp\Client;
  9. use think\helper\Str;
  10. abstract class MobileGetService {
  11. protected function post($url,$data,$headers=[]){
  12. static $client;
  13. if(!$client){
  14. $client=new Client();
  15. }
  16. $body=[
  17. 'json'=>$data,
  18. ];
  19. if($headers){
  20. $body['headers']=$headers;
  21. }
  22. $result=$client->post($url,$body);
  23. return json_decode($result->getBody()->getContents(),true);
  24. }
  25. public function addMobile($data){
  26. $mobile=Mobile::where('no',$data['no'])->find();
  27. if(!$mobile){
  28. $mobile=new Mobile;
  29. $newer=1;
  30. }else{
  31. #非api号码不予增加
  32. if($mobile['type']!=$mobile::API){
  33. return false;
  34. }
  35. }
  36. $mobile->fill($data);
  37. $mobile['type']=$mobile::API;
  38. if(!empty($data['city'])){
  39. $city=Area::getModelByName($data['city'],2);
  40. if(!$city){
  41. return false;
  42. }
  43. $mobile['city_id']=$city['id'];
  44. $mobile['province_id']=$city['pid'];
  45. }
  46. if(isset($newer)) {
  47. // 底价都是0 ,售价就是我们设置的,代理价设置默认售价的5折,原价是售价的2倍
  48. //`amount_original` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '原价',
  49. //`amount_base` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '售价',
  50. //`amount_di` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '底价',
  51. //`amount_proxy` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '代理价',
  52. list($mobile['amount_base'],$rules)=$this->getAmount($mobile['no']);
  53. $mobile['amount_proxy'] = $mobile['amount_base']*0.5;
  54. $mobile['amount_original'] = $mobile['amount_base']*2;
  55. $mobile['amount_di'] = 0;
  56. $mobile->fill($rules);
  57. }
  58. $info_describe = $mobile['describe'];
  59. unset($mobile['describe']);
  60. if(!$mobile->save()){
  61. return false;
  62. };
  63. if(!$mobile->info) {
  64. $mobile->info()->save(['describe'=>$info_describe]);
  65. }
  66. return $mobile;
  67. }
  68. public function getAmount($no){
  69. static $config;
  70. if(!is_array($config)){
  71. $config=SysConfig::look('api_rule_price',[]);
  72. }
  73. $com=MobileComputer::setMobile($no);
  74. $tailRule=[];
  75. $rules=$com->filter();
  76. foreach ($rules as $key=>$is){
  77. if((Str::startsWith($key,'filter_middle') || Str::startsWith($key,'filter_tail')) && $is==1){
  78. $tailRule[]=MobileConstant::getFilterColumns()[$key];
  79. }
  80. }
  81. $tailRule=array_unique($tailRule);
  82. $prices=[0];
  83. foreach ($config as $key=>$priceArr){
  84. if(in_array($key,$tailRule)){
  85. if(empty($priceArr)||($priceArr['amount_min']==0 && $priceArr['amount_max']==0)){
  86. continue;
  87. }
  88. $prices[]=mt_rand($priceArr['amount_min'],$priceArr['amount_max']);
  89. }
  90. }
  91. if(!array_filter($prices)){
  92. $prices[]=mt_rand($config['none']['amount_min']??0,$config['none']['amount_max']??0);
  93. }
  94. return [
  95. max($prices),
  96. $rules,
  97. ];
  98. }
  99. protected static function log($data){
  100. user_log(basename(static::class),$data);
  101. }
  102. // 删除号码
  103. public function delMobile($api_goods_id){
  104. // 3天前 259200
  105. // $time = strtotime(date("Y-m-d",time())) - 259200;
  106. $num = (new Mobile())->where('mobile.type', Mobile::BEAUTI)
  107. ->whereIn('api_goods_id',$api_goods_id)
  108. ->whereNotIn('status','1')
  109. // ->where('create_time','<',$time)
  110. ->delete();
  111. Mobile::deleteOtherTableInfo();
  112. user_log('Noapidel',$logData=[
  113. 'date'=>date("Y-m-d",time()),
  114. 'num'=>$num,
  115. 'api_goods_id'=>$api_goods_id,
  116. ]);
  117. }
  118. }