Message.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public function getType()
  109. {
  110. $list[0]['id']=0;
  111. $list[0]['name']='URL';
  112. $list[1]['id']=1;
  113. $list[1]['name']='商品列表';
  114. $list[2]['id']=2;
  115. $list[2]['name']='订单列表';
  116. $list[3]['id']=3;
  117. $list[3]['name']='商品详情';
  118. $list[4]['id']=4;
  119. $list[4]['name']='购物车';
  120. $list[5]['id']=5;
  121. $list[5]['name']='消息列表';
  122. $i=$this->request->request('keyValue');
  123. if ($i) {
  124. return json(['list' => $list[$i], 'total' => count($list[$i])]);
  125. }
  126. return json(['list' => $list, 'total' => count($list)]);
  127. }
  128. /**
  129. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  130. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  131. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  132. */
  133. }