Apiconfig.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * API接口配置
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Apiconfig extends Backend
  13. {
  14. /**
  15. * DataApiConfig模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('DataApiConfig');
  22. $this->view->assign("isAllList", $this->model->getIsAllList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('pkey_name')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $maps = [];
  44. if ($this->group == 1) {
  45. //查看全部
  46. } else {
  47. if ($this->group == 2) {
  48. //管理员查看自己创建的
  49. $maps = [
  50. 'admin_id' => $this->auth->id,
  51. ];
  52. } else {
  53. $result = array("total" => 0, "rows" => []);
  54. return json($result);
  55. }
  56. }
  57. $total = $this->model
  58. ->where($where)
  59. ->where($maps)
  60. ->order($sort, $order)
  61. ->count();
  62. $list = $this->model
  63. ->where($where)
  64. ->where($maps)
  65. ->order($sort, $order)
  66. ->limit($offset, $limit)
  67. ->select();
  68. $result = array("total" => $total, "rows" => $list);
  69. return json($result);
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 添加
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isPost()) {
  79. $params = $this->request->post("row/a");
  80. if ($params) {
  81. /*
  82. * 已经弃用,如果为了兼容老版可取消注释
  83. foreach ($params as $k => &$v)
  84. {
  85. $v = is_array($v) ? implode(',', $v) : $v;
  86. }
  87. */
  88. if ($this->dataLimit) {
  89. $params[$this->dataLimitField] = $this->auth->id;
  90. }
  91. try {
  92. //是否采用模型验证
  93. if ($this->modelValidate) {
  94. $name = basename(str_replace('\\', '/', get_class($this->model)));
  95. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  96. $this->model->validate($validate);
  97. }
  98. //随机生成token和client_id
  99. $params['admin_id'] = $this->auth->id;
  100. $params['client_id'] = '1000'.mt_rand(1000, 9999);
  101. $params['token'] = $this->rand_str(18, 0, 1);
  102. //处理channel_ids
  103. if ($params['channel_ids']) {
  104. $channel_ids = str_replace(",", ",", trim($params['channel_ids'], ','));
  105. $params['channel_ids'] = implode(",", array_filter(array_unique(explode(',', $channel_ids))));
  106. }
  107. $result = $this->model->allowField(true)->save($params);
  108. if ($result !== false) {
  109. $this->success();
  110. } else {
  111. $this->error($this->model->getError());
  112. }
  113. } catch (\think\exception\PDOException $e) {
  114. $this->error($e->getMessage());
  115. }
  116. }
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 编辑
  123. */
  124. public function edit($ids = null)
  125. {
  126. $row = $this->model->get($ids);
  127. if (!$row) {
  128. $this->error(__('No Results were found'));
  129. }
  130. $adminIds = $this->getDataLimitAdminIds();
  131. if (is_array($adminIds)) {
  132. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  133. $this->error(__('You have no permission'));
  134. }
  135. }
  136. if ($this->request->isPost()) {
  137. $params = $this->request->post("row/a");
  138. if ($params) {
  139. /*
  140. * 已经弃用,如果为了兼容老版可取消注释
  141. foreach ($params as $k => &$v)
  142. {
  143. $v = is_array($v) ? implode(',', $v) : $v;
  144. }
  145. */
  146. try {
  147. //是否采用模型验证
  148. if ($this->modelValidate) {
  149. $name = basename(str_replace('\\', '/', get_class($this->model)));
  150. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  151. $row->validate($validate);
  152. }
  153. //处理channel_ids
  154. if ($params['channel_ids']) {
  155. $channel_ids = str_replace(",", ",", trim($params['channel_ids'], ','));
  156. $params['channel_ids'] = implode(",", array_filter(array_unique(explode(',', $channel_ids))));
  157. }
  158. $result = $row->allowField(true)->save($params);
  159. if ($result !== false) {
  160. Redis::instance()->del("DATAAPI:".$row['client_id']);
  161. $this->success();
  162. } else {
  163. $this->error($row->getError());
  164. }
  165. } catch (\think\exception\PDOException $e) {
  166. $this->error($e->getMessage());
  167. }
  168. }
  169. $this->error(__('Parameter %s can not be empty', ''));
  170. }
  171. $this->view->assign("row", $row);
  172. return $this->view->fetch();
  173. }
  174. /**
  175. * 删除
  176. */
  177. public function del($ids = "")
  178. {
  179. if ($ids) {
  180. $pk = $this->model->getPk();
  181. $adminIds = $this->getDataLimitAdminIds();
  182. if (is_array($adminIds)) {
  183. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  184. }
  185. $list = $this->model->where($pk, 'in', $ids)->select();
  186. $count = 0;
  187. foreach ($list as $k => $v) {
  188. Redis::instance()->del("DATAAPI:".$v['client_id']);
  189. $count += $v->delete();
  190. }
  191. if ($count) {
  192. $this->success();
  193. } else {
  194. $this->error(__('No rows were deleted'));
  195. }
  196. }
  197. $this->error(__('Parameter %s can not be empty', 'ids'));
  198. }
  199. /**
  200. * 随机token
  201. * @param int $randLength
  202. * @param int $addtime
  203. * @param int $includenumber
  204. * @return string
  205. */
  206. private function rand_str($randLength = 6, $addtime = 1, $includenumber = 0)
  207. {
  208. if ($includenumber) {
  209. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
  210. } else {
  211. $chars = 'abcdefghijklmnopqrstuvwxyz';
  212. }
  213. $len = strlen($chars);
  214. $randStr = '';
  215. for ($i = 0; $i < $randLength; $i++) {
  216. $randStr .= $chars[mt_rand(0, $len - 1)];
  217. }
  218. $tokenvalue = $randStr;
  219. if ($addtime) {
  220. $tokenvalue = $randStr . time();
  221. }
  222. return $tokenvalue;
  223. }
  224. }