Usercenter.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. // 'truename',
  19. 'mobile',
  20. 'avatar',
  21. 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'idcard_status',
  28. 'cityname',
  29. 'photo_images',
  30. 'tag_ids',
  31. 'attribute',
  32. 'shoesize',
  33. 'wechat_account',
  34. ];
  35. //获取他人用户信息,留下足迹
  36. public function getuserinfo(){
  37. $uid = input_post('uid',0);
  38. $userinfo = Db::name('user')->field($this->allowFields)->where('id|username',$uid)->find();
  39. if(!$userinfo){
  40. $this->error('不存在的用户');
  41. }
  42. //用户数据
  43. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  44. $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$uid)->find();
  45. $new_data = [
  46. 'age' => birthtime_to_age($userinfo['birthday']),
  47. 'truename' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['truename'])) ? $idcard_confirm['truename'] : '',
  48. ];
  49. //合并
  50. $userinfo = array_merge($userinfo,$new_data);
  51. //枚举
  52. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  53. //vip
  54. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  55. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  56. //是否喜欢和关注
  57. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  58. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  59. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  60. $userinfo['is_like'] = $is_like ? 1 : 0;
  61. //是否拉黑
  62. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  63. $userinfo['is_black'] = $is_black ? 1 : 0;
  64. //关注人数,粉丝人数
  65. $follow_num = Db::name('user_follow')->where(['uid'=>$this->auth->id])->count('id');
  66. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$this->auth->id])->count('id');
  67. $userinfo['follow_num'] = $follow_num;
  68. $userinfo['fans_num'] = $fans_num;
  69. //查看别人信息,就要留下痕迹
  70. if($this->user_power($this->auth->id,'wuhen') != 1){
  71. $data = [
  72. 'uid' => $this->auth->id,
  73. 'to_uid' => $uid,
  74. ];
  75. $check = Db::name('user_visit')->where($data)->find();
  76. if($check){
  77. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  78. }else{
  79. $data['number'] = 1;
  80. $data['updatetime'] = time();
  81. Db::name('user_visit')->insertGetId($data);
  82. }
  83. }
  84. //活跃,在线
  85. $userinfo['is_active'] = $this->user_isactive($uid);
  86. //用户权限
  87. //$userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  88. $this->success('success',$userinfo);
  89. }
  90. /////////////////////////////////////////////////////////////////////////////////////
  91. //这里不用连user_active表,完全使用user表的active_time,user_active表只做离线用
  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. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  107. // 'user.active_time' => ['gt',time()-86400],
  108. ];
  109. if($gender != 'all'){
  110. $map['user.gender'] = $gender;
  111. }
  112. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  113. //dump($map);
  114. $field = [
  115. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  116. ];
  117. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  118. //dump($list);
  119. $list = list_domain_image($list,['avatar']);
  120. foreach($list as $key => $one){
  121. $one['age'] = birthtime_to_age($one['birthday']);
  122. // $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  123. $one['distance'] = rand(0,10).'km';
  124. //状态
  125. $one['active_text'] = '刚刚离线';
  126. if($one['active_time'] > time()-21600){
  127. $one['active_text'] = '1小时内在线';
  128. }
  129. if($one['active_time'] > time()-3600){
  130. $one['active_text'] = '在线';
  131. }
  132. $list[$key] = $one;
  133. }
  134. $this->success('success',$list);
  135. }
  136. //附近
  137. public function nearuser(){
  138. $gender = input_post('gender','all');
  139. $agemin = input_post('agemin',0);
  140. $agemax = input_post('agemax',100);
  141. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  142. // $this->success('success',[]);
  143. }
  144. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  145. $map = [
  146. 'user.status' => 1,
  147. ////'user.cityname' => $this->auth->cityname,
  148. 'user.id' => ['neq',$this->auth->id],
  149. // 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  150. // 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  151. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  152. // 'user.active_time' => ['gt',time()-86400],
  153. ];
  154. if($gender != 'all'){
  155. $map['user.gender'] = $gender;
  156. }
  157. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  158. //dump($map);
  159. $field = [
  160. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  161. ];
  162. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  163. //dump($list);exit;
  164. $list = list_domain_image($list,['avatar']);
  165. foreach($list as $key => $one){
  166. $one['age'] = birthtime_to_age($one['birthday']);
  167. //$one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  168. $one['distance'] = rand(0,10).'km';
  169. //状态
  170. $one['active_text'] = '刚刚离线';
  171. if($one['active_time'] > time()-21600){
  172. $one['active_text'] = '1小时内在线';
  173. }
  174. if($one['active_time'] > time()-3600){
  175. $one['active_text'] = '在线';
  176. }
  177. $list[$key] = $one;
  178. }
  179. $this->success('success',$list);
  180. }
  181. //因为接受者拿不到发起者的uid,废弃了
  182. //视频和语音,接收方使用,如果是性别劣势方,检查钱是否够用
  183. public function video_audio_moneycheck(){
  184. //检测用户,发起方的uid
  185. $to_user_id = input_post('from_user_id');
  186. $to_user_info = Db::name('user')->field('id,real_status,gender')->where('id',$to_user_id)->find();
  187. if(!$to_user_info){
  188. $this->error('不存在的用户');
  189. }
  190. //扣费金币
  191. $type = input_post('type','video'); //类型
  192. $price = $type == 'video' ? config('site.video_min_price') : config('site.audio_min_price');
  193. //发起用户的分数,被发起用户的分数。按性别给分
  194. $auth_level = 0;
  195. $tous_level = 0;
  196. //打分
  197. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  198. $auth_level = 30;//实名女最高
  199. }
  200. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  201. $auth_level = 20;//未实名女次之
  202. }
  203. if($this->auth->gender == 1){
  204. $auth_level = 10;//男性最低
  205. }
  206. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  207. $tous_level = 30;
  208. }
  209. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  210. $tous_level = 20;
  211. }
  212. if($to_user_info['gender'] == 1){
  213. $tous_level = 10;
  214. }
  215. //同性不收钱
  216. //都是男的,不扣钱
  217. //都是实名认证的女性,不扣钱
  218. //都是未实名认证的女性,不扣钱
  219. if($auth_level == $tous_level){
  220. $price = 0;
  221. $this->success('success');
  222. }
  223. //扣钱uid,收钱uid,收钱free_video
  224. //分数少扣钱,分数多收益
  225. if($auth_level < $tous_level){
  226. $kou_user = $this->auth->id;
  227. }else{
  228. $kou_user = $to_user_info['id'];
  229. }
  230. //需要扣我的(接收方的)钱,判断钱是否够
  231. if($price > 0 && $kou_user == $this->auth->id){
  232. Db::startTrans();
  233. $gold = model('wallet')->getWallet($kou_user,'gold');
  234. if(bccomp($price,$gold) == 1){
  235. Db::rollback();
  236. $this->error('金币不足');
  237. }
  238. Db::commit();
  239. }
  240. $this->success('success');
  241. }
  242. //视频通话每分钟调用一次
  243. public function video_onemin(){
  244. //检测用户
  245. $to_user_id = input_post('to_user_id');
  246. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  247. if(!$to_user_info){
  248. $this->error('不存在的用户');
  249. }
  250. //正常价格
  251. $price = config('site.video_min_price'); //扣费金币
  252. $bili = config('site.money_to_gold'); //兑换比例
  253. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  254. $money = bcdiv($price,$bili,2); //对应人民币
  255. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  256. //发起用户的分数,被发起用户的分数。按性别给分
  257. $auth_level = 0;
  258. $tous_level = 0;
  259. //打分
  260. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  261. $auth_level = 30;//实名女最高
  262. }
  263. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  264. $auth_level = 20;//未实名女次之
  265. }
  266. if($this->auth->gender == 1){
  267. $auth_level = 10;//男性最低
  268. }
  269. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  270. $tous_level = 30;
  271. }
  272. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  273. $tous_level = 20;
  274. }
  275. if($to_user_info['gender'] == 1){
  276. $tous_level = 10;
  277. }
  278. //同性不收钱
  279. //都是男的,不扣钱
  280. //都是实名认证的女性,不扣钱
  281. //都是未实名认证的女性,不扣钱
  282. if($auth_level == $tous_level){
  283. $price = 0;$money = 0;
  284. }
  285. Db::startTrans();
  286. //记录日志
  287. $data = [
  288. 'user_id' => $this->auth->id,
  289. 'price' => $price,
  290. 'createtime' => time(),
  291. 'to_user_id' => $to_user_id,
  292. 'money' => $money,
  293. ];
  294. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  295. if(!$log_id){
  296. Db::rollback();
  297. $this->error('扣费失败');
  298. }
  299. //同性别,提前结束
  300. if($auth_level == $tous_level){
  301. Db::commit();
  302. $this->success('success');
  303. }
  304. //扣钱uid,收钱uid,收钱free_video
  305. //分数少扣钱,分数多收益
  306. if($auth_level < $tous_level){
  307. $kou_user = $this->auth->id;
  308. $get_user = $to_user_info['id'];
  309. $get_user_free = $to_user_info['free_video'];
  310. }else{
  311. $kou_user = $to_user_info['id'];
  312. $get_user = $this->auth->id;
  313. $get_user_free = $this->auth->free_video;
  314. }
  315. //需要扣别人的钱,判断钱是否购
  316. if($price > 0 && $kou_user != $this->auth->id){
  317. $gold = model('wallet')->getWallet($kou_user,'gold');
  318. if(bccomp($price,$gold) == 1){
  319. Db::rollback();
  320. $this->error('对方金币不足');
  321. }
  322. }
  323. //有性别差,扣费
  324. if($price > 0){
  325. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,11,'','user_match_video_log',$log_id);
  326. if($rs['status'] === false){
  327. Db::rollback();
  328. $this->error($rs['msg']);
  329. }
  330. }
  331. //另一方加钱,0收费
  332. if($money > 0 && $get_user_free == 0){
  333. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,21,'','user_match_video_log',$log_id);
  334. if($rs['status'] === false){
  335. Db::rollback();
  336. $this->error($rs['msg']);
  337. }
  338. }
  339. Db::commit();
  340. $this->success('success');
  341. }
  342. //语音通话每分钟调用一次
  343. public function audio_onemin(){
  344. //检测用户
  345. $to_user_id = input_post('to_user_id');
  346. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  347. if(!$to_user_info){
  348. $this->error('不存在的用户');
  349. }
  350. //正常价格
  351. $price = config('site.audio_min_price'); //扣费金币
  352. $bili = config('site.money_to_gold'); //兑换比例
  353. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  354. $money = bcdiv($price,$bili,2); //对应人民币
  355. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  356. //发起用户的分数,被发起用户的分数。按性别给分
  357. $auth_level = 0;
  358. $tous_level = 0;
  359. //打分
  360. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  361. $auth_level = 30;//实名女最高
  362. }
  363. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  364. $auth_level = 20;//未实名女次之
  365. }
  366. if($this->auth->gender == 1){
  367. $auth_level = 10;//男性最低
  368. }
  369. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  370. $tous_level = 30;
  371. }
  372. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  373. $tous_level = 20;
  374. }
  375. if($to_user_info['gender'] == 1){
  376. $tous_level = 10;
  377. }
  378. //同性不收钱
  379. //都是男的,不扣钱
  380. //都是实名认证的女性,不扣钱
  381. //都是未实名认证的女性,不扣钱
  382. if($auth_level == $tous_level){
  383. $price = 0;$money = 0;
  384. }
  385. Db::startTrans();
  386. //记录日志
  387. $data = [
  388. 'user_id' => $this->auth->id,
  389. 'price' => $price,
  390. 'createtime' => time(),
  391. 'to_user_id' => $to_user_id,
  392. 'money' => $money,
  393. ];
  394. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  395. if(!$log_id){
  396. Db::rollback();
  397. $this->error('扣费失败');
  398. }
  399. //同性别,提前结束
  400. if($auth_level == $tous_level){
  401. Db::commit();
  402. $this->success('success');
  403. }
  404. //扣钱uid,收钱uid,收钱free_video
  405. //分数少扣钱,分数多收益
  406. if($auth_level < $tous_level){
  407. $kou_user = $this->auth->id;
  408. $get_user = $to_user_info['id'];
  409. $get_user_free = $to_user_info['free_audio'];
  410. }else{
  411. $kou_user = $to_user_info['id'];
  412. $get_user = $this->auth->id;
  413. $get_user_free = $this->auth->free_audio;
  414. }
  415. //需要扣别人的钱,判断钱是否购
  416. if($price > 0 && $kou_user != $this->auth->id){
  417. $gold = model('wallet')->getWallet($kou_user,'gold');
  418. if(bccomp($price,$gold) == 1){
  419. Db::rollback();
  420. $this->error('对方金币不足');
  421. }
  422. }
  423. //有性别差,扣费
  424. if($price > 0){
  425. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,12,'','user_match_audio_log',$log_id);
  426. if($rs['status'] === false){
  427. Db::rollback();
  428. $this->error($rs['msg']);
  429. }
  430. }
  431. //另一方加钱,0收费
  432. if($money > 0 && $get_user_free == 0){
  433. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,22,'','user_match_audio_log',$log_id);
  434. if($rs['status'] === false){
  435. Db::rollback();
  436. $this->error($rs['msg']);
  437. }
  438. }
  439. Db::commit();
  440. $this->success('success');
  441. }
  442. //打字聊天每句话调用一次
  443. public function typing_once(){
  444. //检测用户
  445. $to_user_id = input_post('to_user_id');
  446. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  447. if(!$to_user_info){
  448. $this->error('不存在的用户');
  449. }
  450. //正常价格
  451. $price = config('site.typing_min_price'); //扣费金币
  452. $bili = config('site.money_to_gold'); //兑换比例
  453. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  454. $money = bcdiv($price,$bili,2); //对应人民币
  455. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  456. //发起用户的分数,被发起用户的分数。按性别给分
  457. $auth_level = 0;
  458. $tous_level = 0;
  459. //打分
  460. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  461. $auth_level = 30;//实名女最高
  462. }
  463. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  464. $auth_level = 20;//未实名女次之
  465. }
  466. if($this->auth->gender == 1){
  467. $auth_level = 10;//男性最低
  468. }
  469. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  470. $tous_level = 30;
  471. }
  472. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  473. $tous_level = 20;
  474. }
  475. if($to_user_info['gender'] == 1){
  476. $tous_level = 10;
  477. }
  478. //同性不收钱
  479. //都是男的,不扣钱
  480. //都是实名认证的女性,不扣钱
  481. //都是未实名认证的女性,不扣钱
  482. if($auth_level == $tous_level){
  483. $price = 0;$money = 0;
  484. }
  485. //性别优势的人发起,免费
  486. if($auth_level > $tous_level){
  487. $price = 0;$money = 0;
  488. }
  489. Db::startTrans();
  490. //记录日志
  491. $data = [
  492. 'user_id' => $this->auth->id,
  493. 'price' => $price,
  494. 'createtime' => time(),
  495. 'to_user_id' => $to_user_id,
  496. 'money' => $money,
  497. ];
  498. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  499. if(!$log_id){
  500. Db::rollback();
  501. $this->error('扣费失败');
  502. }
  503. //同性别,提前结束
  504. if($auth_level == $tous_level){
  505. Db::commit();
  506. $this->success('success');
  507. }
  508. //零消费,零收益消费,提前结束,其实这条没必要,下面金钱操作还会过滤一次
  509. if($price == 0 && $money == 0){
  510. Db::commit();
  511. $this->success('success');
  512. }
  513. //扣钱uid,收钱uid,收钱free_video
  514. //分数少扣钱,分数多收益
  515. if($auth_level < $tous_level){
  516. $kou_user = $this->auth->id;
  517. $get_user = $to_user_info['id'];
  518. $get_user_free = $to_user_info['free_typing'];
  519. }else{
  520. //这种已经没有了
  521. $kou_user = $to_user_info['id'];
  522. $get_user = $this->auth->id;
  523. $get_user_free = $this->auth->free_typing;
  524. }
  525. //有性别差,扣费
  526. if($price > 0){
  527. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,13,'','user_match_typing_log',$log_id);
  528. if($rs['status'] === false){
  529. Db::rollback();
  530. $this->error($rs['msg']);
  531. }
  532. }
  533. //另一方加钱,0收费
  534. if($money > 0 && $get_user_free == 0){
  535. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,23,'','user_match_typing_log',$log_id);
  536. if($rs['status'] === false){
  537. Db::rollback();
  538. $this->error($rs['msg']);
  539. }
  540. }
  541. //tag任务赠送金币
  542. //搭讪奖励
  543. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  544. if($task_rs === false){
  545. Db::rollback();
  546. $this->error('完成任务赠送奖励失败');
  547. }
  548. Db::commit();
  549. $this->success('success');
  550. }
  551. //语音匹配
  552. public function getaudiouser(){
  553. //判断资格
  554. /*$start = strtotime(date('Y-m-d'));
  555. $end = $start + 86399;
  556. $map = [
  557. 'user_id' => $this->auth->id,
  558. 'createtime' => ['between',[$start,$end]],
  559. 'price' => 0,
  560. ];
  561. $check = Db::name('user_match_audio_log')->where($map)->find();*/
  562. $check = true;
  563. //已经用掉免费的了,判断金额
  564. if($check){
  565. $price = config('site.audio_min_price');
  566. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  567. if($gold < $price){
  568. $this->error('您的金币已经不足,请充值');
  569. }
  570. }
  571. //找到互关的人,排除
  572. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  573. //dump($follow_me);
  574. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  575. //dump($my_follow);exit;
  576. //给出备选用户
  577. $map = [
  578. 'status' =>1, //未封禁用户
  579. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  580. 'is_online' => 0, //不在语聊间的
  581. 'is_livebc' => 0, //不在直播的
  582. 'is_active' => 1, //在线的
  583. //'real_status' => 1, //真人认证
  584. //'idcard_status' => 1, //实名认证
  585. 'open_match_audio' => 1, //打开语聊开关
  586. //'id' => ['NOT IN',$my_follow] //不是好友的
  587. 'id' => ['BETWEEN',[368,382]]
  588. ];
  589. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  590. $lists = $this->fliter_user($lists,10);
  591. $result = [];
  592. if(!empty($lists)){
  593. foreach($lists as $key => $val){
  594. $result[] = ['id'=>$val];
  595. }
  596. }
  597. //tag任务赠送金币
  598. //语音匹配奖励 +5金币
  599. if(!empty($result)){
  600. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  601. if($task_rs === false){
  602. $this->error('完成任务赠送奖励失败');
  603. }
  604. }
  605. $this->success('success',$result);
  606. }
  607. //视频匹配
  608. public function getvideouser(){
  609. //判断资格
  610. /*$start = strtotime(date('Y-m-d'));
  611. $end = $start + 86399;
  612. $map = [
  613. 'user_id' => $this->auth->id,
  614. 'createtime' => ['between',[$start,$end]],
  615. 'price' => 0,
  616. ];
  617. $check = Db::name('user_match_video_log')->where($map)->find();*/
  618. $check = true;
  619. //已经用掉免费的了,判断金额
  620. if($check){
  621. $price = config('site.video_min_price');
  622. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  623. if($gold < $price){
  624. $this->error('您的金币已经不足,请充值');
  625. }
  626. }
  627. //找到互关的人,排除
  628. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  629. //dump($follow_me);
  630. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  631. //dump($my_follow);exit;
  632. //给出备选用户
  633. $map = [
  634. 'status' =>1, //未封禁用户
  635. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  636. 'is_online' => 0, //不在语聊间的
  637. 'is_livebc' => 0, //不在直播的
  638. 'is_active' => 1, //在线的
  639. //'real_status' => 1, //真人认证
  640. //'idcard_status' => 1, //实名认证
  641. 'open_match_video' => 1, //打开视频开关的
  642. // 'id' => ['NOT IN',$my_follow] //不是好友的
  643. 'id' => ['BETWEEN',[368,382]]
  644. ];
  645. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  646. $lists = $this->fliter_user($lists,10);
  647. $result = [];
  648. if(!empty($lists)){
  649. foreach($lists as $key => $val){
  650. $result[] = ['id'=>$val];
  651. }
  652. }
  653. //tag任务赠送金币
  654. //视频匹配奖励 +5金币
  655. if(!empty($result)){
  656. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,12);
  657. if($task_rs === false){
  658. $this->error('完成任务赠送奖励失败');
  659. }
  660. }
  661. $this->success('success',$result);
  662. }
  663. //聊天匹配
  664. public function gettypinguser(){
  665. //找到互关的人,排除
  666. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  667. //dump($follow_me);
  668. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  669. //dump($my_follow);exit;
  670. //给出备选用户
  671. $map = [
  672. 'status' =>1, //未封禁用户
  673. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  674. //'real_status' => 1, //真人认证
  675. //'idcard_status' => 1, //实名认证
  676. //'is_active' => 1, //在线的
  677. //打开聊天开关的
  678. 'open_match_typing' => 1, //打开文字聊天开关的
  679. //'id' => ['NOT IN',$my_follow] //不是好友的
  680. 'id' => ['BETWEEN',[368,382]]
  681. ];
  682. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  683. //$lists = $this->fliter_user($lists,100);
  684. $lists = array_column($lists,'id');
  685. $result = [];
  686. if(!empty($lists)){
  687. /*foreach($lists as $key => $val){
  688. $result[] = ['id'=>$val];
  689. }*/
  690. $result = Db::name('user')->field('id,nickname,username,avatar,audio_bio')->where(['id'=>['IN',$lists]])->select();
  691. $result = list_domain_image($result,['avatar,audio_bio']);
  692. }
  693. //tag任务赠送金币
  694. //缘分匹配奖励 +5金币
  695. if(!empty($result)){
  696. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,10);
  697. if($task_rs === false){
  698. $this->error('完成任务赠送奖励失败');
  699. }
  700. }
  701. $this->success('success',$result);
  702. }
  703. //过滤规则
  704. private function fliter_user($lists,$number = 1){
  705. if(empty($lists)){
  706. return $lists;
  707. }
  708. //dump($lists);
  709. //过滤掉通话中的
  710. foreach($lists as $key => $val){
  711. if(redis_matching_get($val['id']) == 1){
  712. unset($lists[$key]);
  713. }
  714. }
  715. //预留全部
  716. $all_result = array_column($lists,'id');
  717. //dump($all_result);
  718. //提取同城的
  719. $citydata = [];
  720. foreach($lists as $key => $val){
  721. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  722. $citydata[] = $val['id'];
  723. }
  724. }
  725. //dump($citydata);
  726. //有标签交集的
  727. $tagdata = [];
  728. foreach($lists as $key => $val){
  729. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  730. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  731. $val_tag_ids = explode(',',$val['tag_ids']);
  732. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  733. $tagdata[] = $val['id'];
  734. }
  735. }
  736. }
  737. //dump($tagdata);
  738. //双条件都满足
  739. $double_data = [];
  740. if(!empty($citydata) && !empty($tagdata)){
  741. $double_data = array_intersect($citydata,$tagdata);
  742. }
  743. //dump($double_data);
  744. if(count($double_data) >= $number){
  745. return $double_data;
  746. }
  747. //两种条件合并,去重。空数组合并没影响
  748. $merge_data = array_merge($citydata,$tagdata);
  749. $merge_data = array_flip(array_flip($merge_data));
  750. //dump($merge_data);
  751. if(count($merge_data) >= $number){
  752. return $merge_data;
  753. }
  754. return $all_result;
  755. }
  756. /**
  757. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  758. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  759. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  760. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  761. * @return float | false | string
  762. */
  763. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  764. if( empty( $point_1 ) || empty( $point_2 ) ){
  765. return false;
  766. }
  767. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  768. $p1_x = $point_1[0];
  769. $p1_y = $point_1[1];
  770. $p2_x = $point_2[0];
  771. $p2_y = $point_2[1];
  772. if(
  773. $p1_x < -180 || $p1_x > 180
  774. || $p2_x < -180 || $p2_x > 180
  775. || $p1_y < -90 || $p1_y > 90
  776. || $p2_y < -90 || $p2_y > 90
  777. ){
  778. return '0公里';
  779. }
  780. // 根据2点各自的坐标,计算2点之间直线距离的公式
  781. $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);
  782. // 是否计算为字符串公里距离
  783. if( !$calc_as_string ){
  784. return (string)round( $distance / 1000 , 1 ) . '公里';
  785. }
  786. // 如果计算为字符串公里距离
  787. if( $distance / 1000 > 1 ){
  788. $k = (string)round( $distance / 1000 , 1 );
  789. $m = (string)$distance % 1000 ;
  790. $distance = "{$k}公里{$m}米";
  791. }
  792. else{
  793. $distance = "{$distance}米";
  794. }
  795. return $distance;
  796. }
  797. //地图api,根据两地坐标,获得两地距离,打卡用的
  798. //type=0直线,type=1开车
  799. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  800. $result = 0;
  801. $apiurl = 'https://restapi.amap.com/v3/distance?';
  802. $param = [
  803. 'key' => '398c424811d1a59beac2f915323d334e',
  804. 'origins' => $start_lon.','.$start_lat,
  805. 'destination' => $end_lon.','.$end_lat,
  806. 'type' => $type,
  807. 'output' => 'json',
  808. ];
  809. $apiurl .= http_build_query($param);
  810. $request_rs = json_decode(curl_get($apiurl),true);
  811. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  812. if(isset($request_rs['results'][0]['distance']))
  813. {
  814. $result = $request_rs['results'][0]['distance'];
  815. }
  816. }
  817. //dump($result);
  818. return $result;
  819. }
  820. public function distance()
  821. {
  822. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  823. dump($a);
  824. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  825. dump($a);
  826. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  827. dump($b);
  828. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  829. dump($b);
  830. }
  831. }