Usercenter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. public function test(){
  14. $a = [1,4,10,22,66,36,102,45,23,52,35,76,7];
  15. $b = [1,10,7,102];
  16. $c = [10,102];
  17. //dump(array_intersect($a,$b));
  18. $data = array_merge($c,$b);
  19. dump($data);
  20. dump(array_flip(array_flip($data)));
  21. dump(array_flip([]));
  22. }
  23. //同城
  24. public function samecity(){
  25. $gender = input_post('gender','all');
  26. $agemin = input_post('agemin',0);
  27. $agemax = input_post('agemax',100);
  28. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  29. $this->success('success',[]);
  30. }
  31. $map = [
  32. 'user.status' => 1,
  33. 'user.cityname' => $this->auth->cityname,
  34. 'user.id' => ['neq',$this->auth->id],
  35. 'user.longitude' => ['neq',''],
  36. 'user.latitude' => ['neq',''],
  37. ];
  38. if($gender != 'all'){
  39. $map['user.gender'] = $gender;
  40. }
  41. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  42. //dump($map);
  43. $field = [
  44. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  45. ];
  46. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  47. //dump($list);
  48. $list = list_domain_image($list,['avatar']);
  49. foreach($list as $key => $one){
  50. $one['age'] = birthtime_to_age($one['birthday']);
  51. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  52. $list[$key] = $one;
  53. }
  54. $this->success('success',$list);
  55. }
  56. //附近
  57. public function nearuser(){
  58. $gender = input_post('gender','all');
  59. $agemin = input_post('agemin',0);
  60. $agemax = input_post('agemax',100);
  61. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  62. $this->success('success',[]);
  63. }
  64. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  65. $map = [
  66. 'user.status' => 1,
  67. //'user.cityname' => $this->auth->cityname,
  68. 'user.id' => ['neq',$this->auth->id],
  69. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  70. 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  71. ];
  72. if($gender != 'all'){
  73. $map['user.gender'] = $gender;
  74. }
  75. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  76. //dump($map);
  77. $field = [
  78. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  79. ];
  80. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  81. //dump($list);exit;
  82. $list = list_domain_image($list,['avatar']);
  83. foreach($list as $key => $one){
  84. $one['age'] = birthtime_to_age($one['birthday']);
  85. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  86. $list[$key] = $one;
  87. }
  88. $this->success('success',$list);
  89. }
  90. //视频通话每分钟调用一次
  91. public function video_onemin(){
  92. $to_user_id = input_post('to_user_id');
  93. //先检查今天免费的一分钟
  94. $start = strtotime(date('Y-m-d'));
  95. $end = $start + 86399;
  96. $map = [
  97. 'user_id' => $this->auth->id,
  98. 'createtime' => ['between',[$start,$end]],
  99. 'price' => 0,
  100. ];
  101. $check = Db::name('user_video_log')->where($map)->find();
  102. //设置价格
  103. $price = config('site.video_min_price');
  104. $price = empty($check) ? 0 : $price;
  105. Db::startTrans();
  106. //记录日志
  107. $data = [
  108. 'user_id' => $this->auth->id,
  109. 'price' => $price,
  110. 'createtime' => time(),
  111. 'to_user_id' => $to_user_id,
  112. ];
  113. $log_id = Db::name('user_video_log')->insertGetId($data);
  114. if(!$log_id){
  115. Db::rollback();
  116. $this->error('扣费失败');
  117. }
  118. //扣费
  119. if(!empty($check) && $log_id){
  120. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_video_log',$log_id);
  121. if($rs['status'] === false){
  122. Db::rollback();
  123. $this->error($rs['msg']);
  124. }
  125. }
  126. Db::commit();
  127. $this->success('success');
  128. }
  129. //视频匹配
  130. public function getvideouser(){
  131. //判断资格
  132. $start = strtotime(date('Y-m-d'));
  133. $end = $start + 86399;
  134. $map = [
  135. 'user_id' => $this->auth->id,
  136. 'createtime' => ['between',[$start,$end]],
  137. 'price' => 0,
  138. ];
  139. $check = Db::name('user_video_log')->where($map)->find();
  140. //已经用掉免费的了,判断金额
  141. if($check){
  142. $price = config('site.video_min_price');
  143. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  144. $moneyname = model('wallet')->getwalletname('gold');
  145. if($gold < $price){
  146. $this->error('您的'.$moneyname.'已经不足,请充值');
  147. }
  148. }
  149. //给出备选用户
  150. $map = [
  151. 'status' =>1,
  152. //'gender' => $this->auth->gender == 1 ? 0 : 1,
  153. //'real_status' => 1,
  154. //打开视频开关的
  155. 'id' => ['neq',$this->auth->id]
  156. ];
  157. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  158. $lists = $this->fliter_user($lists);
  159. $this->success('success',$lists);
  160. }
  161. private function fliter_user($lists){
  162. //预留全部
  163. $result = array_column($lists,'id');
  164. //提取同城的
  165. $citydata = [];
  166. foreach($lists as $key => $val){
  167. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  168. $citydata[] = $val['id'];
  169. }
  170. }
  171. //有标签交集的
  172. $tagdata = [];
  173. foreach($lists as $key => $val){
  174. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  175. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  176. $val_tag_ids = explode(',',$val['tag_ids']);
  177. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  178. $tagdata[] = $val['id'];
  179. }
  180. }
  181. }
  182. //两个都满足
  183. $double_data = [];
  184. if(!empty($citydata) && !empty($tagdata)){
  185. $double_data = array_intersect($citydata,$tagdata);
  186. }
  187. //两种条件合并,去重。空数组合并没影响
  188. $merge_data = array_merge($citydata,$tagdata);
  189. $merge_data = array_flip(array_flip($merge_data));
  190. //最终结果
  191. //双条件数量足够就return
  192. if(count($double_data) >= 1){
  193. //echo __LINE__;
  194. return $double_data;
  195. }
  196. //不够就合并
  197. $result_data = array_merge($double_data,$merge_data);
  198. $result_data = array_flip(array_flip($result_data));
  199. if(count($result_data) >= 1){
  200. //echo __LINE__;
  201. return $result_data;
  202. }
  203. //仍然不够,全合并
  204. $all_data = array_merge($result_data,$result);
  205. $all_data = array_flip(array_flip($all_data));
  206. return $all_data;
  207. }
  208. /**
  209. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  210. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  211. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  212. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  213. * @return float | false | string
  214. */
  215. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  216. if( empty( $point_1 ) || empty( $point_2 ) ){
  217. return false;
  218. }
  219. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  220. $p1_x = $point_1[0];
  221. $p1_y = $point_1[1];
  222. $p2_x = $point_2[0];
  223. $p2_y = $point_2[1];
  224. if(
  225. $p1_x < -180 || $p1_x > 180
  226. || $p2_x < -180 || $p2_x > 180
  227. || $p1_y < -90 || $p1_y > 90
  228. || $p2_y < -90 || $p2_y > 90
  229. ){
  230. return '0公里';
  231. }
  232. // 根据2点各自的坐标,计算2点之间直线距离的公式
  233. $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);
  234. // 是否计算为字符串公里距离
  235. if( !$calc_as_string ){
  236. return (string)round( $distance / 1000 , 1 ) . '公里';
  237. }
  238. // 如果计算为字符串公里距离
  239. if( $distance / 1000 > 1 ){
  240. $k = (string)round( $distance / 1000 , 1 );
  241. $m = (string)$distance % 1000 ;
  242. $distance = "{$k}公里{$m}米";
  243. }
  244. else{
  245. $distance = "{$distance}米";
  246. }
  247. return $distance;
  248. }
  249. //地图api,根据两地坐标,获得两地距离,打卡用的
  250. //type=0直线,type=1开车
  251. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  252. $result = 0;
  253. $apiurl = 'https://restapi.amap.com/v3/distance?';
  254. $param = [
  255. 'key' => '398c424811d1a59beac2f915323d334e',
  256. 'origins' => $start_lon.','.$start_lat,
  257. 'destination' => $end_lon.','.$end_lat,
  258. 'type' => $type,
  259. 'output' => 'json',
  260. ];
  261. $apiurl .= http_build_query($param);
  262. $request_rs = json_decode(curl_get($apiurl),true);
  263. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  264. if(isset($request_rs['results'][0]['distance']))
  265. {
  266. $result = $request_rs['results'][0]['distance'];
  267. }
  268. }
  269. //dump($result);
  270. return $result;
  271. }
  272. public function distance()
  273. {
  274. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  275. dump($a);
  276. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  277. dump($a);
  278. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  279. dump($b);
  280. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  281. dump($b);
  282. }
  283. }