Index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Apply;
  5. use app\common\model\Area;
  6. use app\common\model\Banner;
  7. use app\common\model\Like;
  8. use app\common\model\LikeMutually;
  9. use app\common\model\Report;
  10. use app\common\model\Shield;
  11. use app\common\model\Unlockprogress;
  12. use app\common\model\User;
  13. use app\common\model\UserObject;
  14. use DateTime;
  15. use think\Db;
  16. use think\Exception;
  17. /**
  18. * 首页接口
  19. */
  20. class Index extends Api
  21. {
  22. protected $noNeedLogin = ['index','slideshow','recommend'];
  23. protected $noNeedRight = ['*'];
  24. /**
  25. * 首页
  26. *
  27. */
  28. public function index()
  29. {
  30. $this->success('请求成功');
  31. }
  32. /**
  33. * 搜索
  34. * @param string $min_age 最小年龄
  35. * @param string $max_age 最大年龄
  36. * @param string $min_height 最小身高
  37. * @param string $max_height 最大身高
  38. * @param string $region_province 家乡所在省份id
  39. * @param string $region_city 家乡所在市区id
  40. * @param string $province 所在城市省份id
  41. * @param string $city 所在城市市区id
  42. * @param string $education 学历
  43. * @param string $gender 男女1男0女
  44. */
  45. public function search(){
  46. $where = [
  47. 'status'=>'normal',
  48. 'id'=>['neq',$this->auth->id]
  49. ];
  50. $where['height']=['Between',[input('min_height'),input('max_height')]];
  51. if(input('region_province')){
  52. $where['region_province']=input('region_province');
  53. $where['region_city']=input('region_city');
  54. }
  55. if(input('province')){
  56. $where['province']=input('province');
  57. $where['city']=input('city');
  58. }
  59. input('education')?$where['education']=input('education'):'';
  60. if(input('gender')!='')$where['gender']=input('gender');
  61. //屏蔽用户
  62. $shield = Shield::where(['uid'=>$this->auth->id])->field('nid')->select();
  63. $shieldArr = [];
  64. foreach ($shield as $v){
  65. array_push($shieldArr,$v['nid']);
  66. }
  67. if($shieldArr){
  68. $data = User::where($where)
  69. ->where(['id_authentication'=>1])
  70. ->whereNotIn('id',$shieldArr)
  71. ->field('id,avatar,height,region_province,region_city,province,city,birthday,education,nickname,gender')
  72. ->select();
  73. }else{
  74. $data = User::where($where)
  75. ->where(['id_authentication'=>1])
  76. ->field('id,avatar,height,region_province,region_city,province,city,birthday,education,nickname,gender')
  77. ->select();
  78. }
  79. foreach ($data as $k=>&$v){
  80. $v['birthday'] = getAge($v['birthday']);
  81. if($v['birthday']<input('min_age') || $v['birthday']>input('max_age')){
  82. unset($data[$k]);
  83. }
  84. }
  85. $data = array_values($data);
  86. $this->success('',$data);
  87. }
  88. /**
  89. * 搜索内容详情
  90. * @ApiMethod (POST)
  91. * @param string $nid 解锁用户id
  92. */
  93. public function searchDetails(){
  94. $where = [
  95. 'uid'=>$this->auth->id,
  96. 'nid'=>input('nid')
  97. ];
  98. $res = User::where('id',input('nid'))
  99. ->with('userObject.education,education,marriedtime')
  100. ->find();
  101. $unlockprogress = Unlockprogress::where($where)->find();
  102. if($unlockprogress){
  103. $res['unlockprogress'] =$unlockprogress['status'];
  104. }else{
  105. $res['unlockprogress'] =0;
  106. }
  107. $res['birthday'] = getAge($res['birthday']);
  108. $res['province'] = Area::where('id',$res['province'])->value('name');
  109. $res['city'] = Area::where('id',$res['city'])->value('name');
  110. $res['area'] = Area::where('id',$res['area'])->value('name');
  111. Like::where($where)->where('type',1)->find()?$res['like']=1:$res['like']=0;
  112. $u_user = user::get(['id'=>$this->auth->id]);
  113. if($u_user&&$u_user['constellation']&&$u_user['birthday']&&$u_user['height']&&$u_user['marriage']&&$u_user['children']&&$u_user['marriedtime']&&$u_user['education']&&$u_user['school']&&$u_user['house']&&$u_user['car']&&$u_user['father_work']&&$u_user['mother_work']&&$u_user['brother']){
  114. $res['is_about_yourself'] = 1;
  115. }else{
  116. $res['is_about_yourself'] = 0;
  117. }
  118. $UserObject = UserObject::get(['uid'=>input('nid')]);
  119. if($UserObject&&$UserObject['constellation']&&$UserObject['min_age']&&$UserObject['min_height']&&$UserObject['education']&&$UserObject['car']&&$UserObject['house']&&$UserObject['marry']&&$UserObject['children']&&$UserObject['locality']){
  120. $res['is_about_object'] = 1;
  121. }else{
  122. $res['is_about_object'] = 0;
  123. }
  124. if($res)$this->success('',$res);
  125. }
  126. /**
  127. * 解锁进度页
  128. * @ApiMethod (POST)
  129. * @param string $nid 解锁用户id
  130. * @param string $type 解锁类型1照片2资料3微信
  131. */
  132. public function unlockProgress(){
  133. $type = input('type');
  134. $uid = $this->auth->id;
  135. if($type > 1) {
  136. $user = Unlockprogress::where(['uid'=>$this->auth->id,'nid'=>input('nid')])->find();
  137. if(isset($user)){
  138. if($user['status'] == input('type')){
  139. $this->error('无需重复解锁');
  140. }
  141. }else{
  142. $this->error('非法操作');
  143. }
  144. }
  145. if($type == 1){
  146. $cost = config('site.expenditure_grain')['解锁用户照片'];
  147. if($cost>$this->auth->money)$this->error('余额不足');
  148. Db::startTrans();
  149. try {
  150. $data = [
  151. 'uid'=>$this->auth->id,
  152. 'nid'=>input('nid'),
  153. 'status'=>1
  154. ];
  155. Unlockprogress::insert($data);
  156. user::money(-$cost,$uid,'解锁用户照片');
  157. Db::commit();
  158. $this->success('解锁照片成功');
  159. }catch (Exception $e){
  160. Db::rollback();
  161. $this->error($e);
  162. return false;
  163. }
  164. }elseif($type == 2){
  165. $cost = config('site.expenditure_grain')['解锁用户资料'];
  166. if($cost>$this->auth->money)$this->error('余额不足');
  167. Db::startTrans();
  168. try {
  169. $update = ['status'=>2];
  170. $user->save($update);
  171. $look = [
  172. 'uid'=>$this->auth->id,
  173. 'nid'=>input('nid'),
  174. 'type'=>2,
  175. 'status'=>0
  176. ];
  177. Like::insert($look);
  178. user::money(-$cost,$uid,'解锁用户资料');
  179. Db::commit();
  180. $this->success('解锁资料成功');
  181. }catch (Exception $e){
  182. Db::rollback();
  183. $this->error($e);
  184. return false;
  185. }
  186. }elseif($type == 3){
  187. $cost = config('site.expenditure_grain')['申请认识支出'];
  188. if($cost>$this->auth->money)$this->error('余额不足');
  189. Db::startTrans();
  190. try {
  191. $update = ['status'=>3];
  192. $user->save($update);
  193. $apply=[
  194. 'uid'=>$this->auth->id,
  195. 'nid'=>input('nid'),
  196. 'status'=>0,
  197. 'grain'=>$cost
  198. ];
  199. Apply::insert($apply);
  200. user::money(-$cost,$uid,'申请认识支出');
  201. Apply::wxMessage(input('nid'),Date('Y-m-d H:i:s'));
  202. Db::commit();
  203. $this->success('解锁成功');
  204. }catch (Exception $e){
  205. Db::rollback();
  206. $this->error($e);
  207. return false;
  208. }
  209. }
  210. }
  211. /**
  212. * 轮播图
  213. */
  214. public function slideshow(){
  215. $this->success('',Banner::all());
  216. }
  217. /**
  218. * 首页推荐会员
  219. * @ApiMethod (POST)
  220. * @param string $gender 性别
  221. * @param string $city 城市
  222. */
  223. public function recommend(){
  224. $uid = $this->auth->id;
  225. $city = $this->request->post('city');
  226. if($this->auth->gender){
  227. $gender = 0;
  228. }else{
  229. $gender = 1;
  230. }
  231. //屏蔽用户
  232. $shield = Shield::where(['uid'=>$uid])->field('nid')->select();
  233. $shieldArr = [];
  234. foreach ($shield as $v){
  235. array_push($shieldArr,$v['nid']);
  236. }
  237. $where = [
  238. 'gender'=>input('gender',$gender),
  239. 'is_recommend'=>1
  240. ];
  241. if($city)$where['city']=$city;
  242. if($shieldArr){
  243. $recommenduser = user::where($where)->whereNotIn('id',$shieldArr)->order('recommend_weight','desc')->paginate(3);
  244. }else{
  245. $recommenduser = user::where($where)->order('recommend_weight','desc')->paginate(3);
  246. }
  247. foreach ($recommenduser as $k=>&$v){
  248. $v['birthday'] = $v['birthday']?getAge($v['birthday']):'';
  249. Like::get(['uid'=>$uid,'nid'=>$v['id'],'type'=>1])?$v['is_like']=1:$v['is_like']=0;
  250. Apply::get(['uid'=>$uid,'nid'=>$v['id'],'status'=>['in','1,2']])?$v['is_apply']=1:$v['is_apply']=0;
  251. $v['province'] = $v['province']?Area::where('id',$v['province'])->value('name'):'';
  252. $v['city'] = $v['city']?Area::where('id',$v['city'])->value('name'):'';
  253. $v['area'] = $v['area']?Area::where('id',$v['area'])->value('name'):'';
  254. }
  255. $this->success('',$recommenduser);
  256. }
  257. /**
  258. * 会员详情
  259. * @ApiMethod (POST)
  260. * @param string $id 会员id
  261. */
  262. public function recommendUserDetail(){
  263. $recommenduser = user::where('id',input('id'))
  264. ->with('userObject.education,education,marriedtime')
  265. ->find();
  266. $recommenduser['birthday'] = $recommenduser['birthday']?getAge($recommenduser['birthday']):0;
  267. $recommenduser['photo'] = explode(',',$recommenduser['photo']);
  268. $recommenduser['province'] = Area::where('id',$recommenduser['province'])->value('name');
  269. $recommenduser['city'] = Area::where('id',$recommenduser['city'])->value('name');
  270. $recommenduser['area'] = Area::where('id',$recommenduser['area'])->value('name');
  271. $recommenduser['age'] = $recommenduser['birthday'];
  272. Like::where([
  273. 'uid'=>$this->auth->id,
  274. 'nid'=>input('id')
  275. ])->where('type',1)->find()?$recommenduser['like']=1:$recommenduser['like']=0;
  276. $u_user = user::get(['id'=>$this->auth->id]);
  277. if($u_user&&$u_user['constellation']&&$u_user['birthday']&&$u_user['height']&&$u_user['marriage']&&$u_user['children']&&$u_user['marriedtime']&&$u_user['education']&&$u_user['school']&&$u_user['house']&&$u_user['car']&&$u_user['father_work']&&$u_user['mother_work']&&$u_user['brother']){
  278. $recommenduser['is_about_yourself'] = 1;
  279. }else{
  280. $recommenduser['is_about_yourself'] = 0;
  281. }
  282. $UserObject = UserObject::get(['uid'=>$this->auth->id]);
  283. if($UserObject&&$UserObject['constellation']&&$UserObject['min_age']&&$UserObject['min_height']&&$UserObject['education']&&$UserObject['car']&&$UserObject['house']&&$UserObject['marry']&&$UserObject['children']&&$UserObject['locality']){
  284. $recommenduser['is_about_object'] = 1;
  285. }else{
  286. $recommenduser['is_about_object'] = 0;
  287. }
  288. //查看
  289. $look = Like::get(['uid'=>$this->auth->id,'nid'=>input('id')]);
  290. if(!$look){
  291. $look = [
  292. 'uid'=>$this->auth->id,
  293. 'nid'=>input('id'),
  294. 'type'=>2,
  295. 'status'=>0
  296. ];
  297. Like::insert($look);
  298. }
  299. Apply::get(['nid'=>$recommenduser['id'],'uid'=>$this->auth->id])?$recommenduser['is_user_wx']=1:$recommenduser['is_user_wx']=0;
  300. //发送微信模板消息
  301. Apply::wxVisitorMessage($u_user['username'],input('id'),Date('Y-m-d H:i:s'));
  302. $this->success('',$recommenduser);
  303. }
  304. /**
  305. * 喜欢
  306. * @ApiMethod (POST)
  307. * @param string $nid 喜欢用户id
  308. */
  309. public function like(){
  310. $uid = $this->auth->id;
  311. if($this->auth->score > 0){
  312. $data = [
  313. 'uid'=>$uid,
  314. 'nid'=>input('nid'),
  315. 'type'=>1
  316. ];
  317. $user = Like::where($data)->find();
  318. if($user)$this->error('您已喜欢该用户');
  319. User::score(-1,$uid,'喜欢用户');
  320. if(Like::insert($data)){
  321. //查询是否喜欢我
  322. $mutually = [
  323. 'uid' => input('nid'),
  324. 'nid' => $uid
  325. ];
  326. $mutually_data = Like::get($mutually);
  327. if($mutually_data){
  328. $like_mutually = [
  329. 'uid'=>$uid,
  330. 'nid'=>input('nid')
  331. ];
  332. LikeMutually::create($like_mutually);
  333. $like_mutually2 = [
  334. 'uid'=>input('nid'),
  335. 'nid'=>$uid
  336. ];
  337. LikeMutually::create($like_mutually2);
  338. }
  339. $this->success('已喜欢');
  340. }else{
  341. $this->error('系统异常');
  342. }
  343. }else{
  344. $this->error('喜欢额度不足');
  345. }
  346. }
  347. /**
  348. * 解锁微信
  349. * @ApiMethod (POST)
  350. * @param (name="id",description='解锁用户id')
  351. */
  352. public function unlockWx(){
  353. $cost = config('site.expenditure_grain')['申请认识支出'];
  354. if($cost>$this->auth->money)$this->error('余额不足');
  355. $apply_=Apply::where(['nid'=>input('id'),'status'=>0])->find();
  356. $apply_?$this->error('您已申请认识'):'';
  357. Db::startTrans();
  358. try {
  359. $apply=[
  360. 'uid'=>$this->auth->id,
  361. 'nid'=>input('id'),
  362. 'status'=>0,
  363. 'grain'=>$cost
  364. ];
  365. Apply::insert($apply);
  366. user::money(-$cost,$this->auth->id,'申请认识支出');
  367. Apply::wxMessage(input('id'),Date('Y-m-d H:i:s'));
  368. Db::commit();
  369. $this->success('解锁成功');
  370. }catch (Exception $e){
  371. Db::rollback();
  372. $this->error($e);
  373. return false;
  374. }
  375. }
  376. /**
  377. * 举报
  378. * @ApiMethod (POST)
  379. * @param string $nid 举报用户id
  380. * @param string $content 举报内容
  381. * @param string $image 举报图片
  382. */
  383. public function report(){
  384. $rule = [
  385. 'content|举报内容'=>'require',
  386. 'image|图片'=>'require',
  387. 'nid|举报用户'=>'require'
  388. ];
  389. $data = $this->_validate($rule);
  390. $data['uid'] = $this->auth->id;
  391. $res = Report::insert($data);
  392. if($res){
  393. $this->success('提交成功');
  394. }
  395. $this->error('提交失败');
  396. }
  397. /**
  398. * 屏蔽
  399. * @ApiMethod (POST)
  400. * @param string $type 1屏蔽2解除屏蔽
  401. * @param string $nid 屏蔽用户id(type为1时传)
  402. * @param string $id id(type为2时传)
  403. */
  404. public function shield(){
  405. if(input('type') == 1){
  406. $data = [
  407. 'uid'=>$this->auth->id,
  408. 'nid'=>input('nid')
  409. ];
  410. Shield::where($data)->find()?$this->error('您已屏蔽该用户,无需重复操作'):Shield::insert($data);
  411. $this->success('操作成功');
  412. }else{
  413. Shield::where('id',input('id'))->delete()?$this->success('解除成功'):$this->error('解除失败');
  414. }
  415. }
  416. /**
  417. * 申请认识你弹窗
  418. * @ApiReturnParams (name='count',description='会员个数')
  419. */
  420. public function applyList(){
  421. $data = Apply::all(['nid'=>$this->auth->id,'n_is_read'=>0],['uidinfo']);
  422. $count = count($data);
  423. $this->success('',['data'=>$data,'count'=>$count]);
  424. }
  425. }