CompanyComment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $id = $this->request->param('id',0);
  53. $companyId = $this->request->param('company_id',0);
  54. $companyId = !empty($companyId) ? $companyId : $this->auth->company_id;
  55. $info = $this->request->param('info','');
  56. $images = $this->request->param('images','');
  57. if (empty($info) && $images) {
  58. throw new Exception('参数有误');
  59. }
  60. $userId = $this->auth->id;
  61. $scene = !empty($id) ? 'edit' : 'add';
  62. $validate = validate('CompanyComment');
  63. if(!$validate->check($this->request->param(),[],$scene)){
  64. throw new Exception($validate->getError());
  65. }
  66. $time = time();
  67. $data = [
  68. 'info' => $info,
  69. 'images' => $images,
  70. ];
  71. $limitCreatetime = $time - 60;
  72. $commentWhere['user_id'] = $userId;
  73. $commentWhere['company_id'] = $companyId;
  74. $commentWhere['createtime'] = ['gt',$limitCreatetime];
  75. $commentData = $this->model->where($commentWhere)->find();
  76. if (!empty($commentData) && empty($id)) {
  77. throw new Exception('请不要频繁操作');
  78. }
  79. if (empty($id)) {
  80. $data['company_id'] = $companyId;
  81. $data['user_id'] = $userId;
  82. $data['createtime'] = $time;
  83. $res = $this->model->insertGetId($data);
  84. } else {
  85. $where['id'] = $id;
  86. $where['user_id'] = $userId;
  87. $res = $this->model->where($where)->update($data);
  88. }
  89. if (!$res) {
  90. throw new Exception('操作失败');
  91. }
  92. $this->success('操作成功');
  93. } catch (Exception $e) {
  94. $this->error($e->getMessage());
  95. }
  96. }
  97. }