Admin.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. <?php
  2. namespace app\controller;
  3. use app\AdminBaseController;
  4. use think\facade\Db;
  5. use phpu\facade\ThinkCaptcha;
  6. use Ramsey\Uuid\Uuid;
  7. //后台控制器
  8. class Admin extends AdminBaseController
  9. {
  10. protected $noNeedLogin = ['login', 'logout', 'getAdminCaptchaImage'];
  11. //获取管理员登录验证码图片
  12. public function getAdminCaptchaImage()
  13. {
  14. return ThinkCaptcha::printImg('admin');
  15. }
  16. //登录
  17. public function login()
  18. {
  19. $username = $this->request->post('username');
  20. $password = $this->request->post('password');
  21. //$captcha = $this->request->post('captcha');
  22. if (empty($username) || empty($password)) {
  23. $this->fail(500, '参数校验错误');
  24. }
  25. //if (!ThinkCaptcha::check($captcha, 'admin', 2)) {
  26. // $this->fail(501, '验证码错误');
  27. //}
  28. $admin = Db::table('tb_admin')
  29. ->where(['username' => $username])
  30. ->where(['password' => md5($password)])
  31. ->find();
  32. if (!$admin) {
  33. $this->fail(502, '管理员用户名或密码错误');
  34. }
  35. //写登录日志
  36. Db::table('tb_admin_login_log')
  37. ->insert([
  38. 'user_id' => $admin['id'],
  39. 'ip' => $this->request->ip(),
  40. 'user_agent' => $this->request->header('user-agent'),
  41. 'created_at' => time(),
  42. ]);
  43. //写登录Token
  44. $token = Uuid::uuid4()->toString();
  45. Db::table('tb_admin_token')
  46. ->insert([
  47. 'user_id' => $admin['id'],
  48. 'token' => $token,
  49. 'expired_at' => time() + 24 * 60 * 60,
  50. ]);
  51. $this->success('success', [
  52. 'token' => $token,
  53. ]);
  54. }
  55. //退出登录
  56. public function logout()
  57. {
  58. $token = $this->request->post('token');
  59. if (empty($token)) {
  60. $this->fail(500, 'token参数校验错误');
  61. }
  62. Db::table('tb_admin_token')
  63. ->where(['token' => $token])
  64. ->delete();
  65. $this->success('success', null);
  66. }
  67. //修改密码,修改后前端需要跳到登录页重新登录
  68. public function changePwd()
  69. {
  70. $oldPassword = $this->request->post('oldPassword');
  71. $newPassword = $this->request->post('newPassword');
  72. if (empty($oldPassword) || empty($newPassword)) {
  73. $this->fail(500, '参数校验错误');
  74. }
  75. $user = $this->getUser();
  76. $data = Db::table('tb_user')
  77. ->where(['id' => $user->user_id])
  78. ->find();
  79. if ($data['password'] != md5($oldPassword)) {
  80. $this->fail(501, '当前密码错误');
  81. }
  82. Db::table('tb_user')
  83. ->where(['id' => $user->user_id])
  84. ->update([
  85. 'password' => md5($newPassword),
  86. ]);
  87. //释放token
  88. Db::table('tb_user_token')
  89. ->where(['token' => $user->token])
  90. ->delete();
  91. $this->success('success', null);
  92. }
  93. //获取控制台数据
  94. public function getConsoleData()
  95. {
  96. $userCount = Db::table('tb_user')->count();
  97. $todayUserCount = Db::table('tb_user')
  98. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  99. ->count();
  100. $totalWebsitePay = Db::table('tb_user_order')
  101. ->where(['state' => 1])
  102. ->sum('balance');
  103. $todayWebsitePay = Db::table('tb_user_order')
  104. ->where(['state' => 1])
  105. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  106. ->sum('balance');
  107. $totalTaobaoPay = Db::table('tb_user_game')
  108. ->where(['channel' => 2])
  109. ->sum('price');
  110. $todayTaobaoPay = Db::table('tb_user_game')
  111. ->where(['channel' => 2])
  112. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  113. ->sum('price');
  114. $totalPddPay = Db::table('tb_user_game')
  115. ->where(['channel' => 3])
  116. ->sum('price');
  117. $todayPddPay = Db::table('tb_user_game')
  118. ->where(['channel' => 3])
  119. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  120. ->sum('price');
  121. $this->success('success', [
  122. //用户数据
  123. 'userData' => [
  124. 'totalUserCount' => $userCount, //总会员数
  125. 'todayUserCount' => $todayUserCount, //今日新增会员数
  126. ],
  127. //网站消费数据
  128. 'websitePayData' => [
  129. 'totalPay' => number_format(floatval($totalWebsitePay), 2), //网站总直充
  130. 'todayPay' => number_format(floatval($todayWebsitePay), 2), //今日网站直充
  131. ],
  132. //淘宝平台消费数据
  133. 'taobaoPayData' => [
  134. 'totalPay' => number_format(floatval($totalTaobaoPay), 2), //淘宝平台总消费
  135. 'todayPay' => number_format(floatval($todayTaobaoPay), 2), //淘宝平台今日消费
  136. ],
  137. //拼多多平台消费数据
  138. 'pddPayData' => [
  139. 'totalPay' => number_format(floatval($totalPddPay), 2), //拼多多平台总消费
  140. 'todayPay' => number_format(floatval($todayPddPay), 2), //拼多多平台今日消费
  141. ],
  142. ]);
  143. }
  144. //获取用户数据
  145. public function getUserList()
  146. {
  147. // 获取分页参数,确保有默认值
  148. $pageSize = $this->request->post('pageSize', 10); // 每页显示的数据数量
  149. $pageNum = $this->request->post('pageNum', 1); // 当前页码
  150. // 参数校验
  151. if (!is_numeric($pageSize) || !is_numeric($pageNum)) {
  152. $this->fail(500, '分页参数校验错误');
  153. }
  154. // 转换为整数
  155. $pageSize = intval($pageSize);
  156. $pageNum = intval($pageNum);
  157. // 确保最小值
  158. $pageSize = max(1, $pageSize);
  159. $pageNum = max(1, $pageNum);
  160. // 获取搜索参数
  161. $keyword = $this->request->post('keyword', '');
  162. $startDate = $this->request->post('startDate', '');
  163. $endDate = $this->request->post('endDate', '');
  164. // 构建查询
  165. $query = Db::table('tb_user')
  166. ->field('id,username,created_at,last_login,login_count,last_login_ip,avatar,balance,email,state');
  167. // 添加搜索条件
  168. if (!empty($keyword)) {
  169. $query->where(function($q) use ($keyword) {
  170. $q->where('username', 'like', "%{$keyword}%")
  171. ->whereOr('id', '=', $keyword)
  172. ->whereOr('email', 'like', "%{$keyword}%");
  173. });
  174. }
  175. // 添加日期范围条件
  176. if (!empty($startDate)) {
  177. $query->where('created_at', '>=', $startDate . ' 00:00:00');
  178. }
  179. if (!empty($endDate)) {
  180. $query->where('created_at', '<=', $endDate . ' 23:59:59');
  181. }
  182. // 排序
  183. $query->order('created_at', 'DESC');
  184. // 分页查询
  185. $paginator = $query->paginate([
  186. 'list_rows' => $pageSize,
  187. 'page' => $pageNum,
  188. 'path' => '' // 保持URL简洁
  189. ]);
  190. // 处理头像数据
  191. $users = $paginator->getCollection()->each(function($user) {
  192. if (str_starts_with($user['avatar'], 'system://')) {
  193. $data = explode('//', $user['avatar']);
  194. $avatarId = intval($data[1]);
  195. $avatar = Db::table('tb_system_avatar')
  196. ->where(['id' => $avatarId])
  197. ->find();
  198. $user['avatar'] = $avatar['image_url'] ?? '';
  199. }
  200. return $user;
  201. });
  202. // 返回分页数据
  203. $this->success('success', [
  204. 'data' => $users,
  205. 'total' => $paginator->total(),
  206. 'current_page' => $paginator->currentPage(),
  207. 'per_page' => $paginator->listRows(),
  208. 'last_page' => $paginator->lastPage(),
  209. ]);
  210. }
  211. //用户封号或解封
  212. public function banUser()
  213. {
  214. $userId = $this->request->post('userId'); //用户ID
  215. $state = $this->request->post('state'); //状态 1:设置为封号 0:设置为解封
  216. if (!is_numeric($userId) || !is_numeric($state)) {
  217. $this->fail(500, '参数校验错误');
  218. }
  219. $user = Db::table('tb_user')
  220. ->where(['id' => $userId])
  221. ->find();
  222. if (!$user) {
  223. $this->fail(501, 'userId不存在');
  224. }
  225. if (intval($state) == 0) {
  226. $state = 1;
  227. } else {
  228. $state = 2;
  229. }
  230. Db::table('tb_user')
  231. ->where(['id' => $userId])
  232. ->update(['state' => $state]);
  233. $this->success('success', null);
  234. }
  235. //新增游戏分类
  236. public function addGameCategory()
  237. {
  238. $name = $this->request->post('name');
  239. $imageUrl = $this->request->post('imageUrl');
  240. $description = $this->request->post('description');
  241. $priority = $this->request->post('priority');
  242. if (empty($name) || empty($imageUrl) || empty($description) || !is_numeric($priority)) {
  243. $this->fail(500, '参数校验错误');
  244. }
  245. Db::table('tb_game_category')
  246. ->insert([
  247. 'name' => $name,
  248. 'image_url' => $imageUrl,
  249. 'description' => $description,
  250. 'priority' => $priority,
  251. 'created_at' => time(),
  252. 'updated_at' => time(),
  253. ]);
  254. $this->success('success', null);
  255. }
  256. //获取游戏分类,无需分类
  257. public function getGameCategory()
  258. {
  259. $data = Db::table('tb_game_category')
  260. ->order('priority DESC')
  261. ->select();
  262. $this->success('success', $data);
  263. }
  264. //编辑游戏分类
  265. public function editGameCategory()
  266. {
  267. $categoryId = $this->request->post('categoryId');
  268. $name = $this->request->post('name');
  269. $imageUrl = $this->request->post('imageUrl');
  270. $description = $this->request->post('description');
  271. $priority = $this->request->post('priority');
  272. if (empty($name) || empty($imageUrl) || empty($description) || !is_numeric($priority) || !is_numeric($categoryId)) {
  273. $this->fail(500, '参数校验错误');
  274. }
  275. Db::table('tb_game_category')
  276. ->where(['id' => $categoryId])
  277. ->update([
  278. 'name' => $name,
  279. 'image_url' => $imageUrl,
  280. 'description' => $description,
  281. 'priority' => $priority,
  282. 'updated_at' => time(),
  283. ]);
  284. $this->success('success', null);
  285. }
  286. //删除游戏分类
  287. public function removeGameCategory()
  288. {
  289. $categoryId = $this->request->post('categoryId');
  290. if (!is_numeric($categoryId)) {
  291. $this->fail(500, 'categoryId参数校验错误');
  292. }
  293. $data = Db::table('tb_game_category')
  294. ->where(['id' => $categoryId])
  295. ->find();
  296. if (!$data) {
  297. $this->fail(501, 'categoryId不存在');
  298. }
  299. Db::table('tb_game_category')
  300. ->where(['id' => $categoryId])
  301. ->delete();
  302. $this->success('success', null);
  303. }
  304. public function getGameList()
  305. {
  306. $categoryId = $this->request->post('categoryId'); // 游戏分类ID
  307. $pageNum = $this->request->post('pageNum'); // 当前页码
  308. $pageSize = $this->request->post('pageSize'); // 每页数量
  309. if (!is_numeric($categoryId) || !is_numeric($pageNum) || !is_numeric($pageSize)) {
  310. $this->fail(500, '参数校验错误');
  311. }
  312. $query = Db::table('tb_game')->where(['category' => $categoryId]);
  313. // 获取总数
  314. $total = $query->count();
  315. // 获取分页数据
  316. $list = $query->page(intval($pageNum), intval($pageSize))->select();
  317. $this->success('success', [
  318. 'list' => $list,
  319. 'total' => $total,
  320. 'pageNum' => intval($pageNum),
  321. 'pageSize' => intval($pageSize)
  322. ]);
  323. }
  324. //新增游戏
  325. public function addGame()
  326. {
  327. $categoryId = $this->request->post('categoryId'); //游戏分类ID
  328. $title = $this->request->post('title'); //标题
  329. $imageUrl = $this->request->post('imageUrl'); //图片URL
  330. $price = $this->request->post('price'); //价格
  331. $description = $this->request->post('description'); //描述
  332. $priority = $this->request->post('priority'); //优先级,越小越靠前
  333. $browse = $this->request->post('browse'); //浏览次数,营销需要,添加的时候可以自定义
  334. $buyCount = $this->request->post('buyCount'); //购买次数,营销需要,添加的时候可以自定义
  335. $downloadUrl = $this->request->post('downloadUrl'); //下载链接
  336. $extractedCode = $this->request->post('extractedCode'); //提取码
  337. $gameVersion = $this->request->post('gameVersion'); //游戏版本
  338. $language = $this->request->post('language'); //游戏语言
  339. if (!is_numeric($categoryId) || empty($title) || empty($imageUrl) || !is_float($price) || empty($description) || !is_numeric($priority) || !is_numeric($browse) || !is_numeric($buyCount) || empty($downloadUrl) || empty($extractedCode) || empty($gameVersion) || empty($language)) {
  340. $this->fail(500, '参数校验错误');
  341. }
  342. //判断游戏分类ID是否存在
  343. $category = Db::table('tb_game_category')
  344. ->where(['id' => $categoryId])
  345. ->find();
  346. if (!$category) {
  347. $this->fail(501, '游戏分类ID不存在');
  348. }
  349. Db::table('tb_game')
  350. ->insert([
  351. 'category' => $categoryId,
  352. 'title' => $title,
  353. 'image_url' => $imageUrl,
  354. 'price' => $price,
  355. 'description' => $description,
  356. 'priority' => $priority,
  357. 'browse' => $browse,
  358. 'buy_count' => $buyCount,
  359. 'game_version' => $gameVersion,
  360. 'language' => $language,
  361. 'created_at' => time(),
  362. 'updated_at' => time(),
  363. 'download_url' => $downloadUrl,
  364. 'extracted_code' => $extractedCode,
  365. ]);
  366. $this->success('success', null);
  367. }
  368. //编辑游戏信息
  369. public function editGame()
  370. {
  371. $gameId = $this->request->post('gameId'); //游戏ID
  372. $categoryId = $this->request->post('categoryId'); //游戏分类ID
  373. $title = $this->request->post('title'); //标题
  374. $imageUrl = $this->request->post('imageUrl'); //图片URL
  375. $price = $this->request->post('price'); //价格
  376. $description = $this->request->post('description'); //描述
  377. $priority = $this->request->post('priority'); //优先级,越小越靠前
  378. $browse = $this->request->post('browse'); //浏览次数, 营销需要,修改的时候可以自定义
  379. $buyCount = $this->request->post('buyCount'); //购买次数,营销需要,修改的时候可以自定义
  380. $downloadUrl = $this->request->post('downloadUrl'); //下载链接
  381. $extractedCode = $this->request->post('extractedCode'); //提取码
  382. $language = $this->request->post('language'); //游戏语言
  383. $gameVersion = $this->request->post('gameVersion'); //游戏版本
  384. if (!is_numeric($categoryId) || empty($title) || empty($imageUrl) || !is_float($price) || empty($description) || !is_numeric($priority) || !is_numeric($browse) || !is_numeric($buyCount) || !is_numeric($gameId) || empty($downloadUrl) || empty($extractedCode) || empty($language) || empty($gameVersion)) {
  385. $this->fail(500, '参数校验错误');
  386. }
  387. //判断游戏分类ID是否存在
  388. $category = Db::table('tb_game_category')
  389. ->where(['id' => $categoryId])
  390. ->find();
  391. if (!$category) {
  392. $this->fail(501, '游戏分类ID不存在');
  393. }
  394. $game = Db::table('tb_game')
  395. ->where(['id' => $gameId])
  396. ->find();
  397. if (!$game) {
  398. $this->fail(502, '游戏ID不存在');
  399. }
  400. Db::table('tb_game')
  401. ->where(['id' => $gameId])
  402. ->update([
  403. 'category' => $categoryId,
  404. 'title' => $title,
  405. 'image_url' => $imageUrl,
  406. 'price' => $price,
  407. 'description' => $description,
  408. 'priority' => $priority,
  409. 'browse' => $browse,
  410. 'buy_count' => $buyCount,
  411. 'updated_at' => time(),
  412. 'download_url' => $downloadUrl,
  413. 'extracted_code' => $extractedCode,
  414. 'language' => $language,
  415. 'game_version' => $gameVersion
  416. ]);
  417. $this->success('success', null);
  418. }
  419. //删除游戏
  420. public function removeGame()
  421. {
  422. $gameId = $this->request->post('gameId'); //游戏ID
  423. if (!is_numeric($gameId)) {
  424. $this->fail(500, '参数校验错误');
  425. }
  426. $game = Db::table('tb_game')
  427. ->where(['id' => $gameId])
  428. ->find();
  429. if (!$game) {
  430. $this->fail(502, '游戏ID不存在');
  431. }
  432. Db::table('tb_game')
  433. ->where(['id' => $gameId])
  434. ->delete();
  435. $this->success('success', null);
  436. }
  437. //获取单个游戏信息
  438. public function getGame()
  439. {
  440. $gameId = $this->request->post('gameId'); //游戏ID
  441. $game = Db::table('tb_game')
  442. ->where(['id' => $gameId])
  443. ->find();
  444. if (!$game) {
  445. $this->fail(502, '游戏ID不存在');
  446. }
  447. $this->success('success', $game);
  448. }
  449. //获取所有的CDKEY
  450. public function getCdKeyList()
  451. {
  452. // 获取分页参数
  453. $pageNum = $this->request->post('pageNum', 10);
  454. if (!is_numeric($pageNum)) {
  455. $this->fail(500, 'pageNum参数校验错误');
  456. }
  457. // 获取搜索参数
  458. $user_id = $this->request->post('user_id', '');
  459. $createTimeRange = $this->request->post('createTimeRange', []);
  460. // 构建查询条件
  461. $query = Db::table('tb_cdkey');
  462. // 用户ID搜索
  463. if (!empty($user_id)) {
  464. $query->where('user_id', $user_id);
  465. }
  466. // 创建时间范围搜索(时间戳直接比较)
  467. if (!empty($createTimeRange) && count($createTimeRange) == 2) {
  468. $startTime = $createTimeRange[0];
  469. $endTime = $createTimeRange[1];
  470. $query->whereBetween('created_at', [$startTime, $endTime]);
  471. }
  472. // 执行分页查询
  473. $data = $query->order('created_at DESC')
  474. ->paginate(intval($pageNum));
  475. $this->success('success', $data);
  476. }
  477. //批量创建CDKEY
  478. public function addCdKeyBatch()
  479. {
  480. $num = $this->request->post('num'); //生成CDKEY的数量
  481. $days = $this->request->post('days'); //有效期天数
  482. $gameId = $this->request->post('gameId'); //CDKEY关联的购买游戏ID
  483. $channel = $this->request->post('channel'); //CDKEY销售渠道:1:淘宝 2:拼多多
  484. if (!is_numeric($num) || !is_numeric($days) || !is_numeric($gameId) || !is_numeric($channel)) {
  485. $this->fail(500, '参数校验错误');
  486. }
  487. $game = Db::table('tb_game')
  488. ->where(['id' => $gameId])
  489. ->find();
  490. if (!$game) {
  491. $this->fail(501, '游戏ID不存在');
  492. }
  493. $batchData = [];
  494. for ($i = 0; $i < $num; $i++) {
  495. $prefix = '';
  496. if ($channel == 1) {
  497. $prefix = 'tb';
  498. } elseif ($channel == 2) {
  499. $prefix = 'pdd';
  500. }
  501. $cdKey = $prefix . '-' . Uuid::uuid4()->toString();
  502. $data = [
  503. 'cdkey' => $cdKey,
  504. 'game_id' => $gameId,
  505. 'expired_at' => time() + 60 * 60 * 24 * intval($days),
  506. 'state' => 0, //未使用状态
  507. 'created_at' => time(),
  508. ];
  509. Db::table('tb_cdkey')
  510. ->insert($data);
  511. $batchData[] = $data;
  512. }
  513. $this->success('success', $batchData);
  514. }
  515. //删除CDKEY
  516. public function removeCdKey()
  517. {
  518. $cdKey = $this->request->post('cdKey'); //CDKEY
  519. if (empty($cdKey)) {
  520. $this->fail(500, 'cdKey参数校验错误');
  521. }
  522. $data = Db::table('tb_cdkey')
  523. ->where(['cdkey' => $cdKey])
  524. ->find();
  525. if (!$data) {
  526. $this->fail(501, 'cdKey不存在');
  527. }
  528. Db::table('tb_cdkey')
  529. ->where(['cdkey' => $cdKey])
  530. ->delete();
  531. $this->success('success', null);
  532. }
  533. //获取充值订单列表
  534. public function getOrderList()
  535. {
  536. $pageSize = $this->request->post('pageSize', 10); // 每页数量
  537. $pageNum = $this->request->post('pageNum', 1); // 当前页码
  538. // 参数校验
  539. if (!is_numeric($pageSize) || !is_numeric($pageNum)) {
  540. $this->fail(500, '参数校验错误');
  541. }
  542. // 搜索条件
  543. $startDate = $this->request->post('startDate', '');
  544. $endDate = $this->request->post('endDate', '');
  545. $query = Db::table('tb_user_order')->order('created_at DESC');
  546. // 添加日期范围 - 修复时间查询
  547. if (!empty($startDate)) {
  548. // 将日期转换为时间戳格式进行比较
  549. $startTimestamp = strtotime($startDate);
  550. $query->where('created_at', '>=', $startTimestamp);
  551. }
  552. if (!empty($endDate)) {
  553. // 结束日期需要包含一整天,所以加上86399秒(23:59:59)
  554. $endTimestamp = strtotime($endDate) + 86399;
  555. $query->where('created_at', '<=', $endTimestamp);
  556. }
  557. $data = $query->paginate([
  558. 'list_rows' => $pageSize,
  559. 'page' => $pageNum
  560. ]);
  561. $this->success('success', $data);
  562. }
  563. //上传图片
  564. public function uploadImage()
  565. {
  566. $file = $this->request->file('file');
  567. if (empty($file)) {
  568. $this->fail(500, '上传的图片文件不能为空');
  569. }
  570. // 使用验证器验证上传的文件
  571. validate(['file' => [
  572. // 限制文件大小(单位b),这里限制为2M
  573. 'fileSize' => 200 * 1024 * 1024,
  574. // 限制文件后缀,多个后缀以英文逗号分割
  575. 'fileExt' => 'jpg,jpeg,png',
  576. ]])->check(['file' => $file]);
  577. $saveName = \think\facade\Filesystem::disk('public')->putFile('upload', $file);
  578. $this->success('success', [
  579. 'filePath' => $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName,
  580. ]);
  581. }
  582. public function uploadFile()
  583. {
  584. $file = $this->request->file('file');
  585. if (empty($file)) {
  586. $this->fail(500, '上传的文件不能为空');
  587. }
  588. // 使用验证器验证上传的文件
  589. validate(['file' => [
  590. // 限制文件大小(单位b),这里限制为50M
  591. 'fileSize' => 50 * 1024 * 1024,
  592. // 限制文件后缀,多个后缀以英文逗号分割,支持压缩包、文档和图片
  593. 'fileExt' => 'zip,rar,7z,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,jpg,jpeg,png,gif,bmp',
  594. ]])->check(['file' => $file]);
  595. $saveName = \think\facade\Filesystem::disk('public')->putFile('upload', $file);
  596. $filePath = $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName;
  597. $fileSize = $file->getSize(); // 获取文件大小
  598. $this->success('success', [
  599. 'filePath' => $filePath,
  600. 'fileSize' => $fileSize,
  601. ]);
  602. }
  603. //后台手动给用户上下余额
  604. //amount为正整数给用户加余额,如果为负数则给用户减余额
  605. public function addUserBalance()
  606. {
  607. $userId = $this->request->post('userId');
  608. $amount = $this->request->post('amount');
  609. if (!is_numeric($amount) || !is_numeric($userId)) {
  610. $this->fail(500, '参数校验错误');
  611. }
  612. $user = Db::table('tb_user')
  613. ->where(['id' => $userId])
  614. ->find();
  615. if (!$user) {
  616. $this->fail(501, '找不到userId的用户');
  617. }
  618. Db::table('tb_user')
  619. ->where(['id' => $userId])
  620. ->update([
  621. 'balance' => $amount > 0 ? $user['balance'] + $amount : $user['balance'] + $amount,
  622. ]);
  623. //写记录
  624. Db::table('tb_user_balance_record')
  625. ->insert([
  626. 'user_id' => $userId,
  627. 'amount' => $amount,
  628. 'created_at' => time(),
  629. ]);
  630. $this->success('success', null);
  631. }
  632. //获取用户购买游戏的记录
  633. public function getUserGames()
  634. {
  635. $userId = $this->request->post('userId');
  636. if (!is_numeric($userId)) {
  637. $this->fail(500, 'userId参数校验错误');
  638. }
  639. $user = Db::table('tb_user')
  640. ->where(['id' => $userId])
  641. ->find();
  642. if (!$user) {
  643. $this->fail(501, 'userId不存在');
  644. }
  645. $data = Db::table('tb_user_game')
  646. ->where(['user_id' => $userId])
  647. ->select();
  648. $this->success('success', $data);
  649. }
  650. //获取管理员手动变动用户余额记录
  651. public function getUserBalanceRecord()
  652. {
  653. $userId = $this->request->post('userId');
  654. $pageNum = $this->request->post('pageNum', 1);
  655. $pageSize = $this->request->post('pageSize', 10);
  656. if (!is_numeric($userId)) {
  657. $this->fail(500, 'userId参数校验错误');
  658. }
  659. $user = Db::table('tb_user')
  660. ->where(['id' => $userId])
  661. ->find();
  662. if (!$user) {
  663. $this->fail(501, 'userId不存在');
  664. }
  665. $query = Db::table('tb_user_balance_record')
  666. ->where(['user_id' => $userId]);
  667. $total = $query->count();
  668. $data = $query->order('created_at', 'desc')
  669. ->limit(($pageNum - 1) * $pageSize, $pageSize)
  670. ->select();
  671. $this->success('success', [
  672. 'data' => $data,
  673. 'total' => $total
  674. ]);
  675. }
  676. //获取VIP用户列表
  677. public function getVipUserList()
  678. {
  679. $page = input('page', 1);
  680. $pageSize = input('pageSize', 10);
  681. $userId = input('user_id', '');
  682. $type = input('type', '');
  683. $createdAtStart = input('created_at_start', '');
  684. $createdAtEnd = input('created_at_end', '');
  685. $expiredAtStart = input('expired_at_start', '');
  686. $expiredAtEnd = input('expired_at_end', '');
  687. $search = input('search', '');
  688. $query = Db::table('tb_user_vip');
  689. if (!empty($userId)) {
  690. $query->where('user_id', $userId);
  691. }
  692. if (!empty($type)) {
  693. $query->where('type', $type);
  694. }
  695. if (!empty($createdAtStart) && !empty($createdAtEnd)) {
  696. $query->where('created_at', '>=', $createdAtStart)
  697. ->where('created_at', '<=', $createdAtEnd);
  698. }
  699. if (!empty($expiredAtStart) && !empty($expiredAtEnd)) {
  700. $query->where('expired_at', '>=', $expiredAtStart)
  701. ->where('expired_at', '<=', $expiredAtEnd);
  702. }
  703. if (!empty($search)) {
  704. $query->where(function($q) use ($search) {
  705. $q->where('user_id', 'like', "%{$search}%");
  706. });
  707. }
  708. // 获取总数
  709. $total = $query->count();
  710. // 获取分页数据
  711. $data = $query->order('created_at DESC')
  712. ->limit(($page - 1) * $pageSize, $pageSize)
  713. ->select();
  714. // 返回结果
  715. return $this->success('success', [
  716. 'data' => $data,
  717. 'total' => $total,
  718. 'page' => $page,
  719. 'pageSize' => $pageSize
  720. ]);
  721. }
  722. public function getCustomerList()
  723. {
  724. $data = Db::table('tb_kefu')
  725. ->select();
  726. $this->success('success', $data);
  727. }
  728. public function addCustomerList()
  729. {
  730. $name = $this->request->post('name');
  731. $jumpUrl = $this->request->post('jumpUrl');
  732. $type = $this->request->post('type');
  733. if (!is_numeric($type) || empty($name) || empty($jumpUrl)) {
  734. $this->fail(500, '参数校验错误');
  735. }
  736. Db::table('tb_kefu')
  737. ->insert([
  738. 'name' => $name,
  739. 'jump_url' => $jumpUrl,
  740. 'type' => $type,
  741. 'created_at' => time(),
  742. 'updated_at' => time(),
  743. ]);
  744. $this->success('success', null);
  745. }
  746. public function editCustomerList()
  747. {
  748. $name = $this->request->post('name');
  749. $jumpUrl = $this->request->post('jumpUrl');
  750. $type = $this->request->post('type');
  751. $id = $this->request->post('id');
  752. if (!is_numeric($type) || empty($name) || empty($jumpUrl)) {
  753. $this->fail(500, '参数校验错误');
  754. }
  755. $game = Db::table('tb_kefu')
  756. ->where(['id' => $id])
  757. ->find();
  758. if (!$game) {
  759. $this->fail(502, '数据不存在');
  760. }
  761. Db::table('tb_kefu')
  762. ->where(['id' => $id])
  763. ->update([
  764. 'name' => $name,
  765. 'jump_url' => $jumpUrl,
  766. 'type' => $type,
  767. 'created_at' => time(),
  768. 'updated_at' => time(),
  769. ]);
  770. $this->success('success', null);
  771. }
  772. public function deleteCustomerList()
  773. {
  774. $id = $this->request->post('id'); //CDKEY
  775. if (empty($id)) {
  776. $this->fail(500, '参数校验错误');
  777. }
  778. $data = Db::table('tb_kefu')
  779. ->where(['id' => $id])
  780. ->find();
  781. if (!$data) {
  782. $this->fail(501, '数据不存在');
  783. }
  784. Db::table('tb_kefu')
  785. ->where(['id' => $id])
  786. ->delete();
  787. $this->success('success', null);
  788. }
  789. //获取用户访问网站记录,用于统计用户行为
  790. public function getVisitRecord()
  791. {
  792. // 获取分页参数,确保有默认值
  793. $pageSize = $this->request->post('pageSize', 10); // 每页显示的数据数量
  794. $pageNum = $this->request->post('pageNum', 1); // 当前页码
  795. // 获取日期筛选参数
  796. $startDate = $this->request->post('startDate', '');
  797. $endDate = $this->request->post('endDate', '');
  798. // 构建查询
  799. $query = Db::table('tb_website_visit');
  800. // 添加日期筛选条件
  801. if (!empty($startDate)) {
  802. $startTimestamp = strtotime($startDate . ' 00:00:00');
  803. $query->where('created_at', '>=', $startTimestamp);
  804. }
  805. if (!empty($endDate)) {
  806. $endTimestamp = strtotime($endDate . ' 23:59:59');
  807. $query->where('created_at', '<=', $endTimestamp);
  808. }
  809. // 获取总数和数据
  810. $total = $query->count();
  811. $data = $query
  812. ->order('created_at DESC')
  813. ->page(intval($pageNum), intval($pageSize))
  814. ->select();
  815. $this->success('success', [
  816. 'data' => $data, // 当前页数据
  817. 'total' => $total, // 总记录数
  818. ]);
  819. }
  820. //获取日均,总PV,日均,总IP访问数据
  821. public function visitData()
  822. {
  823. $totalPV = Db::table('tb_website_visit')->count();
  824. $todayPV = Db::table('tb_website_visit')
  825. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  826. ->count();
  827. $totalIP = Db::table('tb_website_visit')
  828. ->distinct(true)
  829. ->count('ip_address');
  830. $todayIP = Db::table('tb_website_visit')
  831. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  832. ->distinct(true)
  833. ->count('ip_address');
  834. $this->success('success', [
  835. 'totalPV' => $totalPV, //总PV
  836. 'todayPV' => $todayPV, //今日PV
  837. 'todayIP' => $todayIP, //今日IP
  838. 'totalIP' => $totalIP, //总IP
  839. ]);
  840. }
  841. //获取用户组
  842. public function getUserGroup()
  843. {
  844. $name = $this->request->post('name', '');
  845. $level = $this->request->post('level', '');
  846. $startDate = $this->request->post('start_date', '');
  847. $endDate = $this->request->post('end_date', '');
  848. $query = Db::table('tb_user_group');
  849. // 按名称搜索
  850. if (!empty($name)) {
  851. $query->where('name', 'like', '%' . $name . '%');
  852. }
  853. // 按等级搜索
  854. if (!empty($level) && is_numeric($level)) {
  855. $query->where('level', $level);
  856. }
  857. // 按时间范围搜索
  858. if (!empty($startDate)) {
  859. $startTimestamp = strtotime($startDate);
  860. $query->where('created_at', '>=', $startTimestamp);
  861. }
  862. if (!empty($endDate)) {
  863. $endTimestamp = strtotime($endDate . ' 23:59:59');
  864. $query->where('created_at', '<=', $endTimestamp);
  865. }
  866. $data = $query->order('created_at DESC')->select();
  867. $this->success('success', $data);
  868. }
  869. //添加用户组
  870. public function addUserGroup()
  871. {
  872. $name = $this->request->post('name'); //用户组名称
  873. $level = $this->request->post('level'); //等级
  874. $exp = $this->request->post('exp'); //所需经验值
  875. if (empty($name) || !is_numeric($level) || !is_numeric($exp)) {
  876. $this->fail(500, '参数校验错误');
  877. }
  878. Db::table('tb_user_group')
  879. ->insert([
  880. 'name' => $name,
  881. 'level' => $level,
  882. 'exp' => $exp,
  883. 'created_at' => time(),
  884. 'updated_at' => time(),
  885. ]);
  886. $this->success('success', null);
  887. }
  888. //编辑用户组
  889. public function editUserGroup()
  890. {
  891. $id = $this->request->post('id'); //用户组ID
  892. $name = $this->request->post('name'); //用户组名称
  893. $level = $this->request->post('level'); //等级
  894. $exp = $this->request->post('exp'); //所需经验值
  895. if (empty($name) || !is_numeric($id) || !is_numeric($level) || !is_numeric($exp)) {
  896. $this->fail(500, '参数校验错误');
  897. }
  898. $userGroup = Db::table('tb_user_group')
  899. ->where(['id' => $id])
  900. ->find();
  901. if (!$userGroup) {
  902. $this->fail(501, '找不到用户组ID');
  903. }
  904. Db::table('tb_user_group')
  905. ->where(['id' => $id])
  906. ->update([
  907. 'name' => $name,
  908. 'level' => $level,
  909. 'exp' => $exp,
  910. 'updated_at' => time(),
  911. ]);
  912. $this->success('success', null);
  913. }
  914. //删除用户组
  915. public function removeUserGroup()
  916. {
  917. $id = $this->request->post('id'); //用户组ID
  918. if (!is_numeric($id)) {
  919. $this->fail(500, '参数校验错误');
  920. }
  921. $userGroup = Db::table('tb_user_group')
  922. ->where(['id' => $id])
  923. ->find();
  924. if (!$userGroup) {
  925. $this->fail(501, '找不到用户组ID');
  926. }
  927. Db::table('tb_user_group')
  928. ->where(['id' => $id])
  929. ->delete();
  930. $this->success('success', null);
  931. }
  932. // 获取用户消息,支持分页和时间筛选
  933. public function getUserMessage()
  934. {
  935. $page = $this->request->post('page', 1);
  936. $limit = $this->request->post('limit', 10);
  937. $startDate = $this->request->post('start_date', '');
  938. $endDate = $this->request->post('end_date', '');
  939. $query = Db::table('tb_message');
  940. // 按时间范围搜索
  941. if (!empty($startDate)) {
  942. $startTimestamp = strtotime($startDate);
  943. $query->where('created_at', '>=', $startTimestamp);
  944. }
  945. if (!empty($endDate)) {
  946. $endTimestamp = strtotime($endDate . ' 23:59:59');
  947. $query->where('created_at', '<=', $endTimestamp);
  948. }
  949. // 获取总数
  950. $total = $query->count();
  951. // 分页查询
  952. $data = $query->order('created_at DESC')
  953. ->limit(($page - 1) * $limit, $limit)
  954. ->select();
  955. $this->success('success', [
  956. 'data' => $data,
  957. 'total' => $total,
  958. 'page' => $page,
  959. 'limit' => $limit
  960. ]);
  961. }
  962. //获取所有论坛版块列表
  963. public function getForumCategoryList()
  964. {
  965. $categoryId = $this->request->post('categoryId');
  966. $page = $this->request->post('page', 1);
  967. $limit = $this->request->post('limit', 10);
  968. // 构建查询条件
  969. $query = Db::table('tb_forum_category');
  970. if (!empty($categoryId)) {
  971. $query->where('category_id', $categoryId);
  972. }
  973. // 获取总数
  974. $total = $query->count();
  975. // 获取分页数据
  976. $data = $query->order('created_at DESC')
  977. ->page($page, $limit)
  978. ->select();
  979. $this->success('success', [
  980. 'list' => $data,
  981. 'total' => $total,
  982. 'page' => $page,
  983. 'limit' => $limit
  984. ]);
  985. }
  986. public function getForumThreadCategory()
  987. {
  988. $categoryid = $this->request->post('forumId');
  989. $data = Db::table('tb_forum_thread_category')->where('category_id',$categoryid)
  990. ->order('created_at DESC')
  991. ->select();
  992. $this->success('success', $data);
  993. }
  994. public function saveThreadCategories()
  995. {
  996. // 获取前端传来的板块ID和分类数据
  997. $forumId = $this->request->post('forumId');
  998. $categories = $this->request->post('categories');
  999. // 开启数据库事务
  1000. Db::startTrans();
  1001. try {
  1002. // 获取当前板块的所有帖子分类
  1003. $existingCategories = Db::table('tb_forum_thread_category')
  1004. ->where('category_id', $forumId)
  1005. ->select()
  1006. ->toArray();
  1007. // 将现有分类的 ID 存入数组,方便后续对比
  1008. $existingCategoryIds = array_column($existingCategories, 'id');
  1009. // 收集新分类的ID(包括已有的和新增的)
  1010. $newCategoryIds = [];
  1011. // 遍历前端传来的分类数据
  1012. foreach ($categories as $category) {
  1013. if (isset($category['id']) && !empty($category['id'])) {
  1014. // 如果有 ID,表示是已存在的分类,需要更新
  1015. $newCategoryIds[] = $category['id'];
  1016. // 检查是否需要更新
  1017. $existingCategory = array_filter($existingCategories, function($item) use ($category) {
  1018. return $item['id'] == $category['id'];
  1019. });
  1020. if (!empty($existingCategory)) {
  1021. $existingCategory = array_values($existingCategory);
  1022. if ($existingCategory[0]['title'] != $category['title']) {
  1023. // 更新分类
  1024. Db::table('tb_forum_thread_category')
  1025. ->where('id', $category['id'])
  1026. ->update(['title' => $category['title'], 'updated_at' => time()]);
  1027. }
  1028. }
  1029. } else {
  1030. // 如果没有 ID,表示是新增的分类
  1031. // 插入新的分类
  1032. $newId = Db::table('tb_forum_thread_category')
  1033. ->insertGetId([
  1034. 'category_id' => $forumId,
  1035. 'title' => $category['title'],
  1036. 'created_at' => time(),
  1037. 'updated_at' => time()
  1038. ]);
  1039. // 将新插入的ID加入到数组中
  1040. $newCategoryIds[] = $newId;
  1041. }
  1042. }
  1043. // 删除被移除的分类
  1044. if (!empty($newCategoryIds)) {
  1045. $deletedCategoryIds = array_diff($existingCategoryIds, $newCategoryIds);
  1046. if (!empty($deletedCategoryIds)) {
  1047. Db::table('tb_forum_thread_category')
  1048. ->where('id', 'in', $deletedCategoryIds)
  1049. ->delete();
  1050. }
  1051. } else {
  1052. // 如果没有新分类,删除所有现有分类
  1053. if (!empty($existingCategoryIds)) {
  1054. Db::table('tb_forum_thread_category')
  1055. ->where('id', 'in', $existingCategoryIds)
  1056. ->delete();
  1057. }
  1058. }
  1059. // 提交事务
  1060. Db::commit();
  1061. return json(['code' => 200, 'msg' => '保存成功']);
  1062. } catch (\Exception $e) {
  1063. // 回滚事务
  1064. Db::rollback();
  1065. return json(['code' => 500, 'msg' => '保存失败:' . $e->getMessage()]);
  1066. }
  1067. }
  1068. //添加论坛版块
  1069. public function addForumCategory()
  1070. {
  1071. $name = $this->request->post('name'); //版块名称
  1072. $description = $this->request->post('description'); //版块描述
  1073. $imageUrl = $this->request->post('imageUrl'); //版块图片URL,需要用uploadImage接口先上传拿到URL
  1074. $adminId = $this->request->post('adminId'); //版主用户ID
  1075. $priority = $this->request->post('priority'); //优先级
  1076. $categoryId = $this->request->post('categoryId'); //分类ID
  1077. if (empty($name) || empty($description) || empty($imageUrl) || !is_numeric($adminId) || !is_numeric($priority) || !is_numeric($categoryId)) {
  1078. $this->fail(500, '参数校验错误');
  1079. }
  1080. Db::table('tb_forum_category')
  1081. ->insert([
  1082. 'name' => $name,
  1083. 'description' => $description,
  1084. 'image_url' => $imageUrl,
  1085. 'admin_id' => $adminId,
  1086. 'priority' => $priority,
  1087. 'category_id' => $categoryId, // 这里是分类ID
  1088. 'created_at' => time(),
  1089. 'updated_at' => time(),
  1090. ]);
  1091. $this->success('success', null);
  1092. }
  1093. //编辑论坛版块
  1094. public function editForumCategory()
  1095. {
  1096. $id = $this->request->post('id'); //版块ID(主键)
  1097. $name = $this->request->post('name'); //版块名称
  1098. $description = $this->request->post('description'); //版块描述
  1099. $imageUrl = $this->request->post('imageUrl'); //版块图片URL
  1100. $adminId = $this->request->post('adminId'); //版主用户ID
  1101. $priority = $this->request->post('priority'); //优先级
  1102. $categoryId = $this->request->post('categoryId'); //分类ID
  1103. if (!is_numeric($id) || empty($name) || empty($description) || empty($imageUrl) || !is_numeric($adminId) || !is_numeric($priority) || !is_numeric($categoryId)) {
  1104. $this->fail(500, '参数校验错误');
  1105. }
  1106. Db::table('tb_forum_category')
  1107. ->where(['id' => $id]) // 使用版块ID作为主键
  1108. ->update([
  1109. 'name' => $name,
  1110. 'description' => $description,
  1111. 'image_url' => $imageUrl,
  1112. 'admin_id' => $adminId,
  1113. 'priority' => $priority,
  1114. 'category_id' => $categoryId, // 这里是分类ID
  1115. 'updated_at' => time(),
  1116. ]);
  1117. $this->success('success', null);
  1118. }
  1119. //移除版块
  1120. public function removeForumCategory()
  1121. {
  1122. $categoryId = $this->request->post('id'); //版块ID
  1123. if (!is_numeric($categoryId)) {
  1124. $this->fail(500, 'categoryId参数校验错误');
  1125. }
  1126. $forumCategory = Db::table('tb_forum_category')
  1127. ->where(['id' => $categoryId])
  1128. ->find();
  1129. if (!$forumCategory) {
  1130. $this->fail(501, '找不到categoryId对应的版块');
  1131. }
  1132. Db::table('tb_forum_category')
  1133. ->where(['id' => $categoryId])
  1134. ->delete();
  1135. $this->success('success', null);
  1136. }
  1137. //获取单个对应ID论坛版块信息
  1138. public function getForumCategory()
  1139. {
  1140. $categoryId = $this->request->post('categoryId'); //版块ID
  1141. if (!is_numeric($categoryId)) {
  1142. $this->fail(500, 'categoryId参数校验错误');
  1143. }
  1144. $forumCategory = Db::table('tb_forum_category')
  1145. ->where(['id' => $categoryId])
  1146. ->find();
  1147. if (!$forumCategory) {
  1148. $this->fail(501, '找不到categoryId对应的版块');
  1149. }
  1150. $this->success('success', $forumCategory);
  1151. }
  1152. // 获取所有辅助列表
  1153. public function getFuzhuList()
  1154. {
  1155. $page = $this->request->post('page'); // 页数
  1156. $list = $this->request->post('list'); // 每页记录数
  1157. $key = $this->request->post('key'); // 搜索关键字
  1158. if(!is_numeric($page)|| !is_numeric($list)) {
  1159. $this->fail(500, '参数校验错误!');
  1160. }
  1161. $query = Db::table('tb_fuzhu');
  1162. if ($key) {
  1163. $query->whereLike('title', '%'.$key.'%');
  1164. }
  1165. $data = $query->paginate([
  1166. 'page' => $page,
  1167. 'list_rows' => $list
  1168. ]);
  1169. $this->success('success', $data);
  1170. }
  1171. // 新增辅助
  1172. public function addFuzhu()
  1173. {
  1174. $title = $this->request->post('title'); // 辅助名称
  1175. $image_url = $this->request->post('image_url'); // 图片Url地址
  1176. $version = $this->request->post('version'); // 辅助版本
  1177. $file_size = $this->request->post('file_size'); // 辅助文件大小(字节)
  1178. $language = $this->request->post('language'); // 辅助语言
  1179. $game = $this->request->post('game'); // 游戏名称
  1180. $type = $this->request->post('type'); // 授权类型 0:免费辅助 1:收费辅助
  1181. $priority = $this->request->post('priority'); // 排序优先级,越小越靠前原则
  1182. $rate = $this->request->post('rate'); // 评分星星数,最多5分
  1183. $description = $this->request->post('description'); // 辅助详细描述
  1184. $download_url = $this->request->post('download_url'); // 下载地址URL
  1185. // if (!$title || !$image_url || !$version || !$file_size || $language || $game || $type || $priority || $rate || $description || $download_url) {
  1186. // $this->fail(500, '请检查传参!');
  1187. // }
  1188. // if ( !$language || !$game || !$priority || !$rate || !$description || !$download_url) {
  1189. // $this->fail(5001, '请检查传参!');
  1190. // }
  1191. if (!$title || !$image_url || !$version || !$file_size || !$language || !$game || !$priority || !$rate || !$description || !$download_url) {
  1192. $this->fail(500, '请检查传参!');
  1193. }
  1194. // 校验通过 插入数据库
  1195. Db::table('tb_fuzhu')->insert([
  1196. 'title' => $title,
  1197. 'image_url' => $image_url,
  1198. 'version' => $version,
  1199. 'file_size' => $file_size,
  1200. 'language' => $language,
  1201. 'game' => $game,
  1202. 'type' => $type,
  1203. 'priority' => $priority,
  1204. 'rate' => $rate,
  1205. 'description' => $description,
  1206. 'download_url' => $download_url,
  1207. 'created_at' => time(),
  1208. 'updated_at' => time(),
  1209. ]);
  1210. $this->success('success', null);
  1211. }
  1212. // 修改辅助
  1213. public function updateFuzhu()
  1214. {
  1215. $id = $this->request->post('id'); // 辅助ID
  1216. $title = $this->request->post('title'); // 辅助名称
  1217. $image_url = $this->request->post('image_url'); // 图片Url地址
  1218. $version = $this->request->post('version'); // 辅助版本
  1219. $file_size = $this->request->post('file_size'); // 辅助文件大小(字节)
  1220. $language = $this->request->post('language'); // 辅助语言
  1221. $game = $this->request->post('game'); // 游戏名称
  1222. $type = $this->request->post('type'); // 授权类型 0:免费辅助 1:收费辅助
  1223. $priority = $this->request->post('priority'); // 排序优先级,越小越靠前原则
  1224. $rate = $this->request->post('rate'); // 评分星星数,最多5分
  1225. $description = $this->request->post('description'); // 辅助详细描述
  1226. $download_url = $this->request->post('download_url'); // 下载地址URL
  1227. if (!$id || !$title || !$image_url || !$version || !$file_size || !$language || !$game || !$priority || !$rate || !$description || !$download_url) {
  1228. $this->fail(500, '请检查传参!');
  1229. }
  1230. // 校验通过 插入数据库
  1231. Db::table('tb_fuzhu')
  1232. ->where('id', $id)
  1233. ->update([
  1234. 'title' => $title,
  1235. 'image_url' => $image_url,
  1236. 'version' => $version,
  1237. 'file_size' => $file_size,
  1238. 'language' => $language,
  1239. 'game' => $game,
  1240. 'type' => $type,
  1241. 'priority' => $priority,
  1242. 'rate' => $rate,
  1243. 'description' => $description,
  1244. 'download_url' => $download_url,
  1245. 'created_at' => time(),
  1246. 'updated_at' => time(),
  1247. ]);
  1248. $this->success('success', null);
  1249. }
  1250. // 删除辅助
  1251. public function deleteFuzhu()
  1252. {
  1253. $id = $this->request->post('id'); // 辅助ID
  1254. if (!$id) {
  1255. $this->fail(500, '缺少参数id!');
  1256. }
  1257. $deleted = Db::table('tb_fuzhu')->where('id', $id)->delete();
  1258. if ($deleted) {
  1259. $this->success('删除成功', null);
  1260. } else {
  1261. $this->fail(500, '删除失败,未找到对应记录');
  1262. }
  1263. }
  1264. //获取辅助下载记录,需要支持分页
  1265. public function getFuzhuDownload()
  1266. {
  1267. $page = $this->request->post('page'); // 当前页数
  1268. $list = $this->request->post('list'); // 每页记录数
  1269. $fuzhuId = $this->request->post('fuzhuId'); //辅助ID
  1270. if (!is_numeric($fuzhuId)) {
  1271. $this->fail(500, 'fuzhuId参数校验错误');
  1272. }
  1273. $data = Db::table('tb_fuzhu_download')
  1274. ->alias('tfd')
  1275. ->where(['tfd.fuzhu_id' => $fuzhuId])
  1276. ->order('tfd.created_at DESC')
  1277. ->join('tb_user tu', 'tfd.user_id = tu.id', 'LEFT')
  1278. ->field('tfd.*, tu.username, tu.avatar')
  1279. ->paginate([
  1280. 'page' => $page,
  1281. 'list_rows' => $list
  1282. ]);
  1283. // 处理头像数据
  1284. $items = $data->getCollection()->toArray();
  1285. if ($items) {
  1286. foreach($items as &$item) {
  1287. if (!empty($item['avatar'])) {
  1288. $avatarParts = explode('//', $item['avatar']);
  1289. if (count($avatarParts) > 1) {
  1290. $avatarId = intval($avatarParts[1]);
  1291. $avatar = Db::table('tb_system_avatar')
  1292. ->where(['id' => $avatarId])
  1293. ->find();
  1294. $item['avatar'] = $avatar['image_url'] ?? '';
  1295. }
  1296. }
  1297. }
  1298. // 将处理后的数据重新设置到分页对象中
  1299. $data->setCollection(collect($items));
  1300. }
  1301. $this->success('success', $data);
  1302. }
  1303. /**
  1304. * 获取全局配置
  1305. */
  1306. public function getGlobalConfig()
  1307. {
  1308. $page = $this->request->post('page',1); // 当前页数
  1309. $list = $this->request->post('list',10); // 每页记录数
  1310. $key = $this->request->post('key'); // 搜索关键词 不传则不搜索
  1311. if (!is_numeric($page) ||!is_numeric($list)) {
  1312. $this->fail(500, '参数错误!');
  1313. }
  1314. $query = Db::table('tb_global_config');
  1315. if($key){
  1316. $query->whereLike('key', '%' . $key . '%');
  1317. }
  1318. $data = $query->paginate([
  1319. 'page' => $page,
  1320. 'list_rows' => $list
  1321. ]);
  1322. $this->success('success', $data);
  1323. }
  1324. /**
  1325. * 新增全局配置
  1326. */
  1327. public function addGlobalConfig()
  1328. {
  1329. $key = $this->request->post('key'); // 配置名称
  1330. $value = $this->request->post('value'); // 配置值
  1331. $description = $this->request->post('description'); // 配置说明
  1332. if(!$key || !$value) {
  1333. $this->fail(500, '参数校验错误!');
  1334. }
  1335. Db::table('tb_global_config')
  1336. ->insert([
  1337. 'key' => $key,
  1338. 'value' => $value,
  1339. 'description' => $description,
  1340. 'create_at' => time()
  1341. ]);
  1342. $this->success('success', null);
  1343. }
  1344. /**
  1345. * 修改全局配置
  1346. */
  1347. public function updateGlobalConfig()
  1348. {
  1349. $id = $this->request->post('id'); // 主键ID
  1350. $key = $this->request->post('key'); // 配置名称
  1351. $value = $this->request->post('value'); // 配置值
  1352. $description = $this->request->post('description'); // 配置说明
  1353. if(!is_numeric($id)|| !$key || !$value) {
  1354. $this->fail(500, '参数校验错误!');
  1355. }
  1356. Db::table('tb_global_config')
  1357. ->where('id', $id)
  1358. ->update([
  1359. 'key' => $key,
  1360. 'value' => $value,
  1361. 'description' => $description,
  1362. 'update_at' => time()
  1363. ]);
  1364. $this->success('success', null);
  1365. }
  1366. /**
  1367. * 删除全局配置
  1368. */
  1369. public function deleteGlobalConfig()
  1370. {
  1371. $id = $this->request->post('id'); // 主键ID
  1372. if(!is_numeric($id)) {
  1373. $this->fail(500, '参数校验错误!');
  1374. }
  1375. Db::table('tb_global_config')
  1376. ->where('id', $id)
  1377. ->delete();
  1378. $this->success('success', null);
  1379. }
  1380. public function getFeedBack()
  1381. {
  1382. $page = $this->request->post('page',1); // 当前页数
  1383. $list = $this->request->post('list',10); // 每页记录数
  1384. $key = $this->request->post('key'); // 搜索关键词 不传则不搜索
  1385. if (!is_numeric($page) ||!is_numeric($list)) {
  1386. $this->fail(500, '参数错误!');
  1387. }
  1388. $query = Db::table('tb_feedback');
  1389. if($key){
  1390. $query->whereLike('key', '%' . $key . '%');
  1391. }
  1392. $data = $query->paginate([
  1393. 'page' => $page,
  1394. 'list_rows' => $list
  1395. ]);
  1396. $this->success('success', $data);
  1397. }
  1398. public function getUserBug()
  1399. {
  1400. $page = $this->request->post('page',1); // 当前页数
  1401. $list = $this->request->post('list',10); // 每页记录数
  1402. $key = $this->request->post('key'); // 搜索关键词 不传则不搜索
  1403. if (!is_numeric($page) ||!is_numeric($list)) {
  1404. $this->fail(500, '参数错误!');
  1405. }
  1406. $query = Db::table('tb_user_bug');
  1407. if($key){
  1408. $query->whereLike('key', '%' . $key . '%');
  1409. }
  1410. $data = $query->paginate([
  1411. 'page' => $page,
  1412. 'list_rows' => $list
  1413. ]);
  1414. $this->success('success', $data);
  1415. }
  1416. public function getThreadList()
  1417. {
  1418. $page = $this->request->post('page', 1); // 当前页数
  1419. $list = $this->request->post('limit', 10); // 每页记录数
  1420. $key = $this->request->post('key'); // 搜索关键词
  1421. $category_id = $this->request->post('category_id'); // 板块ID
  1422. $user_id = $this->request->post('user_id'); // 用户ID
  1423. $start_date = $this->request->post('start_date'); // 开始日期
  1424. $end_date = $this->request->post('end_date'); // 结束日期
  1425. if (!is_numeric($page) || !is_numeric($list)) {
  1426. $this->fail(500, '参数错误!');
  1427. }
  1428. $query = Db::table('tb_thread')
  1429. ->alias('t')
  1430. ->leftJoin('tb_forum_category fc', 't.category_id = fc.id')
  1431. ->field('t.*, fc.name as category_name');
  1432. if ($key) {
  1433. $query->whereLike('t.title', '%' . $key . '%')
  1434. ->whereOrLike('t.content', '%' . $key . '%');
  1435. }
  1436. if ($category_id) {
  1437. $query->where('t.category_id', $category_id);
  1438. }
  1439. if ($user_id) {
  1440. $query->where('t.user_id', $user_id);
  1441. }
  1442. if ($start_date && $end_date) {
  1443. $query->whereBetween('t.created_at', [strtotime($start_date), strtotime($end_date) + 86399]);
  1444. }
  1445. $data = $query->paginate([
  1446. 'page' => $page,
  1447. 'list_rows' => $list
  1448. ]);
  1449. $this->success('success', $data);
  1450. }
  1451. public function deleteThreadList()
  1452. {
  1453. $id = $this->request->post('id'); // 主键ID
  1454. if(!is_numeric($id)) {
  1455. $this->fail(500, '参数校验错误!');
  1456. }
  1457. $result = Db::table('tb_thread')
  1458. ->where('id', $id)
  1459. ->delete();
  1460. if ($result) {
  1461. $this->success('删除成功', null);
  1462. } else {
  1463. $this->fail(500, '删除失败');
  1464. }
  1465. }
  1466. public function getForumCategories()
  1467. {
  1468. $categories = Db::table('tb_forum_category')
  1469. ->field('id, name')
  1470. ->select()
  1471. ->toArray();
  1472. $this->success('success', $categories);
  1473. }
  1474. // public function getOrderList()
  1475. // {
  1476. // $page = $this->request->post('page',1); // 当前页数
  1477. // $list = $this->request->post('list',10); // 每页记录数
  1478. // $key = $this->request->post('key'); // 搜索关键词 不传则不搜索
  1479. // if (!is_numeric($page) ||!is_numeric($list)) {
  1480. // $this->fail(500, '参数错误!');
  1481. // }
  1482. // $query = Db::table('tb_user_order');
  1483. // if($key){
  1484. // $query->whereLike('key', '%' . $key . '%');
  1485. // }
  1486. // $data = $query->paginate([
  1487. // 'page' => $page,
  1488. // 'list_rows' => $list
  1489. // ]);
  1490. // $this->success('success', $data);
  1491. // }
  1492. }