Usercenter.php 12 KB

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