Channelfanstransfer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Channelfanstransfer extends Backend
  13. {
  14. /**
  15. * ChannelFansTransfer模型对象
  16. * @var \app\admin\model\ChannelFansTransfer
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('ChannelFansTransfer');
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 添加
  31. */
  32. public function add()
  33. {
  34. if ($this->request->isPost())
  35. {
  36. $params = $this->request->post("row/a");
  37. if ($params)
  38. {
  39. //查询是否有相同的条目
  40. $wherer['send_channel_id'] = ['eq',$params['send_channel_id']];
  41. $wherer['receive_channel_id'] = ['eq',$params['receive_channel_id']];
  42. $wherer['status'] = ['eq',1];
  43. if($row = $this->model->where($wherer)->find()){
  44. $this->error('有重复数据!');
  45. }
  46. /*
  47. * 已经弃用,如果为了兼容老版可取消注释
  48. foreach ($params as $k => &$v)
  49. {
  50. $v = is_array($v) ? implode(',', $v) : $v;
  51. }
  52. */
  53. if ($this->dataLimit)
  54. {
  55. $params[$this->dataLimitField] = $this->auth->id;
  56. }
  57. try
  58. {
  59. //是否采用模型验证
  60. if ($this->modelValidate)
  61. {
  62. $name = basename(str_replace('\\', '/', get_class($this->model)));
  63. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  64. $this->model->validate($validate);
  65. }
  66. $params['endtime'] = strtotime(date('Y-m-d H:i:s',time())."+5 day");
  67. $result = $this->model->allowField(true)->save($params);
  68. if ($result !== false)
  69. {
  70. $this->addCache($params);
  71. $this->success();
  72. }
  73. else
  74. {
  75. $this->error($this->model->getError());
  76. }
  77. }
  78. catch (\think\exception\PDOException $e)
  79. {
  80. $this->error($e->getMessage());
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 切换状态
  89. */
  90. public function switchstatus(){
  91. $params = $this->request->post();
  92. $data['status'] = $params['status'];
  93. $row = $this->model->where('id','in',$params['ids'])->select();
  94. if(empty($row)){
  95. $this->error(__('No Results were found'));
  96. }
  97. $result = $this->model->where('id','in',$params['ids'])->update($data);
  98. if ($result !== false)
  99. {
  100. foreach ($row as $item) {
  101. $item = $item->toArray();
  102. if ($data['status'] == 1) {
  103. $this->addCache($item);
  104. } else {
  105. $this->delCache($item['receive_channel_id']);
  106. }
  107. }
  108. $this->success();
  109. }
  110. else
  111. {
  112. $this->error($row->getError());
  113. }
  114. }
  115. /**
  116. * 新增缓存
  117. * @param $data
  118. */
  119. private function addCache($data){
  120. $redisKey = 'STU:'.$data['receive_channel_id'];
  121. Redis::instance()->hMSet($redisKey,$data);
  122. Redis::instance()->expire($redisKey,$data['endtime']-time());
  123. }
  124. /**
  125. * 删除缓存
  126. * @param $channel_id
  127. */
  128. private function delCache($channel_id){
  129. $redisKey = 'STU:'.$channel_id;
  130. Redis::instance()->del($redisKey);
  131. }
  132. }