ChannelController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 渠道管理 
  4. * @author xiabian
  5. */
  6. declare(strict_types=1);
  7. namespace Modules\Manage\Controllers;
  8. use App\Model\ChannelModel as Model;
  9. use App\Model\ConfigModel;
  10. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  11. use Psr\SimpleCache\CacheInterface;
  12. class ChannelController extends BaseController
  13. {
  14. /**
  15. * 列表
  16. * @param ValidatorFactoryInterface $validatorFactory
  17. * @return mixed
  18. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"channel":""}}}
  19. */
  20. public function lists(ValidatorFactoryInterface $validatorFactory)
  21. {
  22. $inputData = $this->getJsonData();
  23. $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
  24. $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
  25. $select = ['*'];
  26. $model = Model::query();
  27. $search = isset($inputData['search']) ? $inputData['search'] : array();
  28. if ($search) {
  29. foreach ($search as $k => $v) {
  30. if (!strlen((string)$v)) {
  31. continue;
  32. }
  33. switch ($k) {
  34. case "channel": // 渠道码
  35. $model->where($k, 'like', "{$v}%");
  36. break;
  37. default:
  38. $model->where($k, $v);
  39. }
  40. }
  41. }
  42. $result = $model->select($select)
  43. ->orderBy('id', 'desc')
  44. ->paginate($page_size, ['*'], 'page', $page);
  45. return json_success($result);
  46. }
  47. /**
  48. * 新增
  49. * @param ValidatorFactoryInterface $validatorFactory
  50. * @return mixed
  51. * @example param_json_data string:
  52. * {"handshake":"","data":{"channel":"csdemo","remark":"备注","status":0}}
  53. */
  54. public function add(ValidatorFactoryInterface $validatorFactory)
  55. {
  56. $inputData = $this->getJsonData();
  57. $rules = [
  58. 'channel' => 'required|string',
  59. 'remark' => 'string',
  60. 'status' => 'required|in:0,1',
  61. ];
  62. $validator = $validatorFactory->make($inputData, $rules);
  63. if ($validator->fails()) {
  64. return json_fail($validator->errors()->first());
  65. }
  66. $channel = $inputData['channel'];
  67. $info = Model::query()
  68. ->where('channel', $channel)
  69. ->first();
  70. if ($info) {
  71. return json_fail('此渠道码已使用,请更换一个');
  72. }
  73. $url = self::getShareUrl().'?channel='.$channel;
  74. $data = [
  75. 'channel' => $channel,
  76. 'remark' => $inputData['remark'] ?? "",
  77. 'status' => $inputData['status'],
  78. 'url' => $url,
  79. ];
  80. $result = Model::query()
  81. ->create($data);
  82. $this->clearCache();
  83. // 创建管理员日志
  84. $newitem = clone $result;
  85. $logSubject = "新增渠道信息,渠道码:{$channel}";
  86. $logSvalue = $result->id;
  87. $logDbData = [
  88. 'after_items' => [
  89. $newitem->toArray(),
  90. ],
  91. 'before_items' => [],
  92. ];
  93. $this->log($logSubject, $logSvalue, $logDbData);
  94. return json_success($result);
  95. }
  96. /**
  97. * 内用-获取分享地址
  98. * @return string
  99. */
  100. private function getShareUrl()
  101. {
  102. $r = ConfigModel::query()
  103. ->where('skey', 'share_url')
  104. ->first();
  105. if (!$r) {
  106. return '';
  107. } else {
  108. return $r->svalue;
  109. }
  110. }
  111. /**
  112. * 编辑
  113. * @param ValidatorFactoryInterface $validatorFactory
  114. * @return mixed
  115. * @example param_json_data string: {"handshake":"","data":{"id":1,"channel":"csdemo","remark":"备注2","status":1}}
  116. */
  117. public function update(ValidatorFactoryInterface $validatorFactory)
  118. {
  119. $inputData = $this->getJsonData();
  120. $rules = [
  121. 'id' => 'required|int',
  122. 'channel' => 'required|string',
  123. 'remark' => 'string',
  124. 'status' => 'required|in:0,1',
  125. ];
  126. $validator = $validatorFactory->make($inputData, $rules);
  127. if ($validator->fails()) {
  128. return json_fail($validator->errors()->first());
  129. }
  130. $id = $inputData['id'];
  131. $channel = $inputData['channel'];
  132. $check = Model::query()
  133. ->where('channel', $channel)
  134. ->first();
  135. if ($check) {
  136. if ($check->id != $id) {
  137. return json_fail('此渠道码已使用,请更换一个');
  138. }
  139. }
  140. $info = Model::query()
  141. ->where('id', $id)
  142. ->first();
  143. if (!$info) {
  144. return json_fail('渠道信息不存在');
  145. }
  146. $url = self::getShareUrl().'?channel='.$channel;
  147. $data = [
  148. 'channel' => $channel,
  149. 'remark' => $inputData['remark'] ?? "",
  150. 'status' => $inputData['status'],
  151. 'url' => $url,
  152. ];
  153. $data = action_data($data);
  154. $result = Model::query()
  155. ->where('id', $inputData['id'])
  156. ->update($data);
  157. $this->clearCache();
  158. // 创建管理员日志
  159. $olditem = clone $info;
  160. $newitem = Model::query()
  161. ->where('id', $inputData['id'])
  162. ->first();
  163. $logSubject = "修改渠道信息,渠道ID:{$id}";
  164. $logSvalue = $olditem->id;
  165. $logDbData = [
  166. 'after_items' => [
  167. $newitem->toArray(),
  168. ],
  169. 'before_items' => [
  170. $olditem->toArray(),
  171. ],
  172. ];
  173. $this->log($logSubject, $logSvalue, $logDbData);
  174. return json_success($result);
  175. }
  176. /**
  177. * 删除 (真实删除数据)
  178. * @param ValidatorFactoryInterface $validatorFactory
  179. * @return mixed
  180. * @example param_json_data string: {"handshake":"","data":{"id":1}}
  181. */
  182. public function delete(ValidatorFactoryInterface $validatorFactory)
  183. {
  184. $inputData = $this->getJsonData();
  185. $rules = [
  186. 'id' => 'required|int',
  187. ];
  188. $validator = $validatorFactory->make($inputData, $rules);
  189. if ($validator->fails()) {
  190. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  191. return json_fail($validator->errors()->first());
  192. }
  193. $info = Model::query()
  194. ->where('id', $inputData['id'])
  195. ->first();
  196. if (!$info) {
  197. return json_fail('渠道信息不存在');
  198. }
  199. $result = Model::query()
  200. ->where('id', $inputData['id'])
  201. ->delete();
  202. if (!$result) {
  203. return json_fail('删除失败');
  204. }
  205. $this->clearCache();
  206. // 创建管理员日志
  207. $olditem = clone $info;
  208. $logSubject = "删除渠道信息";
  209. $logSvalue = $olditem->id;
  210. $logDbData = [
  211. 'after_items' => [
  212. ],
  213. 'before_items' => [
  214. $olditem->toArray(),
  215. ],
  216. ];
  217. $this->log($logSubject, $logSvalue, $logDbData);
  218. return json_success($result);
  219. }
  220. public function clearCache()
  221. {
  222. return make(CacheInterface::class)->delete('channel_list');
  223. }
  224. }