Sdkcp.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * SDK CP管理
  4. * Date: 2018/1/22
  5. * Time: 10:18
  6. */
  7. namespace app\admin\controller;
  8. use think\Db;
  9. use think\Loader;
  10. class Sdkcp 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. $this->selfGameList = $selfGameList = model('Common/Game')->getAllByCondition('id,name', [],'','self');
  18. }
  19. /**
  20. *SDK密钥列表
  21. */
  22. public function index()
  23. {
  24. $condition = [];
  25. $gameId = $this->request->param('appid', '', 'intval');
  26. if (!empty($gameId)) {
  27. $condition ['appid'] = $gameId;
  28. }
  29. $appList = model('Common/Cpurl')->where($condition)->order('id desc')->paginate(20);
  30. $gameList = $this->relationData(1);
  31. $gameName = array_column($gameList['applist'], 'name', 'appid');
  32. $tmpSelfGameList = $gameList['glist'];
  33. $game_list = array();
  34. foreach ($tmpSelfGameList as $game) {
  35. $game_list[ $game['id']] = $game;
  36. }
  37. $this->assign('game_list', $game_list);
  38. $this->assign('game_name', $gameName);
  39. $this->assign('appList', $appList);
  40. $this->assign('page', $appList->render());
  41. return $this->fetch();
  42. }
  43. /**
  44. *
  45. * appid - gameid 关联数据
  46. * @return array
  47. */
  48. public function relationData($tr = 0)
  49. {
  50. $appList = Db::table('cy_app')->order('id desc')->column('gameid,id');
  51. $gameList = array_column($this->selfGameList, 'name', 'id');
  52. if (empty($tr)){
  53. foreach ($appList as $key => $v) {
  54. $tmp = [];
  55. $tmp['appid'] = $v;
  56. $tmp['name'] = isset($gameList[ $key ]) ? $gameList[ $key ] : '';
  57. $appList[ $key ] = $tmp;
  58. }
  59. return $appList;
  60. }else{
  61. $GList = [];
  62. foreach ($appList as $key => $v) {
  63. $tmp = [];
  64. $tmp['appid'] = $v;
  65. $tmp['name'] = isset($gameList[ $key ]) ? $gameList[ $key ] : '';
  66. $appList[ $key ] = $tmp;
  67. $GList[] = ['id'=>$v,'name'=>$tmp['name']];
  68. }
  69. return ['applist'=>$appList,'glist'=>$GList];
  70. }
  71. }
  72. /**
  73. *增
  74. */
  75. public function add()
  76. {
  77. if ($this->request->isPost()) {
  78. $data = $this->request->post();
  79. $data['url'] = trim($data['url']);
  80. if (true !== ($res = $this->validate($data, 'sdkcp'))) {
  81. $this->error($res);
  82. }
  83. $appModel = model('Common/Cpurl');
  84. if ($appModel->save($data)) {
  85. $this->success('添加成功', url('index'));
  86. }
  87. $this->error($appModel->getError() ?: '添加失败');
  88. }
  89. $tmpSelfGameList = $this->relationData(1)['glist'];
  90. $game_list = array();
  91. foreach ($tmpSelfGameList as $game) {
  92. $game_list[ $game['id']] = $game;
  93. }
  94. $this->assign('game_list', $game_list);
  95. return $this->fetch();
  96. }
  97. /**
  98. *删
  99. */
  100. public function delete()
  101. {
  102. $id = $this->request->param('id', '', 'intval');
  103. if (empty($id)) {
  104. $this->error('参数错误!');
  105. }
  106. $appModel = model('Common/Cpurl');
  107. if (!$appModel->find($id)) {
  108. $this->error('参数错误,不存在该数据');
  109. }
  110. if ($appModel->where('id', '=', $id)->delete()) {
  111. $this->success('删除成功', url('index'));
  112. }
  113. $error = $appModel->getError();
  114. $this->error($error ?: '删除失败');
  115. }
  116. /**
  117. *编辑SDK密钥
  118. */
  119. public function edit()
  120. {
  121. $id = $this->request->param('id', '', 'intval');
  122. $appModel = model('Common/Cpurl');
  123. if (!$data = $appModel->find($id)) {
  124. $this->error('参数错误,不存在该数据');
  125. }
  126. if (empty($id)) {
  127. $this->error('参数错误!');
  128. }
  129. if ($this->request->isPost()) {
  130. $data = $this->request->post();
  131. $data['url'] = trim($data['url']);
  132. $appKeyValidate = Loader::validate('sdkcp');
  133. if (!$res = $appKeyValidate->check($data)) {
  134. $this->error($appKeyValidate->getError());
  135. }
  136. if ($appModel->save($data, ['id' => $id]) !== false) {
  137. $this->success('修改成功', url('index'));
  138. }
  139. $this->error($appModel->getError() ?: '修改失败');
  140. }
  141. $tmpSelfGameList = $this->relationData(1)['glist'];
  142. $game_list = array();
  143. foreach ($tmpSelfGameList as $game) {
  144. $game_list[ $game['id']] = $game;
  145. }
  146. $this->assign('game_list', $game_list);
  147. $this->assign('data', $data);
  148. return $this->fetch();
  149. }
  150. }