Message.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\admin\model\Template;
  5. use app\admin\model\UserGroup;
  6. use Exception;
  7. use think\Db;
  8. use think\exception\DbException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. use think\response\Json;
  12. /**
  13. *
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Message extends Backend
  18. {
  19. /**
  20. * Message模型对象
  21. * @var \app\admin\model\Message
  22. */
  23. protected $model = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\Message;
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. }
  30. /**
  31. * 查看
  32. *
  33. * @return string|Json
  34. * @throws \think\Exception
  35. * @throws DbException
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if (false === $this->request->isAjax()) {
  42. return $this->view->fetch();
  43. }
  44. //如果发送的来源是 Selectpage,则转发到 Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  49. $list = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $v) {
  54. $wheres['message_id']=$v->id;
  55. $count = Db('message_read')->where($wheres)->count();
  56. $v['counts'] = $count;
  57. }
  58. $result = ['total' => $list->total(), 'rows' => $list->items()];
  59. return json($result);
  60. }
  61. /**
  62. * 添加
  63. *
  64. * @return string
  65. * @throws \think\Exception
  66. */
  67. public function add()
  68. {
  69. if (false === $this->request->isPost()) {
  70. return $this->view->fetch();
  71. }
  72. $params = $this->request->post('row/a');
  73. $template_id = $params['template_id'];
  74. $template= Template::getById($template_id);
  75. $params['template_name']=$template->name;
  76. $params['template_dsc']=$template->description;
  77. $group_id =$params['group_id'];
  78. $group= UserGroup::getById($group_id);
  79. $params['template_image']=$template->image;
  80. $params['group_name']=$group->name;
  81. if (empty($params)) {
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. $params = $this->preExcludeFields($params);
  85. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  86. $params[$this->dataLimitField] = $this->auth->id;
  87. }
  88. $result = false;
  89. Db::startTrans();
  90. try {
  91. //是否采用模型验证
  92. if ($this->modelValidate) {
  93. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  95. $this->model->validateFailException()->validate($validate);
  96. }
  97. $result = $this->model->allowField(true)->save($params);
  98. Db::commit();
  99. } catch (ValidateException|PDOException|Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result === false) {
  104. $this->error(__('No rows were inserted'));
  105. }
  106. $this->success();
  107. }
  108. /**
  109. * 编辑
  110. *
  111. * @param $ids
  112. * @return string
  113. * @throws DbException
  114. * @throws \think\Exception
  115. */
  116. public function edit($ids = null)
  117. {
  118. $row = $this->model->get($ids);
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. $adminIds = $this->getDataLimitAdminIds();
  123. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  124. $this->error(__('You have no permission'));
  125. }
  126. if (false === $this->request->isPost()) {
  127. $this->view->assign('row', $row);
  128. return $this->view->fetch();
  129. }
  130. $params = $this->request->post('row/a');
  131. if (empty($params)) {
  132. $this->error(__('Parameter %s can not be empty', ''));
  133. }
  134. if($row->status==2){
  135. $this->error(__('该消息已经发送,不能修改!'));
  136. }
  137. $params = $this->preExcludeFields($params);
  138. $template_id = $params['template_id'];
  139. $template= Template::getById($template_id);
  140. $params['template_name']=$template->name;
  141. $params['template_dsc']=$template->description;
  142. $group_id =$params['group_id'];
  143. $group= UserGroup::getById($group_id);
  144. $params['template_image']=$template->image;
  145. $params['group_name']=$group->name;
  146. $result = false;
  147. Db::startTrans();
  148. try {
  149. //是否采用模型验证
  150. if ($this->modelValidate) {
  151. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  152. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  153. $row->validateFailException()->validate($validate);
  154. }
  155. $result = $row->allowField(true)->save($params);
  156. Db::commit();
  157. } catch (ValidateException|PDOException|Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. if (false === $result) {
  162. $this->error(__('No rows were updated'));
  163. }
  164. $this->success();
  165. }
  166. public function getType()
  167. {
  168. $list[0]['id']=0;
  169. $list[0]['name']='URL';
  170. $list[1]['id']=1;
  171. $list[1]['name']='商品列表';
  172. $list[2]['id']=2;
  173. $list[2]['name']='订单列表';
  174. $list[3]['id']=3;
  175. $list[3]['name']='商品详情';
  176. $list[4]['id']=4;
  177. $list[4]['name']='购物车';
  178. $list[5]['id']=5;
  179. $list[5]['name']='消息列表';
  180. $list[6]['id']=6;
  181. $list[6]['name']='explore首页';
  182. $list[7]['id']=7;
  183. $list[7]['name']='explore详情';
  184. $list[8]['id']=8;
  185. $list[8]['name']='我的积分';
  186. $list[9]['id']=9;
  187. $list[9]['name']='koc';
  188. $list[10]['id']=10;
  189. $list[10]['name']='vip Center';
  190. $list[11]['id']=11;
  191. $list[11]['name']='我的优惠券';
  192. $list[12]['id']=12;
  193. $list[12]['name']='登录注册';
  194. $list[13]['id']=13;
  195. $list[13]['name']='签到';
  196. $list[14]['id']=14;
  197. $list[14]['name']='首页';
  198. $i=$this->request->request('keyValue');
  199. if ($i) {
  200. return json(['list' => $list[$i], 'total' => count($list[$i])]);
  201. }
  202. return json(['list' => $list, 'total' => count($list)]);
  203. }
  204. /**
  205. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  206. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  207. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  208. */
  209. }