Construction.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\Community;
  4. use app\model\Construction as ConstructionModel;
  5. use app\model\ConstructionRecord;
  6. use app\model\ConstructionStep;
  7. use app\model\UserCollect;
  8. use think\facade\Console;
  9. use xiaohongwu\Vr;
  10. use app\model\Decostyle;
  11. use app\model\Designer;
  12. use app\model\Housetype;
  13. use app\model\Employee as EmployeeModel;
  14. use app\model\Org;
  15. use think\facade\Request;
  16. class Construction extends Base
  17. {
  18. public function index()
  19. {
  20. $page = input('page', 1, 'intval');
  21. $limit = input('limit', 10, 'intval');
  22. $root_id = $this->request->token['root_org'];
  23. $where[] = ['root_id', '=', $root_id];
  24. $type = input('type',1) ? 1 : 0;
  25. $where[] = ['type','=',$type];
  26. if($type == 1) $where[] = ['employee_id','=',$this->request->token['employee_id']];
  27. $status = input('status','');
  28. if($status !== '') $where[] = ['status','=',$status];
  29. $community_id = input('community_id', '', 'intval');
  30. if ($community_id) {
  31. $where[] = ['community_id', '=', $community_id];
  32. }
  33. $step_list = ConstructionStep::where('root_id', '=', $root_id)->order('order asc')->select()->toArray();
  34. $order_input = input('order', 'new', 'trim');
  35. $order = 'recommend desc, id desc';
  36. $model = ConstructionModel::with(['style', 'designer', 'housetype', 'community'])->withCount(['share_log'=> function ($query) {
  37. $query->where([['type', '=', 'construction']]);
  38. }]);
  39. if ($order_input == 'new'){
  40. $order = 'recommend desc, update_time desc';
  41. $list = $model->where($where)->page($page, $limit)->order($order)->select();
  42. } else if ($order_input == 'hot') {
  43. $list = $model->where($where)->order($order)->select();
  44. }
  45. $list = $list->each(function ($item) use ($step_list) {
  46. $record_list = ConstructionRecord::where([['construction_id', '=', $item['id']]])->order('update_time desc')->select();
  47. $step_now = 0; //进行到哪一步了
  48. foreach ($step_list as $k => $v) {
  49. foreach ($record_list as $kk => $vv) {
  50. if ($v['id'] == $vv['step_id'] && $v['order'] > $step_now) {
  51. $step_now = $v['order'];
  52. }
  53. }
  54. }
  55. foreach ($step_list as $k => $v) {
  56. $step_list[$k]['upload'] = 0; // 判断是否上传
  57. $step_list[$k]['need_upload'] = 0; // 判断节点是否变色(已过此节点,但是没上传)
  58. $have = false; //是否有上传 true有
  59. $need_have = false; //没上传,但是后面的节点上传了 true是
  60. foreach ($record_list as $kk => $vv) {
  61. if ($v['id'] == $vv['step_id']) {
  62. $have = true;
  63. }
  64. }
  65. if ($have == false && $v['order'] < $step_now) {
  66. $need_have = true;
  67. }
  68. if ($have) {
  69. $step_list[$k]['upload'] = 1;
  70. } elseif ($need_have) {
  71. $step_list[$k]['need_upload'] = 1;
  72. }
  73. }
  74. $item->step_list = $step_list;
  75. //收藏状态
  76. $user_id = $this->request->token['uid'];
  77. $had = UserCollect::where(['user_id' => $user_id , 'content_type' => 'construction' , 'content_id' => $item['id']])->count();
  78. if ($had) {
  79. $item['collect'] = 1;
  80. } else {
  81. $item['collect'] = 0;
  82. }
  83. })->toArray();
  84. if ($order_input == 'hot') {
  85. $list_all = $list;
  86. $share_log_count = array_column($list_all, 'share_log_count');
  87. array_multisort($share_log_count, SORT_DESC, $list_all);
  88. $list = array_slice($list_all, $limit*($page-1), $limit);
  89. }
  90. $count = $model->where($where)->count();
  91. return json(['code' => self::success, 'data' => $list, 'count' => $count]);
  92. }
  93. /**
  94. * 装修案例小区列表
  95. */
  96. public function communitylist()
  97. {
  98. $communityArr = Community::where(['root_id' => request()->token['root_org']])->where([['type', '=', 0]])->field('id,name,pinyin,case_num')->order('pinyin asc')->select()->each(function ($item){
  99. $item['construction_count'] = ConstructionModel::where('community_id', '=', $item['id'])->count();
  100. })->toArray();
  101. return json(['code' => 0, 'data' => $communityArr, 'msg' => '获取成功']);
  102. }
  103. /**
  104. * 小区,风格,户型,设计师下拉列表
  105. */
  106. public function selectList()
  107. {
  108. $token = request()->token;
  109. $condition[] = ['root_id','=',$token['root_org']];
  110. //小区
  111. $data['communities'] = Community::where($condition)->where([['type', '=', 0]])->order('pinyin asc')->column('id,name');
  112. //风格
  113. $data['decostyles'] = Decostyle::where($condition)->where([['type', '=', 0]])->column('id,name');
  114. //户型获取
  115. $data['housetype'] = Housetype::where($condition)->column('id,name');
  116. //设计师
  117. $root_id = $token['root_org'];
  118. $w[] = ['path', 'like', $root_id . '-%'];
  119. $w[] = ['org_type','=',2];
  120. $orgs = Org::where($w)->column('id');
  121. $w3[] = ['org_id','in',$orgs];
  122. $w3[] = ['root_id','=', $root_id];
  123. $w3[] = ['state','=','在职'];
  124. $w3[] = ['uid','>',0];
  125. $data['designers'] = EmployeeModel::where($w3)->column('id,name');
  126. //施工阶段
  127. $data['step'] = ConstructionStep::where('root_id', '=', $root_id)->order('order asc')->select();
  128. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  129. }
  130. /**
  131. * 获取施工阶段
  132. */
  133. public function getConstructionStage()
  134. {
  135. $param = Request::only(['construction_id'=>0]);
  136. $token = request()->token;
  137. $where = [
  138. ['root_id','=',$token['root_org']],
  139. ['construction_id','=',$param['construction_id']]
  140. ];
  141. $list = ConstructionRecord::with(['step'=>function($query){
  142. $query->bind(['name','order','update_date'=>'addtime']);
  143. }])->where($where)->select()->toArray();
  144. $vrObj = new Vr();
  145. foreach ($list as $k => $v) {
  146. $list[$k]['update_date'] = date('Y-m-d',strtotime($v['update_date']));
  147. $list[$k]['vr'] = [];
  148. if ($v['vr'] && $v['type']=='vr'){
  149. $arr = explode(',',$v['vr']);
  150. foreach ($arr as $k2 => $v2) {
  151. $vr = [];
  152. $vr['vr_url'] = $v2;
  153. $vr['vr_img'] = $vrObj->getFirstImg($v2);
  154. $list[$k]['vr'][] = $vr;
  155. }
  156. }
  157. }
  158. return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']);
  159. }
  160. public function view()
  161. {
  162. $id = input('id', '', 'intval');
  163. if (empty($id)) {
  164. return json(['code' => self::error_msg, 'msg' => '获取失败']);
  165. }
  166. $info = ConstructionModel::with(['style', 'designer', 'housetype', 'community'])->find($id);
  167. $root_id = $this->request->token['root_org'];
  168. $step_list = ConstructionStep::where('root_id', '=', $root_id)->order('order asc')->select()->toArray();
  169. $record_list = ConstructionRecord::where([['construction_id', '=', $id]])->order('update_time desc')->select();
  170. $step_now = 0; //进行到哪一步了
  171. foreach ($step_list as $k => $v) {
  172. foreach ($record_list as $kk => $vv) {
  173. if ($v['id'] == $vv['step_id'] && $v['order'] > $step_now) {
  174. $step_now = $v['order'];
  175. }
  176. }
  177. }
  178. $vrObj = new Vr();
  179. foreach ($step_list as $k => $v) {
  180. $step_list[$k]['upload'] = 0; // 判断是否上传
  181. $step_list[$k]['need_upload'] = 0; // 判断节点是否变色(已过此节点,但是没上传)
  182. $have = false; //是否有上传 true有
  183. $need_have = false; //没上传,但是后面的节点上传了 true是
  184. $step_list[$k]['data'] = [];
  185. foreach ($record_list as $kk => $vv) {
  186. if ($v['id'] == $vv['step_id']) {
  187. $have = true;
  188. if (!empty($vv['vr']) && $vv['type'] == 'vr') {
  189. $vrUrlList = explode(',', $vv['vr']);
  190. $vrData = [];
  191. foreach ($vrUrlList as $url) {
  192. $vrData[] = [
  193. 'vrUrl' => $url,
  194. 'vrfirstImg' => $vrObj->getFirstImg($url)
  195. ];
  196. }
  197. $vv['vr'] = $vrData;
  198. }
  199. $step_list[$k]['data'] = $vv;
  200. }
  201. }
  202. if ($have == false && $v['order'] < $step_now) {
  203. $need_have = true;
  204. }
  205. if ($have) {
  206. $step_list[$k]['upload'] = 1;
  207. } elseif ($need_have) {
  208. $step_list[$k]['need_upload'] = 1;
  209. }
  210. }
  211. $info['step_list'] = $step_list;
  212. //收藏状态
  213. $user_id = $this->request->token['uid'];
  214. $had = UserCollect::where(['user_id' => $user_id , 'content_type' => 'construction' , 'content_id' => $id])->count();
  215. if ($had) {
  216. $info['collect'] = 1;
  217. } else {
  218. $info['collect'] = 0;
  219. }
  220. $info['housetype']['name'] = $info->housetype->name;
  221. return json(['code'=> self::success, 'msg'=> '请求成功', 'data'=> $info]);
  222. }
  223. /**
  224. * 收藏
  225. */
  226. public function collect($id)
  227. {
  228. $user_id = $this->request->token['uid'];
  229. $had = UserCollect::where(['user_id' => $user_id , 'content_type' => 'construction' , 'content_id' => $id])->count();
  230. if ($had > 0) return json(['code' => 1, 'msg' => '您已收藏']);
  231. UserCollect::insert(['user_id' => $user_id , 'content_type' => 'construction' , 'content_id' => $id]);
  232. return json(['code' => 0, 'msg' => '收藏成功']);
  233. }
  234. /**
  235. * 取消收藏
  236. */
  237. public function collectCancel($id)
  238. {
  239. $user_id = $this->request->token['uid'];
  240. UserCollect::where(['user_id' => $user_id , 'content_type' => 'construction' , 'content_id' => $id])->delete();
  241. return json(['code' => 0, 'msg' => '取消收藏']);
  242. }
  243. /**
  244. * 添加
  245. */
  246. public function add(){
  247. $param = request()->only(['name', 'area', 'cover'=> '', 'start_time', 'style_id', 'designer_id', 'community_id', 'housetype_id', 'cover_share_img', 'housetype_type'=>'housetype']);
  248. $token = $this->request->token;
  249. $param['root_id'] = $token['root_org'];
  250. $param['del'] = 0;
  251. $param['employee_id'] = $token['employee_id'];
  252. $param['step_id'] = 0;
  253. $param['type'] = 1;
  254. $param['status'] = 0;
  255. $result = ConstructionModel::create($param);
  256. if ($result) {
  257. return json(['code'=> 0, 'msg'=> '添加成功','data'=>$result->id]);
  258. } else {
  259. return json(['code'=> 1, 'msg'=> '添加失败']);
  260. }
  261. }
  262. /**
  263. * 编辑
  264. */
  265. public function edit(){
  266. $param = request()->only(['id', 'name', 'area', 'cover', 'start_time', 'style_id', 'designer_id', 'community_id', 'housetype_id', 'cover_share_img', 'housetype_type'=>'housetype']);
  267. $token = $this->request->token;
  268. $find = ConstructionModel::where([['id', '=', $param['id']],['root_id','=',$token['root_org']]])->findOrEmpty();
  269. if($find->isEmpty()) return json(['code'=> 1, 'msg'=> '保存失败']);
  270. unset($param['id']);
  271. foreach ($param as $k => $v) {
  272. $find->$k = $v;
  273. }
  274. if($find->status != 1) $find->status = 0;
  275. $find->save();
  276. return json(['code'=> 0, 'msg'=> '保存成功']);
  277. // $result = ConstructionModel::where('id', '=', $param['id'])->save($param);
  278. // if ($result) {
  279. // return json(['code'=> 0, 'msg'=> '保存成功']);
  280. // } else {
  281. // return json(['code'=> 1, 'msg'=> '保存失败']);
  282. // }
  283. }
  284. /**
  285. * 删除
  286. */
  287. public function delete(){
  288. $id = input('id', '', 'intval');
  289. $root_id = request()->token['root_org'];
  290. $find = ConstructionModel::find($id);
  291. if (empty($find)) {
  292. return json(['code'=> 0, 'msg'=> '删除成功']);
  293. }
  294. if ($find['root_id'] != $root_id) {
  295. return json(['code'=> 1, 'msg'=> '操作异常,请刷新后重试']);
  296. }
  297. $result = $find->delete();
  298. if ($result !== false) {
  299. return json(['code'=> 0, 'msg'=> '删除成功']);
  300. } else {
  301. return json(['code'=> 1, 'msg'=> '删除失败']);
  302. }
  303. }
  304. /**
  305. * 更新施工进度
  306. */
  307. public function update_step(){
  308. $id = input('id', '', 'intval');
  309. $construction_id = input('construction_id', '', 'intval');
  310. $step_id = input('step_id', '', 'intval');
  311. $step = ConstructionStep::where('id',$step_id)->findOrEmpty();
  312. if($step->isEmpty()) return json(['code'=> 1, 'msg'=> '施工阶段已删除,请重新选择']);
  313. if ($id) {
  314. // 编辑
  315. $find = ConstructionRecord::find($id);
  316. $data_vr = input('vr', '', 'trim');
  317. $video = input('video', '', 'trim');
  318. $video_cover = input('video_cover', '', 'trim');
  319. $type = input('type', '', 'trim');
  320. $media_id = input('media_id', '', 'trim');
  321. $img = input('img','','trim');
  322. if (!empty($data_vr) && $type == 'vr') {
  323. $vr = explode(',', $data_vr);
  324. foreach ($vr as $k => $v) {
  325. if (!empty($v)){
  326. $vr[$k] = $this->vrlink_set($v);
  327. }
  328. }
  329. $find->vr = implode(',', $vr);
  330. } else {
  331. $find->vr = '';
  332. }
  333. if ($type == 'img'){
  334. $find->img = $img;
  335. if ($media_id) {
  336. $find->media_id = $media_id;
  337. $find->down_status = 1;
  338. }
  339. // $data['down_status'] = 1;
  340. // $data['media_id'] = $media_id;
  341. } else {
  342. $find->img = '';
  343. }
  344. if ($type == 'video'){
  345. $find->video = $video;
  346. $find->video_cover = $video_cover;
  347. } else {
  348. $find->video = '';
  349. $find->video_cover = '';
  350. }
  351. $find->type = $type;
  352. $find->step_id = $step_id;
  353. $find->content = input('content', '', 'trim');
  354. $find->update_time = date('Y-m-d H:i:s', time());
  355. if ($step_id != $find['step_id']) {
  356. $type_find = ConstructionRecord::where([['construction_id', '=', $find['construction_id']], ['step_id', '=', $step_id]])->find();
  357. if (!empty($type_find)){
  358. return json(['code'=> 1, 'msg'=> '该阶段已上传']);
  359. }
  360. }
  361. $result = $find->save();
  362. Console::call('download', ['construction_record']);
  363. } else {
  364. //添加
  365. $data = request()->only(['img', 'step_id', 'content', 'vr', 'video', 'video_cover', 'type', 'media_id'=> '']);
  366. $data['construction_id'] = $construction_id;
  367. if (!empty($data['media_id']) && $data['type'] == 'img') {
  368. $data['down_status'] = 1;
  369. }
  370. if (!empty($data['vr']) && $data['type'] == 'vr') {
  371. $vr = explode(',', $data['vr']);
  372. foreach ($vr as $k => $v) {
  373. if (!empty($v)){
  374. $vr[$k] = $this->vrlink_set($v);
  375. }
  376. }
  377. $data['vr'] = implode(',', $vr);
  378. } else {
  379. $data['vr'] = '';
  380. }
  381. if ($data['type'] != 'video') {
  382. $data['video'] = '';
  383. $data['video_cover'] = '';
  384. }
  385. $data['employee_id'] = request()->token['employee_id'];
  386. $data['root_id'] = request()->token['root_org'];
  387. $data['update_time'] = date('Y-m-d H:i:s', time());
  388. $type_find = ConstructionRecord::where([['construction_id', '=', $construction_id], ['step_id', '=', $data['step_id']]])->find();
  389. if (!empty($type_find)){
  390. return json(['code'=> 1, 'msg'=> '该阶段已上传']);
  391. }
  392. $result = ConstructionRecord::create($data);
  393. Console::call('download', ['construction_record']);
  394. }
  395. if ($result) {
  396. //编辑进度后如果在施工地为审核不通过状态,则改为待审核状态
  397. ConstructionModel::where([['id','=',$construction_id],['status','=',2]])->update(['status'=>0]);
  398. ConstructionModel::where('id', '=', $construction_id)->save(['update_time'=> date('Y-m-d H:i:s', time())]);
  399. return json(['code'=> 0, 'msg'=> '保存成功']);
  400. } else {
  401. return json(['code'=> 1, 'msg'=> '保存失败']);
  402. }
  403. }
  404. /**
  405. * vr链接设置修改
  406. * @param $vr_link
  407. * @return string|string[]
  408. */
  409. public function vrlink_set($vr_link)
  410. {
  411. // 旧域名 替换成 新域名
  412. $links = [
  413. 'xiaohongwu' => 'hnweizhihui.xiaohongwu.nczyzs.com',
  414. 'kujiale' => 'pano337.p.kujiale.com',
  415. 'justeasy' => 'vr-17.justeasy.nczyzs.com',
  416. '3d66' => 'vr.3d66.nczyzs.com',
  417. ];
  418. $url = parse_url($vr_link);
  419. if ($url === false || !isset($url['host'])) {
  420. echo json_encode(['code' => 1, 'msg' => '链接格式错误']);
  421. exit;
  422. }
  423. if (strpos($url['host'], 'xiaohongwu') !== false) {
  424. return str_replace($url['host'], $links['xiaohongwu'], $vr_link);
  425. }
  426. if (strpos($url['host'], 'kujiale') !== false) {
  427. return str_replace($url['host'], $links['kujiale'], $vr_link);
  428. }
  429. if (strpos($url['host'], 'justeasy') !== false) {
  430. return str_replace($url['host'], $links['justeasy'], $vr_link);
  431. }
  432. if (strpos($url['host'], '3d66') !== false) {
  433. return str_replace($url['host'], $links['3d66'], $vr_link);
  434. }
  435. echo json_encode(['code' => 1, 'msg' => '无效的VR链接']);
  436. exit;
  437. }
  438. }