Appkey.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * SDK密钥管理
  4. * Date: 2018/1/22
  5. * Time: 10:18
  6. */
  7. namespace app\admin\controller;
  8. use think\Loader;
  9. use think\Db;
  10. class Appkey extends Admin
  11. {
  12. protected $gameList;
  13. public function _initialize()
  14. {
  15. parent::_initialize(); // TODO: Change the autogenerated stub
  16. $this->gameList = $gameList = model('Common/Game')->getAllByCondition('id,name');
  17. $tmpSelfGameList = model('Common/Game')->getAllByCondition('id,name', [],'','self');
  18. $selfGameList = array();
  19. foreach ($tmpSelfGameList as $game) {
  20. $selfGameList[ $game['id']] = $game;
  21. }
  22. $this->selfGameList = $selfGameList;
  23. }
  24. /**
  25. *SDK密钥列表
  26. */
  27. public function index()
  28. {
  29. $condition = [];
  30. $gameId = $this->request->param('gameid', '', 'intval');
  31. if (!empty($gameId)) {
  32. $condition ['gameid'] = $gameId;
  33. }
  34. $appList = model('Common/App')->alias('a')->join('cy_game g', 'a.gameid=g.id', 'left')->where($condition)->where("g.is_default = 0")->field('a.id,a.appid,a.app_package,a.appkey,a.client_appkey,a.create_time,a.beizhu,a.gameid,g.is_default')->order('id desc')->paginate(20);
  35. $data = $appList->toArray();
  36. $gameid = array_column($data['data'], 'gameid');
  37. $gameid = array_unique($gameid);
  38. $gameName = model('game')->whereIn('id', $gameid)->column('id,name');
  39. $this->assign('game_list', $this->selfGameList);
  40. $this->assign('game_name', $gameName);
  41. $this->assign('appList', $appList);
  42. $this->assign('page', $appList->render());
  43. return $this->fetch();
  44. }
  45. /**
  46. *增
  47. */
  48. public function add()
  49. {
  50. if ($this->request->isPost()) {
  51. $data = $this->request->post();
  52. if (true !== ($res = $this->validate($data, 'appkey'))) {
  53. $this->error($res);
  54. }
  55. $data['appkey'] = md5($data['appid'] . $data['gameid'] . time());
  56. $data['client_appkey'] = md5(random(10).$data['appid']); //客户端appkey
  57. //
  58. // if (!$gameInfo = model("game")->field('initial')->where(['id' => $data['gameid']])->find()) {
  59. // $this->error('游戏信息不存在');
  60. // }
  61. // $data['appid'] = $gameInfo['initial'];
  62. $appModel = model('Common/App');
  63. if ($appModel->save($data)) {
  64. $this->success('添加成功',url('index'));
  65. }
  66. $this->error($appModel->getError() ?: '添加失败');
  67. }
  68. $this->assign('game_list', $this->selfGameList);
  69. return $this->fetch();
  70. }
  71. /**
  72. *删
  73. */
  74. public function delete()
  75. {
  76. $id = $this->request->param('id', '', 'intval');
  77. if (empty($id)) {
  78. $this->error('参数错误!');
  79. }
  80. $appModel = model('Common/App');
  81. if (!$appModel->find($id)) {
  82. $this->error('参数错误,不存在该数据');
  83. }
  84. if ($appModel->where('id', '=', $id)->delete()) {
  85. $this->success('删除成功',url('index'));
  86. }
  87. $error = $appModel->getError();
  88. $this->error($error ?: '删除失败');
  89. }
  90. /**
  91. *编辑SDK密钥
  92. */
  93. public function edit()
  94. {
  95. $id = $this->request->param('id', '', 'intval');
  96. $appModel = model('Common/App');
  97. if (!$data = $appModel->find($id)) {
  98. $this->error('参数错误,不存在该数据');
  99. }
  100. if (empty($id)) {
  101. $this->error('参数错误!');
  102. }
  103. if ($this->request->isPost()) {
  104. $data = $this->request->post();
  105. $appKeyValidate = Loader::validate('appkey');
  106. if (!$res = $appKeyValidate->check($data)) {
  107. $this->error($appKeyValidate->getError());
  108. }
  109. if ($appModel->save($data, ['id' => $id]) !== false) {
  110. $this->success('修改成功',url('index'));
  111. }
  112. $this->error($appModel->getError() ?: '修改失败');
  113. }
  114. $this->assign('game_list', $this->selfGameList);
  115. $this->assign('data', $data);
  116. return $this->fetch();
  117. }
  118. }