Startup.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * api 游戏启动时设备信息收集控制器
  4. */
  5. namespace app\api\controller\v1;
  6. use app\api\controller\Api;
  7. use think\Db;
  8. class Startup extends Api
  9. {
  10. /**
  11. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  12. */
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. /**
  18. * 平台币劵列表
  19. * return array
  20. */
  21. public function index()
  22. {
  23. $game_id = $this->input('gameid'); //游戏ID
  24. $channel_id = $this->input('channel_id'); //渠道ID
  25. $imei = trim($this->input('imeil')); //手机imei码
  26. $os_version = trim($this->input('os_version')); //操作系统版本
  27. $checkResult = $this->validate($this->input,
  28. [
  29. ['gameid', 'require|positiveInteger', '游戏id不能为空|游戏ID必须为整型'],
  30. ['channel_id', 'require|positiveInteger', '渠道ID不能为空|渠道ID必须为整型'],
  31. ['imeil', 'require', '手机imei码不能为空|手机imei码不能超过40个字符'],
  32. ['platform', 'require|integer|in:0,1', '手机类型不能为空|手机类型必须为整型|手机类型参数错误'],
  33. ['phone_brand', 'max:40', '手机品牌不能超过40个字符'],
  34. ['phone_model', 'max:100', '手机型号不能超过100个字符'],
  35. ['os_version', 'max:50', '系统版本不能超过50个字符'],
  36. ]);
  37. if (true !== $checkResult) {
  38. $this->jsonResult('', 0, $checkResult);
  39. }
  40. //如果当天该设备记录不存在时
  41. if(!Db::table('nw_startup_devices')->where(['game_id'=>$game_id,'channel_id'=>$channel_id,'imei'=>$imei,'create_time'=>['>=',strtotime(date('Y-m-d'))]])->find())
  42. {
  43. // 启动事务
  44. Db::startTrans();
  45. try{
  46. $startupData['game_id'] = $game_id;
  47. $startupData['channel_id'] = $this->input('channel_id');
  48. $startupData['imei'] = $imei;
  49. $startupData['ip'] = request()->ip();
  50. $startupData['create_time'] = NOW_TIMESTAMP;
  51. //游戏启动时设备信息表
  52. Db::table('nw_startup_devices')->insert($startupData);
  53. //设备信息记录不存在时
  54. if(!Db::table('nw_devices')->where(['game_id'=>$game_id,'imei'=>$imei,'os_version'=>$os_version])->find())
  55. {
  56. $devicesData['game_id'] = $game_id;
  57. $devicesData['platform'] = $this->input('platform');
  58. $devicesData['imei'] = $imei;
  59. $devicesData['phone_brand'] = $this->input('phone_brand');
  60. $devicesData['phone_model'] = $this->input('phone_model');
  61. $devicesData['os_version'] = $os_version;
  62. $devicesData['update_time'] = NOW_TIMESTAMP;
  63. $devicesData['create_time'] = NOW_TIMESTAMP;
  64. Db::table('nw_devices')->insert($devicesData);
  65. }
  66. // 提交事务
  67. Db::commit();
  68. } catch (\Exception $e) {
  69. // 回滚事务
  70. Db::rollback();
  71. $this->jsonResult('', 0, '设备信息保存失败'.$e->getMessage());
  72. }
  73. $this->jsonResult('', 1, '设备信息保存成功');
  74. }
  75. $this->jsonResult('', 0, '今日,游戏启动时设备信息收集已经收集');
  76. }
  77. }