Modules_Manage_Controllers_ChannelController.proxy.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. use \Hyperf\Di\Aop\ProxyTrait;
  15. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  16. function __construct()
  17. {
  18. if (method_exists(parent::class, '__construct')) {
  19. parent::__construct(...func_get_args());
  20. }
  21. $this->__handlePropertyHandler(__CLASS__);
  22. }
  23. /**
  24. * 列表
  25. * @param ValidatorFactoryInterface $validatorFactory
  26. * @return mixed
  27. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"channel":""}}}
  28. */
  29. public function lists(ValidatorFactoryInterface $validatorFactory)
  30. {
  31. $inputData = $this->getJsonData();
  32. $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
  33. $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
  34. $select = ['*'];
  35. $model = Model::query();
  36. $search = isset($inputData['search']) ? $inputData['search'] : array();
  37. if ($search) {
  38. foreach ($search as $k => $v) {
  39. if (!strlen((string) $v)) {
  40. continue;
  41. }
  42. switch ($k) {
  43. case "channel":
  44. // 渠道码
  45. $model->where($k, 'like', "{$v}%");
  46. break;
  47. default:
  48. $model->where($k, $v);
  49. }
  50. }
  51. }
  52. $result = $model->select($select)->orderBy('id', 'desc')->paginate($page_size, ['*'], 'page', $page);
  53. return json_success($result);
  54. }
  55. /**
  56. * 新增
  57. * @param ValidatorFactoryInterface $validatorFactory
  58. * @return mixed
  59. * @example param_json_data string:
  60. * {"handshake":"","data":{"channel":"csdemo","remark":"备注","status":0}}
  61. */
  62. public function add(ValidatorFactoryInterface $validatorFactory)
  63. {
  64. $inputData = $this->getJsonData();
  65. $rules = ['channel' => 'required|string', 'remark' => 'string', 'status' => 'required|in:0,1'];
  66. $validator = $validatorFactory->make($inputData, $rules);
  67. if ($validator->fails()) {
  68. return json_fail($validator->errors()->first());
  69. }
  70. $channel = $inputData['channel'];
  71. $info = Model::query()->where('channel', $channel)->first();
  72. if ($info) {
  73. return json_fail('此渠道码已使用,请更换一个');
  74. }
  75. $url = self::getShareUrl() . '?channel=' . $channel;
  76. $data = ['channel' => $channel, 'remark' => $inputData['remark'] ?? "", 'status' => $inputData['status'], 'url' => $url];
  77. $result = Model::query()->create($data);
  78. $this->clearCache();
  79. // 创建管理员日志
  80. $newitem = clone $result;
  81. $logSubject = "新增渠道信息,渠道码:{$channel}";
  82. $logSvalue = $result->id;
  83. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => []];
  84. $this->log($logSubject, $logSvalue, $logDbData);
  85. return json_success($result);
  86. }
  87. /**
  88. * 内用-获取分享地址
  89. * @return string
  90. */
  91. private function getShareUrl()
  92. {
  93. $r = ConfigModel::query()->where('skey', 'share_url')->first();
  94. if (!$r) {
  95. return '';
  96. } else {
  97. return $r->svalue;
  98. }
  99. }
  100. /**
  101. * 编辑
  102. * @param ValidatorFactoryInterface $validatorFactory
  103. * @return mixed
  104. * @example param_json_data string: {"handshake":"","data":{"id":1,"channel":"csdemo","remark":"备注2","status":1}}
  105. */
  106. public function update(ValidatorFactoryInterface $validatorFactory)
  107. {
  108. $inputData = $this->getJsonData();
  109. $rules = ['id' => 'required|int', 'channel' => 'required|string', 'remark' => 'string', 'status' => 'required|in:0,1'];
  110. $validator = $validatorFactory->make($inputData, $rules);
  111. if ($validator->fails()) {
  112. return json_fail($validator->errors()->first());
  113. }
  114. $id = $inputData['id'];
  115. $channel = $inputData['channel'];
  116. $check = Model::query()->where('channel', $channel)->first();
  117. if ($check) {
  118. if ($check->id != $id) {
  119. return json_fail('此渠道码已使用,请更换一个');
  120. }
  121. }
  122. $info = Model::query()->where('id', $id)->first();
  123. if (!$info) {
  124. return json_fail('渠道信息不存在');
  125. }
  126. $url = self::getShareUrl() . '?channel=' . $channel;
  127. $data = ['channel' => $channel, 'remark' => $inputData['remark'] ?? "", 'status' => $inputData['status'], 'url' => $url];
  128. $data = action_data($data);
  129. $result = Model::query()->where('id', $inputData['id'])->update($data);
  130. $this->clearCache();
  131. // 创建管理员日志
  132. $olditem = clone $info;
  133. $newitem = Model::query()->where('id', $inputData['id'])->first();
  134. $logSubject = "修改渠道信息,渠道ID:{$id}";
  135. $logSvalue = $olditem->id;
  136. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$olditem->toArray()]];
  137. $this->log($logSubject, $logSvalue, $logDbData);
  138. return json_success($result);
  139. }
  140. /**
  141. * 删除 (真实删除数据)
  142. * @param ValidatorFactoryInterface $validatorFactory
  143. * @return mixed
  144. * @example param_json_data string: {"handshake":"","data":{"id":1}}
  145. */
  146. public function delete(ValidatorFactoryInterface $validatorFactory)
  147. {
  148. $inputData = $this->getJsonData();
  149. $rules = ['id' => 'required|int'];
  150. $validator = $validatorFactory->make($inputData, $rules);
  151. if ($validator->fails()) {
  152. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  153. return json_fail($validator->errors()->first());
  154. }
  155. $info = Model::query()->where('id', $inputData['id'])->first();
  156. if (!$info) {
  157. return json_fail('渠道信息不存在');
  158. }
  159. $result = Model::query()->where('id', $inputData['id'])->delete();
  160. if (!$result) {
  161. return json_fail('删除失败');
  162. }
  163. $this->clearCache();
  164. // 创建管理员日志
  165. $olditem = clone $info;
  166. $logSubject = "删除渠道信息";
  167. $logSvalue = $olditem->id;
  168. $logDbData = ['after_items' => [], 'before_items' => [$olditem->toArray()]];
  169. $this->log($logSubject, $logSvalue, $logDbData);
  170. return json_success($result);
  171. }
  172. public function clearCache()
  173. {
  174. return make(CacheInterface::class)->delete('channel_list');
  175. }
  176. }