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