Usercenter.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_post('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. $userinfo = Db::name('user')->field($this->allowFields)->where('id|username',$uid)->find();
  61. if(!$userinfo){
  62. $this->error('不存在的用户');
  63. }
  64. //用户数据
  65. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  66. $userinfo['age'] = birthtime_to_age($userinfo['birthday']);
  67. $this->success('success',$userinfo);
  68. }
  69. //获取他人用户信息,留下足迹
  70. public function getuserinfo(){
  71. $uid = input_post('uid',0);
  72. $userinfo = Db::name('user')->field($this->allowFields)->where('id|username',$uid)->find();
  73. if(!$userinfo){
  74. $this->error('不存在的用户');
  75. }
  76. //用户数据
  77. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  78. $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$uid)->find();
  79. $new_data = [
  80. 'age' => birthtime_to_age($userinfo['birthday']),
  81. 'truename' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['truename'])) ? $idcard_confirm['truename'] : '',
  82. ];
  83. //合并
  84. $userinfo = array_merge($userinfo,$new_data);
  85. //枚举
  86. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  87. //vip
  88. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  89. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  90. //是否喜欢和关注
  91. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  92. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  93. $userinfo['is_like'] = $is_like ? 1 : 0;
  94. //是否拉黑
  95. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  96. $userinfo['is_black'] = $is_black ? 1 : 0;
  97. //关注人数,粉丝人数
  98. $follow_num = Db::name('user_follow')->where(['uid'=>$this->auth->id])->count('id');
  99. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$this->auth->id])->count('id');
  100. $userinfo['follow_num'] = $follow_num;
  101. $userinfo['fans_num'] = $fans_num;
  102. //查看别人信息,就要留下痕迹
  103. if($this->user_power($this->auth->id,'wuhen') != 1){
  104. $data = [
  105. 'uid' => $this->auth->id,
  106. 'to_uid' => $uid,
  107. ];
  108. $check = Db::name('user_visit')->where($data)->find();
  109. if($check){
  110. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  111. }else{
  112. $data['number'] = 1;
  113. $data['updatetime'] = time();
  114. Db::name('user_visit')->insertGetId($data);
  115. }
  116. }
  117. //活跃,在线
  118. $userinfo['active_info'] = $this->user_activeinfo($uid);
  119. //用户权限
  120. //$userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  121. $this->success('success',$userinfo);
  122. }
  123. /**
  124. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  125. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  126. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  127. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  128. * @return float | false | string
  129. */
  130. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  131. if( empty( $point_1 ) || empty( $point_2 ) ){
  132. return false;
  133. }
  134. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  135. $p1_x = $point_1[0];
  136. $p1_y = $point_1[1];
  137. $p2_x = $point_2[0];
  138. $p2_y = $point_2[1];
  139. if(
  140. $p1_x < -180 || $p1_x > 180
  141. || $p2_x < -180 || $p2_x > 180
  142. || $p1_y < -90 || $p1_y > 90
  143. || $p2_y < -90 || $p2_y > 90
  144. ){
  145. return '0公里';
  146. }
  147. // 根据2点各自的坐标,计算2点之间直线距离的公式
  148. $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);
  149. // 是否计算为字符串公里距离
  150. if( !$calc_as_string ){
  151. return (string)round( $distance / 1000 , 1 ) . '公里';
  152. }
  153. // 如果计算为字符串公里距离
  154. if( $distance / 1000 > 1 ){
  155. $k = (string)round( $distance / 1000 , 1 );
  156. $m = (string)$distance % 1000 ;
  157. $distance = "{$k}公里{$m}米";
  158. }
  159. else{
  160. $distance = "{$distance}米";
  161. }
  162. return $distance;
  163. }
  164. //地图api,根据两地坐标,获得两地距离,打卡用的
  165. //type=0直线,type=1开车
  166. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  167. $result = 0;
  168. $apiurl = 'https://restapi.amap.com/v3/distance?';
  169. $param = [
  170. 'key' => '398c424811d1a59beac2f915323d334e',
  171. 'origins' => $start_lon.','.$start_lat,
  172. 'destination' => $end_lon.','.$end_lat,
  173. 'type' => $type,
  174. 'output' => 'json',
  175. ];
  176. $apiurl .= http_build_query($param);
  177. $request_rs = json_decode(curl_get($apiurl),true);
  178. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  179. if(isset($request_rs['results'][0]['distance']))
  180. {
  181. $result = $request_rs['results'][0]['distance'];
  182. }
  183. }
  184. //dump($result);
  185. return $result;
  186. }
  187. public function distance()
  188. {
  189. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  190. dump($a);
  191. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  192. dump($a);
  193. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  194. dump($b);
  195. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  196. dump($b);
  197. }
  198. }