CompanyComment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class CompanyComment extends Api
  6. {
  7. protected $noNeedLogin = [];
  8. protected $noNeedRight = '*';
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = Db::name('company_comment');
  14. }
  15. /**
  16. * 列表
  17. * @return void
  18. */
  19. public function getList()
  20. {
  21. try {
  22. $companyId = $this->request->param('company_id',0);
  23. $companyId = !empty($companyId) ? $companyId : $this->auth->company_id;
  24. $cc = 'company_comment';
  25. $u = 'user';
  26. $field = $cc.'.id,'.$cc.'.user_id,info,images,'.$cc.'.createtime,'.$u.'.avatar,'.$u.'.nickname';
  27. $where[$cc.'.company_id'] = $companyId;
  28. $result = $this->model->alias($cc)->field($field)
  29. ->join($u,$u.'.id = '.$cc.'.user_id','LEFT')
  30. ->where($where)->order($cc.'.createtime desc')->autopage()->select();
  31. if (!empty($result)) {
  32. foreach ($result as $key => &$value) {
  33. empty($value['info']) && $value['info'] = '此用户没有填写评价。';
  34. $value['createtime'] = !empty($value['createtime']) ? date('m月d日',$value['createtime']) : '';
  35. unset($value['user_id']);
  36. }
  37. $result = list_domain_image($result,['images','avatar']);
  38. }
  39. $this->success('获取成功',$result);
  40. } catch (Exception $e) {
  41. $this->error($e->getMessage());
  42. }
  43. }
  44. /**
  45. * 保存
  46. * @return void
  47. */
  48. public function save()
  49. {
  50. try {
  51. //用于上架审核提示
  52. $commentSwitch = config('site.comment_switch');
  53. if ($commentSwitch == 1) {
  54. $this->success('提交成功,请等待审核。');
  55. }
  56. //验证参数
  57. $id = $this->request->param('id',0);
  58. $companyId = $this->request->param('company_id',0);
  59. $companyId = !empty($companyId) ? $companyId : $this->auth->company_id;
  60. $info = $this->request->param('info','');
  61. $images = $this->request->param('images','');
  62. if (empty($info) && $images) {
  63. throw new Exception('参数有误');
  64. }
  65. $userId = $this->auth->id;
  66. $scene = !empty($id) ? 'edit' : 'add';
  67. $validate = validate('CompanyComment');
  68. if(!$validate->check($this->request->param(),[],$scene)){
  69. throw new Exception($validate->getError());
  70. }
  71. $time = time();
  72. $data = [
  73. 'info' => $info,
  74. 'images' => $images,
  75. ];
  76. $limitCreatetime = $time - 60;
  77. $commentWhere['user_id'] = $userId;
  78. $commentWhere['company_id'] = $companyId;
  79. $commentWhere['createtime'] = ['gt',$limitCreatetime];
  80. $commentData = $this->model->where($commentWhere)->find();
  81. if (!empty($commentData) && empty($id)) {
  82. throw new Exception('请不要频繁操作');
  83. }
  84. if (empty($id)) {
  85. $data['company_id'] = $companyId;
  86. $data['user_id'] = $userId;
  87. $data['createtime'] = $time;
  88. $res = $this->model->insertGetId($data);
  89. } else {
  90. $where['id'] = $id;
  91. $where['user_id'] = $userId;
  92. $res = $this->model->where($where)->update($data);
  93. }
  94. if (!$res) {
  95. throw new Exception('操作失败');
  96. }
  97. $this->success('操作成功');
  98. } catch (Exception $e) {
  99. $this->error($e->getMessage());
  100. }
  101. }
  102. }