Manuscript.php 12 KB

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