Manuscript.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\Service;
  4. use app\admin\model\cms\Comments;
  5. use app\admin\model\EmailContent;
  6. use app\admin\model\User;
  7. use app\common\controller\Backend;
  8. use app\common\library\Email;
  9. use think\Db;
  10. use think\Exception;
  11. use think\exception\PDOException;
  12. use think\exception\ValidateException;
  13. /**
  14. * 手稿
  15. *
  16. * @icon fa fa-th
  17. */
  18. class Manuscript extends Backend
  19. {
  20. /**
  21. * Model模型对象
  22. */
  23. protected $model = null;
  24. protected $searchFields = '';
  25. protected $noNeedRight = ['add_editor', 'comments', 'comments_detail', 'send_user_list'];
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\admin\model\cms\AuthorManuscript;
  30. $site_manuscript_status = config('site.manuscript_status');
  31. $this->assignconfig('site_manuscript_status', $site_manuscript_status);
  32. $site_type_list = $this->model->typeList();
  33. $this->assignconfig('site_type_list', $site_manuscript_status);
  34. }
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model->with(['user'])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. $result = array("total" => $list->total(), "rows" => $list->items());
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. public function edit($ids = null)
  55. {
  56. $row = $this->model->get($ids);
  57. if (!$row) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $row->author_content = json_decode($row->author_content, true);
  61. $row->review_content = json_decode($row->review_content, true);
  62. $adminIds = $this->getDataLimitAdminIds();
  63. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  64. $this->error(__('You have no permission'));
  65. }
  66. if (false === $this->request->isPost()) {
  67. $this->view->assign('row', $row);
  68. return $this->view->fetch();
  69. }
  70. $params = $this->request->post('row/a');
  71. if (empty($params)) {
  72. $this->error(__('Parameter %s can not be empty', ''));
  73. }
  74. $params = $this->preExcludeFields($params);
  75. $result = false;
  76. Db::startTrans();
  77. try {
  78. //是否采用模型验证
  79. if ($this->modelValidate) {
  80. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  81. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  82. $row->validateFailException()->validate($validate);
  83. }
  84. $result = $row->allowField(true)->save($params);
  85. Db::commit();
  86. } catch (ValidateException|PDOException|Exception $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. }
  90. if (false === $result) {
  91. $this->error(__('No rows were updated'));
  92. }
  93. $this->success();
  94. }
  95. /**
  96. * 添加编辑
  97. *
  98. * @return string|void
  99. * @throws Exception
  100. */
  101. public function add_editor($ids = null)
  102. {
  103. $row = $this->model->get($ids);
  104. // 获取编辑信息
  105. $editor_list = User::where(['is_editor' => 'correct'])->column('nickname', 'id');
  106. // 获取编辑状态
  107. $status_list = config('site.edit_manuscript_status');
  108. // 模板
  109. if (false === $this->request->isPost()) {
  110. $this->view->assign('row', $row);
  111. $this->view->assign('status_list', $status_list);
  112. $this->view->assign('editor_list', $editor_list);
  113. return $this->view->fetch();
  114. }
  115. $params = $this->request->post('row/a');
  116. if (empty($params)) {
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. $params = $this->preExcludeFields($params);
  120. $result = false;
  121. Db::startTrans();
  122. try {
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  127. $row->validateFailException()->validate($validate);
  128. }
  129. $params['editor_ids'] = implode(',', $params['editor_ids']);
  130. $result = $row->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException|PDOException|Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. if (false === $result) {
  137. $this->error(__('No rows were updated'));
  138. }
  139. $this->success();
  140. }
  141. /**
  142. * 查看对应手稿意见
  143. *
  144. * @param $ids
  145. * @return string|\think\response\Json
  146. * @throws Exception
  147. */
  148. public function comments($ids = null)
  149. {
  150. //设置过滤方法
  151. $this->request->filter(['strip_tags', 'trim']);
  152. if ($this->request->isAjax()) {
  153. //如果发送的来源是Selectpage,则转发到Selectpage
  154. if ($this->request->request('keyField')) {
  155. return $this->selectpage();
  156. }
  157. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  158. $model = new Comments();
  159. $list = $model->with(['user', 'manuscript'])
  160. ->where(['manuscript_id' => $ids])
  161. ->where($where)
  162. ->order($sort, $order)
  163. ->paginate($limit);
  164. $result = array("total" => $list->total(), "rows" => $list->items());
  165. return json($result);
  166. }
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 意见详情
  171. *
  172. * @param $ids
  173. * @return string|void
  174. * @throws Exception
  175. */
  176. public function comments_detail($ids = null)
  177. {
  178. $model = new Comments();
  179. $row = $model->get($ids);
  180. // 模板
  181. if (false === $this->request->isPost()) {
  182. $this->view->assign('row', $row);
  183. return $this->view->fetch();
  184. }
  185. }
  186. /**
  187. * 发送邮件
  188. *
  189. * @param $ids
  190. * @return string
  191. * @throws \think\Exception
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public function email($ids = null)
  197. {
  198. $row = $this->model->get($ids);
  199. $typeList = ['author' => __('Author'), 'editor' => __('Editor'), 'reviewer' => __('Reviewer')];
  200. if ($this->request->isPost()) {
  201. $params = $this->request->post("row/a");
  202. $user = User::where(['email' => $params['send_user_email']])->find();
  203. if (empty($user)) $this->error(__('No corresponding user found'));
  204. $option = [
  205. 'mail_smtp_host' => $this->auth->mail_smtp_host,
  206. 'mail_smtp_port' => $this->auth->mail_smtp_port,
  207. 'mail_smtp_user' => $this->auth->mail_smtp_user,
  208. 'mail_smtp_pass' => $this->auth->mail_smtp_pass,
  209. 'mail_from' => $this->auth->mail_smtp_user,
  210. ];
  211. $email = new Email($option);
  212. $result = $email
  213. ->to($user->email)
  214. ->subject($row->title)
  215. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . $params['send_content'] . '</div>')
  216. ->send();
  217. if ($result) {
  218. $email_content_model = new EmailContent();
  219. // 数据库记录发送信息
  220. Db::startTrans();
  221. try {
  222. $email_content_param['user_id'] = $this->auth->id;
  223. $email_content_param['email'] = $params['send_user_email'];
  224. $email_content_param['content'] = $params['send_content'];
  225. $model_result = $email_content_model->allowField(true)->save($email_content_param);
  226. Db::commit();
  227. } catch (ValidateException|PDOException|Exception $e) {
  228. Db::rollback();
  229. $this->error($e->getMessage());
  230. }
  231. if ($model_result) {
  232. $this->success();
  233. }
  234. } else {
  235. $this->error(__('Sending failed. Please check whether the SMTP settings are correct'));
  236. }
  237. }
  238. $this->view->assign('row', $row);
  239. $this->view->assign('typeList', $typeList);
  240. return $this->view->fetch();
  241. }
  242. /**
  243. * 对应手稿内相关人员
  244. *
  245. * @return \think\response\Json
  246. * @throws \think\exception\DbException
  247. */
  248. public function send_user_list()
  249. {
  250. $manuscript_id = $this->request->request('manuscript_id');
  251. $sendUserList = [];
  252. $row = $this->model->get($manuscript_id);
  253. if (!$row) {
  254. $this->error(__('No Results were found'));
  255. }
  256. $email_arr = [];
  257. // 整合作者邮箱
  258. $author_email_arr = json_decode($row['author_content'], true);
  259. foreach ($author_email_arr as $author_email) {
  260. $email_arr[] = $author_email['email'];
  261. }
  262. // 整合编辑邮箱
  263. $editor_email_arr = User::where(['id' => ['in', explode(',', $row['editor_ids'])]])->column('email');
  264. foreach ($editor_email_arr as $editor_email) {
  265. $email_arr[] = $editor_email;
  266. }
  267. // 整合审稿人邮箱
  268. $reviewer_email_arr = User::where(['id' => ['in', explode(',', $row['editor_ids'])]])->column('email');
  269. foreach ($reviewer_email_arr as $reviewer_email) {
  270. $email_arr[] = $reviewer_email;
  271. }
  272. // 去重数组
  273. $unique_email_arr = array_filter(array_unique($email_arr));
  274. foreach ($unique_email_arr as $key => $item) {
  275. $sendUserList[] = [
  276. 'id' => $item,
  277. 'name' => $item,
  278. ];
  279. }
  280. $result = array("total" => count($sendUserList), "rows" => $sendUserList);
  281. return json($result);
  282. }
  283. }