Usercenter.php 15 KB

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