| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- use think\facade\Log;
- //授权控制器
- class Authorize extends BaseController
- {
- protected $noNeedLogin = [];
-
- private $secretKey = 'E4skfomP*Pz4MOUm'; //用于计算签名的密钥key
-
- //绑定机器,需要购买游戏后绑定
- //签名:md5(machineCode+gameId+secretKey)
- public function bindMachine()
- {
- $machineCode = $this->request->post('machineCode');
- $gameId = $this->request->post('gameId');
- $sign = $this->request->post('sign');
- if (empty($machineCode) || !is_numeric($gameId) || empty($sign)) {
- $this->fail(500, '参数校验错误');
- }
- $comparedSign = md5($machineCode . $gameId . $this->secretKey);
- if ($sign != $comparedSign) {
- $this->fail(504, 'sign签名校验错误');
- }
- $user = $this->getUser();
- //判断gameId是否存在
- $game = Db::table('tb_game')
- ->where(['id' => $gameId])
- ->find();
- if (!$game) {
- $this->fail(502, 'gameId不存在');
- }
- //判断是否已经购买过此游戏
- $userGame = Db::table('tb_user_game')
- ->where(['user_id' => $user->user_id])
- ->where(['game_id' => $gameId])
- ->find();
- if (!$userGame) {
- $this->fail(503, '未购买过此游戏,无法绑定机器');
- }
- $gameMachine = Db::table('tb_game_machine')
- ->where(['game_id' => $gameId])
- ->where(['user_id' => $user->user_id])
- ->find();
- if ($gameMachine) {
- $this->fail(501, '已经绑定过此款类型游戏了');
- }
- Db::table('tb_game_machine')
- ->insert([
- 'user_id' => $user->user_id,
- 'game_id' => $gameId,
- 'machine_code' => $machineCode,
- 'created_at' => time(),
- 'expired_at' => time() + 60 * 60 * 24 * 30 * 12 * 3,
- ]);
- //写绑定记录
- Db::table('tb_game_bind_record')
- ->insert([
- 'user_id' => $user->user_id,
- 'game_id' => $gameId,
- 'machine_code' => $machineCode,
- 'created_at' => time(),
- ]);
- $this->success('success', null);
- }
-
- //验证机器码
- //签名:md5(machineCode+gameId+secretKey)
- public function verifyMachine()
- {
- $machineCode = $this->request->post('machineCode');
- $gameId = $this->request->post('gameId');
- $sign = $this->request->post('sign');
- if (empty($machineCode) || !is_numeric($gameId) || empty($sign)) {
- $this->fail(500, '参数校验错误');
- }
- $comparedSign = md5($machineCode . $gameId . $this->secretKey);
- if ($sign != $comparedSign) {
- $this->fail(503, 'sign签名校验错误');
- }
- $user = $this->getUser();
- $data = Db::table('tb_game_machine')
- ->where(['user_id' => $user->user_id])
- ->where(['game_id' => $gameId])
- ->where(['machine_code' => $machineCode])
- ->find();
- if (!$data) {
- $this->fail(501, '验证失败,没有机器绑定记录');
- }
- if ($data['expired_at'] < time()) {
- $this->fail(502, '验证失败,机器绑定已过期');
- }
- $this->success('success', null);
- }
- }
|