Usercenter.php 38 KB

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