Usercenter.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. 'audio_seconds',
  30. 'video_bio',
  31. 'photo_images',
  32. 'marital_id',
  33. 'job_id',
  34. 'suqiu_id',
  35. 'wages_id',
  36. 'tag_ids',
  37. 'hobby_ids',
  38. 'open_match_video',
  39. 'open_match_audio',
  40. 'match_video_price',
  41. 'match_audio_price',
  42. 'match_typing_price',
  43. 'wealth_level','charm_level',
  44. ];
  45. protected $allowFields_simple = [
  46. 'id',
  47. 'username',
  48. 'nickname',
  49. 'avatar',
  50. 'idcard_status',
  51. 'real_status',
  52. 'gender',
  53. 'open_match_video',
  54. 'open_match_audio',
  55. 'match_video_price',
  56. 'match_audio_price',
  57. 'match_typing_price',
  58. ];
  59. //获取他人用户信息
  60. public function getuserinfo_simple(){
  61. $uid = input_post('uid',0);
  62. $field = $this->allowFields_simple;
  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']);
  69. //是否喜欢和关注
  70. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  71. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  72. $userinfo['is_friend'] = ($userinfo['is_follow'] && $userinfo['is_fans']) ? 1 : 0;
  73. //是否拉黑
  74. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  75. $userinfo['is_black'] = $is_black ? 1 : 0;
  76. //活跃,在线
  77. $userinfo['active_info'] = $this->user_activeinfo($uid);
  78. //此用户与我的亲密度 信息
  79. $userinfo['intimacy_info'] = $this->get_intimacy_info($uid);
  80. //我给他的备注
  81. $nickname_remark = Db::name('nickname_remark')->where(['uid'=>$this->auth->id,'to_uid'=>$uid])->value('nickname_remark');
  82. $userinfo['nickname_remark'] = !empty($nickname_remark) ? $nickname_remark : '';
  83. //登录用户的金币余额
  84. $userinfo['my_goldtotal'] = model('wallet')->getWallettotal($this->auth->id);
  85. //是否客服
  86. $userinfo['is_kefu'] = 0;
  87. $kefu_ids = config('site.kefu_user_ids');
  88. if(in_array($uid,explode(',',$kefu_ids))){
  89. $userinfo['is_kefu'] = 1;
  90. }
  91. $this->success('success',$userinfo);
  92. }
  93. //两个人的亲密度
  94. //api/match/intimacylevel
  95. private function get_intimacy_info($user_id){
  96. //$user_id = input('user_id', 0, 'intval'); //对方id
  97. if ($this->auth->id > $user_id) { //大的在后
  98. $where['uid'] = $user_id;
  99. $where['other_uid'] = $this->auth->id;
  100. } else { //小的在前
  101. $where['uid'] = $this->auth->id;
  102. $where['other_uid'] = $user_id;
  103. }
  104. $level = 0; //当前等级
  105. $level_name = ''; //当前等级名称
  106. $qinmi_sum = 0; //当前亲密度
  107. $level_info = [];
  108. $next_level_info = [];
  109. $next_level_diff = 0; //距下一等级亲密度差值
  110. $next_level_name = ''; //下一等级名称
  111. $next_level_value = 0;//下一等级亲密度值
  112. $user_intimacy_info = Db::name('user_intimacy')->where($where)->find();
  113. if ($user_intimacy_info) {
  114. //当前亲密度
  115. $qinmi_sum = $user_intimacy_info['value'];
  116. //当前等级信息
  117. $level_info = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_info['value']]])->order('level desc')->find();
  118. if ($level_info) {
  119. $level = $level_info['level'];
  120. $level_name = $level_info['name'];
  121. }
  122. //下一等级信息
  123. $next_level_info = Db::name('intimacy_level')->where(['value' => ['gt', $user_intimacy_info['value']]])->order('value')->find();
  124. if ($next_level_info) {
  125. $next_level_name = $next_level_info['name'];
  126. $next_level_value = $next_level_info['value'];
  127. $next_level_diff = $next_level_info['value'] - $user_intimacy_info['value'];
  128. }
  129. }else{
  130. $level = 1; //当前等级
  131. $level_name = '初级'; //当前等级名称
  132. $qinmi_sum = 0; //当前亲密度
  133. $level_info = Db::name('intimacy_level')->where('level',1)->find();
  134. $next_level_info = Db::name('intimacy_level')->where('level',2)->find();
  135. $next_level_diff = $next_level_info['value'];
  136. $next_level_name = $next_level_info['name'];
  137. $next_level_value = $next_level_info['value'];
  138. }
  139. unset($level_info['id']);
  140. unset($level_info['name']);
  141. unset($level_info['level']);
  142. unset($level_info['value']);
  143. $data['level'] = $level; //当前等级
  144. $data['level_name'] = $level_name; //当前等级名称
  145. $data['qinmi_sum'] = $qinmi_sum; //当前亲密度
  146. $data['level_info'] = $level_info; //当前亲密度
  147. $data['next_level_diff'] = $next_level_diff; //距下一等级亲密度差值
  148. $data['next_level_name'] = $next_level_name; //下一等级名称
  149. $data['next_level_value'] = $next_level_value; //下一等级亲密度值
  150. return $data;
  151. }
  152. //获取他人用户信息,留下足迹
  153. public function getuserinfo(){
  154. $uid = input_post('uid',0);
  155. $field = $this->allowFields;
  156. $userinfo = Db::name('user')->field($field)->where('id',$uid)->find();
  157. if(!$userinfo){
  158. $this->error('不存在的用户');
  159. }
  160. //用户数据
  161. $userinfo = info_domain_image($userinfo,['avatar','photo_images','video_bio','audio_bio']);
  162. $new_data = [
  163. 'age' => birthtime_to_age($userinfo['birthday']),
  164. 'birthday' => date('Y-m-d',$userinfo['birthday']),
  165. ];
  166. //合并
  167. $userinfo = array_merge($userinfo,$new_data);
  168. //vip
  169. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  170. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  171. //是否喜欢和关注
  172. $userinfo['is_follow'] = $this->is_follow($this->auth->id,$uid);
  173. $userinfo['is_fans'] = $this->is_follow($uid,$this->auth->id);
  174. $userinfo['is_friend'] = ($userinfo['is_follow'] && $userinfo['is_fans']) ? 1 : 0;
  175. //是否拉黑
  176. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  177. $userinfo['is_black'] = $is_black ? 1 : 0;
  178. //关注人数,粉丝人数
  179. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  180. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  181. $userinfo['follow_num'] = $follow_num;
  182. $userinfo['fans_num'] = $fans_num;
  183. //查看别人信息,就要留下痕迹
  184. if($this->apiLimit(1,1000) == true){
  185. $data = [
  186. 'uid' => $this->auth->id,
  187. 'to_uid' => $uid,
  188. ];
  189. $check = Db::name('user_visit')->where($data)->find();
  190. if($check){
  191. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  192. }else{
  193. $data['number'] = 1;
  194. $data['updatetime'] = time();
  195. Db::name('user_visit')->insertGetId($data);
  196. }
  197. }
  198. //活跃,在线
  199. $userinfo['active_info'] = $this->user_activeinfo($uid);
  200. //用户权限
  201. $user_power = Db::name('user_power')->where('user_id',$uid)->find();
  202. //此用户与我的亲密度 信息
  203. $userinfo['intimacy_info'] = $this->get_intimacy_info($uid);
  204. //我给他的备注
  205. $nickname_remark = Db::name('nickname_remark')->where(['uid'=>$this->auth->id,'to_uid'=>$uid])->value('nickname_remark');
  206. $userinfo['nickname_remark'] = !empty($nickname_remark) ? $nickname_remark : '';
  207. //vip如果开了隐私保护,需要隐藏距离
  208. if($user_power['weizhi'] == 0){
  209. $userinfo['cityname'] = '';
  210. }
  211. //追加登录用户的头像
  212. $userinfo['my_avatar'] = localpath_to_netpath($this->auth->avatar);
  213. //此用户与我的亲密度 信息
  214. //api/match/intimacylevel
  215. //最新一条动态
  216. $last_dongtai = Db::name('topic_dongtai')->field('content,images')->where('user_id',$uid)->where('type',1)->where('auditstatus',1)->order('id desc')->find();
  217. $last_dongtai = info_domain_image($last_dongtai,['images']);
  218. $userinfo['last_dongtai'] = $last_dongtai;
  219. //礼物墙
  220. $userinfo['gift_wall'] = [];
  221. if($user_power['giftwall'] == 1){
  222. $gift_wall = Db::name('gift_user_typing')->alias('log')
  223. ->join('gift', 'gift.id = log.gift_id', 'LEFT')
  224. ->field('log.id,log.gift_name,sum(log.number) as number,gift.image')
  225. ->where(['log.user_to_id' => $uid])
  226. ->group('log.gift_id')
  227. ->order('gift.price desc')
  228. ->select();
  229. $gift_wall = list_domain_image($gift_wall,['image']);
  230. $userinfo['gift_wall'] = $gift_wall;
  231. }
  232. //价格显示开关
  233. $userinfo['usersite_matchinfo_switch'] = config('site.usersite_matchinfo_switch');
  234. //财富等级,魅力等级
  235. if($user_power['meili'] == 1){
  236. $userinfo['charm_image'] = Db::name('charm_level')->where('level',$userinfo['charm_level'])->value('image');
  237. }else{
  238. $userinfo['charm_image'] = '';
  239. $userinfo['charm_level'] = '';
  240. }
  241. if($user_power['caifu'] == 1){
  242. $userinfo['wealth_image'] = Db::name('wealth_level')->where('level',$userinfo['wealth_level'])->value('image');
  243. }else{
  244. $userinfo['wealth_image'] = '';
  245. $userinfo['wealth_level'] = '';
  246. }
  247. //是否客服
  248. $userinfo['is_kefu'] = 0;
  249. $kefu_ids = config('site.kefu_user_ids');
  250. if(in_array($uid,explode(',',$kefu_ids))){
  251. $userinfo['is_kefu'] = 1;
  252. }
  253. $this->success('success',$userinfo);
  254. }
  255. //给对方设置备注
  256. public function set_nickname_remark(){
  257. if($this->apiLimit(1,1000) == false){
  258. $this->error('操作频繁');
  259. }
  260. $user_id = input('user_id');
  261. $remark = input('nickname_remark','');
  262. $map = [
  263. 'uid' => $this->auth->id,
  264. 'to_uid' => $user_id,
  265. ];
  266. $check = Db::name('nickname_remark')->where($map)->find();
  267. if($check){
  268. Db::name('nickname_remark')->where($map)->update(['nickname_remark'=>$remark]);
  269. }else{
  270. $map['nickname_remark'] = $remark;
  271. Db::name('nickname_remark')->insertGetId($map);
  272. }
  273. $this->success('设置成功');
  274. }
  275. //搜索用户列表
  276. public function search_user_list(){
  277. $username = input('username','');
  278. if(!$username){
  279. $this->error();
  280. }
  281. $uid = Db::name('user')->where('username',$username)->value('id');
  282. if(empty($uid)){
  283. $this->error('不存在的用户');
  284. }
  285. $this->success('success',$uid);
  286. }
  287. /**
  288. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  289. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  290. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  291. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  292. * @return float | false | string
  293. */
  294. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  295. if( empty( $point_1 ) || empty( $point_2 ) ){
  296. return false;
  297. }
  298. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  299. $p1_x = $point_1[0];
  300. $p1_y = $point_1[1];
  301. $p2_x = $point_2[0];
  302. $p2_y = $point_2[1];
  303. if(
  304. $p1_x < -180 || $p1_x > 180
  305. || $p2_x < -180 || $p2_x > 180
  306. || $p1_y < -90 || $p1_y > 90
  307. || $p2_y < -90 || $p2_y > 90
  308. ){
  309. return '0公里';
  310. }
  311. // 根据2点各自的坐标,计算2点之间直线距离的公式
  312. $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);
  313. // 是否计算为字符串公里距离
  314. if( !$calc_as_string ){
  315. return (string)round( $distance / 1000 , 1 ) . '公里';
  316. }
  317. // 如果计算为字符串公里距离
  318. if( $distance / 1000 > 1 ){
  319. $k = (string)round( $distance / 1000 , 1 );
  320. $m = (string)$distance % 1000 ;
  321. $distance = "{$k}公里{$m}米";
  322. }
  323. else{
  324. $distance = "{$distance}米";
  325. }
  326. return $distance;
  327. }
  328. //地图api,根据两地坐标,获得两地距离,打卡用的
  329. //type=0直线,type=1开车
  330. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  331. $result = 0;
  332. $apiurl = 'https://restapi.amap.com/v3/distance?';
  333. $param = [
  334. 'key' => '398c424811d1a59beac2f915323d334e',
  335. 'origins' => $start_lon.','.$start_lat,
  336. 'destination' => $end_lon.','.$end_lat,
  337. 'type' => $type,
  338. 'output' => 'json',
  339. ];
  340. $apiurl .= http_build_query($param);
  341. $request_rs = json_decode(curl_get($apiurl),true);
  342. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  343. if(isset($request_rs['results'][0]['distance']))
  344. {
  345. $result = $request_rs['results'][0]['distance'];
  346. }
  347. }
  348. //dump($result);
  349. return $result;
  350. }
  351. public function distance()
  352. {
  353. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  354. dump($a);
  355. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  356. dump($a);
  357. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  358. dump($b);
  359. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  360. dump($b);
  361. }
  362. }