PetsController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\Repositories\WxContRepositories;
  4. use App\Models\Pets\WxPetsAdoption;
  5. use App\Models\Pets\WxPetsAdoptionApply;
  6. use App\Models\Pets\WxPetsAdoptionCollect;
  7. use App\Models\Pets\WxPetsComment;
  8. use App\Models\User\WxUser;
  9. use App\Models\WxIcon;
  10. use App\Models\WxSlideshow;
  11. use App\Wen\Utils\BaiduUtils;
  12. use App\Wen\Utils\GatewayUtils;
  13. use App\Wen\Utils\ImageUtils;
  14. use App\Wen\Utils\PetUtils;
  15. use App\Wen\Utils\Settings;
  16. use App\Wen\Utils\StrUtils;
  17. use App\Wen\Utils\UserUtils;
  18. use App\Wen\Utils\Utils;
  19. use Carbon\Carbon;
  20. use Illuminate\Http\Request;
  21. use Illuminate\Support\Facades\DB;
  22. class PetsController extends BaseController{
  23. public function adoption_config(Request $request){
  24. $icon_id_arr = explode(',', Settings::get('pets_adoption_index_nav', ''));
  25. $config = [
  26. 'index' => [
  27. 'banner' => WxSlideshow::wherein('id', explode(',', Settings::get('pets_adoption_index_banner', '', true)))->orderBy(DB::raw('FIND_IN_SET(id, "' . Settings::get('pets_adoption_index_banner', '', true) . '"' . ")"))->get(['id', 'poster', 'target_id', 'slideshow_type']),
  28. 'notice' => Settings::get('pets_adoption_index_notice', [], true),
  29. 'nav' => $icon_id_arr ? WxIcon::whereIn('id', $icon_id_arr)->orderBy(DB::raw('FIND_IN_SET(id, "' . implode(",", $icon_id_arr) . '"' . ")"))->get() : [],
  30. 'count' => WxPetsAdoption::where('adopt_status', 1)->count()
  31. ]
  32. ];
  33. return $this->success($config);
  34. }
  35. public function adoption_list(Request $request){
  36. $uid = $request->uid;
  37. $limit = $request->input('limit', 10);
  38. $user_id = $request->user_id;
  39. $action = _empty_default_($request->action, 0);
  40. $keyword = _empty_default_($request->keyword, '');
  41. $filter_age_range = _empty_default_($request->filter_age_range, '');
  42. $filter_gender_range = _empty_default_($request->filter_gender_range, '');
  43. $filter_pet_status_range = _empty_default_($request->filter_pet_status_range, '');
  44. if(_empty_($user_id)){
  45. $user_id = null;
  46. }else{
  47. try {
  48. $user_id = _abs($user_id);
  49. }catch (\Exception $e){
  50. return $this->fail(200004, [], 'user_id参数错误');
  51. }
  52. }
  53. $species = $request->species;
  54. if(_empty_($species)){
  55. $species = 0;
  56. }
  57. $excludes = $request->excludes;
  58. if(_empty_($excludes)){
  59. $excludes = null;
  60. }else{
  61. if(is_array($excludes)){
  62. }else{
  63. $excludes = [$excludes];
  64. }
  65. }
  66. $city = $request->city;
  67. if(_empty_($city)){
  68. $city = null;
  69. }
  70. if($city == '全国' || $city == '重新定位' || $city == '定位中'){
  71. $city = null;
  72. }
  73. if(!in_array($species, [0, 1, 2])){
  74. return $this->fail(200004, [], '暂未开放其他物种');
  75. }
  76. $query = WxPetsAdoption::orderBy('adopt_status', 'asc')->orderBy('created_at', 'desc');
  77. if($species > 0){
  78. $query = $query->where('species', $species);
  79. }
  80. if($keyword){
  81. $query = $query->where(function ($query) use ($keyword) {
  82. if(is_numeric($keyword)){
  83. $query->orWhere('id', $keyword)->orWhere('name', 'like', '%' . $keyword . '%')->orWhere('pet_breed', 'like', '%' . $keyword . '%');
  84. }else{
  85. $query->orWhere('name', 'like', '%' . $keyword . '%')->orWhere('pet_breed', 'like', '%' . $keyword . '%');
  86. }
  87. });
  88. }
  89. if($city){
  90. if(!StrUtils::endsWith($city, '市')){
  91. $city .= '市';
  92. }
  93. if($keyword){
  94. }else{
  95. if($city == '北京市' || $city == '上海市' || $city == '重庆市' || $city == '天津市'){
  96. $query = $query->where('province', 'like', $city.'%');
  97. }else{
  98. $query = $query->where('city', 'like', $city.'%');
  99. }
  100. }
  101. }
  102. if($excludes){
  103. $query = $query->whereNotIn('id', $excludes);
  104. }
  105. if($user_id > 0){
  106. $query = $query->where('user_id', $user_id);
  107. }
  108. if($action > 0){
  109. if($action == 1){
  110. // 我的领养
  111. $adoption_arr = WxPetsAdoptionApply::where('user_id', $uid)->where('status', 1)->pluck('pet_adoption_id');
  112. $query = $query->whereIn('id', $adoption_arr);
  113. }else if($action == 2){
  114. // 我的送养
  115. $query = $query->where('user_id', $uid);
  116. }else if($action == 3){
  117. // 我的收藏
  118. $adoption_arr = WxPetsAdoptionCollect::where('user_id', $uid)->pluck('pet_adoption_id');
  119. $query = $query->whereIn('id', $adoption_arr);
  120. }else if($action == 4){
  121. // 已送养
  122. $query = $query->where('adopt_status', 1);
  123. }else if($action == 5){
  124. // 待送养
  125. $query = $query->where('adopt_status', 0);
  126. }
  127. }
  128. if($filter_age_range){
  129. // 1: 0-1岁 2:2-6岁 3:7-10岁
  130. if(StrUtils::startsWith($filter_age_range,'[')){
  131. $filter_age_range = ltrim($filter_age_range, '[');
  132. $filter_age_range = rtrim($filter_age_range, ']');
  133. }
  134. $filter_age_arr = explode(',', $filter_age_range);
  135. $arr_ = [];
  136. foreach ($filter_age_arr as $a){
  137. if($a == 1){
  138. $arr_[] = ['pet_age', '>=', Carbon::now()->subYears(1)];
  139. }else if($a == 2){
  140. $arr_[] = ['pet_age', '>=', Carbon::now()->subYears(6)];
  141. $arr_[] = ['pet_age', '<=', Carbon::now()->subYears(2)];
  142. }else if($a == 3){
  143. $arr_[] = ['pet_age', '<=', Carbon::now()->subYears(3)];
  144. }
  145. }
  146. $query = $query->Where($arr_);
  147. }
  148. if($filter_gender_range){
  149. if(StrUtils::startsWith($filter_gender_range,'[')){
  150. $filter_gender_range = ltrim($filter_gender_range, '[');
  151. $filter_gender_range = rtrim($filter_gender_range, ']');
  152. }
  153. // 1: 0-1岁 2:2-6岁 3:7-10岁
  154. $filter_gender_arr = explode(',', $filter_gender_range);
  155. $arr_ = [];
  156. if(count($filter_gender_arr) !== 2){
  157. foreach ($filter_gender_arr as $a){
  158. if($a == 1){
  159. $arr_[] = ['pet_gender', '=', 1];
  160. }else if($a == 2){
  161. $arr_[] = ['pet_gender', '=', 2];
  162. }
  163. }
  164. $query = $query->Where($arr_);
  165. }
  166. }
  167. if($filter_pet_status_range){
  168. if(StrUtils::startsWith($filter_pet_status_range, '[')){
  169. $filter_pet_status_range = ltrim($filter_pet_status_range, '[');
  170. $filter_pet_status_range = rtrim($filter_pet_status_range, ']');
  171. }
  172. $filter_pet_status_arr = explode(',', $filter_pet_status_range);
  173. $arr_ = [];
  174. foreach ($filter_pet_status_arr as $a){
  175. if($a == 1){
  176. $arr_[] = ['is_sterilization', '=', 1];
  177. }else if($a == 2){
  178. $arr_[] = ['is_vaccine', '=', 1];
  179. }else if($a == 3){
  180. $arr_[] = ['is_repellent', '=', 1];
  181. }
  182. }
  183. $query = $query->Where($arr_);
  184. }
  185. $data = $query->simplePaginate($limit);
  186. $data->map(function ($v, $k) use ($uid){
  187. return PetUtils::adoption_process($v, $uid);
  188. });
  189. return $this->success($data);
  190. }
  191. public function comment_process(Request $request){
  192. $action = _empty_default_($request->action, '');
  193. $uid = $request->uid;
  194. if(_empty_($action)){
  195. return $this->fail(200001);
  196. }
  197. if(!in_array($action, ['add', 'delete'])){
  198. return $this->fail(200004);
  199. }
  200. if(!UserUtils::is_user_can_speak($uid)){
  201. return $this->fail(200043, [
  202. 'title' => '禁言中,无法言论',
  203. 'content' => '是否前往解除限制',
  204. 'confirmText' => '去解除',
  205. 'target_type' => 6,
  206. 'target_id' => '/pagesA/mine/unlock/unlock?user_id='.$uid
  207. ], '您当前处于禁言期间,无法言论');
  208. }
  209. if($action == 'add'){
  210. $pet_adoption_id = _empty_default_($request->pet_adoption_id, 0);
  211. if(_empty_($pet_adoption_id)){
  212. return $this->fail(200001);
  213. }
  214. if(!in_array(WxPetsAdoption::where('id', $pet_adoption_id)->value('adopt_status'), [0, 1])){
  215. return $this->fail(200000, [], '该领养不是正常状态');
  216. }
  217. if(mb_strlen( str_replace( [Settings::get('default_user_name', '微信用户'), '微信用户', '普通用户'], '', UserUtils::get_cached_user_name($uid) )) != mb_strlen(UserUtils::get_cached_user_name($uid))){
  218. return $this->fail(200041);
  219. }
  220. $comment_id = _empty_default_($request->comment_id, 0);
  221. if(_empty_($comment_id)){
  222. $comment_id = 0;
  223. }
  224. if($comment_id > 0){
  225. if(!WxPetsComment::where('id', $comment_id)->where('comment_state', 1)->exists()){
  226. return $this->fail(200004, [], '该父级评论不存在');
  227. }
  228. }
  229. $comment_agent_id = _empty_default_($request->comment_agent_id, 0);
  230. if(_empty_($comment_agent_id)){
  231. $comment_agent_id = 0;
  232. }
  233. if($comment_agent_id > 0){
  234. $comment_agent_name = UserUtils::get_cached_user($comment_agent_id)['user_name'];
  235. }
  236. $comment_content = _empty_default_($request->comment_content, '');
  237. $comment_img_url = _empty_default_($request->comment_img_url, '');
  238. if(_empty_($comment_content)){
  239. return $this->fail(200001);
  240. }
  241. $comment_content = strip_tags($comment_content);
  242. if(mb_strlen($comment_content) <= 0 || mb_strlen($comment_content) > 240){
  243. return $this->fail(200004, [], '评论内容过长或过短');
  244. }
  245. global $__MINI_GLOBAL_IP__;
  246. // 百度审核
  247. if(UserUtils::user_permissions_check_by_config($uid, 'audit_pets_comment_white_list')){
  248. $need_manual_review = false;
  249. }else{
  250. $need_manual_review = true;
  251. $is_audit_pets_comment = Settings::get('is_audit_pets_comment', 0);
  252. if ($is_audit_pets_comment == 2) {
  253. $filter_result = BaiduUtils::text_filter($comment_content);
  254. if($filter_result){
  255. if($filter_result['hit_level'] == 2){
  256. // 不合规
  257. return $this->fail(200016, ['tip'=>$filter_result['tip_list'], 'hit_word'=>$filter_result['hit_word']]);
  258. }else if($filter_result['hit_level'] == 0){
  259. // 审核通过
  260. $need_manual_review = false;
  261. }
  262. }
  263. }else if($is_audit_pets_comment == 0){
  264. $need_manual_review = true;
  265. }else{
  266. $need_manual_review = false;
  267. }
  268. }
  269. // 0:审核中,1:审核通过,2:驳回
  270. $comment_state = 1;
  271. if($need_manual_review){
  272. $comment_state = 0;
  273. }
  274. $model = new WxPetsComment();
  275. $model->user_id = $uid;
  276. $model->pet_adoption_id = $pet_adoption_id;
  277. if($comment_id > 0){
  278. $model->comment_id = $comment_id;
  279. }else{
  280. $model->comment_id = 0;
  281. }
  282. if($comment_agent_id > 0 && $comment_agent_name){
  283. $model->comment_agent_id = $comment_agent_id;
  284. $model->comment_agent_name = $comment_agent_name;
  285. }else{
  286. $model->comment_agent_id = 0;
  287. }
  288. $model->comment_content = $comment_content;
  289. if(!_empty_($comment_img_url)){
  290. $model->comment_img_url = $comment_img_url;
  291. }
  292. if($__MINI_GLOBAL_IP__){
  293. $model->ip = $__MINI_GLOBAL_IP__;
  294. }
  295. $model->comment_state = $comment_state;
  296. $r = $model->save();
  297. if($r){
  298. return $this->success();
  299. }
  300. return $this->fail(200002);
  301. }else if($action == 'delete'){
  302. }
  303. }
  304. public function adoption_apply_action(Request $request){
  305. $apply_id = _empty_default_($request->apply_id, 0);
  306. $flag = _empty_default_($request->flag, 0);
  307. if(_empty_($apply_id)){
  308. return $this->fail(200004, [], 'apply_id参数错误');
  309. }
  310. $adopt_apply = WxPetsAdoptionApply::where('id', $apply_id)->first();
  311. if($adopt_apply->status == 1){
  312. return $this->fail(200010, [], '您已经处理过该申请');
  313. }
  314. if($adopt_apply->status == 2){
  315. return $this->fail(200010, [], '您已经拒绝了该申请');
  316. }
  317. DB::beginTransaction();
  318. try {
  319. WxPetsAdoptionApply::where('id',$apply_id )->update([
  320. 'status' => $flag == 1 ? 1 : 2
  321. ]);
  322. if($flag == 1){
  323. // 自动拒绝其他的
  324. WxPetsAdoptionApply::where('pet_adoption_id',$adopt_apply->pet_adoption_id )->where('id', '<>', $apply_id)->where('status', 0)->update([
  325. 'status' => 2
  326. ]);
  327. WxPetsAdoption::where('id', $adopt_apply->pet_adoption_id)->update([
  328. 'adopt_status' => 1,
  329. 'success_user_id' => $adopt_apply->user_id
  330. ]);
  331. }
  332. DB::commit();
  333. return $this->success(['status'=>$flag ? 1 : 2]);
  334. } catch (\Exception $e) {
  335. DB::rollBack();
  336. _logger_(__file__, __line__, $e->getMessage());
  337. return $this->fail(200002);
  338. }
  339. }
  340. public function adoption_collect(Request $request){
  341. $uid = $request->uid;
  342. $adoption_id = _empty_default_($request->id, 0);
  343. if(_empty_($adoption_id)){
  344. return $this->fail(200001);
  345. }
  346. if(WxPetsAdoptionCollect::where('user_id', $uid)->where('pet_adoption_id', $adoption_id)->exists()){
  347. WxPetsAdoptionCollect::where('user_id', $uid)->where('pet_adoption_id', $adoption_id)->delete();
  348. }else{
  349. $collect = new WxPetsAdoptionCollect();
  350. $collect->user_id = $uid;
  351. $collect->pet_adoption_id = $adoption_id;
  352. $collect->save();
  353. }
  354. return $this->success();
  355. }
  356. public function adoption_apply(Request $request){
  357. $uid = $request->uid;
  358. $pet_adoption_id = _empty_default_($request->pet_adoption_id);
  359. $the_adoption = WxPetsAdoption::where('id', $pet_adoption_id)->first();
  360. if(!$the_adoption){
  361. return $this->fail(200004, [], 'adoptionId参数错误');
  362. }
  363. if($the_adoption->adopt_status == 1){
  364. return $this->fail(200004, [], '该宠物已被领养');
  365. }
  366. if($the_adoption->adopt_status == 2){
  367. return $this->fail(200004, [], '该领养信息已废弃');
  368. }
  369. $the_apply = WxPetsAdoptionApply::where('pet_adoption_id', $the_adoption->id)->where('user_id', $uid)->first();
  370. if($the_apply && $the_apply->status == 1){
  371. _logger_(__file__, __line__, '出现逻辑错误');
  372. return $this->fail(200004, [], '您的申请已经通过了');
  373. }
  374. $gender = _empty_default_($request->gender, 0);
  375. if(!in_array($gender, [0, 1, 2])){
  376. return $this->fail(200004, [], '性别参数错误');
  377. }
  378. $is_experience = _empty_default_($request->is_experience, 0);
  379. if(!in_array($is_experience, [0, 1])){
  380. return $this->fail(200004, [], '养宠经验参数错误');
  381. }
  382. // 0:单身 1:恋爱中 2:已婚
  383. $marital_status = _empty_default_($request->marital_status, 0);
  384. if(!in_array($marital_status, array_keys(PetUtils::marital_status()))){
  385. return $this->fail(200004, [], '婚姻状况参数错误');
  386. }
  387. // 0: 自住房 1:整租房 2:合租
  388. $housing_status = _empty_default_($request->housing_status, 0);
  389. if(!in_array($housing_status, array_keys(PetUtils::housing_status()))){
  390. return $this->fail(200004, [], '房屋情况参数错误');
  391. }
  392. // 0: 无业 1: 学生 2:工作 3: 退休
  393. $working_status = _empty_default_($request->working_status, 0);
  394. if(!in_array($working_status, array_keys(PetUtils::working_status()))){
  395. return $this->fail(200004, [], '工作状态参数错误');
  396. }
  397. $job = _empty_default_($request->job);
  398. $birthday = _empty_default_($request->birthday);
  399. try {
  400. $age_timestamp = strtotime($birthday);
  401. }catch (\Exception $e){
  402. return $this->fail(200004, [], '生日不合法');
  403. }
  404. if((time() - $age_timestamp) < (31536000 * 18)){
  405. return $this->fail(200004, [], '未成年不能申请');
  406. }
  407. // 0:小于3千 1:3千~6千 2:6千~1万 3:1万以上
  408. $income = _empty_default_($request->income, 0);
  409. if(!in_array($income, array_keys(PetUtils::income_types()))){
  410. return $this->fail(200004, [], '收入参数错误');
  411. }
  412. $wechatId = _empty_default_($request->wechatId);
  413. $mobile = _empty_default_($request->mobile);
  414. $remark = _empty_default_($request->remark);
  415. $region = _empty_default_($request->region, []);
  416. if(count($region) !== 3){
  417. return $this->fail(200004, [], '请选择完整的地址');
  418. }
  419. if(_empty_($pet_adoption_id) || _empty_($job) || _empty_($age_timestamp) || _empty_($wechatId) || _empty_($mobile) || _empty_($remark)){
  420. return $this->fail(200001);
  421. }
  422. DB::beginTransaction();
  423. try {
  424. // todo:
  425. if($the_apply && $the_apply->status == 0){
  426. $apply = $the_apply;
  427. }else{
  428. $apply = new WxPetsAdoptionApply();
  429. }
  430. $apply->user_id = $uid;
  431. $apply->pet_adoption_id = $pet_adoption_id;
  432. $apply->gender = $gender;
  433. $apply->is_experience = $is_experience;
  434. $apply->marital_status = $marital_status;
  435. $apply->housing_status = $housing_status;
  436. $apply->working_status = $working_status;
  437. $apply->job = $job;
  438. $apply->birthday = timetostr($age_timestamp);
  439. $apply->income = $income;
  440. $apply->province = $region[0];
  441. $apply->city = $region[1] == '市辖区' ? $region[0] : $region[1];;
  442. $apply->district = $region[2];
  443. $apply->mobile = $mobile;
  444. $apply->wechatId = $wechatId;
  445. $apply->remark = $remark;
  446. $r = $apply->save();
  447. if($r){
  448. $model = WxContRepositories::add($uid, $the_adoption->user_id, null, null, null, 0, null, $apply->id);
  449. DB::commit();
  450. return $this->success(['id'=>$apply->id, 'object_id'=>$the_adoption->user_id], 200, '发送成功');
  451. }else{
  452. DB::rollBack();
  453. return $this->fail(200006);
  454. }
  455. } catch (\Exception $e) {
  456. DB::rollBack();
  457. _logger_(__file__, __line__, $e->getMessage());
  458. return false;
  459. }
  460. }
  461. public function comment_list(Request $request){
  462. $limit = _between_(_empty_default_($request->limit, 10), 1, 100);
  463. $pet_adoption_id = _empty_default_($request->pet_adoption_id, 0);
  464. if(_empty_($pet_adoption_id)){
  465. $this->fail(200001);
  466. }
  467. $data = WxPetsComment::where('pet_adoption_id', $pet_adoption_id)->where('comment_id', 0)->where('comment_state', 1)->orderBy('id', 'desc')->simplePaginate($limit);
  468. if($data){
  469. $data->map(function ($v, $k){
  470. $v->user = UserUtils::get_cached_user($v->user_id);
  471. if(_empty_($v->comment_id)){
  472. $childData = WxPetsComment::where('comment_id', $v->id)->limit(100)->get();
  473. if($childData){
  474. $childData->map(function ($v2, $k2){
  475. $v2->user = UserUtils::get_cached_user($v2->user_id);
  476. $v2->format_time = format_datetime($v2->created_at);
  477. return $v2;
  478. });
  479. }
  480. $v->child = $childData;
  481. }else{
  482. $v->child = [];
  483. }
  484. $v->format_time = format_datetime($v->created_at);
  485. return $v;
  486. });
  487. return $this->success($data);
  488. }else{
  489. return $this->fail(200003);
  490. }
  491. }
  492. public function adoption_detail(Request $request){
  493. $id = _empty_default_($request->id, 0);
  494. $uid = $request->uid;
  495. if(_empty_($id)){
  496. return $this->fail(200001);
  497. }
  498. $adoption = WxPetsAdoption::where('id', $id)->first();
  499. $user_info_img = Settings::get('pets_adoption_detail_user_info_img', 0);
  500. if($user_info_img > 0){
  501. $adoption->user_info_img = WxIcon::find($user_info_img);
  502. }else{
  503. $adoption->user_info_img = null;
  504. }
  505. $adoption->comment_count = WxPetsComment::where('pet_adoption_id', $adoption->id)->count();
  506. if($adoption){
  507. return $this->success(PetUtils::adoption_process($adoption, $uid));
  508. }else{
  509. return $this->fail(200003);
  510. }
  511. }
  512. public function adoption_add(Request $request){
  513. global $__MINI_GLOBAL_CURRENT_PLAYER_ID__;
  514. $id = _empty_default_($request->id, 0);
  515. $name = _empty_default_($request->name, '');
  516. $pictures = _empty_default_($request->pictures, []);
  517. $pet_gender = _empty_default_($request->pet_gender, 0);
  518. $species = _empty_default_($request->species, 0);
  519. $pet_breed = _empty_default_($request->pet_breed);
  520. $is_vaccine = _empty_default_($request->is_vaccine, 0);
  521. $is_sterilization = _empty_default_($request->is_sterilization, 0);
  522. $is_repellent = _empty_default_($request->is_repellent, 0);
  523. $is_free = _empty_default_($request->is_free, 0);
  524. $cost = _empty_default_($request->cost, 0);
  525. $pet_introduce = _empty_default_($request->pet_introduce);
  526. $condition = _empty_default_($request->condition, []);
  527. $region = _empty_default_($request->region, []);
  528. $address = _empty_default_($request->address);
  529. $adopter_name = _empty_default_($request->adopter_name);
  530. $adopter_mobile = _empty_default_($request->adopter_mobile, '');
  531. $adopter_wechatId = _empty_default_($request->adopter_wechatId);
  532. $adopt_status = _empty_default_($request->adopt_status, 0);
  533. $video_url = _empty_default_($request->video_url, null);
  534. $video_cover = _empty_default_($request->video_cover, null);
  535. $age_range = _empty_default_($request->age_range, 0);
  536. $image_state_change_list = [];
  537. $uid = $request->uid;
  538. $user = WxUser::find($uid);
  539. // 验证资料
  540. if(Settings::get('pets_adoption_add_force_bind_phone', 0, true) == 1){
  541. if(_empty_($user->phone) && _empty_($__MINI_GLOBAL_CURRENT_PLAYER_ID__)){
  542. return $this->fail(200043, [
  543. 'title' => '未绑定手机号',
  544. 'content' => '前往绑定',
  545. 'confirmText' => '去绑定',
  546. 'target_type' => 6,
  547. 'target_id' => '/pagesA/mine/editmine/editmine?phone=1'
  548. ], '未绑定手机号');
  549. }
  550. }
  551. if(Settings::get('pets_adoption_add_force_real_name', 0, true) == 1){
  552. if(_empty_($user->real_name) && _empty_($__MINI_GLOBAL_CURRENT_PLAYER_ID__)){
  553. return $this->fail(200043, [
  554. 'title' => '未实名认证',
  555. 'content' => '您还没有实名认证,是否前往认证',
  556. 'confirmText' => '去认证',
  557. 'target_type' => 6,
  558. 'target_id' => '/pagesA/mine/realname/realname'
  559. ], '未实名认证');
  560. }
  561. }
  562. try {
  563. $cost = round($cost, 2);
  564. }catch (\Exception $e){
  565. return $this->fail(200004, [], '金额不合法');
  566. }
  567. $the_adoption = null;
  568. if($id > 0){
  569. $the_adoption = WxPetsAdoption::find($id);
  570. if($the_adoption->adopt_status == 1){
  571. return $this->fail(200004, [], '领养已经完成,无法再更改');
  572. }
  573. }
  574. global $__MINI_GLOBAL_IP__;
  575. // 1、必选信息做检查
  576. if(_empty_($name) || _empty_($pictures) || _empty_($pet_introduce) || _empty_($region) || _empty_($address) || _empty_($adopter_name) || _empty_($adopter_wechatId)){
  577. return $this->fail(200001);
  578. }
  579. if(count($region) !== 3){
  580. return $this->fail(200004, [], '请选择完整的地址');
  581. }
  582. try {
  583. $age_timestamp = PetUtils::age_range_transfer($age_range, null);
  584. }catch (\Exception $e){
  585. return $this->fail(200004, [], '宠物生日不合法');
  586. }
  587. if((time() - $age_timestamp) < (20 * 24 * 3600)){
  588. return $this->fail(200004, [], '小于20天的不方便进行发布领养');
  589. }
  590. if(!in_array($species, [1, 2])){
  591. return $this->fail(200004, [], '暂时只开放猫咪和狗狗的领养');
  592. }
  593. if(!in_array($is_free, [0, 1, 2])){
  594. return $this->fail(200004, [], '暂时只开放猫咪和狗狗的领养');
  595. }else{
  596. if($is_free == 1){
  597. // 有偿
  598. if($cost < 1){
  599. return $this->fail(200004, [], '请设置有偿领养的价格');
  600. }
  601. }else if($is_free == 2){
  602. if($cost < 1){
  603. return $this->fail(200004, [], '请设置押金具体金额');
  604. }
  605. }else{
  606. $isFree = 0;
  607. $cost = 0;
  608. }
  609. }
  610. if(!in_array($pet_gender, [0, 1, 2])){
  611. return $this->fail(200004, [], '宠物性别不合法');
  612. }
  613. if(!in_array($is_vaccine, [0, 1]) || !in_array($is_sterilization, [0, 1]) || !in_array($is_repellent, [0, 1])){
  614. return $this->fail(200004, [], '参数错误');
  615. }
  616. $condition_ = [];
  617. if($condition){
  618. $condition = array_unique($condition);
  619. foreach ($condition as $c){
  620. if(in_array($c, array_keys(PetUtils::adoption_condition()))){
  621. $condition_[] = $c;
  622. }
  623. }
  624. }
  625. $pictures_ = [];
  626. if($pictures){
  627. foreach ($pictures as $pic){
  628. $pictures_[] = [
  629. 'type' => 'img',
  630. 'url' => $pic['url']
  631. ];
  632. $image_state_change_list[] = $pic['url'];
  633. }
  634. }else{
  635. return $this->fail(200004, [], '图片参数错误');
  636. }
  637. if(_empty_($video_url)){
  638. $video_cover = null;
  639. }
  640. if(!_empty_($video_url)){
  641. if(_empty_($video_cover)){
  642. $res = ImageUtils::upload_video_screenshot($video_url);
  643. if($res && $res['url']){
  644. $video_cover = $res['url'];
  645. }
  646. }
  647. }
  648. if($video_url){
  649. $image_state_change_list[] = $video_url;
  650. if($video_cover){
  651. $image_state_change_list[] = $video_cover;
  652. }
  653. }
  654. // 百度审核
  655. if(UserUtils::user_permissions_check_by_config($uid, 'audit_pets_adoption_white_list')){
  656. $need_manual_review = false;
  657. }else{
  658. $need_manual_review = true;
  659. $is_audit_pets_adoption = Settings::get('is_audit_pets_adoption', 0);
  660. if ($is_audit_pets_adoption == 2) {
  661. $filter_result = BaiduUtils::text_filter($name);
  662. if($filter_result){
  663. if($filter_result['hit_level'] == 2){
  664. // 不合规
  665. return $this->fail(200016, ['tip'=>$filter_result['tip_list'], 'hit_word'=>$filter_result['hit_word']]);
  666. }else if($filter_result['hit_level'] == 0){
  667. // 审核通过
  668. $need_manual_review = false;
  669. }
  670. }
  671. }else if($is_audit_pets_adoption == 0){
  672. $need_manual_review = true;
  673. }else{
  674. $need_manual_review = false;
  675. }
  676. }
  677. // 0:审核中,1:审核通过,2:驳回
  678. if($need_manual_review){
  679. $adopt_status = 3;
  680. }
  681. if($the_adoption){
  682. $adoption = $the_adoption;
  683. }else{
  684. $adoption = new WxPetsAdoption();
  685. $adoption->user_id = $uid;
  686. }
  687. $adoption->name = $name;
  688. $adoption->pictures = json_encode($pictures_);
  689. if($video_url){
  690. $adoption->video_url = $video_url;
  691. $adoption->video_cover = $video_cover;
  692. }
  693. $adoption->species = $species;
  694. $adoption->pet_breed = $pet_breed;
  695. $adoption->is_free = $is_free;
  696. $adoption->cost = $cost;
  697. $adoption->pet_gender = $pet_gender;
  698. $adoption->pet_age = timetostr($age_timestamp);
  699. $adoption->is_vaccine = $is_vaccine;
  700. $adoption->is_sterilization = $is_sterilization;
  701. $adoption->is_repellent = $is_repellent;
  702. $adoption->pet_introduce = $pet_introduce;
  703. $adoption->condition = implode(',', $condition_);
  704. $adoption->province = $region[0];
  705. $adoption->city = $region[1] == '市辖区' ? $region[0] : $region[1];
  706. $adoption->district = $region[2];
  707. $adoption->address = $address;
  708. $adoption->adopter_name = $adopter_name;
  709. if($adopter_mobile){
  710. $adoption->adopter_mobile = $adopter_mobile;
  711. }
  712. $adoption->adopter_wechatId = $adopter_wechatId;
  713. $adoption->adopter_ip = $__MINI_GLOBAL_IP__;
  714. if($the_adoption){
  715. $adoption->adopt_status = $adopt_status;
  716. }else{
  717. $adoption->adopt_status = 0;
  718. }
  719. $r = $adoption->save();
  720. if($r){
  721. Utils::image_state_change($image_state_change_list, 1);
  722. return $this->success(['id'=>$adoption->id]);
  723. }else{
  724. return $this->fail(200006);
  725. }
  726. }
  727. }