CommonFunction.php 48 KB

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