Taskcomment.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 任务评论及基础
  4. // +----------------------------------------------------------------------
  5. // | Author: yykun
  6. // +----------------------------------------------------------------------
  7. namespace app\work\controller;
  8. use think\Db;
  9. use think\Request;
  10. use think\Hook;
  11. use app\admin\controller\ApiCommon;
  12. class Taskcomment extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['index','save','delete']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. /**
  34. * 添加评论
  35. *
  36. * @return \think\response\Json
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function save()
  42. {
  43. $param = $this->param;
  44. $userId = $this->userInfo['id'];
  45. if (empty($param['content'])) return resultArray(['error' => '缺少回复内容!']);
  46. if (empty($param['type'])) return resultArray(['error' => '缺少回复类型:任务/日志']);
  47. if (empty($param['type_id'])) return resultArray(['error' => '缺少回复数据ID']);
  48. $commentModel = new \app\admin\model\Comment();
  49. # 评论人ID
  50. $param['user_id'] = $userId;
  51. # 1:任务;2:日志
  52. $param['type'] = !empty($param['type']) && $param['type'] == 2 ? 'oa_log' : 'task';
  53. # 任务或日志的ID,取决于$param['type']的值
  54. $param['type_id'] = $param['type_id'];
  55. $flag = $commentModel->createData($param);
  56. if ($flag) {
  57. # 用户信息
  58. $userInfo = Db::name('admin_user')->field(['realname', 'thumb_img'])->where('id', $flag['user_id'])->find();
  59. $flag['userInfo'] = [
  60. 'user_id' => $flag['user_id'],
  61. 'realname' => $userInfo['realname'],
  62. 'thumb_img' => getFullPath($userInfo['thumb_img'])
  63. ];
  64. # 时间格式
  65. if (!empty($flag['create_time'])) $flag['create_time'] = date('Y-m-d H:i:s', $flag['create_time']);
  66. return resultArray(['data' => $flag]);
  67. } else {
  68. return resultArray(['error' => $commentModel->getError()]);
  69. }
  70. }
  71. /**
  72. * 删除评论
  73. * @author yykun
  74. * @return
  75. */
  76. public function delete()
  77. {
  78. $param = $this->param;
  79. $commentModel = new \app\admin\model\Comment();
  80. if (!$param['comment_id']) {
  81. return resultArray(['error'=>'参数错误']);
  82. }
  83. $userInfo = $this->userInfo;
  84. $param['create_user_id'] = $userInfo['id'];
  85. $flag = $commentModel->delDataById($param);
  86. if ( $flag ) {
  87. return resultArray(['data'=>'删除成功']);
  88. } else {
  89. return resultArray(['error'=>$commentModel->getError()]);
  90. }
  91. }
  92. }