Authorize.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use think\facade\Log;
  6. //授权控制器
  7. class Authorize extends BaseController
  8. {
  9. protected $noNeedLogin = [];
  10. private $secretKey = 'E4skfomP*Pz4MOUm'; //用于计算签名的密钥key
  11. //绑定机器,需要购买游戏后绑定
  12. //签名:md5(machineCode+gameId+secretKey)
  13. public function bindMachine()
  14. {
  15. $machineCode = $this->request->post('machineCode');
  16. $gameId = $this->request->post('gameId');
  17. $sign = $this->request->post('sign');
  18. if (empty($machineCode) || !is_numeric($gameId) || empty($sign)) {
  19. $this->fail(500, '参数校验错误');
  20. }
  21. $comparedSign = md5($machineCode . $gameId . $this->secretKey);
  22. if ($sign != $comparedSign) {
  23. $this->fail(504, 'sign签名校验错误');
  24. }
  25. $user = $this->getUser();
  26. //判断gameId是否存在
  27. $game = Db::table('tb_game')
  28. ->where(['id' => $gameId])
  29. ->find();
  30. if (!$game) {
  31. $this->fail(502, 'gameId不存在');
  32. }
  33. //判断是否已经购买过此游戏
  34. $userGame = Db::table('tb_user_game')
  35. ->where(['user_id' => $user->user_id])
  36. ->where(['game_id' => $gameId])
  37. ->find();
  38. if (!$userGame) {
  39. $this->fail(503, '未购买过此游戏,无法绑定机器');
  40. }
  41. $gameMachine = Db::table('tb_game_machine')
  42. ->where(['game_id' => $gameId])
  43. ->where(['user_id' => $user->user_id])
  44. ->find();
  45. if ($gameMachine) {
  46. $this->fail(501, '已经绑定过此款类型游戏了');
  47. }
  48. Db::table('tb_game_machine')
  49. ->insert([
  50. 'user_id' => $user->user_id,
  51. 'game_id' => $gameId,
  52. 'machine_code' => $machineCode,
  53. 'created_at' => time(),
  54. 'expired_at' => time() + 60 * 60 * 24 * 30 * 12 * 3,
  55. ]);
  56. //写绑定记录
  57. Db::table('tb_game_bind_record')
  58. ->insert([
  59. 'user_id' => $user->user_id,
  60. 'game_id' => $gameId,
  61. 'machine_code' => $machineCode,
  62. 'created_at' => time(),
  63. ]);
  64. $this->success('success', null);
  65. }
  66. //验证机器码
  67. //签名:md5(machineCode+gameId+secretKey)
  68. public function verifyMachine()
  69. {
  70. $machineCode = $this->request->post('machineCode');
  71. $gameId = $this->request->post('gameId');
  72. $sign = $this->request->post('sign');
  73. if (empty($machineCode) || !is_numeric($gameId) || empty($sign)) {
  74. $this->fail(500, '参数校验错误');
  75. }
  76. $comparedSign = md5($machineCode . $gameId . $this->secretKey);
  77. if ($sign != $comparedSign) {
  78. $this->fail(503, 'sign签名校验错误');
  79. }
  80. $user = $this->getUser();
  81. $data = Db::table('tb_game_machine')
  82. ->where(['user_id' => $user->user_id])
  83. ->where(['game_id' => $gameId])
  84. ->where(['machine_code' => $machineCode])
  85. ->find();
  86. if (!$data) {
  87. $this->fail(501, '验证失败,没有机器绑定记录');
  88. }
  89. if ($data['expired_at'] < time()) {
  90. $this->fail(502, '验证失败,机器绑定已过期');
  91. }
  92. $this->success('success', null);
  93. }
  94. }