GameLogic.php 19 KB

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