| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- * 渠道管理
- * @author xiabian
- */
- declare(strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Model\ChannelModel as Model;
- use App\Model\ConfigModel;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\SimpleCache\CacheInterface;
- class ChannelController extends BaseController
- {
- /**
- * 列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"channel":""}}}
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
- $select = ['*'];
- $model = Model::query();
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string)$v)) {
- continue;
- }
- switch ($k) {
- case "channel": // 渠道码
- $model->where($k, 'like', "{$v}%");
- break;
- default:
- $model->where($k, $v);
- }
- }
- }
- $result = $model->select($select)
- ->orderBy('id', 'desc')
- ->paginate($page_size, ['*'], 'page', $page);
- return json_success($result);
- }
- /**
- * 新增
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string:
- * {"handshake":"","data":{"channel":"csdemo","remark":"备注","status":0}}
- */
- public function add(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'channel' => 'required|string',
- 'remark' => 'string',
- 'status' => 'required|in:0,1',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $channel = $inputData['channel'];
- $info = Model::query()
- ->where('channel', $channel)
- ->first();
- if ($info) {
- return json_fail('此渠道码已使用,请更换一个');
- }
- $url = self::getShareUrl().'?channel='.$channel;
- $data = [
- 'channel' => $channel,
- 'remark' => $inputData['remark'] ?? "",
- 'status' => $inputData['status'],
- 'url' => $url,
- ];
- $result = Model::query()
- ->create($data);
- $this->clearCache();
- // 创建管理员日志
- $newitem = clone $result;
- $logSubject = "新增渠道信息,渠道码:{$channel}";
- $logSvalue = $result->id;
- $logDbData = [
- 'after_items' => [
- $newitem->toArray(),
- ],
- 'before_items' => [],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 内用-获取分享地址
- * @return string
- */
- private function getShareUrl()
- {
- $r = ConfigModel::query()
- ->where('skey', 'share_url')
- ->first();
- if (!$r) {
- return '';
- } else {
- return $r->svalue;
- }
- }
- /**
- * 编辑
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":1,"channel":"csdemo","remark":"备注2","status":1}}
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|int',
- 'channel' => 'required|string',
- 'remark' => 'string',
- 'status' => 'required|in:0,1',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- $channel = $inputData['channel'];
- $check = Model::query()
- ->where('channel', $channel)
- ->first();
- if ($check) {
- if ($check->id != $id) {
- return json_fail('此渠道码已使用,请更换一个');
- }
- }
- $info = Model::query()
- ->where('id', $id)
- ->first();
- if (!$info) {
- return json_fail('渠道信息不存在');
- }
- $url = self::getShareUrl().'?channel='.$channel;
- $data = [
- 'channel' => $channel,
- 'remark' => $inputData['remark'] ?? "",
- 'status' => $inputData['status'],
- 'url' => $url,
- ];
- $data = action_data($data);
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->update($data);
- $this->clearCache();
- // 创建管理员日志
- $olditem = clone $info;
- $newitem = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- $logSubject = "修改渠道信息,渠道ID:{$id}";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- $newitem->toArray(),
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 删除 (真实删除数据)
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":1}}
- */
- public function delete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|int',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- if (!$info) {
- return json_fail('渠道信息不存在');
- }
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->delete();
- if (!$result) {
- return json_fail('删除失败');
- }
- $this->clearCache();
- // 创建管理员日志
- $olditem = clone $info;
- $logSubject = "删除渠道信息";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- public function clearCache()
- {
- return make(CacheInterface::class)->delete('channel_list');
- }
- }
|