Usercenter.php 16 KB

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