utils.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/9/26 0026
  6. * Time: 下午 3:24
  7. */
  8. class Utils
  9. {
  10. public $timelist;
  11. public $timearray;
  12. private $day = 1;
  13. private $datatamp = 86400;
  14. private $signvar = 1;
  15. public function __construct()
  16. {
  17. unset($this->timelist);
  18. unset($this->timearray);
  19. $this->GetDayList();
  20. }
  21. public function GetDayList()
  22. {
  23. $totalDay = date("t");
  24. //获取本月第一天时间戳
  25. $start_time = strtotime(date('Y-m-01'));
  26. $array = array();
  27. for ($i = 0; $i < $totalDay; $i++) {
  28. //每隔一天赋值给数组
  29. $array[] = date('Ymd', $start_time + $i * $this->datatamp);
  30. }
  31. $this->timearray = $array;
  32. return $array;
  33. }
  34. /***
  35. * 活动 data info
  36. * @param null $array
  37. * @return bool
  38. */
  39. public function SetDay($array = null)
  40. {
  41. $array = (isset($array) && !empty($array)) ? $array : $this->timearray;
  42. if (empty($array) || count($array) <= ZERO) {
  43. return false;
  44. }
  45. $i = $this->day;
  46. foreach ($array as $times) {
  47. $data[$i] = array
  48. (
  49. 'day' => $times,
  50. );
  51. $i++;
  52. }
  53. return ['sign' => $data];
  54. }
  55. /***
  56. * 设置 sign
  57. * @param string $time
  58. * @return array
  59. */
  60. public function SetSignDay($data = array(), $time = '20180902')
  61. {
  62. // $data = $this->SetDay();
  63. $dataOut = [];
  64. foreach ($data['sign'] as $key => $var) {
  65. if ($var['day'] == $time) {
  66. $var['sign'] = $this->signvar;
  67. }
  68. $dataOut[$key] = $var;
  69. }
  70. return ['sign' => $dataOut];
  71. }
  72. public function SetMesage($data, $mesage, $code = ZERO)
  73. {
  74. $ret = ($code == ZERO) ? 'success' : 'failure';
  75. if (isset($data) && count($data) > ZERO) {
  76. $data['ret'] = $ret;
  77. $data['msg'] = $mesage;
  78. $data['login'] = true;
  79. }
  80. return $data;
  81. }
  82. public function GetdefaultConfig()
  83. {
  84. $data = $this->SetDay();
  85. $data['ret'] = 'success';
  86. $data['msg'] = 'default day list ';
  87. $data['login'] = true;
  88. return $data;
  89. }
  90. /***
  91. * 礼包规则配置 获奖励规则
  92. */
  93. public function isGiftDay()
  94. {
  95. }
  96. /***
  97. * Json 生成规则
  98. */
  99. public function ActivityConfig($pc = null, $channel = null)
  100. {
  101. $Acitvity = [
  102. 'pc' => $pc,
  103. 'channel' => $channel
  104. ];
  105. // $dd = json_encode($Acitvity);
  106. }
  107. /**
  108. * 是不是简单基础类型(null, boolean , string, numeric)
  109. * @param $object
  110. * @return bool
  111. */
  112. function isPrimary($object)
  113. {
  114. return is_null($object) || is_bool($object) || is_string($object) || is_numeric($object);
  115. }
  116. function isBlank($object)
  117. {
  118. if (is_null($object) || '' === $object || (is_array($object) && count($object) < 1)) {
  119. return true;
  120. }
  121. return empty($object);
  122. }
  123. /***
  124. * @param $json_config_name
  125. * @return bool|mixed
  126. */
  127. function getFileConfig($file_name)
  128. {
  129. log_message::info(RES_CONFIG_URL . DIR_SEPARATOR . 'res/config/' . $file_name);
  130. if ($headers = get_headers(RES_CONFIG_URL . DIR_SEPARATOR . 'res/config/' . $file_name)) {
  131. if (strpos($headers[ZERO], '404') === false) {
  132. $url = RES_CONFIG_URL . DIR_SEPARATOR . 'res/config/' . $file_name;
  133. $pagecontent = trim(file_get_contents($url));
  134. if (!isBlank($pagecontent)) // runtime
  135. {
  136. return json_decode($pagecontent, true);
  137. }
  138. log_message::info('open url json data null');
  139. return false;
  140. }
  141. log_message::info('open url json false');
  142. return false;
  143. }
  144. log_message::info('json url false');
  145. return false;
  146. }
  147. /***
  148. * 区服区间验证
  149. */
  150. public function serverVerif($role_sid, $min_sid, $max_sid)
  151. {
  152. if (($role_sid >= $min_sid) && ($role_sid <= $max_sid)) {
  153. return true;
  154. }
  155. log_message::info('user sid 区间有误');
  156. return false;
  157. }
  158. /***
  159. * @param $url
  160. * @param $data
  161. * @param string $coding
  162. * @param string $refererUrl
  163. * @param string $method
  164. * @param string $contentType
  165. * @param int $timeout
  166. * @param bool $proxy
  167. * @return bool|null|string
  168. */
  169. public static function send_request($url, $data, $coding = 'gbk', $refererUrl = '',
  170. $method = 'POST', $contentType = 'application/json;', $timeout = 30, $proxy = false)
  171. {
  172. $ch = $responseData = null;
  173. //$data = trim(mb_convert_encoding($data, "gbk", "utf-8"));
  174. if ('POST' === strtoupper($method)) {
  175. $ch = curl_init($url);
  176. curl_setopt($ch, CURLOPT_POST, 1);
  177. curl_setopt($ch, CURLOPT_HEADER, 0);
  178. curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  179. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  180. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  181. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  182. $info = curl_getinfo($ch);
  183. if ($refererUrl) {
  184. curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
  185. }
  186. $contentType = '';
  187. if ($contentType) {
  188. curl_setopt($ch, CURLOPT_HTTPHEADER, $contentType);
  189. }
  190. if (isset($data['upload_file'])) {
  191. $file = new \CURLFile($data['upload_file']['file'],
  192. $data['upload_file']['type'], $data['upload_file']['name']);
  193. $params[$data['upload_file']['get_name']] = $file;
  194. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  195. } else {
  196. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  197. }
  198. } else if ('GET' === strtoupper($method)) {
  199. if (is_string($data)) {
  200. $real_url = $url . rawurlencode($data);
  201. } else {
  202. $real_url = $url . http_build_query($data);
  203. }
  204. //$urldata = rawurlencode($data);
  205. $ch = curl_init($real_url);
  206. curl_setopt($ch, CURLOPT_HEADER, 0);
  207. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:' . $contentType]);
  208. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  209. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  210. if ($refererUrl) {
  211. curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
  212. }
  213. } else {
  214. $args = func_get_args();
  215. return false;
  216. }
  217. if ($proxy) {
  218. curl_setopt($ch, CURLOPT_PROXY, $proxy);
  219. }
  220. $ret = curl_exec($ch);
  221. curl_close($ch);
  222. $responseData = $ret;
  223. //$responseData = mb_convert_encoding($ret, "utf-8", "gbk");
  224. return $responseData;
  225. }
  226. /**
  227. * @param $messige
  228. * @param null $status
  229. * @param null $pact
  230. * @return false|string
  231. */
  232. public static function errorCode($messige, $status = null, $pact = null)
  233. {
  234. if (!isDatas($messige)) {
  235. $status = isDatas($status) ? $status : FAILURE;
  236. }
  237. $data = array(
  238. 'msg' => $messige,
  239. 'status' => $status,
  240. 'pact' => $pact,
  241. );
  242. return json_encode($data, JSON_UNESCAPED_UNICODE);
  243. }
  244. /***
  245. * @param $messige
  246. * @param array $result_dat
  247. * @param null $status
  248. * @return false|string
  249. */
  250. public static function sendResults($messige, $result_dat = null, $status = null)
  251. {
  252. $status = !isDatas($status) ? $status : FAILURE;
  253. $data = array(
  254. 'msg' => $messige,
  255. 'result' => $status,
  256. 'val' => null
  257. );
  258. $result_dat = is_string($result_dat) ? json_decode($result_dat, true) : $result_dat;
  259. if (is_array($result_dat)) {
  260. $data = array_merge($data, $result_dat);
  261. }
  262. return json_encode($data, JSON_UNESCAPED_UNICODE);
  263. //return $data;
  264. }
  265. /***
  266. * @param $tag
  267. * @return bool
  268. */
  269. public static function config($tag = null, $var = null)
  270. {
  271. $url = require "config.php";
  272. if (isset($url[$tag]) && !empty($url[$tag])) {
  273. return !empty($var) ? $url[$tag][$var] : $url[$tag];
  274. }
  275. log_message::info('configs key is null ');
  276. return $url;
  277. }
  278. /***
  279. * @param $data
  280. * @return mixed|null
  281. */
  282. public static function decodeDate($data)
  283. {
  284. if (isDatas($data)) {
  285. if (is_array($data) && count($data) > ZERO) {
  286. return $data;
  287. }
  288. return json_decode($data, true);
  289. }
  290. log_message::info('decode date is null');
  291. return null;
  292. }
  293. /***
  294. * @param $proArr
  295. * @return int|string
  296. */
  297. public static function getPrizeRand($proArr)
  298. {
  299. log_message::info("&&&&&&&&&&&&++", json_encode($proArr, JSON_UNESCAPED_UNICODE));
  300. $result = null;
  301. //概率数组的总概率精度
  302. $proSum = array_sum($proArr);
  303. //概率数组循环
  304. // 修改 key 则需要修改这里的取值范围
  305. foreach ($proArr as $key => $proCur) {
  306. $randNum = mt_rand(ONE, $proSum);
  307. if ($randNum <= $proCur) {
  308. $result = $key;
  309. break;
  310. } else {
  311. $proSum -= $proCur;
  312. }
  313. }
  314. unset ($proArr);
  315. return $result;
  316. }
  317. public static function prizeAssembly($proArr)
  318. {
  319. $data = [];
  320. if (isDatas($proArr)) {
  321. foreach ($proArr as $key => $proCur) {
  322. $data[$proCur['prize_id']] = json_encode($proCur, JSON_UNESCAPED_UNICODE);
  323. }
  324. return $data;
  325. }
  326. return false;
  327. }
  328. /***
  329. * 干预判断 如果 不是无限制的类型并且 game 上限大于或等于用户已抽取的次数
  330. * 表示已经达到上限符合干预条件需要系统重新随机奖品
  331. * @param $role_upper_limit
  332. * @param $game_upper_limit
  333. * @return bool
  334. */
  335. public static function isUpperlimit($role_upper_limit, $game_upper_limit)
  336. {
  337. if (($game_upper_limit != ZERO && $game_upper_limit >= $role_upper_limit)) {
  338. return true;
  339. }
  340. return false;
  341. }
  342. /****
  343. * 判断有效范围内可增加抽取次数并返回客户端
  344. * @param $role_upper_limit
  345. * @param $game_upper_limit
  346. * @return bool
  347. */
  348. public static function isNayUpperLimit($role_upper_limit, $game_upper_limit)
  349. {
  350. if (($game_upper_limit != ZERO && $role_upper_limit > $game_upper_limit) || $game_upper_limit == ZERO) {
  351. return true;
  352. }
  353. return false;
  354. }
  355. /***
  356. * @param $data_at
  357. * @return array
  358. */
  359. public static function successiveTime($data_at)
  360. {
  361. sort($data_at);
  362. $data = [];
  363. for ($i = 1; $i < count($data_at); $i++) {
  364. $lastone = strtotime($data_at[$i - 1]);
  365. $thisone = strtotime($data_at[$i]);
  366. if ($thisone - $lastone != 3600 * 24) {
  367. $data = [];
  368. } else {
  369. $data[] = date('Y-m-d', $thisone);
  370. }
  371. }
  372. return $data;
  373. }
  374. // 二维数组按指定的键值排序 $reque_data_out, "power", "desc", HUNDRED
  375. public static function array_sort($array, $keys, $type = 'asc', $limit = null, $dimen = TWO)
  376. {
  377. if (!is_array($array) || empty($array) || !in_array(strtolower($type), array('asc', 'desc'))) return '';
  378. $keysvalue = array();
  379. foreach ($array as $key => $val) {
  380. $val[$keys] = str_replace('-', '', $val[$keys]);
  381. $val[$keys] = str_replace(' ', '', $val[$keys]);
  382. $val[$keys] = str_replace(':', '', $val[$keys]);
  383. $keysvalue[] = $val[$keys];
  384. }
  385. asort($keysvalue);//key值排序
  386. reset($keysvalue);//指针重新指向数组第一个
  387. foreach ($keysvalue as $key => $vals) {
  388. $keysort[] = $key;
  389. }
  390. $keysvalue = array();
  391. $count = count($keysort);
  392. if (strtolower($type) != 'asc') {
  393. for ($i = $count - 1; $i >= 0; $i--) {
  394. $keysvalue[] = $array[$keysort[$i]];
  395. }
  396. } else {
  397. for ($i = 0; $i < $count; $i++) {
  398. $keysvalue[] = $array[$keysort[$i]];
  399. }
  400. }
  401. if (!empty($limit)) {
  402. array_slice($keysvalue, ZERO, $limit);
  403. }
  404. return $keysvalue;
  405. }
  406. /***
  407. * @param $filepatch
  408. * @param $filename
  409. * @param $string
  410. * @return bool
  411. */
  412. public static function mkdirFile($filepatch, $filename, $string)
  413. {
  414. if (is_array($string)) {
  415. $string = json_encode($string, JSON_UNESCAPED_UNICODE);
  416. }
  417. if (file_exists($filepatch)) {
  418. $myfile = fopen($filepatch . $filename, "w");
  419. fwrite($myfile, $string);
  420. return true;
  421. }
  422. log_message::info('mkdir file false...');
  423. return false;
  424. }
  425. /***
  426. * @param array $data
  427. * @return bool|null
  428. */
  429. public static function prizeFormat($data = [])
  430. {
  431. $cache_data = null;
  432. sort($data);
  433. foreach ($data as $val) {
  434. $cache_data[$val['prize_id']] = $val;
  435. }
  436. if (isDatas($cache_data)) {
  437. return $cache_data;
  438. }
  439. return false;
  440. }
  441. /**
  442. * 组发消息
  443. */
  444. public static function senFormatData($pact = ZERO, $data = array())
  445. {
  446. $package = array(
  447. 'pact' => $pact,
  448. 'data' => $data
  449. );
  450. $string = json_encode($package, JSON_UNESCAPED_UNICODE);
  451. log_message::info($string);
  452. return $string;
  453. }
  454. /***
  455. *
  456. */
  457. public static function encodeData($string, $fd)
  458. {
  459. }
  460. /***
  461. * 数组维度判断
  462. */
  463. public statiC function arrayDimension($array)
  464. {
  465. if (isDatas($array)) {
  466. log_message::info('arrayDimension is days ');
  467. if (count($array) == count($array, ONE)) {
  468. log_message::info('arrayDimension is daysq1 ');
  469. return ONE;
  470. } else {
  471. log_message::info('arrayDimension is daysq 2 ');
  472. return TWO;
  473. }
  474. }
  475. log_message::info('arrayDimension is daysq 000 ');
  476. return ZERO;
  477. }
  478. }