Usercenter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. 'avatar',
  19. 'mobile',
  20. 'idcard_status',
  21. 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'audio_bio',
  28. 'video_bio',
  29. 'photo_images',
  30. 'marital_id',
  31. 'job_id',
  32. 'suqiu_id',
  33. 'wages_id',
  34. 'tag_ids',
  35. 'hobby_ids',
  36. 'open_match_video',
  37. 'open_match_audio',
  38. 'open_match_typing',
  39. 'match_video_price',
  40. 'match_audio_price',
  41. 'match_typing_price',
  42. ];
  43. //获取他人用户信息
  44. public function getuserinfo_simple(){
  45. $uid = input_post('uid',0);
  46. $field = $this->allowFields;
  47. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  48. if(!$userinfo){
  49. $this->error('不存在的用户');
  50. }
  51. //用户数据
  52. $userinfo = info_domain_image($userinfo,['avatar','photo_images','video_bio','audio_bio']);
  53. $new_data = [
  54. 'age' => birthtime_to_age($userinfo['birthday']),
  55. 'birthday' => date('Y-m-d',$userinfo['birthday']),
  56. ];
  57. //合并
  58. $userinfo = array_merge($userinfo,$new_data);
  59. //枚举
  60. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  61. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->field(['id','name'])->select();
  62. //vip
  63. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  64. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  65. //是否喜欢和关注
  66. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  67. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  68. $userinfo['is_friend'] = $this->is_friend($this->auth->id,$uid);
  69. //是否拉黑
  70. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  71. $userinfo['is_black'] = $is_black ? 1 : 0;
  72. //关注人数,粉丝人数
  73. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  74. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  75. $userinfo['follow_num'] = $follow_num;
  76. $userinfo['fans_num'] = $fans_num;
  77. //活跃,在线
  78. $userinfo['active_info'] = $this->user_activeinfo($uid);
  79. //用户权限
  80. $userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  81. //vip如果开了隐私保护,需要隐藏距离
  82. $weizhi = $this->user_power($userinfo['id'],'weizhi');
  83. if($weizhi == 1){
  84. // $userinfo['cityname'] = '';
  85. }
  86. //追加登录用户的头像
  87. $userinfo['my_avatar'] = localpath_to_netpath($this->auth->avatar);
  88. //此用户与我的亲密度 信息
  89. //最新一条动态
  90. $last_dongtai = Db::name('topic_dongtai')->where('user_id',$uid)->where('type',1)->where('auditstatus',1)->order('id desc')->find();
  91. $last_dongtai = info_domain_image($last_dongtai,['images']);
  92. $this->success('success',$userinfo);
  93. }
  94. //获取他人用户信息,留下足迹
  95. public function getuserinfo(){
  96. $uid = input_post('uid',0);
  97. $field = $this->allowFields;
  98. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  99. if(!$userinfo){
  100. $this->error('不存在的用户');
  101. }
  102. //用户数据
  103. $userinfo = info_domain_image($userinfo,['avatar','photo_images','video_bio','audio_bio']);
  104. $new_data = [
  105. 'age' => birthtime_to_age($userinfo['birthday']),
  106. 'birthday' => date('Y-m-d',$userinfo['birthday']),
  107. ];
  108. //合并
  109. $userinfo = array_merge($userinfo,$new_data);
  110. //枚举
  111. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  112. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->field(['id','name'])->select();
  113. //vip
  114. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  115. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  116. //是否喜欢和关注
  117. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  118. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  119. $userinfo['is_friend'] = $this->is_friend($this->auth->id,$uid);
  120. //是否拉黑
  121. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  122. $userinfo['is_black'] = $is_black ? 1 : 0;
  123. //关注人数,粉丝人数
  124. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  125. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  126. $userinfo['follow_num'] = $follow_num;
  127. $userinfo['fans_num'] = $fans_num;
  128. //查看别人信息,就要留下痕迹
  129. if($this->apiLimit(1,1000) == true){
  130. $data = [
  131. 'uid' => $this->auth->id,
  132. 'to_uid' => $uid,
  133. ];
  134. $check = Db::name('user_visit')->where($data)->find();
  135. if($check){
  136. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  137. }else{
  138. $data['number'] = 1;
  139. $data['updatetime'] = time();
  140. Db::name('user_visit')->insertGetId($data);
  141. }
  142. }
  143. //活跃,在线
  144. $userinfo['active_info'] = $this->user_activeinfo($uid);
  145. //用户权限
  146. $userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  147. //vip如果开了隐私保护,需要隐藏距离
  148. $weizhi = $this->user_power($userinfo['id'],'weizhi');
  149. if($weizhi == 1){
  150. // $userinfo['cityname'] = '';
  151. }
  152. //追加登录用户的头像
  153. $userinfo['my_avatar'] = localpath_to_netpath($this->auth->avatar);
  154. //此用户与我的亲密度 信息
  155. $this->success('success',$userinfo);
  156. }
  157. //给对方设置备注
  158. public function set_nickname_remark(){
  159. if($this->apiLimit(1,1000) == false){
  160. $this->error('操作频繁');
  161. }
  162. $user_id = input('user_id');
  163. $remark = input('nickname_remark','');
  164. $map = [
  165. 'uid' => $this->auth->id,
  166. 'to_uid' => $user_id,
  167. ];
  168. $check = Db::name('nickname_remark')->where($map)->find();
  169. if($check){
  170. Db::name('nickname_remark')->where($map)->update(['nickname_remark'=>$remark]);
  171. }else{
  172. $map['nickname_remark'] = $remark;
  173. Db::name('nickname_remark')->insertGetId($map);
  174. }
  175. $this->success('设置成功');
  176. }
  177. //搜索用户列表
  178. public function search_user_list(){
  179. $username = input_post('username','');
  180. if(!$username){
  181. $this->error();
  182. }
  183. $list = Db::name('user')->alias('user')
  184. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  185. ->field('user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  186. ->where('user.username',$username)->order('is_active desc,id asc')->autopage()->select();
  187. $list = list_domain_image($list,['avatar']);
  188. $list = list_birthday_age($list);
  189. if(!empty($list)){
  190. foreach($list as $key => &$val){
  191. //用户vip
  192. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  193. unset($val['vip_endtime']);
  194. //是否关注
  195. $val['is_follow'] = $this->is_follow($this->auth->id,$val['id']);
  196. }
  197. }
  198. $this->success('success',$list);
  199. }
  200. /**
  201. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  202. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  203. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  204. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  205. * @return float | false | string
  206. */
  207. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  208. if( empty( $point_1 ) || empty( $point_2 ) ){
  209. return false;
  210. }
  211. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  212. $p1_x = $point_1[0];
  213. $p1_y = $point_1[1];
  214. $p2_x = $point_2[0];
  215. $p2_y = $point_2[1];
  216. if(
  217. $p1_x < -180 || $p1_x > 180
  218. || $p2_x < -180 || $p2_x > 180
  219. || $p1_y < -90 || $p1_y > 90
  220. || $p2_y < -90 || $p2_y > 90
  221. ){
  222. return '0公里';
  223. }
  224. // 根据2点各自的坐标,计算2点之间直线距离的公式
  225. $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);
  226. // 是否计算为字符串公里距离
  227. if( !$calc_as_string ){
  228. return (string)round( $distance / 1000 , 1 ) . '公里';
  229. }
  230. // 如果计算为字符串公里距离
  231. if( $distance / 1000 > 1 ){
  232. $k = (string)round( $distance / 1000 , 1 );
  233. $m = (string)$distance % 1000 ;
  234. $distance = "{$k}公里{$m}米";
  235. }
  236. else{
  237. $distance = "{$distance}米";
  238. }
  239. return $distance;
  240. }
  241. //地图api,根据两地坐标,获得两地距离,打卡用的
  242. //type=0直线,type=1开车
  243. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  244. $result = 0;
  245. $apiurl = 'https://restapi.amap.com/v3/distance?';
  246. $param = [
  247. 'key' => '398c424811d1a59beac2f915323d334e',
  248. 'origins' => $start_lon.','.$start_lat,
  249. 'destination' => $end_lon.','.$end_lat,
  250. 'type' => $type,
  251. 'output' => 'json',
  252. ];
  253. $apiurl .= http_build_query($param);
  254. $request_rs = json_decode(curl_get($apiurl),true);
  255. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  256. if(isset($request_rs['results'][0]['distance']))
  257. {
  258. $result = $request_rs['results'][0]['distance'];
  259. }
  260. }
  261. //dump($result);
  262. return $result;
  263. }
  264. public function distance()
  265. {
  266. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  267. dump($a);
  268. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  269. dump($a);
  270. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  271. dump($b);
  272. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  273. dump($b);
  274. }
  275. }