Manuscript.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 = ['edit_chief', 'comments', 'comments_detail', 'send_user_list', 'email'];
  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 edit_chief($ids = null)
  102. {
  103. $row = $this->model->get($ids);
  104. // 获取编辑信息
  105. $chief_list = User::where(['is_chief' => '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('chief_list', $chief_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. $result = $row->allowField(true)->save($params);
  130. // 添加手稿状态日志
  131. $log = new \app\admin\model\cms\ManuscriptLog();
  132. $log->manuscript_id = $ids;
  133. $log->user_id = $this->auth->id;
  134. $log->type = 'admin';
  135. $log->status = $params['status'];
  136. $log->save();
  137. Db::commit();
  138. } catch (ValidateException|PDOException|Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if (false === $result) {
  143. $this->error(__('No rows were updated'));
  144. }
  145. $this->success();
  146. }
  147. /**
  148. * 查看对应手稿意见
  149. *
  150. * @param $ids
  151. * @return string|\think\response\Json
  152. * @throws Exception
  153. */
  154. public function comments($ids = null)
  155. {
  156. //设置过滤方法
  157. $this->request->filter(['strip_tags', 'trim']);
  158. if ($this->request->isAjax()) {
  159. //如果发送的来源是Selectpage,则转发到Selectpage
  160. if ($this->request->request('keyField')) {
  161. return $this->selectpage();
  162. }
  163. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  164. $model = new Comments();
  165. $list = $model->with(['user', 'manuscript'])
  166. ->where(['manuscript_id' => $ids])
  167. ->where($where)
  168. ->order($sort, $order)
  169. ->paginate($limit);
  170. $result = array("total" => $list->total(), "rows" => $list->items());
  171. return json($result);
  172. }
  173. return $this->view->fetch();
  174. }
  175. /**
  176. * 意见详情
  177. *
  178. * @param $ids
  179. * @return string|void
  180. * @throws Exception
  181. */
  182. public function comments_detail($ids = null)
  183. {
  184. $model = new Comments();
  185. $row = $model->get($ids);
  186. // 模板
  187. if (false === $this->request->isPost()) {
  188. $this->view->assign('row', $row);
  189. return $this->view->fetch();
  190. }
  191. }
  192. /**
  193. * 发送邮件
  194. *
  195. * @param $ids
  196. * @return string
  197. * @throws \think\Exception
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. * @throws \think\exception\DbException
  201. */
  202. public function email($ids = null)
  203. {
  204. $row = $this->model->get($ids);
  205. $typeList = ['author' => __('Author'), 'editor' => __('Editor'), 'reviewer' => __('Reviewer')];
  206. if ($this->request->isPost()) {
  207. $params = $this->request->post("row/a");
  208. $user = User::where(['email' => $params['send_user_email']])->find();
  209. if (empty($user)) $this->error(__('No corresponding user found'));
  210. $option = [
  211. 'mail_smtp_host' => $this->auth->mail_smtp_host,
  212. 'mail_smtp_port' => $this->auth->mail_smtp_port,
  213. 'mail_smtp_user' => $this->auth->mail_smtp_user,
  214. 'mail_smtp_pass' => $this->auth->mail_smtp_pass,
  215. 'mail_from' => $this->auth->mail_smtp_user,
  216. ];
  217. $email = new Email($option);
  218. $result = $email
  219. ->to($user->email)
  220. ->subject($row->title)
  221. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . $params['send_content'] . '</div>')
  222. ->send();
  223. if ($result) {
  224. $email_content_model = new EmailContent();
  225. // 数据库记录发送信息
  226. Db::startTrans();
  227. try {
  228. $email_content_param['user_id'] = $this->auth->id;
  229. $receiver_user = User::where(['email' => $params['send_user_email']])->find();
  230. $email_content_param['receiver_id'] = $receiver_user['id'];
  231. $email_content_param['email'] = $params['send_user_email'];
  232. $email_content_param['content'] = $params['send_content'];
  233. $model_result = $email_content_model->allowField(true)->save($email_content_param);
  234. Db::commit();
  235. } catch (ValidateException|PDOException|Exception $e) {
  236. Db::rollback();
  237. $this->error($e->getMessage());
  238. }
  239. if ($model_result) {
  240. $this->success();
  241. }
  242. } else {
  243. $this->error(__('Sending failed. Please check whether the SMTP settings are correct'));
  244. }
  245. }
  246. $this->view->assign('row', $row);
  247. $this->view->assign('typeList', $typeList);
  248. return $this->view->fetch();
  249. }
  250. /**
  251. * 对应手稿内相关人员
  252. *
  253. * @return \think\response\Json
  254. * @throws \think\exception\DbException
  255. */
  256. public function send_user_list()
  257. {
  258. $manuscript_id = $this->request->request('manuscript_id');
  259. $sendUserList = [];
  260. $row = $this->model->get($manuscript_id);
  261. if (!$row) {
  262. $this->error(__('No Results were found'));
  263. }
  264. $email_arr = [];
  265. // 整合作者邮箱
  266. $author_email_arr = json_decode($row['author_content'], true);
  267. foreach ($author_email_arr as $author_email) {
  268. $email_arr[] = $author_email['email'];
  269. }
  270. // 整合编辑邮箱
  271. $editor_email_arr = User::where(['id' => ['in', explode(',', $row['editor_ids'])]])->column('email');
  272. foreach ($editor_email_arr as $editor_email) {
  273. $email_arr[] = $editor_email;
  274. }
  275. // 整合审稿人邮箱
  276. $reviewer_email_arr = User::where(['id' => ['in', explode(',', $row['editor_ids'])]])->column('email');
  277. foreach ($reviewer_email_arr as $reviewer_email) {
  278. $email_arr[] = $reviewer_email;
  279. }
  280. // 去重数组
  281. $unique_email_arr = array_filter(array_unique($email_arr));
  282. foreach ($unique_email_arr as $key => $item) {
  283. $sendUserList[] = [
  284. 'id' => $item,
  285. 'name' => $item,
  286. ];
  287. }
  288. $result = array("total" => count($sendUserList), "rows" => $sendUserList);
  289. return json($result);
  290. }
  291. }