UnSettmatchLogic.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/15
  6. * Time: 16:51
  7. */
  8. namespace App\Logic;
  9. use App\Models\Account;
  10. use Illuminate\Database\Capsule\Manager as DB;
  11. use datainf\pplus\Instance;
  12. use datainf\lib\GlobConfigs;
  13. use App\Lib\ModelBase;
  14. use App\Http\Response\Response;
  15. use App\Models\SportsNoteList as MoneyBuySimplexModel;
  16. use App\Models\MoneyBuyStr as MoneyBuyStrModel;
  17. use App\Models\MoneyBuyMatch as MoneyBuyMatchModel;
  18. use App\Models\Money_details as MoneyDetailsModel;
  19. use App\Models\Setinfo as SetinfoModel;
  20. use App\Models\Account as AccountModel;
  21. //按赛事撤销操作(批量优化版)
  22. class UnSettmatchLogic
  23. {
  24. use Instance;
  25. protected $redis = '';
  26. private $fanshui = 0.01; //默认返水比例
  27. protected $game_code = '';
  28. protected $match_id = 0;
  29. protected $resultObj = ''; //比赛结果记录对像
  30. public function doUnsetMatch($request, $response, $datas)
  31. {
  32. $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
  33. $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
  34. if (!in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  35. goto PARAEORROR;
  36. }
  37. $result = $this->getResult($game_code, $match_id);
  38. if (empty($match_id) || empty($game_code) || empty($result)) {
  39. PARAEORROR:
  40. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误!";
  41. unset($datas['token']);
  42. $this->backandret($response, 0, $msg, $datas);
  43. return false;
  44. }
  45. $this->game_code = $game_code;
  46. $this->match_id = $match_id;
  47. $this->resultObj = $request;
  48. //这里只做已结算过,又要撤单的情况 ,未结算的,由后台直接处理掉了
  49. if (!in_array($result->status, [3])) {
  50. unset($datas['token']);
  51. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--数据状态值不为[3]!";
  52. $this->backandret($response, 0, $msg, $datas);
  53. return false;
  54. }
  55. if ($this->optFlag($game_code, $match_id, 2)) {
  56. unset($datas['token']);
  57. $msg = date("Y-m-d H:i:s") . " 正在处理中,不能重复提交!";
  58. $this->backandret($response, 0, $msg, $datas);
  59. return false;
  60. }
  61. $this->getFanshui();
  62. $this->optFlag($game_code, $match_id, 1);
  63. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--开始begin:game_code= $game_code match_id= $match_id ";
  64. $this->backandret($response, 1, $msg, $datas, 1);
  65. DB::beginTransaction();
  66. try {
  67. $this->doSimple();
  68. $this->doStr();
  69. $this->changeStatus($game_code, $match_id);
  70. $this->changeMatchResult($game_code, $match_id);
  71. DB::commit();
  72. } catch (\Exception $e) {
  73. DB::rollBack();
  74. unset($datas['token']);
  75. $this->optFlag($game_code, $match_id, 0);
  76. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--异常结束:game_code= $game_code match_id= $match_id Emsg: " . $e->getMessage();
  77. $this->backandret($response, 0, '撤单失败!' . $msg, $datas);
  78. return;
  79. }
  80. $this->optFlag($game_code, $match_id, 0);
  81. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--结束end:game_code= $game_code match_id= $match_id ";
  82. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  83. return;
  84. }
  85. //撤销单个订单
  86. public function doUnsetMatchOneOrder($request, $response, $datas)
  87. {
  88. $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
  89. $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
  90. $order_id = isset($datas['order_id']) ? trim($datas['order_id']) : 0;
  91. if (empty($match_id) || empty($game_code) || empty($order_id) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  92. PARAEORROR:
  93. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!";
  94. unset($datas['token']);
  95. $this->backandret($response, 0, $msg, $datas);
  96. return false;
  97. }
  98. $bet_type = 1;
  99. $order_main = $this->getSimpleOrders($game_code, $match_id, $order_id);
  100. if (empty($order_main)) {
  101. $order_main = $this->getStrOrders($game_code, $match_id, $order_id);
  102. $bet_type = 2;
  103. }
  104. if (empty($order_main) || empty($order_main->matchdatas)) {
  105. goto PARAEORROR;
  106. return;
  107. }
  108. if ($order_main->status != 1) {
  109. $msg = date("Y-m-d H:i:s") . " 已撤销过的订单(不用处理)!--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  110. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  111. return;
  112. }
  113. foreach ($order_main->matchdatas as $matitem) {
  114. if ($matitem->match_id == $match_id) {
  115. $matitem->result = 0;
  116. $matitem->matchresult = '';
  117. $matitem->save();
  118. }
  119. }
  120. $this->CancelOneOrder($order_main);
  121. DB::table('settlement_middle_detail')->where('order_id', $order_id)->update(['money' => 0]);
  122. $order_main->status = 2;
  123. $order_main->settlement_time = date('Y-m-d H:i:s');
  124. $order_main->use_mark = '';
  125. $order_main->game_status = 0;
  126. $order_main->settle_status = 1;
  127. $order_main->gain_money = 0;
  128. $order_main->save();
  129. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  130. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  131. return;
  132. }
  133. //把一个串式订单 由撤单状态改为 下单状态 (扣钱,写流水,改订单状态)
  134. public function RestartStringOneOrder($request, $response, $datas)
  135. {
  136. $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
  137. $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
  138. $order_id = isset($datas['order_id']) ? trim($datas['order_id']) : 0;
  139. if (empty($match_id) || empty($game_code) || empty($order_id) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  140. PARAEORROR:
  141. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!";
  142. unset($datas['token']);
  143. $this->backandret($response, 0, $msg, $datas);
  144. return false;
  145. }
  146. $OrderObj = $order_main = $this->getStrOrders($game_code, $match_id, $order_id);
  147. if (empty($order_main) || empty($order_main->matchdatas)) {
  148. goto PARAEORROR;
  149. return;
  150. }
  151. if ($order_main->status == 1) {
  152. $msg = date("Y-m-d H:i:s") . " 订单状态为正常下单状态(不用处理)!--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  153. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  154. return;
  155. }
  156. $order_money = $order_main->money;
  157. $fanshuimodel = DB::table('setinfo')->where('identity', '1000')->first();
  158. $fanshui = floatval(trim($fanshuimodel->infocontent));
  159. $money = $order_money * (1 - $fanshui);
  160. $Accobj = (new AccountModel())->with('detail')->where('identity', $order_main->account_identity)->first();
  161. $moneyDetail = new MoneyDetailsModel();
  162. $moneyDetail->info_identity = UUID();
  163. $moneyDetail->trade_id = $OrderObj->order_id;
  164. $moneyDetail->account_name = $Accobj->account;
  165. $moneyDetail->account_identity = $OrderObj->account_identity;
  166. $moneyDetail->money = abs($money);
  167. $moneyDetail->money_time = date("Y-m-d H:i:s");
  168. $moneyDetail->money_type = 2;
  169. $moneyDetail->money_cash = $Accobj->detail->cash - $money;
  170. $moneyDetail->trade_type = 3;
  171. $moneyDetail->trade_desc = '由撤销转为下单状态';
  172. $moneyDetail->reason = '由撤销转为下单状态';
  173. $moneyDetail->status = 1;
  174. $moneyDetail->sysetem_user = 'system';
  175. $moneyDetail->out_order_id = $OrderObj->order_id;
  176. $moneyDetail->remark = '由撤销转为下单状态';
  177. $ret = $moneyDetail->save();
  178. $nowmoney = $Accobj->detail->cash - $money;
  179. DB::table('account_detailed')->where('account_identity', $OrderObj->account_identity)->update(['available_cash' => $nowmoney, 'cash' => $nowmoney]);
  180. $order_main->status = 1;
  181. $order_main->settlement_time = date('Y-m-d H:i:s');
  182. $order_main->use_mark = '';
  183. $order_main->game_status = 0;
  184. $order_main->settle_status = 1;
  185. $order_main->gain_money = 0;
  186. $order_main->save();
  187. $msg = date("Y-m-d H:i:s") . " 串单由撤单改为下单成功--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  188. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  189. return;
  190. }
  191. //作撤销某个订单
  192. public function CancelOneOrder($OrderObj)
  193. {
  194. //撤单时应该为用户加钱还是扣钱 >0加钱 <0扣钱 ==0不用操作
  195. $money = $this->CountDetailsTounSet($OrderObj->order_id);
  196. if ($money == 0) {
  197. return true;
  198. }
  199. $Accobj = (new AccountModel())->with('detail')->where('identity', $OrderObj->account_identity)->first();
  200. $moneyDetail = new MoneyDetailsModel();
  201. $moneyDetail->info_identity = UUID();
  202. $moneyDetail->trade_id = $OrderObj->order_id;
  203. $moneyDetail->account_name = $Accobj->account;
  204. $moneyDetail->account_identity = $OrderObj->account_identity;
  205. $moneyDetail->money = abs($money);
  206. $moneyDetail->money_time = date("Y-m-d H:i:s");
  207. $moneyDetail->money_type = $money > 0 ? 1 : 2;
  208. $moneyDetail->money_cash = $Accobj->detail->cash + $money;
  209. $moneyDetail->trade_type = 3;
  210. $moneyDetail->trade_desc = '撤单资金平账处理';
  211. $moneyDetail->reason = '撤单资金平账处理';
  212. $moneyDetail->status = 1;
  213. $moneyDetail->sysetem_user = 'system';
  214. $moneyDetail->out_order_id = $OrderObj->order_id;
  215. $moneyDetail->remark = '撤单资金平账处理';
  216. $ret = $moneyDetail->save();
  217. $nowmoney = $Accobj->detail->cash + $money;
  218. DB::table('account_detailed')->where('account_identity', $OrderObj->account_identity)->update(['available_cash' => $nowmoney, 'cash' => $nowmoney]);
  219. return $ret;
  220. }
  221. //统计流水表中 所有此订单的 出入总计,用于撤销订单使用
  222. public function CountDetailsTounSet($order_id)
  223. {
  224. $sql = "select sum(money) as msum,money_type from money_details where trade_id='$order_id' group by money_type ";
  225. $rets = DB::select($sql);
  226. $ulose = $uadd = 0;
  227. foreach ($rets as $val) {
  228. if ($val->money_type == 1) {
  229. $uadd = $val->msum;
  230. }
  231. if ($val->money_type == 2) {
  232. $ulose = $val->msum;
  233. }
  234. }
  235. $return = $ulose - $uadd;
  236. return $return;
  237. }
  238. //撤销串式订单(整个订单撤销)
  239. public function UnSettelementOneStringOrder($request, $response, $datas)
  240. {
  241. return $this->doUnsetMatchOneOrder($request, $response, $datas);
  242. }
  243. //返回消息和写日志
  244. public function backandret($response, $code, $msg, $data, $onlylog = 0)
  245. {
  246. $dataRet = Response::generate('', $code, $data, $msg);
  247. echo $dataRet . "\n";
  248. if ($onlylog) {
  249. return;
  250. }
  251. $response->end($dataRet);
  252. return;
  253. }
  254. //得到返水的比例
  255. public function getFanshui()
  256. {
  257. $ret = (new SetinfoModel())->getInfo(1001);
  258. if (is_array($ret)) {
  259. $val = floatval($ret['infocontent']);
  260. $this->fanshui = $val;
  261. return $val;
  262. }
  263. }
  264. //处理单式订单相关逻辑
  265. public function doSimple()
  266. {
  267. $simples = $this->getSimpleOrders($this->game_code, $this->match_id);
  268. if (empty($simples)) {
  269. return;
  270. }
  271. $useraccs = [];
  272. foreach ($simples as $item) {
  273. if (!isset($useraccs[$item->account_identity])) {
  274. $useraccs[$item->account_identity] = $item->account_identity;
  275. }
  276. }
  277. //关联用户
  278. $userInents = $this->getAccountIdents($useraccs);
  279. //用户资金变化记录,以后好一次性更新用户余额
  280. $time = date("Y-m-d H:i:s");
  281. $userMoneyChange = []; //用户资金变化记录,好一次性更新
  282. $userMoneyChange_less = []; //用户余额情况
  283. $orders_array = [];
  284. foreach ($simples as $item) {
  285. $gain_money = floatval($item->gain_money);
  286. $accountIdent = $item->account_identity;
  287. $orders_array[] = $item->order_id;
  288. $mModel = new MoneyDetailsModel();
  289. $mModel->info_identity = UUID();
  290. $mModel->trade_id = $item->order_id;
  291. $mModel->account_name = $userInents[$accountIdent]->account;
  292. $mModel->account_identity = $accountIdent;
  293. $mModel->money_time = $time;
  294. $mModel->money_type = 3;
  295. $mModel->trade_type = 3;
  296. $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id;
  297. if ($gain_money * 100 <= 1) {
  298. $return_money = $item->money * (1 - $this->fanshui);
  299. $mModel->money_type = 1;
  300. $mModel->money = $return_money;
  301. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money;
  302. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] + $userMoneyChange[$accountIdent];
  303. } else {
  304. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  305. $mModel->money_type = 2;
  306. $mModel->money = $return_money;
  307. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money;
  308. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money;
  309. }
  310. $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]);
  311. $mModel->save();
  312. $item->status = 2;
  313. $item->gain_money = 0;
  314. $item->game_status = 2;
  315. $item->settlement_time = date('Y-m-d H:i:s');
  316. $item->use_mark = '';
  317. $item->save();
  318. }
  319. if ($userMoneyChange) {
  320. foreach ($userMoneyChange as $uident => $money) {
  321. if (abs($money) > 0) {
  322. $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money;
  323. $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money;
  324. $userInents[$uident]->detail->save();
  325. }
  326. }
  327. }
  328. return true;
  329. }
  330. //处理串式订单相关逻辑
  331. public function doStr()
  332. {
  333. $strOrders = $this->getStrOrders($this->game_code, $this->match_id);
  334. if (empty($strOrders)) {
  335. return;
  336. }
  337. $userMoneyArr = [];
  338. //这里只把结果改为平局,再提交到旧的重新结算订单接口处理
  339. foreach ($strOrders as $item2) {
  340. //如果存在中奖的,要先扣回赢的钱,再以平处理
  341. if ($item2->gain_money > 0) {
  342. $acc = $item2->account_indentity;
  343. $nowmoney = $item2->gain_money;
  344. $userMoneyArr[$acc] = isset($userMoneyArr[$acc]) ? $userMoneyArr[$acc] + $nowmoney : $nowmoney;
  345. }
  346. $item2->status = 2;
  347. $item2->gain_money = 0;
  348. $item2->game_status = 2;
  349. $item2->settlement_time = date('Y-m-d H:i:s');
  350. $item2->save();
  351. $mats = $item2->matchdatas;
  352. foreach ($mats as $item3) {
  353. $item3->result = 2;
  354. $item3->matchresult = '通过设置赛事比分作废!';
  355. $item3->save();
  356. }
  357. }
  358. if ($userMoneyArr) {
  359. $this->AccountMoneydec($userMoneyArr);
  360. }
  361. $dataArr = [
  362. 'game_code' => $this->game_code,
  363. 'match_id' => $this->match_id,
  364. 'settype' => 2,
  365. 'bettype' => 2,
  366. 'change_status' => 0,
  367. 'order_ids' => '',
  368. ];
  369. $url = 'http://127.0.0.1:9094/Settelement';
  370. post_curls($url, $dataArr);
  371. /*
  372. foreach ($strOrders as $item) {
  373. $dataArr['order_ids'] = $item->order_id;
  374. post_curls($url, $dataArr);
  375. }
  376. */
  377. return;
  378. }
  379. //对于已结算的又撤单的要扣回钱
  380. public function AccountMoneydec($account_identity_array)
  381. {
  382. foreach ($account_identity_array as $acc => $money) {
  383. $accarr[] = $acc;
  384. }
  385. $objs = $this->etAccountIdents($accarr);
  386. foreach ($account_identity_array as $acc2 => $money) {
  387. $nowmoney = $objs[$acc2]->detail->available_cash - $money;
  388. $objs[$acc2]->detail->available_cash = $nowmoney;
  389. $objs[$acc2]->detail->cash = $nowmoney;
  390. $objs[$acc2]->detail->save();
  391. }
  392. return true;
  393. }
  394. //得到某个账户的信息,主要是余额情况
  395. public function getAccountIdents($account_identity_array)
  396. {
  397. $model = new Account();
  398. $ret = [];
  399. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  400. if ($return) {
  401. foreach ($return as $item) {
  402. $ret[$item->identity] = $item;
  403. }
  404. }
  405. return $ret;
  406. }
  407. public function changeStatus($game_code, $match_id)
  408. {
  409. $table = 'st_' . $game_code . '_result';
  410. $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]);
  411. return $ret;
  412. }
  413. public function changeMatchResult($game_code, $match_id, $order_id = 0)
  414. {
  415. $table = 'money_buy_match';
  416. if (empty($order_id)) {
  417. $ret = DB::table($table)->where('match_id', $match_id)->where('game_code', $game_code)->update(['result' => 2, 'matchresult' => '撤单平局!']);
  418. } else {
  419. $ret = DB::table($table)->where([['match_id', '=', $match_id], ['game_code', '=', $game_code], ['order_id', '=', $order_id]])->update(['result' => 2, 'matchresult' => '撤单平局.']);
  420. }
  421. return $ret;
  422. }
  423. //得到比赛结果 ,看是不已结算等信息
  424. public function getResult($game_code, $match_id)
  425. {
  426. $table = 'st_' . $game_code . '_result';
  427. $ret = DB::table($table)->where('match_id', $match_id)->first();
  428. return $ret;
  429. }
  430. //得到 有此赛事的单式订单
  431. public function getSimpleOrders($game_code, $match_id, $order_id = 0)
  432. {
  433. $model = new MoneyBuySimplexModel();
  434. if (empty($order_id)) {
  435. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  436. } else {
  437. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id, 'order_id' => $order_id])->first();
  438. }
  439. return $rets;
  440. }
  441. //得到 有此赛事的串式订单
  442. public function getStrOrders($game_code, $match_id, $order_id = 0)
  443. {
  444. $model = new MoneyBuyStrModel();
  445. if (empty($order_id)) {
  446. $model2 = new MoneyBuyMatchModel();
  447. $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]);
  448. $orders = [];
  449. if ($rets2) {
  450. foreach ($rets2 as $item) {
  451. $orders[] = $item->order_id;
  452. }
  453. } else {
  454. return [];
  455. }
  456. $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get();
  457. return $rets;
  458. }
  459. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  460. return $rets;
  461. }
  462. //按订单查询(串式)
  463. public function getByOrderString($order_id)
  464. {
  465. /*
  466. $ret = [];
  467. $model2 = new MoneyBuyMatchModel();
  468. $rets2 = $model2->getByAttrs([['order_ID', '=', $order_id]]);
  469. if (empty($rets2)) {
  470. return $ret;
  471. }
  472. */
  473. $model = new MoneyBuyStrModel();
  474. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  475. return $rets;
  476. }
  477. //获取相关的订单费用及系统赚送的钱(用于返还)
  478. public function getMoneyDetail($ordesArray)
  479. {
  480. $model = new MoneyDetailsModel();
  481. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  482. return $ret;
  483. }
  484. //设置操作标志位,防止事务未处理完,再次提交的情况
  485. //opt 0删除 1设置 2查询
  486. public function optFlag($game_code, $match_id, $opt = 2)
  487. {
  488. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  489. $redis = $this->getRedis();
  490. if ($opt == 0) {
  491. $redis->del($key);
  492. return true;
  493. }
  494. if ($opt == 1) {
  495. $redis->setex($key, 300, time());
  496. return;
  497. }
  498. if ($opt == 2) {
  499. $ret = $redis->get($key);
  500. return $ret;
  501. }
  502. }
  503. public function getRedis()
  504. {
  505. if ($this->redis) {
  506. return $this->redis;
  507. }
  508. $config = GlobConfigs::getKey('redis');
  509. $redis = new \Redis();
  510. $redis->connect($config['host'], $config['port']);
  511. $redis->auth($config['passwd']);
  512. $redis->select($config['db']);
  513. $this->redis = $redis;
  514. return $redis;
  515. }
  516. }