CommonFunction.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. <?php
  2. namespace Biz\Common;
  3. use Biz\Db\Redis\RedisOP;
  4. class CommonFunction {
  5. function checkDebug()
  6. {
  7. return (isset($_GET['debug']) && intval($_GET['debug'])==-99999) || APP_DEBUG==1?1:0;
  8. }
  9. function View($Path = "") {
  10. if (!$Path) {
  11. global $ViewPath;
  12. $ViewPath = explode("\\", $ViewPath);
  13. $ViewPath[3] = explode("Controller", $ViewPath[3]);
  14. $ViewPath[3] = $ViewPath[3][0];
  15. return APP_PATH . "/" . $ViewPath[1] . "/View/" . $ViewPath[3] . "/" . $ViewPath[4] . ".blade.php";
  16. } else {
  17. return APP_PATH . "/" . $Path . ".blade.php";
  18. }
  19. }
  20. function arrayToOptions($data, $key, $val)
  21. {
  22. $options = array();
  23. if (is_array($data) && count($data)) {
  24. foreach ($data as $k => $value) {
  25. $options[$value[$key]] = $value[$val];
  26. }
  27. }
  28. return $options;
  29. }
  30. function M($TABLE_NAME = "") {
  31. return new DB($TABLE_NAME);
  32. }
  33. function checkClose() {
  34. if (file_exists(ROOT_PATH . '/Cache/system.lock')) {
  35. if (is_mobile()) {
  36. return appExec("Mobile", "Weihu", "index");
  37. } else {
  38. return appExec("home", "weihu", "index");
  39. }
  40. }
  41. }
  42. /**
  43. * 发布消息
  44. *
  45. * @param integer $status
  46. * @param string $msg
  47. * @param string $data
  48. * @return void
  49. */
  50. function publishNotify($channel, $status = 1, $data = '') {
  51. // toLog('publishNotify-'.$channel.'+++'.$data);
  52. C()->get('msg')->publish($channel, $data);
  53. }
  54. /**
  55. * 数组分类排序
  56. * @param [array] $columnsArr [需要进行排序的数组]
  57. * @param [int] $plmid [所属分类ID]
  58. */
  59. function getColumns($columnsArr, $plmid) {
  60. $menu = array();
  61. foreach ($columnsArr as $v) {
  62. if ($v['plmid'] == $plmid) {
  63. $menu[] = $v;
  64. $a = getColumns($columnsArr, $v['lmid']);
  65. foreach ($a as $vv) {
  66. $menu[] = $vv;
  67. }
  68. }
  69. }
  70. return $menu;
  71. }
  72. /**
  73. * 获取客户端真实IP
  74. */
  75. function GETIP() {
  76. global $ip;
  77. if (getenv("HTTP_CLIENT_IP")) {
  78. $ip = getenv("HTTP_CLIENT_IP");
  79. } else if (getenv("HTTP_X_FORWARDED_FOR")) {
  80. $ip = getenv("HTTP_X_FORWARDED_FOR");
  81. } else if (getenv("REMOTE_ADDR")) {
  82. $ip = getenv("REMOTE_ADDR");
  83. } else {
  84. $ip = "Unknow";
  85. }
  86. return $ip;
  87. }
  88. function dump($data, $exit = 1) {
  89. echo "<pre>";
  90. print_r($data);
  91. echo "</pre>";
  92. if ($exit) {
  93. exit;
  94. }
  95. }
  96. function OrderID($prefix = '') {
  97. $num = mt_rand(100, 999);
  98. list($s, $m) = explode(' ', microtime());
  99. $order = date("YmdHis") . ($s * 1000000) . $num;
  100. return $prefix . $order;
  101. }
  102. /**
  103. * 容器
  104. */
  105. function C() {
  106. static $c = array();
  107. if (!isset($c['container'])) {
  108. $c['container'] = new \System\Di();
  109. }
  110. return $c['container'];
  111. }
  112. /**
  113. * 全局变量快捷操作
  114. * @param [type] $key [description]
  115. * @param [type] $value [description]
  116. */
  117. function S($key, $value = null) {
  118. if ($value != null) {
  119. $GLOBALS[$key] = $value;
  120. }
  121. if (isset($GLOBALS[$key])) {
  122. return $GLOBALS[$key];
  123. }
  124. }
  125. /**
  126. * 载入模型
  127. * @param [type] $name [description]
  128. * @param string $proj [description]
  129. * @return [type] [description]
  130. */
  131. function lm($name, $proj = '') {
  132. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  133. if (empty($proj)) {
  134. throw new \Exception("项目{$proj}不存在", 1);
  135. }
  136. $cls = "\\App\\" . ucfirst($proj) . "\\Model\\" . ucfirst($name);
  137. if(!class_exists($cls)){
  138. return;
  139. }
  140. $key = 'model_' . $name . $proj;
  141. C()->shared($key, $cls);
  142. return C()->get($key);
  143. }
  144. /**
  145. * 载入语言包
  146. * @param string $key 语言项
  147. * @param string $file 语言文件名
  148. * @param string $proj 项目名称
  149. * @return [type] [description]
  150. */
  151. function lang($file = '', $proj = '') {
  152. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  153. $file = empty($file) ? S('CUR_CONTROLLER') : $file;
  154. $ckey = "Lang_{$proj}_{$file}";
  155. if (C()->has($ckey)) {
  156. return C()->get($ckey);
  157. } else {
  158. C()->shared($ckey, function () use ($proj, $file) {
  159. $file = ucfirst($file);
  160. $proj = ucfirst($proj);
  161. $lang = new \System\Lang();
  162. $lang_files = array();
  163. if (S('CUR_PROJECT') == $proj && S('CUR_CONTROLLER') == $file) {
  164. $lang_files[] = ROOT_PATH . "/Application/Commons/Lang/Common.php";
  165. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/Common.php";
  166. }
  167. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/{$file}.php";
  168. $data = array();
  169. foreach ($lang_files as $v) {
  170. if (file_exists($v)) {
  171. $data2 = include $v;
  172. if (is_array($data2)) {
  173. $data = array_merge($data, $data2);
  174. }
  175. }
  176. }
  177. $lang->load($data);
  178. return $lang;
  179. });
  180. return C()->get($ckey);
  181. }
  182. }
  183. /**
  184. * 标签替换
  185. *
  186. * @param [type] $message
  187. * @param array $data
  188. * @param string $tag
  189. * @return void
  190. */
  191. function parseTag($message,$data=array(),$tag='#'){
  192. if(is_array($data) && count($data)>0){
  193. foreach ($data as $k => $v) {
  194. $message = str_replace($tag . $k . $tag, $v, $message);
  195. }
  196. }
  197. return $message;
  198. }
  199. function appExec($proj, $ctrl, $method, $exec = 0) {
  200. $proj = ucfirst($proj);
  201. $ctrl = ucfirst($ctrl);
  202. $method = ucfirst($method);
  203. S('CUR_PROJECT', $proj);
  204. S('CUR_CONTROLLER', $ctrl);
  205. S('CUR_METHOD', $method);
  206. checkPlatform();
  207. toDomain();
  208. //设置REMOTE_KEY
  209. lm('setinfo','commons')->setKey();
  210. $cls = "\\App\\{$proj}\\Controller\\{$ctrl}";
  211. C()->set($ctrl, $cls);
  212. $cls = C()->get($ctrl);
  213. if (!$cls) {
  214. exit("404 NOT FOUND");
  215. }
  216. if ($exec) {
  217. $result = $cls->$method();
  218. if (!empty($result)) {
  219. Render($result);
  220. }
  221. }
  222. if (method_exists($cls, $method)) {
  223. $result = $cls->$method();
  224. if (!empty($result)) {
  225. Render($result);
  226. }
  227. } else {
  228. exit("404 METHOD NOT FOUND");
  229. }
  230. }
  231. /**
  232. * UUID 生成
  233. */
  234. function UUID() {
  235. $prefix = '';
  236. $uuid = '';
  237. $str = md5(uniqid(mt_rand(), true));
  238. $uuid = substr($str, 0, 8) . '-';
  239. $uuid .= substr($str, 8, 4) . '-';
  240. $uuid .= substr($str, 12, 4) . '-';
  241. $uuid .= substr($str, 16, 4) . '-';
  242. $uuid .= substr($str, 20, 12);
  243. return $prefix . $uuid;
  244. }
  245. /**
  246. *密码加密码
  247. */
  248. function GenEncryption() {
  249. srand((double) microtime() * 1000000); //create a random number feed.
  250. $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  251. $list = explode(",", $ychar);
  252. $authnum = "";
  253. for ($i = 0; $i < 6; $i++) {
  254. $randnum = rand(0, 61); // 10+26;
  255. $authnum .= $list[$randnum];
  256. }
  257. return $authnum;
  258. }
  259. //密码加密
  260. function GenPassword($password) {
  261. $Enc = GenEncryption();
  262. $Pwd = md5(md5($Enc . $password));
  263. return array("encryption" => $Enc, "password" => $Pwd);
  264. }
  265. //密码验证
  266. function VerPassword($identity, $password) {
  267. $account = M("account_password")->where("account_identity = '$identity' and", "status = ", '1')->select("encryption,account_password")->find();
  268. $VerPwd = md5(md5($account["encryption"] . $password));
  269. if ($VerPwd == $account["account_password"]) {
  270. unset($account);
  271. unset($VerPwd);
  272. return true;
  273. } else {
  274. unset($account);
  275. unset($VerPwd);
  276. return false;
  277. }
  278. }
  279. /**
  280. * Json return
  281. * @param string $data [description]
  282. * @param string $status [description]
  283. * @param string $msg [description]
  284. * @param string $method [description]
  285. */
  286. function JsonReturn($data = "", $status = "200", $msg = "", $method = "") {
  287. // header("Content-type:application/json;charset:utf-8");
  288. // header('content-type:text/html; charset=utf-8');
  289. if(isset($_REQUEST['crossdomain']) && !empty($_REQUEST['crossdomain'])){
  290. header("Access-Control-Allow-Origin:{$_REQUEST['crossdomain']}");
  291. }else{
  292. header("Access-Control-Allow-Origin:*");
  293. }
  294. header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS');
  295. header('Access-Control-Allow-Credentials:true');
  296. $retdata=array("status" => $status, "msg" => $msg, "data" =>$data);
  297. $devicetype=isset($_REQUEST['devicetype'])?trim($_REQUEST['devicetype']):'';
  298. if(!empty($devicetype) && in_array($devicetype,array('ios','android'))){
  299. $path=S('CUR_PROJECT').'/'.S('CUR_CONTROLLER').'/'. S('CUR_METHOD');
  300. $retdata=C()->get($devicetype."Result")->update($path,$retdata);
  301. }
  302. if ($method == '') {
  303. //dump($devicetype);
  304. echo (json_encode($retdata,JSON_UNESCAPED_UNICODE));
  305. } else {
  306. echo ($method . "(" . json_encode($retdata,JSON_UNESCAPED_UNICODE) . ")");
  307. }
  308. }
  309. function XmlReturn($data = "", $status = 200, $msg = '') {
  310. // header("Content-type:text/xml;charset:utf-8");
  311. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  312. $xml .= "<root>";
  313. $xml .= '<status>' . $status . '</status>';
  314. $xml .= '<msg>' . $msg . '</msg>';
  315. $xml .= "<rows>";
  316. $xml .= toXml($data);
  317. $xml .= "</rows>";
  318. $xml .= "</root>";
  319. echo($xml);
  320. }
  321. function toXml($data) {
  322. // print_r($data);
  323. $xml = '';
  324. if (is_array($data) && count($data) > 0) {
  325. foreach ($data as $k => $v) {
  326. $key = $k;
  327. if (is_numeric($k)) {
  328. $key = 'data';
  329. }
  330. if (is_array($v)) {
  331. $xml .= "<{$key}>" . toXml($v) . "</{$key}>";
  332. continue;
  333. }
  334. if (is_string($v)) {
  335. $xml .= "<{$key}>{$v}</{$key}>";
  336. }
  337. }
  338. }
  339. return $xml;
  340. }
  341. function WriteLog($Log) {
  342. $logger = new Logger('LOGGS');
  343. $logURI = "Cache/log/" . date("Y-m-d-H-i") . ".log";
  344. $logger->pushHandler(new StreamHandler($logURI, Logger::DEBUG));
  345. $logger->pushHandler(new FirePHPHandler());
  346. $logger->addInfo(var_export($Log,1));
  347. }
  348. /**
  349. * 计算当前用时
  350. *
  351. * @param string $runName
  352. * @param integer $return
  353. * @return void
  354. */
  355. function computeTime($runName='',$return=0,$start=1){
  356. $curTime= get_millisecond();
  357. $numTime=$curTime-(intval($GLOBALS['startTime'])*1000);
  358. if($return){
  359. return array('curTime'=>$curTime,'numTime'=>$numTime,'runName'=>$runName);
  360. }else{
  361. echo "<pre>执行{$runName} 功能,当前时间为:{$curTime},累积用时: {$numTime}</pre>";
  362. return;
  363. }
  364. }
  365. function get_millisecond()
  366. {
  367. list($usec, $sec) = explode(" ", microtime());
  368. $msec = round($usec * 1000);
  369. return $sec . $msec;
  370. }
  371. function toLog($text, $rewrite = 0) {
  372. $data = "=============" . date("Y-m-d H:i", time()) . "==========================================" . chr(13);
  373. $data .= var_export($text, 1) . chr(13);
  374. $data .= "======================================================================================" . chr(13);
  375. $data = iconv('utf-8', 'gb2312', $data);
  376. if ($rewrite) {
  377. file_put_contents(ROOT_PATH . '/logs.txt', $data);
  378. } else {
  379. file_put_contents(ROOT_PATH . '/logs.txt', $data, FILE_APPEND);
  380. }
  381. }
  382. /**
  383. * 发送数据到前台,根据请求的数据格式返回相应的数据
  384. * @param [type] $data [description]
  385. * @param string $status [description]
  386. * @param string $msg [description]
  387. */
  388. function Render($data, $status = "1", $msg = "", $method = '') {
  389. $format = S('CUR_RETURN_FORMAT');
  390. $msg = empty($msg) ? lang('errors', 'api')->get('error' . $status) : $msg;
  391. if (empty($format) || $format == 'json') {
  392. JsonReturn($data, $status, $msg);
  393. }
  394. if ($format == 'xml') {
  395. XmlReturn($data, $status, $msg);
  396. }
  397. exit;
  398. }
  399. function _beginTransaction() {
  400. S('DB')->beginTransaction();
  401. }
  402. function _rollBack() {
  403. S('DB')->rollBack();
  404. }
  405. function _commit() {
  406. S('DB')->commit();
  407. }
  408. /**
  409. * 服务
  410. *
  411. * @param string 服务名称
  412. * @param array $params
  413. * @return void
  414. */
  415. function SRV($name, $params = array()) {
  416. return C()->get($name)->update($params);
  417. }
  418. /**
  419. * 是否验证token过期
  420. *
  421. * @return bool
  422. */
  423. function isCheckToken() {
  424. $result = true;
  425. $allowMethod = include ROOT_PATH . "/Config/AllowMethod.php";
  426. $key = S('CUR_PROJECT') . '/' . S('CUR_CONTROLLER') . '/' . S('CUR_METHOD');
  427. if ($allowMethod[$key] == 1) {
  428. $result = false;
  429. }
  430. return $result;
  431. }
  432. function checkPlatform(){
  433. if(isset($_GET['platform'])){
  434. $_SESSION['platform']=$_GET['platform']=='wap'?1:0;
  435. }
  436. if(isset($_GET['clearPlatform']) && intval($_GET['clearPlatform'])==1){
  437. $_SESSION['platform']=null;
  438. }
  439. }
  440. function is_mobile() {
  441. if(isset($_SESSION['platform']) && $mobile=intval($_SESSION['platform'])>0 ){
  442. return $mobile==1?true:false;
  443. }
  444. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  445. $mobile_agents = Array("240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte");
  446. $is_mobile = false;
  447. foreach ($mobile_agents as $device) {
  448. if (stristr($user_agent, $device)) {
  449. $is_mobile = true;
  450. break;
  451. }
  452. }
  453. return $is_mobile;
  454. }
  455. function toDomain() {
  456. $domain = $_SERVER['HTTP_HOST'];
  457. $config = include ROOT_PATH . '/Config/Domain.php';
  458. $is_mobile = is_mobile();
  459. $curDomain = $old_domain = $domain;
  460. if (is_array($config) && count($config) > 0) {
  461. $domains = array();
  462. $domains[] = $domain;
  463. if ($is_mobile) {
  464. $domains[] = str_replace('www.', 'm.', $domain);
  465. $domains[] = 'm.' . $domain;
  466. } else {
  467. $domains[] = str_replace('m.', '', $domain);
  468. $domains[] = str_replace('m.', 'www.', $domain);
  469. }
  470. foreach ($domains as $key => $value) {
  471. if (isset($config[$value])) {
  472. $v = $config[$value];
  473. if ($v == 'pay') {
  474. return;
  475. }
  476. if ($is_mobile) {
  477. if ($v == 'pc') {
  478. continue;
  479. }
  480. } else {
  481. if ($v != 'pc') {
  482. continue;
  483. }
  484. }
  485. $curDomain = $value;
  486. // $_SESSION['domain']=$value;
  487. }
  488. }
  489. }
  490. // echo $curDomain;
  491. if ($is_mobile) {
  492. if (S('CUR_PROJECT') == 'Home') {
  493. header("Location:http://" . $curDomain . "/m");
  494. //header("Location:http://" . $curDomain . "/mobile-index/index");
  495. }
  496. } else {
  497. if (S('CUR_PROJECT') == 'Mobile') {
  498. header("Location:http://" . $curDomain . "/home-index/index");
  499. }
  500. }
  501. /**
  502. * 数据签名认证
  503. * @param array $data 被认证的数据
  504. * @return string 签名
  505. */
  506. function data_auth_sign($data) {
  507. //数据类型检测
  508. if (!is_array($data)) {
  509. $data = (array) $data;
  510. }
  511. ksort($data); //排序
  512. $code = http_build_query($data); //url编码并生成query字符串
  513. $sign = sha1($code); //生成签名
  514. return $sign;
  515. }
  516. /*
  517. * 判断用户是否登录
  518. * */
  519. function isLogin($str = '',$type='agent') {
  520. $session = $_SESSION[$type.'Info'];
  521. if(empty($session)){
  522. return null;
  523. }
  524. if($type=='agent'){
  525. if($str=='name'){
  526. return $session['agent_user'];
  527. }
  528. if($str=='uid'){
  529. return $session['user_identity'];
  530. }
  531. }
  532. }
  533. }
  534. function jump($url){
  535. header('Location:'.$url);
  536. }
  537. //手机中间星号
  538. function phoneHide($phone)
  539. {
  540. if (!empty($phone)) {
  541. $phone = substr_replace($phone, '****', 3, 4);
  542. }
  543. return $phone;
  544. }
  545. /**
  546. * @param $game_code 球类代码
  547. * @param int $isJoin 是否用于join
  548. * @return array
  549. * @throws Exception
  550. * 根据球类代码 定义相关model
  551. */
  552. function getModels($game_code,$isJoin=1){
  553. $game_type = lm('GameType', 'Sports')->where('game_code',$game_code)->select('game_code')->first();
  554. // 获取不同球类model
  555. if($isJoin == 1){
  556. switch ($game_type->game_code){
  557. case 'zq':
  558. $model_league = 'st_zq_league';
  559. $model_match = 'st_zq_competition';
  560. $model_odds = 'st_zq_odds';
  561. $model_odds_record = 'st_zq_odds_record';
  562. $model_result = 'st_zq_result';
  563. $model_result_record = 'st_zq_result_record';
  564. $model_league_result = 'st_zq_league_result';
  565. $model_local_league = 'st_zq_local_league';
  566. $model_local_match = 'st_zq_local_match';
  567. $model_result_express = 'st_zq_result_express';
  568. break;
  569. case 'lq':
  570. $model_league = 'st_lq_league';
  571. $model_match = 'st_lq_competition';
  572. $model_odds = 'st_lq_odds';
  573. $model_odds_record = 'st_lq_odds_record';
  574. $model_result = 'st_lq_result';
  575. $model_result_record = 'st_lq_result_record';
  576. $model_league_result = 'st_lq_league_result';
  577. $model_local_league = 'st_lq_local_league';
  578. $model_local_match = 'st_lq_local_match';
  579. $model_result_express = 'st_lq_result_express';
  580. break;
  581. case 'wq':
  582. $model_league = 'st_wq_league';
  583. $model_match = 'st_wq_competition';
  584. $model_odds = 'st_wq_odds';
  585. $model_odds_record = 'st_wq_odds_record';
  586. $model_result = 'st_wq_result';
  587. $model_result_record = 'st_wq_result_record';
  588. $model_league_result = 'st_wq_league_result';
  589. $model_local_league = 'st_wq_local_league';
  590. $model_local_match = 'st_wq_local_match';
  591. $model_result_express = 'st_wq_result_express';
  592. break;
  593. case 'bq':
  594. $model_league = 'st_bq_league';
  595. $model_match = 'st_bq_competition';
  596. $model_odds = 'st_bq_odds';
  597. $model_odds_record = 'st_bq_odds_record';
  598. $model_result = 'st_bq_result';
  599. $model_result_record = 'st_bq_result_record';
  600. $model_league_result = 'st_bq_league_result';
  601. $model_local_league = 'st_bq_local_league';
  602. $model_local_match = 'st_bq_local_match';
  603. $model_result_express = 'st_bq_result_express';
  604. break;
  605. case 'gj':
  606. $model_league = 'st_zq_league';
  607. $model_match = 'st_zq_competition';
  608. $model_odds = 'st_zq_odds';
  609. $model_result = 'st_zq_result';
  610. break;
  611. default:
  612. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  613. }
  614. }else{
  615. switch ($game_type->game_code){
  616. case 'zq':
  617. $model_league = 'st_zq_league';
  618. $model_match = 'st_zq_competition';
  619. $model_odds = 'st_zq_odds';
  620. $model_result = 'SoccerResult';
  621. break;
  622. case 'lq':
  623. $model_league = 'st_lq_league';
  624. $model_match = 'st_lq_competition';
  625. $model_odds = 'st_lq_odds';
  626. $model_result = 'BasketballResult';
  627. break;
  628. case 'wq':
  629. $model_league = 'st_wq_league';
  630. $model_match = 'st_wq_competition';
  631. $model_odds = 'st_wq_odds';
  632. $model_result = 'TennisResult';
  633. break;
  634. case 'bq':
  635. $model_league = 'st_bq_league';
  636. $model_match = 'st_bq_competition';
  637. $model_odds = 'st_bq_odds';
  638. $model_result = 'BaseballResult';
  639. break;
  640. case 'gj':
  641. $model_league = 'st_zq_league';
  642. $model_match = 'st_zq_competition';
  643. $model_odds = 'st_zq_odds';
  644. $model_result = 'SoccerResult';
  645. break;
  646. default:
  647. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  648. }
  649. }
  650. $data = [
  651. 'model_league'=>$model_league,//联赛
  652. 'model_match'=>$model_match,//赛事
  653. 'model_odds'=>$model_odds,//赔率
  654. 'model_result' =>$model_result,//赛事结果
  655. 'model_odds_record' => $model_odds_record,//赔率记录
  656. 'model_result_record' =>$model_result_record,//赛事结果记录
  657. 'model_league_result'=>$model_league_result,//联赛结果
  658. 'model_local_league'=>$model_local_league,//本地关联联赛
  659. 'model_local_match'=>$model_local_match,//本地关联赛事
  660. 'model_result_express' =>$model_result_express,//赛果
  661. ];
  662. return $data;
  663. }
  664. /**
  665. * @param $type_code 状态码
  666. * @param $model_match 赛事表名
  667. * @param $game_code 球类代码
  668. * @return array
  669. * @throws \Exception
  670. * 获取不同状态下的 查询条件
  671. */
  672. function getState($type_code,$model_match = '',$game_code, $p_code){
  673. if($model_match == ''){
  674. switch ($type_code){
  675. case 'StRollBall'://滚球 正在进行
  676. //追加不同球类的滚球时间
  677. if($game_code == 'zq' || $game_code == 'gj'){
  678. $where = [
  679. // ['is_rollball','=',1],
  680. // ['status','<','2'],
  681. // ['match_date','=',date("Y-m-d")],
  682. // ['match_time','>',date("H:i:s", time()-(120*60))],
  683. // ['match_time','<',date("H:i:s", time())]
  684. ['status','=','1']
  685. ];
  686. }
  687. if($game_code == 'lq'){
  688. $where = [
  689. // ['is_rollball','=',1],
  690. // ['status','<','2'],
  691. // ['match_date','=',date("Y-m-d")],
  692. // ['match_time','>',date("H:i:s", time()-(90*60))],
  693. // ['match_time','<',date("H:i:s", time())]
  694. ['status','=','1']
  695. ];
  696. }
  697. if($game_code == 'wq'){
  698. $where = [
  699. // ['is_rollball','=',1],
  700. // ['status','<','2'],
  701. // ['match_date','=',date("Y-m-d")],
  702. // ['match_time','>',date("H:i:s", time()-(300*60))],
  703. // ['match_time','<',date("H:i:s", time())]
  704. ['status','=','1']
  705. ];
  706. }
  707. if($game_code == 'bq'){
  708. $where = [
  709. // ['is_rollball','=',1],
  710. // ['status','<','2'],
  711. // ['match_date','=',date("Y-m-d")],
  712. // ['match_time','>',date("H:i:s", time()-(300*60))],
  713. // ['match_time','<',date("H:i:s", time())]
  714. ['status','=','1']
  715. ];
  716. }
  717. break;
  718. case 'StSoon'://即将 今日两小时内开始
  719. $where = [
  720. ['status','=','0'],
  721. ['match_date','=',date("Y-m-d")],
  722. ['match_time','<',date("H:i:s", strtotime("+2 hour"))],
  723. ['match_time','>',date("H:i:s", time())]
  724. /*
  725. ['utime','>',date("Y-m-d").' 00:00:00']
  726. */
  727. ];
  728. break;
  729. case 'StToday'://今日 今日未开始未结束
  730. $where = [
  731. ['status', '<', '2'],
  732. ['match_date','=',date("Y-m-d")],
  733. ['match_time','>',date("H:i:s", time())],
  734. /*
  735. ['is_today', '=', 1],
  736. ['type', '=', '1'],
  737. ['utime','>',date("Y-m-d").' 00:00:00']
  738. */
  739. ];
  740. break;
  741. case 'StMorningPlate'://早盘
  742. //获取当天结束时间戳
  743. $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  744. $where = [
  745. ['status', '<', '2'],
  746. // ['is_morningplate', '=', 1],
  747. ['us_time','>',$this->qgmdate('Y-m-d H:i:s', $endTime, -4)],
  748. /*
  749. ['match_date','>',date("Y-m-d",time())],
  750. ['match_time','>',date("H:i:s", time())],
  751. ['type', '=', '2'],
  752. ['utime','>',date("Y-m-d").' 00:00:00']
  753. */
  754. ];
  755. break;
  756. case 'StStringScene'://串场
  757. $where = [
  758. ['status', '<', '2'],
  759. ['is_stringscene', '=', 1],
  760. ['us_time','>',$this->qgmdate('Y-m-d H:i:s', '', -4)],
  761. /*
  762. ['match_date','>',date("Y-m-d",time())],
  763. ['match_time','>',date("H:i:s", time())],
  764. ['type', '=', '3'],
  765. ['utime','>',date("Y-m-d").' 00:00:00']
  766. */
  767. ];
  768. break;
  769. case 'StChampion'://冠军
  770. $where = [
  771. 'type'=>1
  772. ];
  773. break;
  774. default:
  775. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  776. }
  777. }else{
  778. switch ($type_code){
  779. case 'StRollBall'://滚球 正在进行
  780. //追加不同球类的滚球时间
  781. if($game_code == 'zq' || $game_code == 'gj'){
  782. $where = [
  783. // [$model_match.'.is_rollball','=',1],
  784. // [$model_match.'.status','<','2'],
  785. // [$model_match.'.match_date','=',date("Y-m-d")],
  786. // [$model_match.'.match_time','>',date("H:i:s", time()-(120*60))],
  787. // [$model_match.'.match_time','<',date("H:i:s", time())]
  788. [$model_match.'.status','=','1']
  789. ];
  790. }
  791. if($game_code == 'lq'){
  792. $where = [
  793. // [$model_match.'.is_rollball','=',1],
  794. // [$model_match.'.status','<','2'],
  795. // [$model_match.'.match_date','=',date("Y-m-d")],
  796. // [$model_match.'.match_time','>',date("H:i:s", time()-(90*60))],
  797. // [$model_match.'.match_time','<',date("H:i:s", time())]
  798. [$model_match.'.status','=','1']
  799. ];
  800. }
  801. if($game_code == 'wq'){
  802. $where = [
  803. // [$model_match.'.is_rollball','=',1],
  804. // [$model_match.'.status','<','2'],
  805. // [$model_match.'.match_date','=',date("Y-m-d")],
  806. // [$model_match.'.match_time','>',date("H:i:s", time()-(300*60))],
  807. // [$model_match.'.match_time','<',date("H:i:s", time())]
  808. [$model_match.'.status','=','1']
  809. ];
  810. }
  811. if($game_code == 'bq'){
  812. $where = [
  813. // [$model_match.'.is_rollball','=',1],
  814. // [$model_match.'.status','<','2'],
  815. // [$model_match.'.match_date','=',date("Y-m-d")],
  816. // [$model_match.'.match_time','>',date("H:i:s", time()-(300*60))],
  817. // [$model_match.'.match_time','<',date("H:i:s", time())]
  818. [$model_match.'.status','=','1']
  819. ];
  820. }
  821. break;
  822. case 'StSoon'://即将 今日两小时内开始
  823. if($p_code == 'kemp'){
  824. $where = [
  825. [$model_match.'.status','<','2']
  826. ];
  827. }else{
  828. $where = [
  829. [$model_match.'.status','=','0'],
  830. [$model_match.'.match_date','=',date("Y-m-d")],
  831. [$model_match.'.match_time','<',date("H:i:s", strtotime("+2 hour"))],
  832. [$model_match.'.match_time','>',date("H:i:s", time())]
  833. /*
  834. [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  835. */
  836. ];
  837. }
  838. break;
  839. case 'StToday'://今日 今日未开始未结束
  840. if($p_code == 'kemp'){
  841. $where = [
  842. [$model_match.'.status', '<', '2'],
  843. ];
  844. }else{
  845. $where = [
  846. [$model_match.'.status', '<', '2'],
  847. [$model_match.'.match_date','=',date("Y-m-d")],
  848. [$model_match.'.match_time','>',date("H:i:s", time())],
  849. /*
  850. [$model_match.'.utime','>',date("Y-m-d").' 00:00:00'],
  851. [$model_match.'.is_today','=',1],
  852. [$model_match.'.type', '=', '1'],
  853. */
  854. ];
  855. }
  856. break;
  857. case 'all'://所有赛事
  858. $where = [
  859. [$model_match.'.status', '<', '2'],
  860. ];
  861. break;
  862. case 'StMorningPlate'://早盘
  863. if($p_code == 'kemp'){
  864. $where = [
  865. [$model_match.'.status', '<', '2']
  866. ];
  867. }else{
  868. //获取当天结束的时间戳
  869. $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  870. $where = [
  871. [$model_match.'.status', '<', '2'],
  872. // [$model_match.'.is_morningplate','=',1],
  873. [$model_match.'.us_time','>',$this->qgmdate('Y-m-d H:i:s',$endTime , -4)],
  874. /*
  875. [$model_match.'.match_date','>',date("Y-m-d",time())],
  876. [$model_match.'.match_time','>',date("H:i:s", time())],
  877. [$model_match.'.type', '=', '2'],
  878. [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  879. */
  880. ];
  881. }
  882. break;
  883. case 'StStringScene'://串场 查询串场状态为1 开赛时间大于当前时间
  884. $where = [
  885. [$model_match.'.is_stringscene','=',1],
  886. [$model_match.'.us_time','>',$this->qgmdate('Y-m-d H:i:s', '', -4)],
  887. [$model_match.'.status', '<', '2'],
  888. /*
  889. [$model_match.'.match_date','>',date("Y-m-d",time())],
  890. [$model_match.'.match_time','>',date("H:i:s", time())],
  891. [$model_match.'.type', '=', '3'],
  892. [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  893. */
  894. ];
  895. break;
  896. case 'StChampion'://冠军
  897. $where = [
  898. 'type'=>1
  899. ];
  900. break;
  901. default:
  902. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  903. }
  904. }
  905. return $where;
  906. }
  907. /**
  908. * @param $data
  909. * @return array
  910. * 处理当国家下无联赛数据 删除该数组
  911. */
  912. function handleArr($data){
  913. $hData = [];
  914. foreach ($data as $key =>$v){
  915. if(empty($v['league_count'])){
  916. $v = [];
  917. }
  918. $hData[]=$v;
  919. }
  920. foreach($hData as $k=>$v){
  921. if(empty($v)){
  922. unset($hData[$k]);
  923. }
  924. }
  925. foreach ($hData as $k=>$v){
  926. $v['league_count'] = array_values($v['league_count']);
  927. $hData[$k]=$v;
  928. }
  929. return array_values($hData);
  930. }
  931. /**
  932. * @param $arr
  933. * @param $key
  934. * @return array
  935. * 去除二维数组重复项
  936. */
  937. function array_unset_tt($arr,$key){
  938. //建立一个目标数组
  939. $res = array();
  940. foreach ($arr as $value) {
  941. //查看有没有重复项
  942. if(isset($res[$value[$key]])){
  943. unset($value[$key]); //有:销毁
  944. }else{
  945. $res[$value[$key]] = $value;
  946. }
  947. }
  948. return $res;
  949. }
  950. /**
  951. * 处理冠军盘口 数组结构
  952. */
  953. function array_gj_tt($data,$oddsData){
  954. foreach ($data as $k=>&$v){
  955. foreach ($v['league_count'] as $kk=>&$vv){
  956. foreach ($oddsData as $kkk=>$vvv){
  957. if($vv['lg_id'] == $vvv['lg_id']){
  958. $vv['oddsData'][]=$vvv;
  959. }
  960. }
  961. if($vv['oddsData']){
  962. $vv['oddsData'] = $this->array_unset_tt($vv['oddsData'],'p_code');
  963. $vv['count'] = count($vv['oddsData']);
  964. unset($vv['oddsData']);
  965. }
  966. if(count($vv) != 5){
  967. unset($v['league_count'][$kk]);
  968. }
  969. }
  970. $v['league_count'] = array_values($v['league_count']);
  971. if(empty($v['league_count'])){
  972. unset($data[$k]);
  973. }
  974. }
  975. return array_values($data);
  976. }
  977. /**
  978. * 处理冠军以外 数组结构
  979. */
  980. function array_other_tt($data,$matchData){
  981. foreach ($data as $k=>&$v){
  982. foreach ($v['league_count'] as $kk=>&$vv){
  983. foreach ($matchData as $kkk=>$vvv){
  984. if($vv['lg_id'] == $vvv['lg_id']){
  985. $vv['matchData'][]=$vvv;
  986. }
  987. }
  988. if($vv['matchData']){
  989. $vv['matchData'] = $this->array_unset_tt($vv['matchData'],'match_id');
  990. $vv['count'] = count($vv['matchData']);
  991. unset($vv['matchData']);
  992. }
  993. if(empty($vv['count'])){
  994. unset($v['league_count'][$kk]);
  995. }
  996. }
  997. $v['league_count'] = array_values($v['league_count']);
  998. if(empty($v['league_count'])){
  999. unset($data[$k]);
  1000. }
  1001. }
  1002. return array_values($data);
  1003. }
  1004. /**
  1005. * 时间格式化
  1006. * @param string $dateformat 时间格式
  1007. * @param int $timestamp 时间戳
  1008. * @param int $timeoffset 时区偏差 默认8 北京时间 -4美东时间
  1009. * @return string
  1010. */
  1011. function qgmdate($dateformat = 'Y-m-d H:i:s', $timestamp = '', $timeoffset = 8,$type = '') {
  1012. if(empty($timestamp)) {
  1013. $timestamp = time();
  1014. }
  1015. $result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
  1016. return $result;
  1017. }
  1018. /**
  1019. * 判断二维数组是否包含一维数组
  1020. * @param mixed $arr 查询条件
  1021. * @param mixed $arrs 运动类型
  1022. * @return string
  1023. */
  1024. function judgeEqual($arr, $arrs){
  1025. foreach ($arrs as $value) {
  1026. if (count($value) === count($arr)) {
  1027. $a = 0;
  1028. foreach ($value as $key => $v) {
  1029. $a++;
  1030. if ($v != $arr[$key]) {
  1031. break;
  1032. }
  1033. if ($a == count($key)) { // 到最后一个元素都没有出现不相等,就说明这两个数组相等。
  1034. return true;
  1035. }
  1036. }
  1037. }
  1038. }
  1039. return false;
  1040. }
  1041. // 排列
  1042. function arrangement($a, $m) {
  1043. $r = array();
  1044. $n = count($a);
  1045. if ($m <= 0 || $m > $n) {
  1046. return $r;
  1047. }
  1048. for ($i=0; $i<$n; $i++) {
  1049. $b = $a;
  1050. $t = array_splice($b, $i, 1);
  1051. if ($m == 1) {
  1052. $r[] = $t;
  1053. } else {
  1054. $c = $this->arrangement($b, $m-1);
  1055. foreach ($c as $v) {
  1056. $r[] = array_merge($t, $v);
  1057. }
  1058. }
  1059. }
  1060. return $r;
  1061. }
  1062. // 组合
  1063. function combination($a, $m) {
  1064. $r = array();
  1065. $n = count($a);
  1066. if ($m <= 0 || $m > $n) {
  1067. return $r;
  1068. }
  1069. for ($i=0; $i<$n; $i++) {
  1070. $t = array($a[$i]);
  1071. if ($m == 1) {
  1072. $r[] = $t;
  1073. } else {
  1074. $b = array_slice($a, $i+1);
  1075. $c = $this->combination($b, $m-1);
  1076. foreach ($c as $v) {
  1077. $r[] = array_merge($t, $v);
  1078. }
  1079. }
  1080. }
  1081. return $r;
  1082. }
  1083. // 阶乘
  1084. function factorial($n) {
  1085. return array_product(range(1, $n));
  1086. }
  1087. // 排列数
  1088. function A1($n, $m) {
  1089. return factorial($n)/factorial($n-$m);
  1090. }
  1091. // 组合数
  1092. function C1($n, $m) {
  1093. return A1($n, $m)/factorial($m);
  1094. }
  1095. //处理空赔率赛事
  1096. function Handle_Odds_Null($matchData=[],$models=[],$oddsTypeWhere=[]){
  1097. //===获取当前赛事是否有赔率,如果没有则去除===
  1098. //获取当前match_id
  1099. $match_ids = [];
  1100. foreach($matchData as $k=>$v){
  1101. $match_ids[] = $v['match_id'];
  1102. }
  1103. //获取下赛事赔率
  1104. $oddsData = lm($models['model_odds'],'Sports')
  1105. ->select('match_id')
  1106. ->whereIn('match_id',$match_ids)
  1107. ->where($oddsTypeWhere)
  1108. ->distinct('match_id')
  1109. ->get()
  1110. ->toArray();
  1111. //如果赔率为空,则返回空
  1112. if(empty($oddsData)) return $oddsData;
  1113. $matchData_new = [];
  1114. foreach($matchData as $k=>$v){
  1115. foreach($oddsData as $kk=>$vv){
  1116. if($v['match_id'] == $vv['match_id']){
  1117. $matchData_new[] = $matchData[$k];
  1118. }
  1119. }
  1120. }
  1121. return $matchData_new;
  1122. //===赛事空赔率 处理end ===
  1123. }
  1124. //处理空赔率联赛
  1125. function Handle_Odds_Null_lg($matchData=[],$models=[]){
  1126. //===获取当前联赛是否有赔率,如果没有则去除===
  1127. //获取当前lg_id
  1128. $lg_ids = [];
  1129. foreach($matchData as $k=>$v){
  1130. $lg_ids[] = $v['lg_id'];
  1131. }
  1132. //获取下赛事赔率
  1133. $oddsData = lm($models['model_odds'],'Sports')
  1134. ->select('lg_id')
  1135. ->whereIn('lg_id',$lg_ids)
  1136. ->where('type',1)
  1137. ->distinct('lg_id')
  1138. ->get()
  1139. ->toArray();
  1140. //如果赔率为空,则返回空
  1141. if(empty($oddsData)) return $oddsData;
  1142. $matchData_new = [];
  1143. foreach($matchData as $k=>$v){
  1144. foreach($oddsData as $kk=>$vv){
  1145. if($v['lg_id'] == $vv['lg_id']){
  1146. $matchData_new[] = $matchData[$k];
  1147. }
  1148. }
  1149. }
  1150. return $matchData_new;
  1151. //===联赛空赔率 处理end ===
  1152. }
  1153. /**
  1154. * 处理各球类投注: 可赢金额计算
  1155. * $game_code 球类代码
  1156. * $p_code 父级玩法代码
  1157. * $odds 赔率
  1158. * $bet_amount 投注金额
  1159. * $type 投注类型 1单式
  1160. */
  1161. function getEarnMoney($game_code='',$p_code='',$odds=0,$bet_amount=0,$type=0){
  1162. /*
  1163. 可赢额直接乘以赔率的父级玩法有
  1164. 足球:球队进球数:大/小 进球:大/小 让球
  1165. 篮球:让球 总分:大/小 球队得分大小
  1166. 网球:让盘 让局 总局数:大/小
  1167. 棒球:让球 总得分:大/小
  1168. */
  1169. //单式投注
  1170. if($type == 1){
  1171. //足球 可赢金额 计算规则
  1172. if($game_code == 'zq'){
  1173. //球队进球数:大/小;进球:大/小;让球;
  1174. if($p_code == 'TB' || $p_code == 'GS' || $p_code == 'CO'){
  1175. //可赢金额 = 赔率*投注金额
  1176. $prize_money = $odds*$bet_amount;
  1177. }else{
  1178. //可赢金额 = (赔率-1)*投注金额
  1179. $prize_money = ($odds-1)*$bet_amount;
  1180. }
  1181. }
  1182. //篮球 可赢金额 计算规则
  1183. if($game_code == 'lq'){
  1184. //让球 总分:大/小 球队得分大小
  1185. if($p_code == 'TN' || $p_code == 'TB' || $p_code == 'CO'){
  1186. //可赢金额 = 赔率*投注金额
  1187. $prize_money = $odds*$bet_amount;
  1188. }else{
  1189. //可赢金额 = (赔率-1)*投注金额
  1190. $prize_money = ($odds-1)*$bet_amount;
  1191. }
  1192. }
  1193. //网球 可赢金额 计算规则
  1194. if($game_code == 'wq'){
  1195. //让盘 让局 总局数:大/小
  1196. if($p_code == 'LD' || $p_code == 'LB' || $p_code == 'TN'){
  1197. //可赢金额 = 赔率*投注金额
  1198. $prize_money = $odds*$bet_amount;
  1199. }else{
  1200. //可赢金额 = (赔率-1)*投注金额
  1201. $prize_money = ($odds-1)*$bet_amount;
  1202. }
  1203. }
  1204. //棒球
  1205. if($game_code == 'bq'){
  1206. //让球 总得分:大/小
  1207. if($p_code == 'CO' || $p_code == 'TN'){
  1208. //可赢金额 = 赔率*投注金额
  1209. $prize_money = $odds*$bet_amount;
  1210. }else{
  1211. //可赢金额 = (赔率-1)*投注金额
  1212. $prize_money = ($odds-1)*$bet_amount;
  1213. }
  1214. }
  1215. }
  1216. return $prize_money;
  1217. }
  1218. //计算赛事当前进行时间
  1219. function getMatchTime($match_date,$match_time){
  1220. $time = time() - strtotime($match_date.' '.$match_time);
  1221. //如果需要时刻格式
  1222. // $time = $this->dataformat($time);
  1223. return floor($time/60);
  1224. }
  1225. //将秒 转换为 时 分 秒
  1226. function dataformat($num=0) {
  1227. $hour = floor($num/3600);
  1228. $minute = floor(($num-3600*$hour)/60);
  1229. $second = floor((($num-3600*$hour)-60*$minute)%60);
  1230. return $hour.':'.$minute.':'.$second;
  1231. }
  1232. //根据赛事进行 时间 获取进程
  1233. function getMatchProcess($time=0,$game_code){
  1234. if($game_code == 'zq'){
  1235. if($time < 45) return '上半场';
  1236. return '下半场';
  1237. }
  1238. if($game_code == 'lq'){
  1239. if($time < 10) return '第一节';
  1240. if(($time > 10) and ($time < 20)) return '第二节';
  1241. if(($time > 20) and ($time < 30)) return '第三节';
  1242. if(($time > 30) and ($time < 40)) return '第四节';
  1243. }
  1244. if($game_code == 'wq'){
  1245. return '';
  1246. }
  1247. if($game_code == 'bq'){
  1248. return '';
  1249. }
  1250. }
  1251. //返回数据源对应ID
  1252. function getSourceID($source){
  1253. $data = [
  1254. 'hg3535'=>1,
  1255. 'hg0088'=>2,
  1256. '188'=>3,
  1257. ];
  1258. if(!empty($data[$source])) {
  1259. $sourceID = $data[$source];
  1260. }else{
  1261. $sourceID = 0;
  1262. }
  1263. return $sourceID;
  1264. }
  1265. //生成订单/批次号
  1266. function orderSn() {
  1267. $year_code = array('A','B','C','D','E','F','G','H','I','J');//年份
  1268. $order_sn = $year_code[intval(date('Y'))-2019].
  1269. strtoupper(dechex(date('m'))).date('d').
  1270. substr(time(),-5).substr(microtime(),2,5).sprintf('d',rand(0,99));
  1271. return $order_sn;
  1272. }
  1273. //根据球类追加限定玩法
  1274. function getOddsPcode($game_code = 'zq'){
  1275. $p_code = [];
  1276. if($game_code == 'zq'){
  1277. $p_code = ['C','B','TG','CB','TB','GS','CO','TS'];
  1278. }
  1279. if($game_code == 'lq'){
  1280. $p_code = ['LN','C','TN','TS','TB','CO'];
  1281. }
  1282. if($game_code == 'wq'){
  1283. $p_code = ['LB','TN','TS','B','LD','C'];
  1284. }
  1285. if($game_code == 'bq'){
  1286. $p_code = ['CO','C','TN','TS'];
  1287. }
  1288. return $p_code;
  1289. }
  1290. /**
  1291. * 追加写投注队列
  1292. * Tank
  1293. * 2019.11.16
  1294. * 订单号 比赛id188 开赛日期时间 比赛状态(早盘,今日,串场,滚球), 父级玩法, 子级玩法,排序
  1295. */
  1296. function setOrderQueue($orderData){
  1297. $data = json_encode($orderData,256);
  1298. $redis = (new RedisOP())->get();
  1299. $redis->select(1);
  1300. $key = "ORDER_QUEUE";
  1301. $ret = $redis->lpush($key,$data);
  1302. }
  1303. }
  1304. ?>