GameLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/12
  6. * Time: 9:32
  7. */
  8. namespace Biz\Game;
  9. use Biz\Account\AccountManager;
  10. use Biz\Game\TranferMoneyLogic;
  11. use App\Commons\Model\Wagent_clientgame_log as Wagent_clientgame_logModel;
  12. use Illuminate\Database\Capsule\Manager as DB;
  13. use Biz\Game\SportbetRecordLogic;
  14. use App\Commons\Model\WagentCheckToken as WagentCheckTokenModel;
  15. class GameLogic
  16. {
  17. private $paras = [];
  18. private $userKey = '';
  19. private $wagetnModel = null;
  20. public function __construct($paras, $userKey, $wagentModel)
  21. {
  22. $this->paras = $paras;
  23. $this->userKey = $userKey;
  24. $this->wagetnModel = $wagentModel;
  25. }
  26. //返回数据
  27. private function makeret($status = 1, $msg = 'success', $datas = [])
  28. {
  29. return [
  30. 'status' => $status,
  31. 'msg' => $msg,
  32. 'datas' => $datas
  33. ];
  34. }
  35. //CheckAccountIsExist(账户是否存在)
  36. public function CheckAccountIsExist()
  37. {
  38. $username = isset($this->paras['username']) ? trim($this->paras['username']) : '';
  39. if (empty($username)) {
  40. return $this->makeret(0, '请求参数缺失', 'key_error');
  41. }
  42. $username = $this->wagetnModel->agent_pre . $username;
  43. $ac = new AccountManager();
  44. $ret = $ac->getAccount($username);
  45. if ($ret) {
  46. return $this->makeret(1, 'success', 1);
  47. } else {
  48. return $this->makeret(1, 'success', 0);
  49. }
  50. }
  51. //CheckAndCreateAccount(检测并创建游戏帐户)
  52. public function CheckAndCreateAccount()
  53. {
  54. $username = isset($this->paras['username']) ? trim($this->paras['username']) : '';
  55. $password = isset($this->paras['password']) ? trim($this->paras['password']) : '';
  56. $remark = [
  57. 'moneysort' => '',
  58. 'limitvideo' => 0,
  59. 'limitroulette' => 0,
  60. ];
  61. $remark['moneysort'] = isset($this->paras['moneysort']) ? trim($this->paras['moneysort']) : '';
  62. $remark['limitvideo'] = isset($this->paras['limitvideo']) ? intval($this->paras['limitvideo']) : 0;
  63. $remark['limitroulette'] = isset($this->paras['limitroulette']) ? intval($this->paras['limitroulette']) : 0;
  64. $remark = json_encode($remark);
  65. if (empty($username) || empty($password)) {
  66. return $this->makeret(0, '请求参数缺失', 'key_error');
  67. }
  68. $username = $this->wagetnModel->agent_pre . $username;
  69. $ac = new AccountManager();
  70. $userInfo = $ac->getAccount($username);
  71. if ($userInfo) {
  72. return $this->makeret(1, 'success', '1');
  73. }
  74. if (!$this->nameCheck($username)) {
  75. return $this->makeret(0, 'false', 3);
  76. }
  77. if (!$this->passCheck($password)) {
  78. return $this->makeret(0, 'false', 2);
  79. }
  80. $wagent_name = isset($this->paras['agent']) ? $this->paras['agent'] : '';
  81. $postdatas = [
  82. 'account' => $username,
  83. 'password' => $password,
  84. 'again_password' => $password,
  85. 'name' => $username,
  86. 'phone' => uniqid('agent_'),
  87. 'remark' => $remark,
  88. 'wagent_name' => $wagent_name,
  89. ];
  90. $ac = new AccountManager();
  91. $ret = $ac->register($postdatas);
  92. if (isset($ret['status']) && $ret['status'] == 1) {
  93. return $this->makeret(1, 'success', '1');
  94. } else {
  95. $errormsg = isset($ret['msg']) ? $ret['msg'] : 'false';
  96. return $this->makeret(0, $errormsg, '0');
  97. }
  98. }
  99. private function nameCheck($name)
  100. {
  101. if (!preg_match("/^(\w){4,24}$/", $name)) {
  102. return false;
  103. }
  104. /*
  105. if (mb_strlen($name) >= 32) {
  106. return false;
  107. }
  108. */
  109. return true;
  110. }
  111. private function passCheck($pass)
  112. {
  113. $skip = ['\'', '"', '\\', '/', '>', '<', '&', '#', '--', '%', '?', '$', ' '];
  114. foreach ($skip as $val) {
  115. if (stripos($pass, $val) !== false) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. //GetBalance(查询余额)
  122. public function GetBalance()
  123. {
  124. $accountModel = $this->accountAndPassCheck();
  125. $account_identity = $accountModel->identity;
  126. $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $account_identity)->first();
  127. if (empty($parentinfo)) {
  128. return $this->makeret(1, '账号记录不存在', '0');
  129. }
  130. return $this->makeret(1, 'success', $parentinfo->cash);
  131. }
  132. public function accountAndPassCheck()
  133. {
  134. $username = isset($this->paras['username']) ? trim($this->paras['username']) : '';
  135. $password = isset($this->paras['password']) ? trim($this->paras['password']) : '';
  136. if (empty($username) || empty($password)) {
  137. $ret = $this->makeret(0, '请求参数缺失', 'key_error');
  138. Render($ret['datas'], $ret['status'], $ret['msg']);
  139. }
  140. $username = $this->wagetnModel->agent_pre . $username;
  141. $ac = new AccountManager();
  142. $account = $ac->getAccount($username);
  143. if (!$account) {
  144. $ret = $this->makeret(0, '账号不存在', 'account_no_exist');
  145. Render($ret['datas'], $ret['status'], $ret['msg']);
  146. }
  147. $account_identity = $account->identity;
  148. $passCheckret = VerPassword($account_identity, $password);
  149. if (!$passCheckret) {
  150. $ret = $this->makeret(0, '密码有误', 'key_error');
  151. Render($ret['datas'], $ret['status'], $ret['msg']);
  152. }
  153. return $account;
  154. }
  155. //TransferCredit(转帐)
  156. public function TransferCredit()
  157. {
  158. $accountModel = $this->accountAndPassCheck();
  159. if ($accountModel->status != 1) {
  160. $ret = $this->makeret(0, 'false', 'userStatus false!');
  161. return $ret;
  162. }
  163. $tlogc = new TranferMoneyLogic($accountModel, $this->paras, $this->wagetnModel);
  164. $ret = $tlogc->dorun();
  165. return $ret;
  166. }
  167. //ConfirmTransferCredit(查询转帐)
  168. public function ConfirmTransferCredit()
  169. {
  170. $accountModel = $this->accountAndPassCheck();
  171. $billno = isset($this->paras['billno']) ? trim($this->paras['billno']) : '';
  172. if (empty($billno)) {
  173. $ret = $this->makeret(0, 'billno error', '0');
  174. return $ret;
  175. }
  176. $type = isset($this->paras['type']) ? strtoupper(trim($this->paras['type'])) : '';
  177. if (empty($type) || !in_array($type, ['IN', 'OUT'])) {
  178. $ret = $this->makeret(0, 'type error', '0');
  179. return $ret;
  180. }
  181. $where = [
  182. 'account_indent' => $accountModel->identity,
  183. 'agent_name' => $this->wagetnModel->agent_name,
  184. 'type' => $type,
  185. 'billno' => $billno
  186. ];
  187. $model = lm('Money_transfer', 'Commons')->where($where)->first();
  188. if ($model) {
  189. $ret = $this->makeret(1, 'success', 1);
  190. return $ret;
  191. } else {
  192. $ret = $this->makeret(0, 'false', 0);
  193. return $ret;
  194. }
  195. }
  196. //TransferGame(进入游戏)(记录登陆日志,并自动登陆一次,返回用户登陆信息)
  197. public function TransferGame()
  198. {
  199. $this->accountAndPassCheck();
  200. $datas = [
  201. 'agent_name' => $this->paras['agent'],
  202. 'domain' => isset($this->paras['domain']) ? trim($this->paras['domain']) : '',
  203. 'userame' => $this->wagetnModel->agent_pre . $this->paras['username'],
  204. 'gametype' => isset($this->paras['gametype']) ? trim($this->paras['gametype']) : '',
  205. 'gamekind' => isset($this->paras['gamekind']) ? trim($this->paras['gamekind']) : '',
  206. 'iframe' => isset($this->paras['iframe']) ? intval(trim($this->paras['iframe'])) : -1,
  207. 'platformname' => isset($this->paras['platformname']) ? trim($this->paras['platformname']) : '',
  208. 'lang' => isset($this->paras['lang']) ? trim($this->paras['lang']) : '',
  209. 'ip' => GETIP(),
  210. ];
  211. $model = new Wagent_clientgame_logModel();
  212. $ret = $model->insert($datas);
  213. if ($ret) {
  214. $username = $this->wagetnModel->agent_pre . $this->paras['username'];
  215. $ac = new AccountManager();
  216. $ret2 = $ac->login($username, $this->paras['password']);
  217. if (isset($ret2['status']) && $ret2['status'] == 1 && isset($ret2['data']['0']['token'])) {
  218. $t_d = $ret2['data']['0'];
  219. $cmodel = new WagentCheckTokenModel();
  220. $retchktoken = $cmodel->AddCheckToken($username);
  221. $token = $retchktoken->token;
  222. //$token = urlencode(json_encode(['img_url' => $t_d['img_url'], 'name' => $t_d['name'], 'token' => $t_d['token'], 'cash' => $t_d['cash']], 256));
  223. $token = urlencode(json_encode(['name' => $username, 'token' => $token, 'cash' => $t_d['cash']], 256));
  224. return $this->makeret(1, $ret2['msg'], ['ret' => 0, 'tokenold' => '', 'token' => $token, 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/m/jump.html?token=' . $token]);
  225. } else {
  226. return $this->makeret(0, 'false', 0);
  227. }
  228. } else {
  229. return $this->makeret(0, 'false', 0);
  230. }
  231. }
  232. //GetReport(获取报表数据)
  233. public function GetReport()
  234. {
  235. $this->accountAndPassCheck();
  236. $datetart = isset($this->paras['datestart']) ? trim($this->paras['datestart']) : '';
  237. $dateends = isset($this->paras['dateend']) ? trim($this->paras['dateend']) : '';
  238. if (empty($datetart) || empty($dateends)) {
  239. return $this->makeret(0, 'begin or endtime is empty', 0);
  240. }
  241. $retstr = '/^(\d{4}-\d{2}-\d{2})$/';
  242. if (!preg_match($retstr, $datetart) || !preg_match($retstr, $dateends)) {
  243. return $this->makeret(0, 'date fromat error', 0);
  244. }
  245. $username = $this->wagetnModel->agent_pre . $this->paras['username'];
  246. $ret = $this->getMoneyBuyData($username, $datetart, $dateends);
  247. $retlast['sports']['UserName'] = $this->paras['username'];
  248. $retlast['sports']['BettingTime'] = intval($ret['counts']);
  249. $retlast['sports']['BettingAmount'] = intval($ret['money']);
  250. $retlast['sports']['BettingEffectiveAmount'] = intval($ret['prize_money']);
  251. $retlast['sports']['BettingLoseWin'] = intval($ret['money'] - $ret['prize_money']);
  252. return $this->makeret(1, 'success', $retlast);
  253. }
  254. //获取某人体育游戏的统计数据
  255. public function getMoneyBuyData($account, $databegin, $dataend)
  256. {
  257. $db = $GLOBALS['DB'];
  258. $ret1 = $db->select("select count(id) as counts,sum(money) as money, sum(prize_money) as money_buy from money_buy_simplex where account_name=:account and status in (1,2) and money_time>=:timebegin and money_time<=:timeend group by account_name ;", ['account' => $account, 'timebegin' => $databegin, 'timeend' => $dataend]);
  259. $ret2 = $db->select("select count(id) as counts,sum(money) as money, sum(prize_money) as money_buy from money_buy_str where account_name=:account and status in (1,2) and money_time>=:timebegin and money_time<=:timeend group by account_name ;", ['account' => $account, 'timebegin' => $databegin, 'timeend' => $dataend]);
  260. $ret = ['counts' => 0, 'money' => 0, 'prize_money' => 0];
  261. if ($ret1) {
  262. $ret['counts'] += $ret1['0']['count'];
  263. $ret['money'] += $ret1['0']['money'];
  264. $ret['prize_money'] += $ret1['0']['prize_money'];
  265. }
  266. if ($ret2) {
  267. $ret['counts'] += $ret2['0']['count'];
  268. $ret['money'] += $ret2['0']['money'];
  269. $ret['prize_money'] += $ret2['0']['prize_money'];
  270. }
  271. return $ret;
  272. }
  273. // GetCashTrade(转帐记录)
  274. public function GetCashTrade()
  275. {
  276. $billno = isset($this->paras['billno']) ? $this->paras['billno'] : '';
  277. if (empty($billno)) {
  278. $ret = $this->makeret(0, 'billno empty', 0);
  279. return $ret;
  280. }
  281. $moneyTransferModel = lm('Money_transfer', 'Commons')->where(['agent_name' => $this->wagetnModel->agent_name, 'billno' => $billno])->first();
  282. if (empty($moneyTransferModel)) {
  283. $ret = $this->makeret(0, 'no data', 0);
  284. return $ret;
  285. }
  286. $AccountArray = lm('Account', 'Commons')->getinfo($moneyTransferModel->account_indent, 2);
  287. if (empty($AccountArray) || !is_array($AccountArray)) {
  288. $ret = $this->makeret(0, 'account error', 0);
  289. return $ret;
  290. }
  291. $datas = [
  292. 'Billno' => $moneyTransferModel->billno,
  293. 'UserName' => substr($AccountArray['account'], strlen($this->wagetnModel->agent_pre)),
  294. 'OrderNumber' => $moneyTransferModel->billno,
  295. 'TradeAmount' => $moneyTransferModel->tradeamount,
  296. 'Balance' => $moneyTransferModel->blance,
  297. 'TradeType' => trim($moneyTransferModel->type),
  298. 'From' => $moneyTransferModel->tfrom,
  299. 'To' => $moneyTransferModel->tto,
  300. 'AddTime' => $moneyTransferModel->addtime,
  301. ];
  302. $ret = $this->makeret(1, 'success', $datas);
  303. return $ret;
  304. }
  305. //GetBettingRecordByVendor(真人游戏记录)
  306. public function GetBettingRecordByVendor()
  307. {
  308. $ret = $this->makeret();
  309. return $ret;
  310. }
  311. //GetSportsBettingRecordByVendor(体育投注记录)
  312. public function GetSportsBettingRecordByVendor()
  313. {
  314. $vendorid = isset($this->paras['vendorid']) ? trim($this->paras['vendorid']) : 0;
  315. $isjs = isset($this->paras['isjs']) ? intval($this->paras['isjs']) : 0;
  316. if (empty($vendorid)) {
  317. $ret = $this->makeret(0, 'paretn error', 0);
  318. return $ret;
  319. }
  320. $logic = new SportbetRecordLogic($this->wagetnModel, $vendorid, $isjs);
  321. $ret = $logic->dorun();
  322. if (!$ret) {
  323. return $this->makeret(0, 'false', 0);
  324. } else {
  325. return $this->makeret(1, 'success', $ret);
  326. }
  327. return $ret;
  328. }
  329. //GetEleBettingRecord(电子投注记录)
  330. public function GetEleBettingRecord()
  331. {
  332. $ret = $this->makeret();
  333. return $ret;
  334. }
  335. //GetFishBettingRecord(捕鱼王投注记录)
  336. public function GetFishBettingRecord()
  337. {
  338. $ret = $this->makeret();
  339. return $ret;
  340. }
  341. //GetLotteryBettingRecord(彩票投注记录)
  342. public function GetLotteryBettingRecord()
  343. {
  344. $ret = $this->makeret();
  345. return $ret;
  346. }
  347. //GetGameResult(游戏结果记录)
  348. public function GetGameResult()
  349. {
  350. $ret = $this->makeret();
  351. return $ret;
  352. }
  353. //UpdateAccount(帐户密码更改)
  354. public function UpdateAccount()
  355. {
  356. $username = isset($this->paras['username']) ? trim($this->paras['username']) : '';
  357. $password = isset($this->paras['password']) ? trim($this->paras['password']) : '';
  358. if (empty($username) || empty($password)) {
  359. return $this->makeret(0, '请求参数缺失', 'key_error');
  360. }
  361. $username = $this->wagetnModel->agent_pre . $username;
  362. $ac = new AccountManager();
  363. $userInfo = $ac->getAccount($username);
  364. if (!$userInfo) {
  365. return $this->makeret(0, 'false', 'key_error');
  366. }
  367. if ($userInfo->status != 1) {
  368. return $this->makeret(0, '用户状态不正确', 'key_error');
  369. }
  370. $pwdData = GenPassword($password);
  371. $passdatas = [
  372. 'account_password' => $pwdData['password'],
  373. "encryption" => $pwdData['encryption'],
  374. ];
  375. $ret = lm('account_password', 'Commons')->where('account_identity', $userInfo->identity)->update($passdatas);
  376. if ($ret) {
  377. $ret = $this->makeret(1, 'success', 1);
  378. } else {
  379. $ret = $this->makeret(0, 'false', 0);
  380. }
  381. return $ret;
  382. }
  383. //GetSportVendorId(获取体育VendorId)
  384. public function GetSportVendorId()
  385. {
  386. $ret = $this->makeret();
  387. return $ret;
  388. }
  389. //代理帮用户请求一次登陆操作,如果成功有用户有效token值
  390. public function AutoLogin()
  391. {
  392. $this->accountAndPassCheck();
  393. $username = $this->wagetnModel->agent_pre . $this->paras['username'];
  394. $ac = new AccountManager();
  395. $ret2 = $ac->login($username, $this->paras['password']);
  396. if (isset($ret2['status']) && $ret2['status'] == 1 && isset($ret2['data']['0']['token'])) {
  397. $t_d = $ret2['data']['0'];
  398. $token = urlencode(json_encode(['img_url' => $t_d['img_url'], 'name' => $t_d['name'], 'token' => $t_d['token'], 'cash' => $t_d['cash']], 256));
  399. return $this->makeret(1, $ret2['msg'], ['ret' => 0, 'token' => $token, 'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/m/jump.html?token=' . $token]);
  400. } else {
  401. return $this->makeret(0, 'false', 0);
  402. }
  403. }
  404. //得到本代理用户排行榜 {1投注排行 4赚钱排行 .... }
  405. public function ConsumptionRanking()
  406. {
  407. $wagetnname = $this->wagetnModel->agent_name;
  408. $type = isset($this->paras['type']) ? intval($this->paras['type']) : 4;
  409. $prlen = strlen($this->wagetnModel->agent_pre);
  410. $dtime = date("Y-m-d") . " 00:00:00";
  411. $cacheObj = C()->get('cache');
  412. $key = md5($wagetnname . '-' . $dtime . '-' . $type);
  413. $cache = $cacheObj->get($key);
  414. if ($cache && isset($cache['data'])) {
  415. return $this->makeret(1, 'success', $cache['data']);
  416. }
  417. $SQL = <<<LONGSQL
  418. select "user".*,"money".money from
  419. (
  420. select "identity",account from account where "identity" in ( select account_identity from account_detailed where wagent_name=:wname)
  421. ) as "user"
  422. inner join
  423. (
  424. select account_identity,sum(money) as money from money_details where account_identity in
  425. ( select account_identity from account_detailed where wagent_name=:wname)
  426. and money_time>=:dtime
  427. and trade_type=:ttype
  428. group by account_identity
  429. order by money desc limit 20
  430. ) as money
  431. on "user"."identity"="money".account_identity
  432. where money>0
  433. order by money desc
  434. LONGSQL;
  435. $ret = S("DB")->select($SQL, [':wname' => $wagetnname, ':ttype' => $type, ':dtime' => $dtime]);
  436. if (!$ret) {
  437. $return = [];
  438. } else {
  439. foreach ($ret as $item) {
  440. $return[] = [
  441. 'name' => substr($item->account, $prlen),
  442. 'money' => $item->money,
  443. ];
  444. }
  445. }
  446. $cachedata = ['datetime' => $dtime, 'data' => $return];
  447. $cacheObj->set($key, $cachedata, 1800);
  448. return $this->makeret(1, 'success', $return);
  449. }
  450. }