/app/Common/Account.php

https://github.com/fan3750060/wpcore · PHP · 387 lines · 206 code · 44 blank · 137 comment · 27 complexity · 5f38329a564daedf4f224305f2d0aad9 MD5 · raw file

  1. <?php
  2. namespace app\Common;
  3. use core\query\DB;
  4. /**
  5. * 账户管理
  6. */
  7. class Account
  8. {
  9. public $country = [
  10. 'enUS' => 0,
  11. 'koKR' => 1,
  12. 'frFR' => 2,
  13. 'deDE' => 3,
  14. 'zhCN' => 4,
  15. 'zhTW' => 5,
  16. 'esES' => 6,
  17. 'esMX' => 7,
  18. 'ruRU' => 8,
  19. ];
  20. /**
  21. * [get_account #如果帐户存在,则从帐户返回帐户,或者返回None]
  22. * ------------------------------------------------------------------------------
  23. * @author by.fan <fan3750060@163.com>
  24. * ------------------------------------------------------------------------------
  25. * @version date:2019-04-28
  26. * ------------------------------------------------------------------------------
  27. * @return [type] [description]
  28. */
  29. public function get_account($account_name = '')
  30. {
  31. $where = [];
  32. $where['username'] = $account_name;
  33. return DB::table('account')->where($where)->find();
  34. }
  35. /**
  36. * [account_banned 账户是否禁止]
  37. * ------------------------------------------------------------------------------
  38. * @author by.fan <fan3750060@163.com>
  39. * ------------------------------------------------------------------------------
  40. * @version date:2019-05-06
  41. * ------------------------------------------------------------------------------
  42. * @param [type] $account_id [description]
  43. * @return [type] [description]
  44. */
  45. public function account_banned($account_id = null)
  46. {
  47. $where = [];
  48. $where['id'] = $account_id;
  49. return DB::table('account_banned')->where($where)->find();
  50. }
  51. /**
  52. * [ip_banned IP是否禁止]
  53. * ------------------------------------------------------------------------------
  54. * @author by.fan <fan3750060@163.com>
  55. * ------------------------------------------------------------------------------
  56. * @version date:2019-05-06
  57. * ------------------------------------------------------------------------------
  58. * @param [type] $account_id [description]
  59. * @return [type] [description]
  60. */
  61. public function ip_banned($account_id = null)
  62. {
  63. $where = [];
  64. $where['ip'] = $account_id;
  65. $info = DB::table('ip_banned')->where($where)->find();
  66. if ($info) {
  67. //解封日期已过
  68. return $info['unbandate'] <= time() ? false : true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * [Offline 下线]
  75. * ------------------------------------------------------------------------------
  76. * @author by.fan <fan3750060@163.com>
  77. * ------------------------------------------------------------------------------
  78. * @version date:2019-07-17
  79. * ------------------------------------------------------------------------------
  80. */
  81. public function Offline($username)
  82. {
  83. $where = [];
  84. $where['username'] = $username;
  85. return DB::table('account')->where($where)->update(['online' => 0]);
  86. }
  87. /**
  88. * [get_realmlist 获取世界服务器信息]
  89. * ------------------------------------------------------------------------------
  90. * @author by.fan <fan3750060@163.com>
  91. * ------------------------------------------------------------------------------
  92. * @version date:2019-06-29
  93. * ------------------------------------------------------------------------------
  94. * @return [type] [description]
  95. */
  96. public function get_realmlist()
  97. {
  98. return DB::table('realmlist')->select();
  99. }
  100. /**
  101. * [get_realmlistuserinfo 获取服务器角色数量]
  102. * ------------------------------------------------------------------------------
  103. * @author by.fan <fan3750060@163.com>
  104. * ------------------------------------------------------------------------------
  105. * @version date:2019-07-13
  106. * ------------------------------------------------------------------------------
  107. * @param [type] $param [description]
  108. * @return [type] [description]
  109. */
  110. public function get_realmlistuserinfo($param)
  111. {
  112. $where = [
  113. 'account' => $param['accountId'],
  114. 'isdel' => 1
  115. ];
  116. return DB::table('characters','characters')->where($where)->count();
  117. }
  118. /**
  119. * [updateinfo 更新用户信息]
  120. * ------------------------------------------------------------------------------
  121. * @author by.fan <fan3750060@163.com>
  122. * ------------------------------------------------------------------------------
  123. * @version date:2019-07-03
  124. * ------------------------------------------------------------------------------
  125. * @param [type] $param [description]
  126. * @return [type] [description]
  127. */
  128. public function updateinfo($param)
  129. {
  130. $data = [];
  131. $data['last_login'] = date('Y-m-d H:i:s');
  132. $data['online'] = 1;
  133. if (!empty($param['ip'])) {
  134. $data['last_ip'] = $param['ip'];
  135. $data['last_attempt_ip'] = $param['ip'];
  136. }
  137. if (!empty($param['os'])) {
  138. $data['os'] = $param['os'];
  139. }
  140. if (!empty($param['country'])) {
  141. $data['locale'] = $this->country[$param['country']];
  142. }
  143. if (!empty($param['sessionkey'])) {
  144. $data['sessionkey'] = $param['sessionkey'];
  145. }
  146. if(!empty($param['v']))
  147. {
  148. $data['v'] = $param['v'];
  149. }
  150. if(!empty($param['s']))
  151. {
  152. $data['s'] = $param['s'];
  153. }
  154. if(!empty($param['token_key']))
  155. {
  156. $data['token_key'] = $param['token_key'];
  157. }
  158. $where = [];
  159. $where['username'] = $param['username'];
  160. DB::table('account')->where($where)->update($data);
  161. }
  162. /**
  163. * [createuser 创建用户]
  164. * ------------------------------------------------------------------------------
  165. * @author by.fan <fan3750060@163.com>
  166. * ------------------------------------------------------------------------------
  167. * @version date:2019-07-04
  168. * ------------------------------------------------------------------------------
  169. * @param array $param [description]
  170. * @return [type] [description]
  171. */
  172. public function createuser($param = [])
  173. {
  174. $param['username'] = strtoupper($param['username']);
  175. $param['password'] = strtoupper($param['password']);
  176. $where = [
  177. 'username' => $param['username'],
  178. ];
  179. if (DB::table('account')->where($where)->find() == false) {
  180. $data = [
  181. 'username' => $param['username'],
  182. 'sha_pass_hash' => strtoupper(sha1($param['username'] . ':' . $param['password'])),
  183. 'joindate' => date('Y-m-d H:i:s'),
  184. 'expansion' => env('EXPANSION',1),
  185. ];
  186. if ($id = DB::table('account')->insert($data)) {
  187. $access_data = [
  188. 'id' => $id,
  189. 'gmlevel' => 0,
  190. ];
  191. DB::table('account_access')->insert($access_data);
  192. $this->commandsuccess('Account created successfully: ' . $param['username']);
  193. } else {
  194. $this->commanderror('Account creation failed: ' . $param['username']);
  195. }
  196. } else {
  197. $this->commanderror('Account already exists: ' . $param['username']);
  198. }
  199. }
  200. /**
  201. * [updategmlevel 更新gm权限]
  202. * ------------------------------------------------------------------------------
  203. * @author by.fan <fan3750060@163.com>
  204. * ------------------------------------------------------------------------------
  205. * @version date:2019-07-04
  206. * ------------------------------------------------------------------------------
  207. * @param array $param [description]
  208. * @return [type] [description]
  209. */
  210. public function updategmlevel($param = [])
  211. {
  212. $param['username'] = strtoupper($param['username']);
  213. $where = [
  214. 'username' => $param['username'],
  215. ];
  216. if ($info = DB::table('account')->where($where)->find()) {
  217. $where = [
  218. 'id' => $info['id'],
  219. ];
  220. $udata = [
  221. 'gmlevel' => $param['gmlevel'],
  222. 'RealmID' => $param['RealmID'],
  223. ];
  224. $info = DB::table('account_access')->where($where)->update($udata);
  225. if ($info !== false) {
  226. $this->commandsuccess('Account permission changed successfully: ' . $param['username']);
  227. } else {
  228. $this->commanderror('Account permission change failed: ' . $param['username']);
  229. }
  230. } else {
  231. $this->commanderror('Account does not exist: ' . $param['username']);
  232. }
  233. }
  234. #################### command ##########################
  235. /**
  236. * [command 处理命令行命令]
  237. * ------------------------------------------------------------------------------
  238. * @author by.fan <fan3750060@163.com>
  239. * ------------------------------------------------------------------------------
  240. * @version date:2019-07-04
  241. * ------------------------------------------------------------------------------
  242. * @return [type] [description]
  243. */
  244. public function command($param)
  245. {
  246. if (empty($param[1])) {
  247. $this->commandparamerror(implode(' ', $param));
  248. return;
  249. }
  250. switch (strtolower($param[1])) {
  251. case 'create':
  252. if (empty($param[2]) || empty($param[3])) {
  253. $this->commandparamerror(implode(' ', $param));
  254. return;
  255. }
  256. $data = [
  257. 'username' => trim($param[2]),
  258. 'password' => trim($param[3]),
  259. ];
  260. $this->createuser($data);
  261. break;
  262. case 'set':
  263. if (empty($param[2])) {
  264. $this->commandparamerror(implode(' ', $param));
  265. return;
  266. }
  267. switch (strtolower($param[2])) {
  268. case 'gmlevel':
  269. if (empty($param[3]) || empty($param[4]) || empty($param[5])) {
  270. $this->commandparamerror(implode(' ', $param));
  271. return;
  272. }
  273. $data = [
  274. 'username' => trim($param[3]),
  275. 'gmlevel' => (int) $param[4],
  276. 'RealmID' => (int) $param[5],
  277. ];
  278. $this->updategmlevel($data);
  279. break;
  280. default:
  281. $this->commandcmderror(implode(' ', $param));
  282. break;
  283. }
  284. break;
  285. default:
  286. $this->commandcmderror(implode(' ', $param));
  287. break;
  288. }
  289. }
  290. /**
  291. * [commandsuccess 成功]
  292. * ------------------------------------------------------------------------------
  293. * @author by.fan <fan3750060@163.com>
  294. * ------------------------------------------------------------------------------
  295. * @version date:2019-07-04
  296. * ------------------------------------------------------------------------------
  297. * @return [type] [description]
  298. */
  299. public function commandsuccess($str = null)
  300. {
  301. AUTH_LOG($str, 'success');
  302. }
  303. /**
  304. * [commanderror 错误]
  305. * ------------------------------------------------------------------------------
  306. * @author by.fan <fan3750060@163.com>
  307. * ------------------------------------------------------------------------------
  308. * @version date:2019-07-04
  309. * ------------------------------------------------------------------------------
  310. * @param [type] $str [description]
  311. * @return [type] [description]
  312. */
  313. public function commanderror($str = null)
  314. {
  315. AUTH_LOG($str, 'warning');
  316. }
  317. /**
  318. * [commandcmderror 没有这样的命令]
  319. * ------------------------------------------------------------------------------
  320. * @author by.fan <fan3750060@163.com>
  321. * ------------------------------------------------------------------------------
  322. * @version date:2019-07-04
  323. * ------------------------------------------------------------------------------
  324. * @return [type] [description]
  325. */
  326. public function commandcmderror($str = null)
  327. {
  328. AUTH_LOG($str . ' No such comand', 'error');
  329. }
  330. /**
  331. * [commandparamerror 参数错误]
  332. * ------------------------------------------------------------------------------
  333. * @author by.fan <fan3750060@163.com>
  334. * ------------------------------------------------------------------------------
  335. * @version date:2019-07-04
  336. * ------------------------------------------------------------------------------
  337. * @return [type] [description]
  338. */
  339. public function commandparamerror($str = null)
  340. {
  341. AUTH_LOG($str . ' Parameter error', 'error');
  342. }
  343. }