| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413 |
- <?php
- namespace Biz\Common;
- use Biz\Db\Redis\RedisOP;
- class CommonFunction {
- function checkDebug()
- {
- return (isset($_GET['debug']) && intval($_GET['debug'])==-99999) || APP_DEBUG==1?1:0;
- }
- function View($Path = "") {
- if (!$Path) {
- global $ViewPath;
- $ViewPath = explode("\\", $ViewPath);
- $ViewPath[3] = explode("Controller", $ViewPath[3]);
- $ViewPath[3] = $ViewPath[3][0];
- return APP_PATH . "/" . $ViewPath[1] . "/View/" . $ViewPath[3] . "/" . $ViewPath[4] . ".blade.php";
- } else {
- return APP_PATH . "/" . $Path . ".blade.php";
- }
- }
- function arrayToOptions($data, $key, $val)
- {
- $options = array();
- if (is_array($data) && count($data)) {
- foreach ($data as $k => $value) {
- $options[$value[$key]] = $value[$val];
- }
- }
- return $options;
- }
- function M($TABLE_NAME = "") {
- return new DB($TABLE_NAME);
- }
- function checkClose() {
- if (file_exists(ROOT_PATH . '/Cache/system.lock')) {
- if (is_mobile()) {
- return appExec("Mobile", "Weihu", "index");
- } else {
- return appExec("home", "weihu", "index");
- }
- }
- }
- /**
- * 发布消息
- *
- * @param integer $status
- * @param string $msg
- * @param string $data
- * @return void
- */
- function publishNotify($channel, $status = 1, $data = '') {
- // toLog('publishNotify-'.$channel.'+++'.$data);
- C()->get('msg')->publish($channel, $data);
- }
- /**
- * 数组分类排序
- * @param [array] $columnsArr [需要进行排序的数组]
- * @param [int] $plmid [所属分类ID]
- */
- function getColumns($columnsArr, $plmid) {
- $menu = array();
- foreach ($columnsArr as $v) {
- if ($v['plmid'] == $plmid) {
- $menu[] = $v;
- $a = getColumns($columnsArr, $v['lmid']);
- foreach ($a as $vv) {
- $menu[] = $vv;
- }
- }
- }
- return $menu;
- }
- /**
- * 获取客户端真实IP
- */
- function GETIP() {
- global $ip;
- if (getenv("HTTP_CLIENT_IP")) {
- $ip = getenv("HTTP_CLIENT_IP");
- } else if (getenv("HTTP_X_FORWARDED_FOR")) {
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- } else if (getenv("REMOTE_ADDR")) {
- $ip = getenv("REMOTE_ADDR");
- } else {
- $ip = "Unknow";
- }
- return $ip;
- }
- function dump($data, $exit = 1) {
- echo "<pre>";
- print_r($data);
- echo "</pre>";
- if ($exit) {
- exit;
- }
- }
- function OrderID($prefix = '') {
- $num = mt_rand(100, 999);
- list($s, $m) = explode(' ', microtime());
- $order = date("YmdHis") . ($s * 1000000) . $num;
- return $prefix . $order;
- }
- /**
- * 容器
- */
- function C() {
- static $c = array();
- if (!isset($c['container'])) {
- $c['container'] = new \System\Di();
- }
- return $c['container'];
- }
- /**
- * 全局变量快捷操作
- * @param [type] $key [description]
- * @param [type] $value [description]
- */
- function S($key, $value = null) {
- if ($value != null) {
- $GLOBALS[$key] = $value;
- }
- if (isset($GLOBALS[$key])) {
- return $GLOBALS[$key];
- }
- }
- /**
- * 载入模型
- * @param [type] $name [description]
- * @param string $proj [description]
- * @return [type] [description]
- */
- function lm($name, $proj = '') {
- $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
- if (empty($proj)) {
- throw new \Exception("项目{$proj}不存在", 1);
- }
- $cls = "\\App\\" . ucfirst($proj) . "\\Model\\" . ucfirst($name);
- if(!class_exists($cls)){
- return;
- }
- $key = 'model_' . $name . $proj;
- C()->shared($key, $cls);
- return C()->get($key);
- }
- /**
- * 载入语言包
- * @param string $key 语言项
- * @param string $file 语言文件名
- * @param string $proj 项目名称
- * @return [type] [description]
- */
- function lang($file = '', $proj = '') {
- $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
- $file = empty($file) ? S('CUR_CONTROLLER') : $file;
- $ckey = "Lang_{$proj}_{$file}";
- if (C()->has($ckey)) {
- return C()->get($ckey);
- } else {
- C()->shared($ckey, function () use ($proj, $file) {
- $file = ucfirst($file);
- $proj = ucfirst($proj);
- $lang = new \System\Lang();
- $lang_files = array();
- if (S('CUR_PROJECT') == $proj && S('CUR_CONTROLLER') == $file) {
- $lang_files[] = ROOT_PATH . "/Application/Commons/Lang/Common.php";
- $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/Common.php";
- }
- $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/{$file}.php";
- $data = array();
- foreach ($lang_files as $v) {
- if (file_exists($v)) {
- $data2 = include $v;
- if (is_array($data2)) {
- $data = array_merge($data, $data2);
- }
- }
- }
- $lang->load($data);
- return $lang;
- });
- return C()->get($ckey);
- }
- }
- /**
- * 标签替换
- *
- * @param [type] $message
- * @param array $data
- * @param string $tag
- * @return void
- */
- function parseTag($message,$data=array(),$tag='#'){
- if(is_array($data) && count($data)>0){
- foreach ($data as $k => $v) {
- $message = str_replace($tag . $k . $tag, $v, $message);
- }
- }
- return $message;
- }
- function appExec($proj, $ctrl, $method, $exec = 0) {
- $proj = ucfirst($proj);
- $ctrl = ucfirst($ctrl);
- $method = ucfirst($method);
- S('CUR_PROJECT', $proj);
- S('CUR_CONTROLLER', $ctrl);
- S('CUR_METHOD', $method);
- checkPlatform();
- toDomain();
- //设置REMOTE_KEY
- lm('setinfo','commons')->setKey();
- $cls = "\\App\\{$proj}\\Controller\\{$ctrl}";
- C()->set($ctrl, $cls);
- $cls = C()->get($ctrl);
- if (!$cls) {
- exit("404 NOT FOUND");
- }
- if ($exec) {
- $result = $cls->$method();
- if (!empty($result)) {
- Render($result);
- }
- }
- if (method_exists($cls, $method)) {
- $result = $cls->$method();
- if (!empty($result)) {
- Render($result);
- }
- } else {
- exit("404 METHOD NOT FOUND");
- }
- }
- /**
- * UUID 生成
- */
- function UUID() {
- $prefix = '';
- $uuid = '';
- $str = md5(uniqid(mt_rand(), true));
- $uuid = substr($str, 0, 8) . '-';
- $uuid .= substr($str, 8, 4) . '-';
- $uuid .= substr($str, 12, 4) . '-';
- $uuid .= substr($str, 16, 4) . '-';
- $uuid .= substr($str, 20, 12);
- return $prefix . $uuid;
- }
- /**
- *密码加密码
- */
- function GenEncryption() {
- srand((double) microtime() * 1000000); //create a random number feed.
- $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";
- $list = explode(",", $ychar);
- $authnum = "";
- for ($i = 0; $i < 6; $i++) {
- $randnum = rand(0, 61); // 10+26;
- $authnum .= $list[$randnum];
- }
- return $authnum;
- }
- //密码加密
- function GenPassword($password) {
- $Enc = GenEncryption();
- $Pwd = md5(md5($Enc . $password));
- return array("encryption" => $Enc, "password" => $Pwd);
- }
- //密码验证
- function VerPassword($identity, $password) {
- $account = M("account_password")->where("account_identity = '$identity' and", "status = ", '1')->select("encryption,account_password")->find();
- $VerPwd = md5(md5($account["encryption"] . $password));
- if ($VerPwd == $account["account_password"]) {
- unset($account);
- unset($VerPwd);
- return true;
- } else {
- unset($account);
- unset($VerPwd);
- return false;
- }
- }
- /**
- * Json return
- * @param string $data [description]
- * @param string $status [description]
- * @param string $msg [description]
- * @param string $method [description]
- */
- function JsonReturn($data = "", $status = "200", $msg = "", $method = "") {
- // header("Content-type:application/json;charset:utf-8");
- // header('content-type:text/html; charset=utf-8');
- if(isset($_REQUEST['crossdomain']) && !empty($_REQUEST['crossdomain'])){
- header("Access-Control-Allow-Origin:{$_REQUEST['crossdomain']}");
- }else{
- header("Access-Control-Allow-Origin:*");
- }
- header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS');
- header('Access-Control-Allow-Credentials:true');
- $retdata=array("status" => $status, "msg" => $msg, "data" =>$data);
- $devicetype=isset($_REQUEST['devicetype'])?trim($_REQUEST['devicetype']):'';
- if(!empty($devicetype) && in_array($devicetype,array('ios','android'))){
- $path=S('CUR_PROJECT').'/'.S('CUR_CONTROLLER').'/'. S('CUR_METHOD');
- $retdata=C()->get($devicetype."Result")->update($path,$retdata);
- }
- if ($method == '') {
- //dump($devicetype);
- echo (json_encode($retdata,JSON_UNESCAPED_UNICODE));
- } else {
- echo ($method . "(" . json_encode($retdata,JSON_UNESCAPED_UNICODE) . ")");
- }
- }
- function XmlReturn($data = "", $status = 200, $msg = '') {
- // header("Content-type:text/xml;charset:utf-8");
- $xml = '<?xml version="1.0" encoding="utf-8"?>';
- $xml .= "<root>";
- $xml .= '<status>' . $status . '</status>';
- $xml .= '<msg>' . $msg . '</msg>';
- $xml .= "<rows>";
- $xml .= toXml($data);
- $xml .= "</rows>";
- $xml .= "</root>";
- echo($xml);
- }
- function toXml($data) {
- // print_r($data);
- $xml = '';
- if (is_array($data) && count($data) > 0) {
- foreach ($data as $k => $v) {
- $key = $k;
- if (is_numeric($k)) {
- $key = 'data';
- }
- if (is_array($v)) {
- $xml .= "<{$key}>" . toXml($v) . "</{$key}>";
- continue;
- }
- if (is_string($v)) {
- $xml .= "<{$key}>{$v}</{$key}>";
- }
- }
- }
- return $xml;
- }
- function WriteLog($Log) {
- $logger = new Logger('LOGGS');
- $logURI = "Cache/log/" . date("Y-m-d-H-i") . ".log";
- $logger->pushHandler(new StreamHandler($logURI, Logger::DEBUG));
- $logger->pushHandler(new FirePHPHandler());
- $logger->addInfo(var_export($Log,1));
- }
- /**
- * 计算当前用时
- *
- * @param string $runName
- * @param integer $return
- * @return void
- */
- function computeTime($runName='',$return=0,$start=1){
- $curTime= get_millisecond();
- $numTime=$curTime-(intval($GLOBALS['startTime'])*1000);
- if($return){
- return array('curTime'=>$curTime,'numTime'=>$numTime,'runName'=>$runName);
- }else{
- echo "<pre>执行{$runName} 功能,当前时间为:{$curTime},累积用时: {$numTime}</pre>";
- return;
- }
- }
- function get_millisecond()
- {
- list($usec, $sec) = explode(" ", microtime());
- $msec = round($usec * 1000);
- return $sec . $msec;
- }
- function toLog($text, $rewrite = 0) {
- $data = "=============" . date("Y-m-d H:i", time()) . "==========================================" . chr(13);
- $data .= var_export($text, 1) . chr(13);
- $data .= "======================================================================================" . chr(13);
- $data = iconv('utf-8', 'gb2312', $data);
- if ($rewrite) {
- file_put_contents(ROOT_PATH . '/logs.txt', $data);
- } else {
- file_put_contents(ROOT_PATH . '/logs.txt', $data, FILE_APPEND);
- }
- }
- /**
- * 发送数据到前台,根据请求的数据格式返回相应的数据
- * @param [type] $data [description]
- * @param string $status [description]
- * @param string $msg [description]
- */
- function Render($data, $status = "1", $msg = "", $method = '') {
- $format = S('CUR_RETURN_FORMAT');
- $msg = empty($msg) ? lang('errors', 'api')->get('error' . $status) : $msg;
- if (empty($format) || $format == 'json') {
- JsonReturn($data, $status, $msg);
- }
- if ($format == 'xml') {
- XmlReturn($data, $status, $msg);
- }
- exit;
- }
- function _beginTransaction() {
- S('DB')->beginTransaction();
- }
- function _rollBack() {
- S('DB')->rollBack();
- }
- function _commit() {
- S('DB')->commit();
- }
- /**
- * 服务
- *
- * @param string 服务名称
- * @param array $params
- * @return void
- */
- function SRV($name, $params = array()) {
- return C()->get($name)->update($params);
- }
- /**
- * 是否验证token过期
- *
- * @return bool
- */
- function isCheckToken() {
- $result = true;
- $allowMethod = include ROOT_PATH . "/Config/AllowMethod.php";
- $key = S('CUR_PROJECT') . '/' . S('CUR_CONTROLLER') . '/' . S('CUR_METHOD');
- if ($allowMethod[$key] == 1) {
- $result = false;
- }
- return $result;
- }
- function checkPlatform(){
- if(isset($_GET['platform'])){
- $_SESSION['platform']=$_GET['platform']=='wap'?1:0;
- }
- if(isset($_GET['clearPlatform']) && intval($_GET['clearPlatform'])==1){
- $_SESSION['platform']=null;
- }
- }
- function is_mobile() {
- if(isset($_SESSION['platform']) && $mobile=intval($_SESSION['platform'])>0 ){
- return $mobile==1?true:false;
- }
- $user_agent = $_SERVER['HTTP_USER_AGENT'];
- $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");
- $is_mobile = false;
- foreach ($mobile_agents as $device) {
- if (stristr($user_agent, $device)) {
- $is_mobile = true;
- break;
- }
- }
- return $is_mobile;
- }
- function toDomain() {
- $domain = $_SERVER['HTTP_HOST'];
- $config = include ROOT_PATH . '/Config/Domain.php';
- $is_mobile = is_mobile();
- $curDomain = $old_domain = $domain;
- if (is_array($config) && count($config) > 0) {
- $domains = array();
- $domains[] = $domain;
- if ($is_mobile) {
- $domains[] = str_replace('www.', 'm.', $domain);
- $domains[] = 'm.' . $domain;
- } else {
- $domains[] = str_replace('m.', '', $domain);
- $domains[] = str_replace('m.', 'www.', $domain);
- }
- foreach ($domains as $key => $value) {
- if (isset($config[$value])) {
- $v = $config[$value];
- if ($v == 'pay') {
- return;
- }
- if ($is_mobile) {
- if ($v == 'pc') {
- continue;
- }
- } else {
- if ($v != 'pc') {
- continue;
- }
- }
- $curDomain = $value;
- // $_SESSION['domain']=$value;
- }
- }
- }
- // echo $curDomain;
- if ($is_mobile) {
- if (S('CUR_PROJECT') == 'Home') {
- header("Location:http://" . $curDomain . "/m");
- //header("Location:http://" . $curDomain . "/mobile-index/index");
- }
- } else {
- if (S('CUR_PROJECT') == 'Mobile') {
- header("Location:http://" . $curDomain . "/home-index/index");
- }
- }
- /**
- * 数据签名认证
- * @param array $data 被认证的数据
- * @return string 签名
- */
- function data_auth_sign($data) {
- //数据类型检测
- if (!is_array($data)) {
- $data = (array) $data;
- }
- ksort($data); //排序
- $code = http_build_query($data); //url编码并生成query字符串
- $sign = sha1($code); //生成签名
- return $sign;
- }
- /*
- * 判断用户是否登录
- * */
- function isLogin($str = '',$type='agent') {
- $session = $_SESSION[$type.'Info'];
- if(empty($session)){
- return null;
- }
- if($type=='agent'){
- if($str=='name'){
- return $session['agent_user'];
- }
- if($str=='uid'){
- return $session['user_identity'];
- }
- }
- }
- }
- function jump($url){
- header('Location:'.$url);
- }
- //手机中间星号
- function phoneHide($phone)
- {
- if (!empty($phone)) {
- $phone = substr_replace($phone, '****', 3, 4);
- }
- return $phone;
- }
- /**
- * @param $game_code 球类代码
- * @param int $isJoin 是否用于join
- * @return array
- * @throws Exception
- * 根据球类代码 定义相关model
- */
- function getModels($game_code,$isJoin=1){
- $game_type = lm('GameType', 'Sports')->where('game_code',$game_code)->select('game_code')->first();
- // 获取不同球类model
- if($isJoin == 1){
- switch ($game_type->game_code){
- case 'zq':
- $model_league = 'st_zq_league';
- $model_match = 'st_zq_competition';
- $model_odds = 'st_zq_odds';
- $model_odds_record = 'st_zq_odds_record';
- $model_result = 'st_zq_result';
- $model_result_record = 'st_zq_result_record';
- $model_league_result = 'st_zq_league_result';
- $model_local_league = 'st_zq_local_league';
- $model_local_match = 'st_zq_local_match';
- $model_result_express = 'st_zq_result_express';
- break;
- case 'lq':
- $model_league = 'st_lq_league';
- $model_match = 'st_lq_competition';
- $model_odds = 'st_lq_odds';
- $model_odds_record = 'st_lq_odds_record';
- $model_result = 'st_lq_result';
- $model_result_record = 'st_lq_result_record';
- $model_league_result = 'st_lq_league_result';
- $model_local_league = 'st_lq_local_league';
- $model_local_match = 'st_lq_local_match';
- $model_result_express = 'st_lq_result_express';
- break;
- case 'wq':
- $model_league = 'st_wq_league';
- $model_match = 'st_wq_competition';
- $model_odds = 'st_wq_odds';
- $model_odds_record = 'st_wq_odds_record';
- $model_result = 'st_wq_result';
- $model_result_record = 'st_wq_result_record';
- $model_league_result = 'st_wq_league_result';
- $model_local_league = 'st_wq_local_league';
- $model_local_match = 'st_wq_local_match';
- $model_result_express = 'st_wq_result_express';
- break;
- case 'bq':
- $model_league = 'st_bq_league';
- $model_match = 'st_bq_competition';
- $model_odds = 'st_bq_odds';
- $model_odds_record = 'st_bq_odds_record';
- $model_result = 'st_bq_result';
- $model_result_record = 'st_bq_result_record';
- $model_league_result = 'st_bq_league_result';
- $model_local_league = 'st_bq_local_league';
- $model_local_match = 'st_bq_local_match';
- $model_result_express = 'st_bq_result_express';
- break;
- case 'gj':
- $model_league = 'st_zq_league';
- $model_match = 'st_zq_competition';
- $model_odds = 'st_zq_odds';
- $model_result = 'st_zq_result';
- break;
- default:
- throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
- }
- }else{
- switch ($game_type->game_code){
- case 'zq':
- $model_league = 'st_zq_league';
- $model_match = 'st_zq_competition';
- $model_odds = 'st_zq_odds';
- $model_result = 'SoccerResult';
- break;
- case 'lq':
- $model_league = 'st_lq_league';
- $model_match = 'st_lq_competition';
- $model_odds = 'st_lq_odds';
- $model_result = 'BasketballResult';
- break;
- case 'wq':
- $model_league = 'st_wq_league';
- $model_match = 'st_wq_competition';
- $model_odds = 'st_wq_odds';
- $model_result = 'TennisResult';
- break;
- case 'bq':
- $model_league = 'st_bq_league';
- $model_match = 'st_bq_competition';
- $model_odds = 'st_bq_odds';
- $model_result = 'BaseballResult';
- break;
- case 'gj':
- $model_league = 'st_zq_league';
- $model_match = 'st_zq_competition';
- $model_odds = 'st_zq_odds';
- $model_result = 'SoccerResult';
- break;
- default:
- throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
- }
- }
- $data = [
- 'model_league'=>$model_league,//联赛
- 'model_match'=>$model_match,//赛事
- 'model_odds'=>$model_odds,//赔率
- 'model_result' =>$model_result,//赛事结果
- 'model_odds_record' => $model_odds_record,//赔率记录
- 'model_result_record' =>$model_result_record,//赛事结果记录
- 'model_league_result'=>$model_league_result,//联赛结果
- 'model_local_league'=>$model_local_league,//本地关联联赛
- 'model_local_match'=>$model_local_match,//本地关联赛事
- 'model_result_express' =>$model_result_express,//赛果
- ];
- return $data;
- }
- /**
- * @param $type_code 状态码
- * @param $model_match 赛事表名
- * @param $game_code 球类代码
- * @return array
- * @throws \Exception
- * 获取不同状态下的 查询条件
- */
- function getState($type_code,$model_match = '',$game_code, $p_code){
- if($model_match == ''){
- switch ($type_code){
- case 'StRollBall'://滚球 正在进行
- //追加不同球类的滚球时间
- if($game_code == 'zq' || $game_code == 'gj'){
- $where = [
- // ['is_rollball','=',1],
- // ['status','<','2'],
- // ['match_date','=',date("Y-m-d")],
- // ['match_time','>',date("H:i:s", time()-(120*60))],
- // ['match_time','<',date("H:i:s", time())]
- ['status','=','1']
- ];
- }
- if($game_code == 'lq'){
- $where = [
- // ['is_rollball','=',1],
- // ['status','<','2'],
- // ['match_date','=',date("Y-m-d")],
- // ['match_time','>',date("H:i:s", time()-(90*60))],
- // ['match_time','<',date("H:i:s", time())]
- ['status','=','1']
- ];
- }
- if($game_code == 'wq'){
- $where = [
- // ['is_rollball','=',1],
- // ['status','<','2'],
- // ['match_date','=',date("Y-m-d")],
- // ['match_time','>',date("H:i:s", time()-(300*60))],
- // ['match_time','<',date("H:i:s", time())]
- ['status','=','1']
- ];
- }
- if($game_code == 'bq'){
- $where = [
- // ['is_rollball','=',1],
- // ['status','<','2'],
- // ['match_date','=',date("Y-m-d")],
- // ['match_time','>',date("H:i:s", time()-(300*60))],
- // ['match_time','<',date("H:i:s", time())]
- ['status','=','1']
- ];
- }
-
- break;
- case 'StSoon'://即将 今日两小时内开始
- $where = [
- ['status','=','0'],
- ['match_date','=',date("Y-m-d")],
- ['match_time','<',date("H:i:s", strtotime("+2 hour"))],
- ['match_time','>',date("H:i:s", time())]
- /*
- ['utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- break;
- case 'StToday'://今日 今日未开始未结束
- $where = [
- ['status', '<', '2'],
- ['match_date','=',date("Y-m-d")],
- ['match_time','>',date("H:i:s", time())],
- /*
- ['is_today', '=', 1],
- ['type', '=', '1'],
- ['utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- break;
- case 'StMorningPlate'://早盘
- //获取当天结束时间戳
- $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
- $where = [
- ['status', '<', '2'],
- // ['is_morningplate', '=', 1],
- ['us_time','>',$this->qgmdate('Y-m-d H:i:s', $endTime, -4)],
- /*
- ['match_date','>',date("Y-m-d",time())],
- ['match_time','>',date("H:i:s", time())],
- ['type', '=', '2'],
- ['utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- break;
- case 'StStringScene'://串场
- $where = [
- ['status', '<', '2'],
- ['is_stringscene', '=', 1],
- ['us_time','>',$this->qgmdate('Y-m-d H:i:s', '', -4)],
- /*
- ['match_date','>',date("Y-m-d",time())],
- ['match_time','>',date("H:i:s", time())],
- ['type', '=', '3'],
- ['utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- break;
- case 'StChampion'://冠军
- $where = [
- 'type'=>1
- ];
- break;
- default:
- throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
- }
- }else{
- switch ($type_code){
- case 'StRollBall'://滚球 正在进行
- //追加不同球类的滚球时间
- if($game_code == 'zq' || $game_code == 'gj'){
- $where = [
- // [$model_match.'.is_rollball','=',1],
- // [$model_match.'.status','<','2'],
- // [$model_match.'.match_date','=',date("Y-m-d")],
- // [$model_match.'.match_time','>',date("H:i:s", time()-(120*60))],
- // [$model_match.'.match_time','<',date("H:i:s", time())]
- [$model_match.'.status','=','1']
- ];
- }
- if($game_code == 'lq'){
- $where = [
- // [$model_match.'.is_rollball','=',1],
- // [$model_match.'.status','<','2'],
- // [$model_match.'.match_date','=',date("Y-m-d")],
- // [$model_match.'.match_time','>',date("H:i:s", time()-(90*60))],
- // [$model_match.'.match_time','<',date("H:i:s", time())]
- [$model_match.'.status','=','1']
- ];
- }
- if($game_code == 'wq'){
- $where = [
- // [$model_match.'.is_rollball','=',1],
- // [$model_match.'.status','<','2'],
- // [$model_match.'.match_date','=',date("Y-m-d")],
- // [$model_match.'.match_time','>',date("H:i:s", time()-(300*60))],
- // [$model_match.'.match_time','<',date("H:i:s", time())]
- [$model_match.'.status','=','1']
- ];
- }
- if($game_code == 'bq'){
- $where = [
- // [$model_match.'.is_rollball','=',1],
- // [$model_match.'.status','<','2'],
- // [$model_match.'.match_date','=',date("Y-m-d")],
- // [$model_match.'.match_time','>',date("H:i:s", time()-(300*60))],
- // [$model_match.'.match_time','<',date("H:i:s", time())]
- [$model_match.'.status','=','1']
- ];
- }
-
- break;
- case 'StSoon'://即将 今日两小时内开始
- if($p_code == 'kemp'){
- $where = [
- [$model_match.'.status','<','2']
- ];
- }else{
- $where = [
- [$model_match.'.status','=','0'],
- [$model_match.'.match_date','=',date("Y-m-d")],
- [$model_match.'.match_time','<',date("H:i:s", strtotime("+2 hour"))],
- [$model_match.'.match_time','>',date("H:i:s", time())]
- /*
- [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- }
- break;
- case 'StToday'://今日 今日未开始未结束
- if($p_code == 'kemp'){
- $where = [
- [$model_match.'.status', '<', '2'],
- ];
- }else{
- $where = [
- [$model_match.'.status', '<', '2'],
- [$model_match.'.match_date','=',date("Y-m-d")],
- [$model_match.'.match_time','>',date("H:i:s", time())],
- /*
- [$model_match.'.utime','>',date("Y-m-d").' 00:00:00'],
- [$model_match.'.is_today','=',1],
- [$model_match.'.type', '=', '1'],
- */
- ];
- }
- break;
- case 'all'://所有赛事
- $where = [
- [$model_match.'.status', '<', '2'],
- ];
- break;
- case 'StMorningPlate'://早盘
- if($p_code == 'kemp'){
- $where = [
- [$model_match.'.status', '<', '2']
- ];
- }else{
- //获取当天结束的时间戳
- $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
- $where = [
- [$model_match.'.status', '<', '2'],
- // [$model_match.'.is_morningplate','=',1],
- [$model_match.'.us_time','>',$this->qgmdate('Y-m-d H:i:s',$endTime , -4)],
- /*
- [$model_match.'.match_date','>',date("Y-m-d",time())],
- [$model_match.'.match_time','>',date("H:i:s", time())],
- [$model_match.'.type', '=', '2'],
- [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- }
- break;
- case 'StStringScene'://串场 查询串场状态为1 开赛时间大于当前时间
- $where = [
- [$model_match.'.is_stringscene','=',1],
- [$model_match.'.us_time','>',$this->qgmdate('Y-m-d H:i:s', '', -4)],
- [$model_match.'.status', '<', '2'],
- /*
- [$model_match.'.match_date','>',date("Y-m-d",time())],
- [$model_match.'.match_time','>',date("H:i:s", time())],
- [$model_match.'.type', '=', '3'],
- [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
- */
- ];
- break;
- case 'StChampion'://冠军
- $where = [
- 'type'=>1
- ];
- break;
- default:
- throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
- }
- }
- return $where;
- }
- /**
- * @param $data
- * @return array
- * 处理当国家下无联赛数据 删除该数组
- */
- function handleArr($data){
- $hData = [];
- foreach ($data as $key =>$v){
- if(empty($v['league_count'])){
- $v = [];
- }
- $hData[]=$v;
- }
- foreach($hData as $k=>$v){
- if(empty($v)){
- unset($hData[$k]);
- }
- }
- foreach ($hData as $k=>$v){
- $v['league_count'] = array_values($v['league_count']);
- $hData[$k]=$v;
- }
- return array_values($hData);
- }
- /**
- * @param $arr
- * @param $key
- * @return array
- * 去除二维数组重复项
- */
- function array_unset_tt($arr,$key){
- //建立一个目标数组
- $res = array();
- foreach ($arr as $value) {
- //查看有没有重复项
- if(isset($res[$value[$key]])){
- unset($value[$key]); //有:销毁
- }else{
- $res[$value[$key]] = $value;
- }
- }
- return $res;
- }
- /**
- * 处理冠军盘口 数组结构
- */
- function array_gj_tt($data,$oddsData){
- foreach ($data as $k=>&$v){
- foreach ($v['league_count'] as $kk=>&$vv){
- foreach ($oddsData as $kkk=>$vvv){
- if($vv['lg_id'] == $vvv['lg_id']){
- $vv['oddsData'][]=$vvv;
- }
- }
- if($vv['oddsData']){
- $vv['oddsData'] = $this->array_unset_tt($vv['oddsData'],'p_code');
- $vv['count'] = count($vv['oddsData']);
- unset($vv['oddsData']);
- }
- if(count($vv) != 5){
- unset($v['league_count'][$kk]);
- }
- }
- $v['league_count'] = array_values($v['league_count']);
- if(empty($v['league_count'])){
- unset($data[$k]);
- }
- }
- return array_values($data);
- }
- /**
- * 处理冠军以外 数组结构
- */
- function array_other_tt($data,$matchData){
- foreach ($data as $k=>&$v){
- foreach ($v['league_count'] as $kk=>&$vv){
- foreach ($matchData as $kkk=>$vvv){
- if($vv['lg_id'] == $vvv['lg_id']){
- $vv['matchData'][]=$vvv;
- }
- }
- if($vv['matchData']){
- $vv['matchData'] = $this->array_unset_tt($vv['matchData'],'match_id');
- $vv['count'] = count($vv['matchData']);
- unset($vv['matchData']);
- }
- if(empty($vv['count'])){
- unset($v['league_count'][$kk]);
- }
- }
- $v['league_count'] = array_values($v['league_count']);
- if(empty($v['league_count'])){
- unset($data[$k]);
- }
- }
- return array_values($data);
- }
- /**
- * 时间格式化
- * @param string $dateformat 时间格式
- * @param int $timestamp 时间戳
- * @param int $timeoffset 时区偏差 默认8 北京时间 -4美东时间
- * @return string
- */
- function qgmdate($dateformat = 'Y-m-d H:i:s', $timestamp = '', $timeoffset = 8,$type = '') {
- if(empty($timestamp)) {
- $timestamp = time();
- }
- $result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
- return $result;
- }
- /**
- * 判断二维数组是否包含一维数组
- * @param mixed $arr 查询条件
- * @param mixed $arrs 运动类型
- * @return string
- */
- function judgeEqual($arr, $arrs){
- foreach ($arrs as $value) {
- if (count($value) === count($arr)) {
- $a = 0;
- foreach ($value as $key => $v) {
- $a++;
- if ($v != $arr[$key]) {
- break;
- }
- if ($a == count($key)) { // 到最后一个元素都没有出现不相等,就说明这两个数组相等。
- return true;
- }
- }
- }
- }
- return false;
- }
- // 排列
- function arrangement($a, $m) {
- $r = array();
- $n = count($a);
- if ($m <= 0 || $m > $n) {
- return $r;
- }
- for ($i=0; $i<$n; $i++) {
- $b = $a;
- $t = array_splice($b, $i, 1);
- if ($m == 1) {
- $r[] = $t;
- } else {
- $c = $this->arrangement($b, $m-1);
- foreach ($c as $v) {
- $r[] = array_merge($t, $v);
- }
- }
- }
- return $r;
- }
- // 组合
- function combination($a, $m) {
- $r = array();
- $n = count($a);
- if ($m <= 0 || $m > $n) {
- return $r;
- }
- for ($i=0; $i<$n; $i++) {
- $t = array($a[$i]);
- if ($m == 1) {
- $r[] = $t;
- } else {
- $b = array_slice($a, $i+1);
- $c = $this->combination($b, $m-1);
- foreach ($c as $v) {
- $r[] = array_merge($t, $v);
- }
- }
- }
- return $r;
- }
- // 阶乘
- function factorial($n) {
- return array_product(range(1, $n));
- }
- // 排列数
- function A1($n, $m) {
- return factorial($n)/factorial($n-$m);
- }
- // 组合数
- function C1($n, $m) {
- return A1($n, $m)/factorial($m);
- }
- //处理空赔率赛事
- function Handle_Odds_Null($matchData=[],$models=[],$oddsTypeWhere=[]){
- //===获取当前赛事是否有赔率,如果没有则去除===
- //获取当前match_id
- $match_ids = [];
- foreach($matchData as $k=>$v){
- $match_ids[] = $v['match_id'];
- }
- //获取下赛事赔率
- $oddsData = lm($models['model_odds'],'Sports')
- ->select('match_id')
- ->whereIn('match_id',$match_ids)
- ->where($oddsTypeWhere)
- ->distinct('match_id')
- ->get()
- ->toArray();
- //如果赔率为空,则返回空
- if(empty($oddsData)) return $oddsData;
- $matchData_new = [];
- foreach($matchData as $k=>$v){
- foreach($oddsData as $kk=>$vv){
- if($v['match_id'] == $vv['match_id']){
- $matchData_new[] = $matchData[$k];
- }
- }
- }
-
- return $matchData_new;
- //===赛事空赔率 处理end ===
- }
- //处理空赔率联赛
- function Handle_Odds_Null_lg($matchData=[],$models=[]){
- //===获取当前联赛是否有赔率,如果没有则去除===
- //获取当前lg_id
- $lg_ids = [];
- foreach($matchData as $k=>$v){
- $lg_ids[] = $v['lg_id'];
- }
- //获取下赛事赔率
- $oddsData = lm($models['model_odds'],'Sports')
- ->select('lg_id')
- ->whereIn('lg_id',$lg_ids)
- ->where('type',1)
- ->distinct('lg_id')
- ->get()
- ->toArray();
- //如果赔率为空,则返回空
- if(empty($oddsData)) return $oddsData;
- $matchData_new = [];
- foreach($matchData as $k=>$v){
- foreach($oddsData as $kk=>$vv){
- if($v['lg_id'] == $vv['lg_id']){
- $matchData_new[] = $matchData[$k];
- }
- }
- }
- return $matchData_new;
- //===联赛空赔率 处理end ===
- }
- /**
- * 处理各球类投注: 可赢金额计算
- * $game_code 球类代码
- * $p_code 父级玩法代码
- * $odds 赔率
- * $bet_amount 投注金额
- * $type 投注类型 1单式
- */
- function getEarnMoney($game_code='',$p_code='',$odds=0,$bet_amount=0,$type=0){
- /*
- 可赢额直接乘以赔率的父级玩法有
- 足球:球队进球数:大/小 进球:大/小 让球
- 篮球:让球 总分:大/小 球队得分大小
- 网球:让盘 让局 总局数:大/小
- 棒球:让球 总得分:大/小
- */
- //单式投注
- if($type == 1){
- //足球 可赢金额 计算规则
- if($game_code == 'zq'){
- //球队进球数:大/小;进球:大/小;让球;
- if($p_code == 'TB' || $p_code == 'GS' || $p_code == 'CO'){
- //可赢金额 = 赔率*投注金额
- $prize_money = $odds*$bet_amount;
- }else{
- //可赢金额 = (赔率-1)*投注金额
- $prize_money = ($odds-1)*$bet_amount;
- }
- }
- //篮球 可赢金额 计算规则
- if($game_code == 'lq'){
- //让球 总分:大/小 球队得分大小
- if($p_code == 'TN' || $p_code == 'TB' || $p_code == 'CO'){
- //可赢金额 = 赔率*投注金额
- $prize_money = $odds*$bet_amount;
- }else{
- //可赢金额 = (赔率-1)*投注金额
- $prize_money = ($odds-1)*$bet_amount;
- }
- }
- //网球 可赢金额 计算规则
- if($game_code == 'wq'){
- //让盘 让局 总局数:大/小
- if($p_code == 'LD' || $p_code == 'LB' || $p_code == 'TN'){
- //可赢金额 = 赔率*投注金额
- $prize_money = $odds*$bet_amount;
- }else{
- //可赢金额 = (赔率-1)*投注金额
- $prize_money = ($odds-1)*$bet_amount;
- }
- }
- //棒球
- if($game_code == 'bq'){
- //让球 总得分:大/小
- if($p_code == 'CO' || $p_code == 'TN'){
- //可赢金额 = 赔率*投注金额
- $prize_money = $odds*$bet_amount;
- }else{
- //可赢金额 = (赔率-1)*投注金额
- $prize_money = ($odds-1)*$bet_amount;
- }
- }
- }
- return $prize_money;
- }
- //计算赛事当前进行时间
- function getMatchTime($match_date,$match_time){
- $time = time() - strtotime($match_date.' '.$match_time);
- //如果需要时刻格式
- // $time = $this->dataformat($time);
- return floor($time/60);
- }
- //将秒 转换为 时 分 秒
- function dataformat($num=0) {
- $hour = floor($num/3600);
- $minute = floor(($num-3600*$hour)/60);
- $second = floor((($num-3600*$hour)-60*$minute)%60);
- return $hour.':'.$minute.':'.$second;
- }
- //根据赛事进行 时间 获取进程
- function getMatchProcess($time=0,$game_code){
- if($game_code == 'zq'){
- if($time < 45) return '上半场';
- return '下半场';
- }
- if($game_code == 'lq'){
- if($time < 10) return '第一节';
- if(($time > 10) and ($time < 20)) return '第二节';
- if(($time > 20) and ($time < 30)) return '第三节';
- if(($time > 30) and ($time < 40)) return '第四节';
- }
- if($game_code == 'wq'){
- return '';
- }
- if($game_code == 'bq'){
- return '';
- }
- }
- //返回数据源对应ID
- function getSourceID($source){
- $data = [
- 'hg3535'=>1,
- 'hg0088'=>2,
- '188'=>3,
- ];
- if(!empty($data[$source])) {
- $sourceID = $data[$source];
- }else{
- $sourceID = 0;
- }
- return $sourceID;
- }
- //生成订单/批次号
- function orderSn() {
- $year_code = array('A','B','C','D','E','F','G','H','I','J');//年份
- $order_sn = $year_code[intval(date('Y'))-2019].
- strtoupper(dechex(date('m'))).date('d').
- substr(time(),-5).substr(microtime(),2,5).sprintf('d',rand(0,99));
- return $order_sn;
- }
-
- //根据球类追加限定玩法
- function getOddsPcode($game_code = 'zq'){
- $p_code = [];
- if($game_code == 'zq'){
- $p_code = ['C','B','TG','CB','TB','GS','CO','TS'];
- }
- if($game_code == 'lq'){
- $p_code = ['LN','C','TN','TS','TB','CO'];
- }
- if($game_code == 'wq'){
- $p_code = ['LB','TN','TS','B','LD','C'];
- }
- if($game_code == 'bq'){
- $p_code = ['CO','C','TN','TS'];
- }
- return $p_code;
- }
- /**
- * 追加写投注队列
- * Tank
- * 2019.11.16
- * 订单号 比赛id188 开赛日期时间 比赛状态(早盘,今日,串场,滚球), 父级玩法, 子级玩法,排序
- */
- function setOrderQueue($orderData){
- $data = json_encode($orderData,256);
- $redis = (new RedisOP())->get();
- dump($redis);exit;
- $redis->select(1);
- $key = "ORDER_QUEUE";
- $ret = $redis->lpush($key,$data);
- }
- }
- ?>
|