Materiallibrary.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\admin\controller\send\message;
  3. use app\common\controller\Backend;
  4. use app\main\service\AdminService;
  5. use app\main\constants\AdminConstants;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. * 高级群发消息素材库
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Materiallibrary extends Backend
  14. {
  15. /**
  16. * SendMessageMaterialLibrary模型对象
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT)){
  23. $is_distributor = AdminService::instance()->checkAdminIsDistributor($this->auth->id);
  24. // 如果登录用户仅仅是代理商,不是配号代理商
  25. if (!$is_distributor) {
  26. $this->error('您没有访问权限');
  27. }
  28. }
  29. $this->model = model('SendMessageMaterialLibrary');
  30. $this->view->assign("messageTypeList", $this->model->getMessageTypeList());
  31. $this->view->assign("userGroupIds", $this->auth->getGroupIds());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags']);
  45. if ($this->request->isAjax())
  46. {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('pkey_name'))
  49. {
  50. return $this->selectpage();
  51. }
  52. //用户组
  53. // $whereauth = [];
  54. // if(
  55. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT) ||
  56. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL) ||
  57. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) ||
  58. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_OPERATOR)
  59. // ){
  60. // $whereauth['admin_id'] = ['eq',$this->auth->id];
  61. // }elseif(
  62. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP) ||
  63. // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR)
  64. // ){
  65. // $wheretmp['flag'] = ['eq',1];
  66. // $wheretmp['admin_id_master'] = ['eq',$this->auth->id];
  67. // $vab_ids = model('vip_admin_bind')->where($wheretmp)->column('admin_id_slave');
  68. // if(count($vab_ids) == 1){
  69. // $whereauth['admin_id'] = ['eq',$vab_ids[0]];
  70. // }else{
  71. // $whereauth['admin_id'] = ['in',implode(',',$vab_ids)];
  72. // }
  73. // }
  74. $whereauth['admin_id'] = ['eq',$this->auth->id];
  75. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  76. $total = $this->model
  77. ->where($where)
  78. ->where($whereauth)
  79. ->order($sort, $order)
  80. ->count();
  81. $list = $this->model
  82. ->where($where)
  83. ->where($whereauth)
  84. ->order($sort, $order)
  85. ->limit($offset, $limit)
  86. ->select();
  87. $result = array("total" => $total, "rows" => $list);
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 添加
  94. */
  95. public function add()
  96. {
  97. $time = time();
  98. if ($this->request->isPost())
  99. {
  100. $params = $this->request->post("row/a");
  101. if ($params)
  102. {
  103. try
  104. {
  105. $this->_submitFormVerify($params);
  106. if(isset($params['test_message'])){
  107. unset($params['test_message']);
  108. }
  109. if(isset($params['test_channel_id'])){
  110. unset($params['test_channel_id']);
  111. }
  112. $params['material_status'] = 1;
  113. $params['createtime'] = $time;
  114. $params['updatetime'] = $time;
  115. $params['admin_id'] = $this->auth->id;
  116. $result = $this->model->allowField(true)->save($params);
  117. if ($result !== false)
  118. {
  119. $this->success();
  120. }
  121. else
  122. {
  123. $this->error($this->model->getError());
  124. }
  125. }
  126. catch (\think\exception\PDOException $e)
  127. {
  128. $this->error($e->getMessage());
  129. }
  130. }
  131. $this->error(__('Parameter %s can not be empty', ''));
  132. }
  133. return $this->view->fetch();
  134. }
  135. public function do_status(){
  136. $params = $this->request->param();
  137. $ids = $params['ids'];
  138. if(is_array($ids)){
  139. foreach($ids as $id){
  140. $result = $this->model->where('id','eq',$id)->delete();
  141. }
  142. }else{
  143. $result = $this->model->where('id','eq',$ids)->delete();
  144. }
  145. if($result){
  146. $this->success('删除成功');
  147. }else{
  148. $this->error('删除失败');
  149. }
  150. }
  151. /**
  152. * 编辑
  153. */
  154. public function edit($ids = NULL)
  155. {
  156. $row = $this->model->get($ids);
  157. if (!$row)
  158. $this->error(__('No Results were found'));
  159. $adminIds = $this->getDataLimitAdminIds();
  160. if (is_array($adminIds))
  161. {
  162. if (!in_array($row[$this->dataLimitField], $adminIds))
  163. {
  164. $this->error(__('You have no permission'));
  165. }
  166. }
  167. if ($this->request->isPost())
  168. {
  169. $params = $this->request->post("row/a");
  170. if ($params)
  171. {
  172. try
  173. {
  174. $this->_submitFormVerify($params);
  175. if(isset($params['test_message'])){
  176. unset($params['test_message']);
  177. }
  178. if(isset($params['test_channel_id'])){
  179. unset($params['test_channel_id']);
  180. }
  181. $params['updatetime'] = time();
  182. $params['admin_id'] = $this->auth->id;
  183. $result = $row->allowField(true)->save($params);
  184. if ($result !== false)
  185. {
  186. $this->success();
  187. }
  188. else
  189. {
  190. $this->error($row->getError());
  191. }
  192. }
  193. catch (\think\exception\PDOException $e)
  194. {
  195. $this->error($e->getMessage());
  196. }
  197. }
  198. $this->error(__('Parameter %s can not be empty', ''));
  199. }
  200. $row['material_content_arr'] = json_decode($row['material_content'],true);
  201. $this->view->assign("row", $row);
  202. return $this->view->fetch();
  203. }
  204. /**
  205. * 消息预览
  206. */
  207. public function message_preview($ids = NULL){
  208. $row = $this->model->get($ids);
  209. if (!$row){
  210. $this->error(__('No Results were found'));
  211. }
  212. $adminIds = $this->getDataLimitAdminIds();
  213. if (is_array($adminIds))
  214. {
  215. if (!in_array($row[$this->dataLimitField], $adminIds))
  216. {
  217. $this->error(__('You have no permission'));
  218. }
  219. }
  220. $row['message_type'] = $row['material_type'];
  221. $row['message_content_arr'] = json_decode($row['material_content'],true);
  222. $this->view->assign("row", $row);
  223. return $this->view->fetch();
  224. }
  225. private function _submitFormVerify($params){
  226. if(empty($params['material_name'])){
  227. $this->error('推送消息名称不能为空');
  228. }
  229. if(empty($params['material_type'])){
  230. $this->error('公众号类型不能为空');
  231. }
  232. if(empty($params['material_content']) || $params['material_content']=='[]'){
  233. $this->error('消息内容不能为空');
  234. }
  235. }
  236. }