Usercenter.php 40 KB

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