Usercenter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. use Redis;
  7. /**
  8. * 会员中心,不是个人中心
  9. */
  10. class Usercenter extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. protected $allowFields = [
  15. 'id',
  16. 'username',
  17. 'nickname',
  18. // 'truename',
  19. 'mobile',
  20. 'avatar',
  21. // 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'idcard_status',
  28. 'cityname',
  29. 'photo_images',
  30. 'tag_ids',
  31. ];
  32. //搜索用户列表
  33. public function search_user_list(){
  34. $username = input('username','');
  35. if(!$username){
  36. $this->error();
  37. }
  38. $list = Db::name('user')->alias('user')
  39. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  40. ->field([
  41. 'user.id','user.username','user.nickname','user.avatar','user.bio','user.birthday','user.gender','user.longitude','user.latitude','user.height','user.weight','user.cityname',
  42. 'uw.vip_endtime',
  43. '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
  44. ])
  45. ->where('user.username',$username)->order('is_active desc,id asc')->autopage()->select();
  46. $list = list_domain_image($list,['avatar']);
  47. $list = list_birthday_age($list);
  48. if(!empty($list)){
  49. foreach($list as $key => &$val){
  50. //用户vip
  51. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  52. unset($val['vip_endtime']);
  53. $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
  54. if($this->auth->longitude == 0 || $val['longitude'] == 0){
  55. $val['distance'] = '未知';
  56. }
  57. //简单信息
  58. $simple_info = '';
  59. if(!empty($val['height'])){
  60. $simple_info .= $val['height'];
  61. }
  62. if(!empty($val['weight'])){
  63. if(!empty($simple_info)){
  64. $simple_info .= '|';
  65. }
  66. $simple_info .= $val['weight'];
  67. }
  68. if(!empty($val['cityname'])){
  69. if(!empty($simple_info)){
  70. $simple_info .= '|';
  71. }
  72. $simple_info .= $val['cityname'];
  73. }
  74. $val['simple_info'] = $simple_info;
  75. //是否关注
  76. $val['is_follow'] = $this->is_follow($this->auth->id,$val['id']);
  77. //
  78. unset($val['longitude']);
  79. unset($val['latitude']);
  80. unset($val['height']);
  81. unset($val['weight']);
  82. unset($val['cityname']);
  83. unset($val['vip_endtime']);
  84. unset($val['birthday']);
  85. }
  86. }
  87. $this->success('success',$list);
  88. }
  89. //获取他人用户信息
  90. public function getuserinfo_simple(){
  91. $uid = input_post('uid',0);
  92. $field = $this->allowFields;
  93. $field[] = 'longitude';
  94. $field[] = '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(longitude,latitude))*111195) as distance';
  95. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  96. if(!$userinfo){
  97. $this->error('不存在的用户');
  98. }
  99. //用户数据
  100. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  101. $new_data = [
  102. 'age' => birthtime_to_age($userinfo['birthday']),
  103. 'truename' => '',
  104. ];
  105. //合并
  106. $userinfo = array_merge($userinfo,$new_data);
  107. //枚举
  108. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name','color'])->select();
  109. //vip
  110. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  111. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  112. //是否喜欢和关注
  113. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  114. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  115. $userinfo['is_friend'] = ($userinfo['is_follow'] && $userinfo['is_fans']) ? 1 : 0;
  116. // $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  117. // $userinfo['is_like'] = $is_like ? 1 : 0;
  118. //是否拉黑
  119. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  120. $userinfo['is_black'] = $is_black ? 1 : 0;
  121. //关注人数,粉丝人数
  122. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  123. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  124. $userinfo['follow_num'] = $follow_num;
  125. $userinfo['fans_num'] = $fans_num;
  126. //活跃,在线
  127. $userinfo['active_info'] = $this->user_activeinfo($uid);
  128. //装扮
  129. $userheader = "";
  130. // 获取用户头像框信息
  131. $map = [
  132. 'a.user_id' => $uid,
  133. 'a.is_using' => 1,
  134. 'a.end_time' => ['gt',time()],
  135. ];
  136. $backResult = Db::name('user_decorate')->alias('a')
  137. ->field('a.id,a.decorate_type,b.base_image,b.play_image')
  138. ->join('decorate b', 'a.decorate_id = b.id')
  139. ->where($map)->order('a.id desc')->select();
  140. $backResult = list_domain_image($backResult,['base_image','play_image']);
  141. if ($backResult) {
  142. foreach ($backResult as $k => $v) {
  143. $v['decorate_type'] == 2 && $userheader = $v['play_image'];
  144. }
  145. }
  146. $userinfo['userheader'] = $userheader;
  147. //用户权限
  148. $userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  149. $userinfo['distance'] = bcdiv(intval($userinfo['distance']),1000,1).'km';
  150. if($this->auth->longitude == 0 || $userinfo['longitude'] == 0){
  151. unset($userinfo['longitude']);
  152. $userinfo['distance'] = '未知';
  153. }
  154. //vip如果开了隐私保护,需要隐藏距离
  155. $yinsi = $this->user_power($userinfo['id'],'yinsi');
  156. if($yinsi == 1){
  157. $userinfo['distance'] = '';
  158. $userinfo['cityname'] = '';
  159. }
  160. $this->success('success',$userinfo);
  161. }
  162. //获取他人用户信息,留下足迹
  163. public function getuserinfo(){
  164. $uid = input_post('uid',0);
  165. $field = $this->allowFields;
  166. $field[] = 'longitude';
  167. $field[] = '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(longitude,latitude))*111195) as distance';
  168. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  169. if(!$userinfo){
  170. $this->error('不存在的用户');
  171. }
  172. //用户数据
  173. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  174. // $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$uid)->find();
  175. $new_data = [
  176. 'age' => birthtime_to_age($userinfo['birthday']),
  177. // 'truename' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['truename'])) ? $idcard_confirm['truename'] : '',
  178. 'truename' => '',
  179. ];
  180. //合并
  181. $userinfo = array_merge($userinfo,$new_data);
  182. //枚举
  183. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name','color'])->select();
  184. //vip
  185. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  186. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  187. //是否喜欢和关注
  188. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  189. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  190. $userinfo['is_friend'] = ($userinfo['is_follow'] && $userinfo['is_fans']) ? 1 : 0;
  191. // $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  192. // $userinfo['is_like'] = $is_like ? 1 : 0;
  193. //是否拉黑
  194. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  195. $userinfo['is_black'] = $is_black ? 1 : 0;
  196. //关注人数,粉丝人数
  197. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  198. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  199. $userinfo['follow_num'] = $follow_num;
  200. $userinfo['fans_num'] = $fans_num;
  201. //查看别人信息,就要留下痕迹
  202. if($this->user_power($this->auth->id,'wuhen') != 1){
  203. if($this->apiLimit(1,1000) == true){
  204. $data = [
  205. 'uid' => $this->auth->id,
  206. 'to_uid' => $uid,
  207. ];
  208. $check = Db::name('user_visit')->where($data)->find();
  209. if($check){
  210. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  211. }else{
  212. $data['number'] = 1;
  213. $data['updatetime'] = time();
  214. Db::name('user_visit')->insertGetId($data);
  215. }
  216. }
  217. }
  218. //活跃,在线
  219. $userinfo['active_info'] = $this->user_activeinfo($uid);
  220. //装扮
  221. $userheader = "";
  222. // 获取用户头像框信息
  223. $map = [
  224. 'a.user_id' => $uid,
  225. 'a.is_using' => 1,
  226. 'a.end_time' => ['gt',time()],
  227. ];
  228. $backResult = Db::name('user_decorate')->alias('a')
  229. ->field('a.id,a.decorate_type,b.base_image,b.play_image')
  230. ->join('decorate b', 'a.decorate_id = b.id')
  231. ->where($map)->order('a.id desc')->select();
  232. $backResult = list_domain_image($backResult,['base_image','play_image']);
  233. if ($backResult) {
  234. foreach ($backResult as $k => $v) {
  235. $v['decorate_type'] == 2 && $userheader = $v['play_image'];
  236. }
  237. }
  238. $userinfo['userheader'] = $userheader;
  239. //用户权限
  240. $userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  241. $userinfo['distance'] = bcdiv(intval($userinfo['distance']),1000,1).'km';
  242. if($this->auth->longitude == 0 || $userinfo['longitude'] == 0){
  243. unset($userinfo['longitude']);
  244. $userinfo['distance'] = '未知';
  245. }
  246. //vip如果开了隐私保护,需要隐藏距离
  247. $yinsi = $this->user_power($userinfo['id'],'yinsi');
  248. if($yinsi == 1){
  249. $userinfo['distance'] = '';
  250. $userinfo['cityname'] = '';
  251. }
  252. $this->success('success',$userinfo);
  253. }
  254. //获取他人主页,简版
  255. public function getuserinfo_domain(){
  256. $uid = input_post('uid',0);
  257. $field = $this->allowFields;
  258. $field[] = 'longitude';
  259. $field[] = '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(longitude,latitude))*111195) as distance';
  260. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  261. if(!$userinfo){
  262. $this->error('不存在的用户');
  263. }
  264. //用户数据
  265. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  266. //是否关注
  267. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  268. $userinfo['is_vip'] = $this->is_vip($uid);
  269. //是否拉黑
  270. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  271. $userinfo['is_black'] = $is_black ? 1 : 0;
  272. //关注人数,粉丝人数
  273. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  274. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  275. $userinfo['follow_num'] = $follow_num;
  276. $userinfo['fans_num'] = $fans_num;
  277. //查看别人信息,就要留下痕迹
  278. if($this->user_power($this->auth->id,'wuhen') != 1){
  279. if($this->apiLimit(1,1000) == true){
  280. $data = [
  281. 'uid' => $this->auth->id,
  282. 'to_uid' => $uid,
  283. ];
  284. $check = Db::name('user_visit')->where($data)->find();
  285. if($check){
  286. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  287. }else{
  288. $data['number'] = 1;
  289. $data['updatetime'] = time();
  290. Db::name('user_visit')->insertGetId($data);
  291. }
  292. }
  293. }
  294. //活跃,在线
  295. $userinfo['active_info'] = $this->user_activeinfo($uid);
  296. $userinfo['distance'] = bcdiv(intval($userinfo['distance']),1000,1).'km';
  297. if($this->auth->longitude == 0 || $userinfo['longitude'] == 0){
  298. unset($userinfo['longitude']);
  299. $userinfo['distance'] = '未知';
  300. }
  301. //vip如果开了隐私保护,需要隐藏距离
  302. $yinsi = $this->user_power($userinfo['id'],'yinsi');
  303. if($yinsi == 1){
  304. $userinfo['distance'] = '';
  305. $userinfo['cityname'] = '';
  306. }
  307. $this->success('success',$userinfo);
  308. }
  309. public function getuserinfo_domain2(){
  310. $uid = input_post('uid',0);
  311. $field = $this->allowFields;
  312. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  313. if(!$userinfo){
  314. $this->error('不存在的用户');
  315. }
  316. $new_data = [
  317. 'age' => birthtime_to_age($userinfo['birthday']),
  318. ];
  319. //合并
  320. $userinfo = array_merge($userinfo,$new_data);
  321. //枚举
  322. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name','color'])->select();
  323. //vip如果开了隐私保护,需要隐藏距离
  324. $yinsi = $this->user_power($userinfo['id'],'yinsi');
  325. if($yinsi == 1){
  326. $userinfo['cityname'] = '';
  327. }
  328. $this->success('success',$userinfo);
  329. }
  330. /**
  331. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  332. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  333. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  334. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  335. * @return float | false | string
  336. */
  337. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  338. if( empty( $point_1 ) || empty( $point_2 ) ){
  339. return false;
  340. }
  341. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  342. $p1_x = $point_1[0];
  343. $p1_y = $point_1[1];
  344. $p2_x = $point_2[0];
  345. $p2_y = $point_2[1];
  346. if(
  347. $p1_x < -180 || $p1_x > 180
  348. || $p2_x < -180 || $p2_x > 180
  349. || $p1_y < -90 || $p1_y > 90
  350. || $p2_y < -90 || $p2_y > 90
  351. ){
  352. return '0公里';
  353. }
  354. // 根据2点各自的坐标,计算2点之间直线距离的公式
  355. $distance = round(6378.138*2*asin(sqrt(pow(sin(( $p1_x *pi()/180-$p2_x*pi()/180)/2),2)+cos( $p1_x *pi()/180)*cos($p2_x*pi()/180)* pow(sin(( $p1_y *pi()/180-$p2_y*pi()/180)/2),2)))*1000);
  356. // 是否计算为字符串公里距离
  357. if( !$calc_as_string ){
  358. return (string)round( $distance / 1000 , 1 ) . '公里';
  359. }
  360. // 如果计算为字符串公里距离
  361. if( $distance / 1000 > 1 ){
  362. $k = (string)round( $distance / 1000 , 1 );
  363. $m = (string)$distance % 1000 ;
  364. $distance = "{$k}公里{$m}米";
  365. }
  366. else{
  367. $distance = "{$distance}米";
  368. }
  369. return $distance;
  370. }
  371. //地图api,根据两地坐标,获得两地距离,打卡用的
  372. //type=0直线,type=1开车
  373. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  374. $result = 0;
  375. $apiurl = 'https://restapi.amap.com/v3/distance?';
  376. $param = [
  377. 'key' => '398c424811d1a59beac2f915323d334e',
  378. 'origins' => $start_lon.','.$start_lat,
  379. 'destination' => $end_lon.','.$end_lat,
  380. 'type' => $type,
  381. 'output' => 'json',
  382. ];
  383. $apiurl .= http_build_query($param);
  384. $request_rs = json_decode(curl_get($apiurl),true);
  385. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  386. if(isset($request_rs['results'][0]['distance']))
  387. {
  388. $result = $request_rs['results'][0]['distance'];
  389. }
  390. }
  391. //dump($result);
  392. return $result;
  393. }
  394. public function distance()
  395. {
  396. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  397. dump($a);
  398. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  399. dump($a);
  400. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  401. dump($b);
  402. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  403. dump($b);
  404. }
  405. }