CompanyStrength.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Request;
  4. use think\facade\View;
  5. use app\model\Employee;
  6. use app\model\Org;
  7. use app\model\CompanyStrength as CompanyStrengthModel;
  8. use app\model\CompanyStrengthCate;
  9. use app\model\UserCollect;
  10. class CompanyStrength extends Base
  11. {
  12. /**
  13. * 公司实力列表
  14. */
  15. public function index()
  16. {
  17. $token = $this->request->token;
  18. $param = Request::only(['page'=>1,'limit'=>10,'cate'=>'','label'=>'','title'=>'','difference'=>0,'order'=>'']);
  19. $w[] = ['root_id','=',$token['root_org']];
  20. $w[] = ['del','=',0];
  21. $w[] = ['publish','=',1];
  22. if ($param['cate']) {
  23. $w[] = ['cate','=',$param['cate']];
  24. }
  25. if ($param['label']) {
  26. $w[] = ['label','=',$param['label']];
  27. }
  28. if ($param['title']) {
  29. $w[] = ['title','like','%'.$param['title'].'%'];
  30. }
  31. if ($param['difference']) {
  32. $w[] = ['difference', '=', $param['difference']];
  33. }
  34. $order = $param['order'] ? $param['order'].' desc' : 'id desc';
  35. $list = CompanyStrengthModel::with(['employee'=>function($query){
  36. $query->visible(['id','name','opt_name']);
  37. }])->where($w)->order($order)->page($param['page'],$param['limit'])->select();
  38. foreach ($list as $k => $v) {
  39. $collect[] = [
  40. ['content_id','=',$v->id],
  41. ['content_type','=','companyStrength'],
  42. ['user_id', '=', $token['uid']]
  43. ];
  44. $is_collect = UserCollect::where($collect)->findOrEmpty();
  45. $v->collect = !$is_collect->isEmpty();
  46. unset($collect);
  47. }
  48. $count = CompanyStrengthModel::where($w)->count();
  49. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  50. }
  51. /**
  52. * 获取分类
  53. */
  54. public function get_cate()
  55. {
  56. $token = $this->request->token;
  57. $type = CompanyStrengthCate::where(['pid' => 0, 'root_id' => $token['root_org']])->select()->toArray();
  58. $label = CompanyStrengthCate::where([['pid', '>', 0], ['root_id', '=', $token['root_org']]])->select()->toArray();
  59. foreach ($type as &$val) {
  60. $val['sonLabel'] = [];
  61. foreach ($label as $typeVal) {
  62. if ($val['id'] == $typeVal['pid']) {
  63. $val['sonLabel'][] = $typeVal;
  64. }
  65. }
  66. }
  67. return json(['code' => 0, 'data' => $type, 'msg' => '获取成功']);
  68. // $token = $this->request->token;
  69. // $w[] = ['root_id','=',$token['root_org']];
  70. // $w[] = ['pid','=',0];
  71. // $cate = CompanyStrengthCate::where($w)->order('id asc')->select();
  72. // return json(['code' => 0, 'data' => $cate, 'msg' => '获取成功']);
  73. }
  74. /**
  75. * 获取全部标签
  76. */
  77. public function get_all_label()
  78. {
  79. $token = $this->request->token;
  80. $label = CompanyStrengthCate::where([['pid', '>', 0], ['root_id', '=', $token['root_org']]])->select()->toArray();
  81. return json(['code' => 0, 'data' => $label, 'msg' => '获取成功']);
  82. }
  83. /**
  84. * 详情
  85. */
  86. public function read()
  87. {
  88. $token = $this->request->token;
  89. $param = Request::only(['id'=>0]);
  90. $w[] = ['root_id','=',$token['root_org']];
  91. $w[] = ['id','=',$param['id']];
  92. $info = CompanyStrengthModel::where($w)->find();
  93. if (empty($info)){
  94. return json(['code' => 0, 'data' => [], 'msg' => '获取成功']);
  95. }
  96. $collect[] = [
  97. ['content_id','=',$info->id],
  98. ['content_type','=','companyStrength'],
  99. ['user_id', '=', $token['uid']]
  100. ];
  101. $collect = UserCollect::where($collect)->findOrEmpty();
  102. //是否收藏
  103. $info->collect = !$collect->isEmpty();
  104. return json(['code' => 0, 'data' => $info, 'msg' => '获取成功']);
  105. }
  106. /*
  107. * 收藏
  108. */
  109. function collect()
  110. {
  111. $param = Request::only(['id'=>0]);
  112. $token = $this->request->token;
  113. $condition[] = ['id', '=', $param['id']];
  114. $condition[] = ['root_id', '=', $token['root_org']];
  115. $info = CompanyStrengthModel::where($condition)->findOrEmpty();
  116. if ($info->isEmpty()) {
  117. return json(['code' => 1, 'msg' => '数据不存在']);
  118. }
  119. $w[] = [
  120. ['content_id','=',$param['id']],
  121. ['content_type','=','companyStrength'],
  122. ['user_id', '=', $token['uid']]
  123. ];
  124. $find = UserCollect::where($w)->findOrEmpty();
  125. if (!$find->isEmpty()) {
  126. return json(['code' => 1, 'msg' => '已收藏']);
  127. }
  128. $save = [
  129. 'content_id' => $param['id'],
  130. 'content_type' => 'companyStrength',
  131. 'user_id' => $token['uid']
  132. ];
  133. UserCollect::insertGetId($save);
  134. return json(['code' => 0, 'msg' => '收藏成功', 'data' => '收藏成功']);
  135. }
  136. /*
  137. * 取消收藏
  138. */
  139. function no_collect()
  140. {
  141. $param = Request::only(['id'=>0]);
  142. $token = $this->request->token;
  143. $condition[] = ['id', '=', $param['id']];
  144. $condition[] = ['root_id', '=', $token['root_org']];
  145. $info = CompanyStrengthModel::where($condition)->findOrEmpty();
  146. if ($info->isEmpty()) {
  147. return json(['code' => 1, 'msg' => '数据不存在']);
  148. }
  149. $w[] = [
  150. ['content_id','=',$param['id']],
  151. ['content_type','=','companyStrength'],
  152. ['user_id', '=', $token['uid']]
  153. ];
  154. $find = UserCollect::where($w)->findOrEmpty();
  155. if ($find->isEmpty()) {
  156. return json(['code' => 1, 'msg' => '没有收藏']);
  157. }
  158. UserCollect::where($w)->delete();
  159. return json(['code' => 0, 'msg' => '取消收藏', 'data' => '取消收藏']);
  160. }
  161. }