Usercenter.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. /**
  7. * 会员中心,不是个人中心
  8. */
  9. class Usercenter extends Api
  10. {
  11. protected $noNeedLogin = ['test'];
  12. protected $noNeedRight = '*';
  13. protected $allowFields = [
  14. 'id',
  15. 'username',
  16. 'nickname',
  17. 'truename',//
  18. 'email',
  19. 'mobile',
  20. 'avatar',
  21. 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'audio_bio',
  28. 'alipay_account',//
  29. 'idcard_status',
  30. 'longitude',
  31. 'latitude',
  32. 'cityname',
  33. 'photo_images',
  34. 'education_id',
  35. 'hobby_ids',
  36. 'job_id',
  37. 'marital_id',
  38. 'tag_ids',
  39. 'wages_id',
  40. 'hometown_cityid',
  41. ];
  42. public function test(){
  43. $a = [1,4,10,22,66,36,102,45,23,52,35,76,7];
  44. $b = [1,10,7,102];
  45. $c = [10,102];
  46. //dump(array_intersect($a,$b));
  47. $data = array_merge($c,$b);
  48. dump($data);
  49. dump(array_flip(array_flip($data)));
  50. dump(array_flip([]));
  51. }
  52. //获取他人用户信息,留下足迹
  53. public function getuserinfo(){
  54. $uid = input_post('uid',0);
  55. $userinfo = Db::name('user')->field($this->allowFields)->where('id',$uid)->find();
  56. if(!$userinfo){
  57. $this->error('不存在的用户');
  58. }
  59. //
  60. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  61. $new_data = [
  62. 'age' => birthtime_to_age($userinfo['birthday']),
  63. 'truename' => $userinfo['idcard_status'] == 1 ? $userinfo['truename'] : '',
  64. 'alipay_account' => $userinfo['idcard_status'] == 1 ? $userinfo['alipay_account'] : '',
  65. ];
  66. $userinfo = array_merge($userinfo,$new_data);
  67. //枚举
  68. $userinfo['education'] = Db::name('enum_education')->where('id',$userinfo['education_id'])->value('name');
  69. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->column(['id','name']);
  70. $userinfo['job'] = Db::name('enum_job')->where('id',$userinfo['job_id'])->value('name');
  71. $userinfo['marital'] = Db::name('enum_marital')->where('id',$userinfo['marital_id'])->value('name');
  72. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->column(['id','name']);
  73. $userinfo['wages'] = Db::name('enum_wages')->where('id',$userinfo['wages_id'])->value('name');
  74. //家乡
  75. $userinfo['hometown_city'] = Db::name('area')->where('id',$userinfo['hometown_cityid'])->value('name');
  76. //vip
  77. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  78. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  79. //是否喜欢和关注
  80. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  81. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  82. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  83. $userinfo['is_like'] = $is_like ? 1 : 0;
  84. //查看别人信息,就要留下痕迹
  85. $data = [
  86. 'uid' => $this->auth->id,
  87. 'to_uid' => $this->auth->id,
  88. ];
  89. Db::name('user_visit')->insertGetId($data);
  90. $this->success('success',$userinfo);
  91. }
  92. //同城
  93. public function samecity(){
  94. $gender = input_post('gender','all');
  95. $agemin = input_post('agemin',0);
  96. $agemax = input_post('agemax',100);
  97. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  98. $this->success('success',[]);
  99. }
  100. $map = [
  101. 'user.status' => 1,
  102. 'user.cityname' => $this->auth->cityname,
  103. 'user.id' => ['neq',$this->auth->id],
  104. 'user.longitude' => ['neq',''],
  105. 'user.latitude' => ['neq',''],
  106. ];
  107. if($gender != 'all'){
  108. $map['user.gender'] = $gender;
  109. }
  110. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  111. //dump($map);
  112. $field = [
  113. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  114. ];
  115. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  116. //dump($list);
  117. $list = list_domain_image($list,['avatar']);
  118. foreach($list as $key => $one){
  119. $one['age'] = birthtime_to_age($one['birthday']);
  120. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  121. $list[$key] = $one;
  122. }
  123. $this->success('success',$list);
  124. }
  125. //附近
  126. public function nearuser(){
  127. $gender = input_post('gender','all');
  128. $agemin = input_post('agemin',0);
  129. $agemax = input_post('agemax',100);
  130. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  131. $this->success('success',[]);
  132. }
  133. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  134. $map = [
  135. 'user.status' => 1,
  136. //'user.cityname' => $this->auth->cityname,
  137. 'user.id' => ['neq',$this->auth->id],
  138. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  139. 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  140. ];
  141. if($gender != 'all'){
  142. $map['user.gender'] = $gender;
  143. }
  144. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  145. //dump($map);
  146. $field = [
  147. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  148. ];
  149. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  150. //dump($list);exit;
  151. $list = list_domain_image($list,['avatar']);
  152. foreach($list as $key => $one){
  153. $one['age'] = birthtime_to_age($one['birthday']);
  154. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  155. $list[$key] = $one;
  156. }
  157. $this->success('success',$list);
  158. }
  159. //视频通话每分钟调用一次
  160. public function video_onemin(){
  161. $to_user_id = input_post('to_user_id');
  162. //先检查今天免费的一分钟
  163. $start = strtotime(date('Y-m-d'));
  164. $end = $start + 86399;
  165. $map = [
  166. 'user_id' => $this->auth->id,
  167. 'createtime' => ['between',[$start,$end]],
  168. 'price' => 0,
  169. ];
  170. $check = Db::name('user_video_log')->where($map)->find();
  171. //设置价格
  172. $price = config('site.video_min_price');
  173. $price = empty($check) ? 0 : $price;
  174. Db::startTrans();
  175. //记录日志
  176. $data = [
  177. 'user_id' => $this->auth->id,
  178. 'price' => $price,
  179. 'createtime' => time(),
  180. 'to_user_id' => $to_user_id,
  181. ];
  182. $log_id = Db::name('user_video_log')->insertGetId($data);
  183. if(!$log_id){
  184. Db::rollback();
  185. $this->error('扣费失败');
  186. }
  187. //扣费
  188. if(!empty($check) && $log_id){
  189. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_video_log',$log_id);
  190. if($rs['status'] === false){
  191. Db::rollback();
  192. $this->error($rs['msg']);
  193. }
  194. }
  195. Db::commit();
  196. $this->success('success');
  197. }
  198. //视频匹配
  199. public function getvideouser(){
  200. //判断资格
  201. $start = strtotime(date('Y-m-d'));
  202. $end = $start + 86399;
  203. $map = [
  204. 'user_id' => $this->auth->id,
  205. 'createtime' => ['between',[$start,$end]],
  206. 'price' => 0,
  207. ];
  208. $check = Db::name('user_video_log')->where($map)->find();
  209. //已经用掉免费的了,判断金额
  210. if($check){
  211. $price = config('site.video_min_price');
  212. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  213. $moneyname = model('wallet')->getwalletname('gold');
  214. if($gold < $price){
  215. $this->error('您的'.$moneyname.'已经不足,请充值');
  216. }
  217. }
  218. //给出备选用户
  219. $map = [
  220. 'status' =>1,
  221. //'gender' => $this->auth->gender == 1 ? 0 : 1,
  222. //'real_status' => 1,
  223. //打开视频开关的
  224. 'id' => ['neq',$this->auth->id]
  225. ];
  226. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  227. $lists = $this->fliter_user($lists);
  228. $this->success('success',$lists);
  229. }
  230. private function fliter_user($lists){
  231. //预留全部
  232. $result = array_column($lists,'id');
  233. //提取同城的
  234. $citydata = [];
  235. foreach($lists as $key => $val){
  236. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  237. $citydata[] = $val['id'];
  238. }
  239. }
  240. //有标签交集的
  241. $tagdata = [];
  242. foreach($lists as $key => $val){
  243. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  244. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  245. $val_tag_ids = explode(',',$val['tag_ids']);
  246. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  247. $tagdata[] = $val['id'];
  248. }
  249. }
  250. }
  251. //两个都满足
  252. $double_data = [];
  253. if(!empty($citydata) && !empty($tagdata)){
  254. $double_data = array_intersect($citydata,$tagdata);
  255. }
  256. //两种条件合并,去重。空数组合并没影响
  257. $merge_data = array_merge($citydata,$tagdata);
  258. $merge_data = array_flip(array_flip($merge_data));
  259. //最终结果
  260. //双条件数量足够就return
  261. if(count($double_data) >= 1){
  262. //echo __LINE__;
  263. return $double_data;
  264. }
  265. //不够就合并
  266. $result_data = array_merge($double_data,$merge_data);
  267. $result_data = array_flip(array_flip($result_data));
  268. if(count($result_data) >= 1){
  269. //echo __LINE__;
  270. return $result_data;
  271. }
  272. //仍然不够,全合并
  273. $all_data = array_merge($result_data,$result);
  274. $all_data = array_flip(array_flip($all_data));
  275. return $all_data;
  276. }
  277. /**
  278. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  279. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  280. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  281. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  282. * @return float | false | string
  283. */
  284. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  285. if( empty( $point_1 ) || empty( $point_2 ) ){
  286. return false;
  287. }
  288. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  289. $p1_x = $point_1[0];
  290. $p1_y = $point_1[1];
  291. $p2_x = $point_2[0];
  292. $p2_y = $point_2[1];
  293. if(
  294. $p1_x < -180 || $p1_x > 180
  295. || $p2_x < -180 || $p2_x > 180
  296. || $p1_y < -90 || $p1_y > 90
  297. || $p2_y < -90 || $p2_y > 90
  298. ){
  299. return '0公里';
  300. }
  301. // 根据2点各自的坐标,计算2点之间直线距离的公式
  302. $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);
  303. // 是否计算为字符串公里距离
  304. if( !$calc_as_string ){
  305. return (string)round( $distance / 1000 , 1 ) . '公里';
  306. }
  307. // 如果计算为字符串公里距离
  308. if( $distance / 1000 > 1 ){
  309. $k = (string)round( $distance / 1000 , 1 );
  310. $m = (string)$distance % 1000 ;
  311. $distance = "{$k}公里{$m}米";
  312. }
  313. else{
  314. $distance = "{$distance}米";
  315. }
  316. return $distance;
  317. }
  318. //地图api,根据两地坐标,获得两地距离,打卡用的
  319. //type=0直线,type=1开车
  320. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  321. $result = 0;
  322. $apiurl = 'https://restapi.amap.com/v3/distance?';
  323. $param = [
  324. 'key' => '398c424811d1a59beac2f915323d334e',
  325. 'origins' => $start_lon.','.$start_lat,
  326. 'destination' => $end_lon.','.$end_lat,
  327. 'type' => $type,
  328. 'output' => 'json',
  329. ];
  330. $apiurl .= http_build_query($param);
  331. $request_rs = json_decode(curl_get($apiurl),true);
  332. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  333. if(isset($request_rs['results'][0]['distance']))
  334. {
  335. $result = $request_rs['results'][0]['distance'];
  336. }
  337. }
  338. //dump($result);
  339. return $result;
  340. }
  341. public function distance()
  342. {
  343. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  344. dump($a);
  345. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  346. dump($a);
  347. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  348. dump($b);
  349. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  350. dump($b);
  351. }
  352. }