Activity.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\policy\Type;
  4. use app\common\controller\Api;
  5. use app\common\model\Activity as ActicityModel;
  6. use app\common\model\City;
  7. use app\common\model\Message;
  8. use app\common\model\Park;
  9. use app\common\model\Protable;
  10. use think\Db;
  11. /**
  12. * 园区接口
  13. */
  14. class Activity extends Api
  15. {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 选择园区
  20. *
  21. * @param string $p_id 园区id
  22. */
  23. public function choosePark()
  24. {
  25. $p_id = $this->request->request('p_id');
  26. $user = $this->auth->getUser();
  27. $ins = [
  28. 'p_id' => $p_id,
  29. 'uid' => $user['id'],
  30. 'create_time' => time(),
  31. ];
  32. Db::name('choose_park_history')->insert($ins);
  33. $userModel = new \app\common\model\User();
  34. $iset = $userModel->where('id',$user['id'])->where('p_id',$p_id)->find();
  35. if ($iset) return $this->success('请选择与之前不同的园区');
  36. $upd = $userModel->where('id',$user['id'])->update(['p_id'=>$p_id]);
  37. if ($upd) {
  38. return $this->success('选择成功');
  39. } else {
  40. return $this->error('选择失败');
  41. }
  42. }
  43. /**
  44. * 园区列表
  45. *
  46. */
  47. public function parkLists()
  48. {
  49. $user = $this->auth->getUser();
  50. if ($user['p_id'] != 0 ) {
  51. $park = Park::where('id',$user['p_id'])->find();
  52. if ($park) {
  53. $data['park_name'] = $park['name'];
  54. } else {
  55. $data['park_name'] = '';
  56. }
  57. } else {
  58. $data['park_name'] = '';
  59. }
  60. $parkHistory = Db::name('choose_park_history')->where('uid',$user['id'])->column('p_id');
  61. $historyStr = implode(',',$parkHistory);
  62. $data['history'] = Park::where('id','in',$historyStr)->column('name');
  63. $cityModel = new City();
  64. $data['park_lists'] = $cityModel
  65. ->with(['park' => function($query) {
  66. $query->where('switch',1);
  67. $query->order('sort desc');
  68. }])
  69. // ->where('park.switch',1)
  70. ->order('eng asc')
  71. ->select();
  72. return $this->success('',$data);
  73. }
  74. /**
  75. * 园区资讯
  76. * @param string $page 页数
  77. * @param string $limit 条数
  78. */
  79. public function parkMessage()
  80. {
  81. $user = $this->auth->getUser();
  82. $page = $this->request->get('page');
  83. $limit = $this->request->get('limit');
  84. if (!$page) {
  85. $fen = '0,10';
  86. } else {
  87. $page = $page - 1;
  88. if ($page<0) $page = 0;
  89. $fen = $page.','.$limit;
  90. }
  91. if (!$user) return $this->success('',[]);
  92. $messageModel = new Message();
  93. $data = $messageModel->where('p_id',$user['p_id'])
  94. ->where('switch',1)
  95. ->order('sort desc')
  96. ->limit($fen)
  97. ->select();
  98. return $this->success('',$data);
  99. }
  100. /**
  101. * 园区活动
  102. * @param string $page 页数
  103. * @param string $limit 条数
  104. */
  105. public function activity()
  106. {
  107. $user = $this->auth->getUser();
  108. $page = $this->request->get('page');
  109. $limit = $this->request->get('limit');
  110. if (!$page) {
  111. $fen = '0,10';
  112. } else {
  113. $page = $page - 1;
  114. if ($page<0) $page = 0;
  115. $fen = $page.','.$limit;
  116. }
  117. if (!$user) return $this->success('',[]);
  118. $activityModel = new ActicityModel();
  119. $data = $activityModel->where('p_id',$user['p_id'])
  120. ->where('switch',1)
  121. ->order('sort desc')
  122. ->limit($fen)
  123. ->select();
  124. $time =time();
  125. foreach ($data as &$v) {
  126. $v['is_collection'] = 0;
  127. $isCollection = Db::name('activity_collection')
  128. ->where('uid',$user['id'])
  129. ->where('a_id',$v['id'])
  130. ->find();
  131. if ($isCollection) $v['is_collection'] = 1;
  132. $startTime = strtotime($v['start_time']);
  133. $endTime = strtotime($v['end_time']);
  134. if ($v['people_num'] == 0) $v['people_num'] = '无限制';
  135. if ($startTime <= $time && $endTime >= $time) {
  136. $v['status'] = 1;
  137. $v['status_name'] = '进行中';
  138. Db::name('park_activity')->where('id',$v['id'])->update(['status' => 1]);
  139. } elseif ( $startTime > $time && $endTime > $time) {
  140. $v['status'] = 0;
  141. $v['status_name'] = '未开始';
  142. Db::name('park_activity')->where('id',$v['id'])->update(['status' => 0]);
  143. } elseif ($startTime <= $time && $endTime <= $time) {
  144. $v['status'] = 2;
  145. $v['status_name'] = '已结束';
  146. Db::name('park_activity')->where('id',$v['id'])->update(['status' => 2]);
  147. }
  148. }
  149. return $this->success('',$data);
  150. }
  151. /**
  152. * 园区活动详情
  153. * @param string $id 活动id
  154. */
  155. public function activityInfo()
  156. {
  157. $id = $this->request->get('id');
  158. $user = $this->auth->getUser();
  159. if (!isset($id) || empty($id)) return $this->error('参数错误');
  160. $activityModel = new ActicityModel();
  161. $data = $activityModel->where('id',$id)
  162. ->find();
  163. $data['is_collection'] = 0;
  164. $isCollection = Db::name('activity_collection')
  165. ->where('uid',$user['id'])
  166. ->where('a_id',$data['id'])
  167. ->find();
  168. if ($isCollection) $data['is_collection'] = 1;
  169. return $this->error('暂无数据',[]);
  170. }
  171. /**
  172. * 园区资讯详情
  173. * @param string $id 活动id
  174. */
  175. public function messageInfo()
  176. {
  177. $id = $this->request->get('id');
  178. if (!isset($id) || empty($id)) return $this->error('参数错误');
  179. $messageModel = new Message();
  180. $data = $messageModel->where('id',$id)
  181. ->find();
  182. return $this->error('暂无数据',[]);
  183. }
  184. /**
  185. * 收藏活动
  186. *
  187. * @param string $id 活动id
  188. */
  189. public function collectionTrue()
  190. {
  191. $id = $this->request->get('id');
  192. if (!isset($id) || empty($id)) return $this->error('参数错误');
  193. $user = $this->auth->getUser();
  194. $data = [
  195. 'uid'=>$user['id'],
  196. 'a_id' =>$id,
  197. 'create_time' => date('Y-m-d H:i:s',time())
  198. ];
  199. $add = Db::name('activity_collection')->insert($data);
  200. if ($add) {
  201. return $this->success('收藏成功');
  202. } else {
  203. return $this->error('收藏失败');
  204. }
  205. }
  206. /**
  207. * 取消收藏
  208. *
  209. * @param string $id 活动id
  210. */
  211. public function collectionFalse()
  212. {
  213. $id = $this->request->get('id');
  214. if (!isset($id) || empty($id)) return $this->error('参数错误');
  215. $user = $this->auth->getUser();
  216. $del = Db::name('activity_collection')->where('uid',$user['id'])->where('a_id',$id)->delete();
  217. if ($del) {
  218. return $this->success('收藏成功');
  219. } else {
  220. return $this->error('收藏失败');
  221. }
  222. }
  223. }