Match.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 匹配 与 匹配的收费
  7. */
  8. class Match extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //视频通话每分钟调用一次
  13. public function video_onemin(){
  14. if ($this->auth->gender == 0) { //女生不花钱
  15. $this->error('您的网络开小差啦~');
  16. }
  17. //检测用户
  18. $to_user_id = input_post('to_user_id');
  19. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  20. $to_user_info = Db::name('user')->field('id,intro_uid,gender,match_video_price')->where('id',$to_user_id)->find();
  21. if(!$to_user_info){
  22. $this->error('不存在的用户');
  23. }
  24. if ($to_user_info['gender'] != 0) {
  25. $this->error('同性不能聊天~');
  26. }
  27. //正常价格
  28. $price = $to_user_info['match_video_price']; //扣费金币
  29. $gift_plat_scale = config('site.pipei_plat_scale'); //抽成比例
  30. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,2),100,2); //抽成后收益
  31. Db::startTrans();
  32. //查询是否有匹配记录
  33. $user_match_video_log_info = [];
  34. if ($request_id) {
  35. $user_match_video_log_info = Db::name('user_match_video_log')->where(['user_id' => $this->auth->id, 'to_user_id' => $to_user_id, 'request_id' => $request_id])->find();
  36. }
  37. if ($user_match_video_log_info) {
  38. //修改记录日志
  39. $data = [
  40. 'price' => $user_match_video_log_info['price'] + $price,
  41. 'updatetime' => time(),
  42. 'money' => $user_match_video_log_info['money'] + $money,
  43. 'call_minutes' => $user_match_video_log_info['call_minutes'] + 1
  44. ];
  45. $log_id = Db::name('user_match_video_log')->where(['id' => $user_match_video_log_info['id']])->setField($data);
  46. if (!$log_id) {
  47. Db::rollback();
  48. $this->error('扣费失败');
  49. }
  50. } else {
  51. //添加记录日志
  52. $data = [
  53. 'user_id' => $this->auth->id,
  54. 'price' => $price,
  55. 'createtime' => time(),
  56. 'to_user_id' => $to_user_id,
  57. 'money' => $money,
  58. 'request_id' => $request_id,
  59. 'call_minutes' => 1
  60. ];
  61. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  62. if (!$log_id) {
  63. Db::rollback();
  64. $this->error('扣费失败');
  65. }
  66. }
  67. //需要扣别人的钱,判断钱是否购
  68. if($price > 0){
  69. $gold = model('wallet')->getWallettotal($this->auth->id);
  70. if(bccomp($price,$gold) == 1){
  71. Db::rollback();
  72. $this->error('金币不足');
  73. }
  74. }
  75. //有性别差,扣费
  76. if($price > 0){
  77. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_match_video_log',$log_id);
  78. if($rs['status'] === false){
  79. Db::rollback();
  80. $this->error($rs['msg']);
  81. }
  82. }
  83. //另一方加钱,0收费
  84. if($money > 0){
  85. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,21,'','user_match_video_log',$log_id);
  86. if($rs['status'] === false){
  87. Db::rollback();
  88. $this->error($rs['msg']);
  89. }
  90. }
  91. //tag任务赠送金币
  92. //与1名异性语音通话奖励
  93. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  94. if($task_rs === false){
  95. Db::rollback();
  96. $this->error('完成任务赠送奖励失败');
  97. }
  98. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  99. if($task_rs === false){
  100. Db::rollback();
  101. $this->error('完成任务赠送奖励失败');
  102. }
  103. Db::commit();
  104. $this->success('success');
  105. }
  106. //语音通话每分钟调用一次
  107. public function audio_onemin(){
  108. if ($this->auth->gender == 0) { //女生不花钱
  109. $this->error('您的网络开小差啦~');
  110. }
  111. //检测用户
  112. $to_user_id = input_post('to_user_id');
  113. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  114. $to_user_info = Db::name('user')->field('id,intro_uid,gender,match_audio_price')->where('id',$to_user_id)->find();
  115. if(!$to_user_info){
  116. $this->error('不存在的用户');
  117. }
  118. if ($to_user_info['gender'] != 0) {
  119. $this->error('同性不能聊天~');
  120. }
  121. //正常价格
  122. $price = $to_user_info['match_audio_price']; //扣费金币
  123. $gift_plat_scale = config('site.pipei_plat_scale'); //抽成比例
  124. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,2),100,2); //抽成后收益
  125. Db::startTrans();
  126. //查询是否有匹配记录
  127. $user_match_audio_log_info = [];
  128. if ($request_id) {
  129. $user_match_audio_log_info = Db::name('user_match_audio_log')->where(['user_id' => $this->auth->id, 'to_user_id' => $to_user_id, 'request_id' => $request_id])->find();
  130. }
  131. if ($user_match_audio_log_info) {
  132. //修改记录日志
  133. $data = [
  134. 'price' => $user_match_audio_log_info['price'] + $price,
  135. 'updatetime' => time(),
  136. 'money' => $user_match_audio_log_info['money'] + $money,
  137. 'call_minutes' => $user_match_audio_log_info['call_minutes'] + 1
  138. ];
  139. $log_id = Db::name('user_match_audio_log')->where(['id' => $user_match_audio_log_info['id']])->setField($data);
  140. if (!$log_id) {
  141. Db::rollback();
  142. $this->error('扣费失败');
  143. }
  144. } else {
  145. //添加记录日志
  146. $data = [
  147. 'user_id' => $this->auth->id,
  148. 'price' => $price,
  149. 'createtime' => time(),
  150. 'to_user_id' => $to_user_id,
  151. 'money' => $money,
  152. 'request_id' => $request_id,
  153. 'call_minutes' => 1
  154. ];
  155. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  156. if (!$log_id) {
  157. Db::rollback();
  158. $this->error('扣费失败');
  159. }
  160. }
  161. //需要扣别人的钱,判断钱是否购
  162. if($price > 0){
  163. $gold = model('wallet')->getWallettotal($this->auth->id);
  164. if(bccomp($price,$gold) == 1){
  165. Db::rollback();
  166. $this->error('金币不足');
  167. }
  168. }
  169. //有性别差,扣费
  170. if($price > 0){
  171. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,12,'','user_match_audio_log',$log_id);
  172. if($rs['status'] === false){
  173. Db::rollback();
  174. $this->error($rs['msg']);
  175. }
  176. }
  177. //另一方加钱,0收费
  178. if($money > 0){
  179. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,22,'','user_match_audio_log',$log_id);
  180. if($rs['status'] === false){
  181. Db::rollback();
  182. $this->error($rs['msg']);
  183. }
  184. }
  185. //tag任务赠送金币
  186. //与1名异性语音通话奖励
  187. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  188. if($task_rs === false){
  189. Db::rollback();
  190. $this->error('完成任务赠送奖励失败');
  191. }
  192. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  193. if($task_rs === false){
  194. Db::rollback();
  195. $this->error('完成任务赠送奖励失败');
  196. }
  197. Db::commit();
  198. $this->success('success');
  199. }
  200. //打字聊天每句话调用一次
  201. public function typing_once(){
  202. //检测用户
  203. $to_user_id = input_post('to_user_id');
  204. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  205. if(!$to_user_info){
  206. $this->error('不存在的用户');
  207. }
  208. //正常价格
  209. $price = config('site.typing_min_price'); //扣费金币
  210. $bili = config('site.money_to_gold'); //兑换比例
  211. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  212. $money = bcdiv($price,$bili,2); //对应人民币
  213. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  214. //发起用户的分数,被发起用户的分数。按性别给分
  215. $auth_level = 0;
  216. $tous_level = 0;
  217. //打分
  218. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  219. $auth_level = 30;//实名女最高
  220. }
  221. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  222. $auth_level = 20;//未实名女次之
  223. }
  224. if($this->auth->gender == 1){
  225. $auth_level = 10;//男性最低
  226. }
  227. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  228. $tous_level = 30;
  229. }
  230. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  231. $tous_level = 20;
  232. }
  233. if($to_user_info['gender'] == 1){
  234. $tous_level = 10;
  235. }
  236. //同性不收钱
  237. //都是男的,不扣钱
  238. //都是实名认证的女性,不扣钱
  239. //都是未实名认证的女性,不扣钱
  240. if($auth_level == $tous_level){
  241. $price = 0;$money = 0;
  242. }
  243. //性别优势的人发起,免费
  244. if($auth_level > $tous_level){
  245. $price = 0;$money = 0;
  246. }
  247. Db::startTrans();
  248. //记录日志
  249. $data = [
  250. 'user_id' => $this->auth->id,
  251. 'price' => $price,
  252. 'createtime' => time(),
  253. 'to_user_id' => $to_user_id,
  254. 'money' => $money,
  255. ];
  256. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  257. if(!$log_id){
  258. Db::rollback();
  259. $this->error('扣费失败');
  260. }
  261. //同性别,提前结束
  262. if($auth_level == $tous_level){
  263. Db::commit();
  264. $this->success('success');
  265. }
  266. //零消费,零收益消费,提前结束,其实这条没必要,下面金钱操作还会过滤一次
  267. if($price == 0 && $money == 0){
  268. Db::commit();
  269. $this->success('success');
  270. }
  271. //扣钱uid,收钱uid,收钱free_video
  272. //分数少扣钱,分数多收益
  273. if($auth_level < $tous_level){
  274. $kou_user = $this->auth->id;
  275. $get_user = $to_user_info['id'];
  276. $get_user_free = $to_user_info['free_typing'];
  277. }else{
  278. //这种已经没有了
  279. $kou_user = $to_user_info['id'];
  280. $get_user = $this->auth->id;
  281. $get_user_free = $this->auth->free_typing;
  282. }
  283. //有性别差,扣费
  284. if($price > 0){
  285. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,13,'','user_match_typing_log',$log_id);
  286. if($rs['status'] === false){
  287. Db::rollback();
  288. $this->error($rs['msg']);
  289. }
  290. }
  291. //另一方加钱,0收费
  292. if($money > 0 && $get_user_free == 0){
  293. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,23,'','user_match_typing_log',$log_id);
  294. if($rs['status'] === false){
  295. Db::rollback();
  296. $this->error($rs['msg']);
  297. }
  298. }
  299. //tag任务赠送金币
  300. //搭讪奖励
  301. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  302. if($task_rs === false){
  303. Db::rollback();
  304. $this->error('完成任务赠送奖励失败');
  305. }
  306. Db::commit();
  307. $this->success('success');
  308. }
  309. //语音匹配
  310. public function getaudiouser(){
  311. //给出备选用户
  312. $map = [
  313. 'user.status' =>1, //未封禁用户
  314. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  315. 'user.is_active' => 1, //在线的
  316. 'user.open_match_audio' => 1, //打开语聊开关
  317. ];
  318. if($this->auth->gender == 0){
  319. //或者未首充用户,且还有免费分钟数
  320. $map2['user.is_shouchong'] = 0;
  321. $map2['uw.audio_sec'] = ['gt',0];
  322. //男性要有最少一分钟的钱
  323. $map3['uw.gold'] = ['egt',$this->auth->match_audio_price];
  324. }else{
  325. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  326. $map2['user.match_audio_price'] = ['elt',$my_gold];
  327. }
  328. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  329. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  330. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  331. if(empty($lists) && $this->auth->gender == 0){
  332. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  333. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  334. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  335. }
  336. if(!empty($lists)){
  337. foreach($lists as $key => &$val){
  338. $val = info_domain_image($val,['avatar']);
  339. $val['age'] = birthtime_to_age($val['birthday']);
  340. unset($val['birthday']);
  341. $val['match_audio_price'] = $this->auth->gender == 0 ? $this->auth->match_audio_price : $val['match_audio_price'];
  342. }
  343. }
  344. $this->success('success',$lists);
  345. }
  346. //视频匹配
  347. public function getvideouser(){
  348. //给出备选用户
  349. $map = [
  350. 'user.status' =>1, //未封禁用户
  351. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  352. 'user.is_active' => 1, //在线的
  353. 'user.open_match_video' => 1, //打开语聊开关
  354. ];
  355. if($this->auth->gender == 0){
  356. //或者未首充用户,且还有免费分钟数
  357. $map2['user.is_shouchong'] = 0;
  358. $map2['uw.video_sec'] = ['gt',0];
  359. //男性要有最少一分钟的钱
  360. $map3['uw.gold'] = ['egt',$this->auth->match_video_price];
  361. }else{
  362. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  363. $map2['user.match_video_price'] = ['elt',$my_gold];
  364. }
  365. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  366. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  367. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  368. if(empty($lists) && $this->auth->gender == 0){
  369. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  370. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  371. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  372. }
  373. if(!empty($lists)){
  374. foreach($lists as $key => &$val){
  375. $val = info_domain_image($val,['avatar']);
  376. $val['age'] = birthtime_to_age($val['birthday']);
  377. unset($val['birthday']);
  378. $val['match_audio_price'] = $this->auth->gender == 0 ? $this->auth->match_audio_price : $val['match_audio_price'];
  379. }
  380. }
  381. $this->success('success',$lists);
  382. }
  383. //聊天匹配
  384. public function gettypinguser(){
  385. //给出备选用户
  386. $map = [
  387. 'user.status' =>1, //未封禁用户
  388. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  389. ];
  390. if($this->auth->gender == 0){
  391. //或者未首充用户,且还有免费分钟数
  392. $map2['user.is_shouchong'] = 0;
  393. $map2['uw.typing_times'] = ['gt',0];
  394. //男性要有最少一分钟的钱
  395. $map3['uw.gold'] = ['egt',$this->auth->match_typing_price];
  396. }else{
  397. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  398. $map2['user.match_typing_price'] = ['elt',$my_gold];
  399. }
  400. $lists = Db::name('user')->alias('user')->field('user.id')
  401. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  402. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  403. if(empty($lists) && $this->auth->gender == 0){
  404. $lists = Db::name('user')->alias('user')->field('user.id')
  405. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  406. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  407. }
  408. $this->success('success',$lists);
  409. }
  410. //过滤规则
  411. private function fliter_user($lists){
  412. if(empty($lists)){
  413. return $lists;
  414. }
  415. //过滤掉通话中的
  416. foreach($lists as $key => $val){
  417. if(redis_matching_get($val['id']) == 1){
  418. unset($lists[$key]);
  419. }
  420. }
  421. return $lists;
  422. }
  423. //亲密度等级信息
  424. public function intimacylevel() {
  425. $user_id = input('user_id', 0, 'intval'); //对方id
  426. if (!$user_id) {
  427. $this->error('参数缺失');
  428. }
  429. if ($this->auth->gender == 0) { //女用户
  430. $where['uid'] = $user_id;
  431. $where['other_uid'] = $this->auth->id;
  432. } else { //男用户
  433. $where['uid'] = $this->auth->id;
  434. $where['other_uid'] = $user_id;
  435. }
  436. $level = 0; //当前等级
  437. $level_name = ''; //当前等级名称
  438. $qinmi_sum = 0; //当前亲密度
  439. $next_level_diff = 0; //距下一等级亲密度差值
  440. $next_level_name = 0; //下一等级名称
  441. $next_level_value = 0;//下一等级亲密度值
  442. //亲密度等级列表
  443. $list = Db::name('intimacy_level')->field('name,level,value')->order('value')->select();
  444. //当前亲密度信息
  445. $user_intimacy_info = Db::name('user_intimacy')->where($where)->find();
  446. if ($user_intimacy_info) {
  447. //当前亲密度
  448. $qinmi_sum = $user_intimacy_info['value'];
  449. if ($list) {
  450. //当前等级信息
  451. $level_info = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_info['value']]])->order('level desc')->find();
  452. if ($level_info) {
  453. $level = $level_info['level'];
  454. $level_name = $level_info['name'];
  455. }
  456. //下一等级信息
  457. $next_level_info = Db::name('intimacy_level')->where(['value' => ['gt', $user_intimacy_info['value']]])->order('value')->find();
  458. if ($next_level_info) {
  459. $next_level_name = $next_level_info['name'];
  460. $next_level_value = $next_level_info['value'];
  461. $next_level_diff = $next_level_info['value'] - $user_intimacy_info['value'];
  462. }
  463. }
  464. } else {
  465. $next_level = Db::name('intimacy_level')->order('value')->find();
  466. $next_level_diff = $next_level['value'];
  467. $next_level_name = $next_level['name'];
  468. $next_level_value = $next_level['value'];
  469. }
  470. if ($list) {
  471. foreach ($list as &$v) {
  472. if ($v['level'] < $level) {
  473. $v['is_unlock'] = 1; //当前等级是否解锁: 1已解锁 2当前等级 3未解锁
  474. } elseif ($v['level'] == $level) {
  475. $v['is_unlock'] = 2;
  476. } else {
  477. $v['is_unlock'] = 3;
  478. }
  479. }
  480. }
  481. $data['level'] = $level; //当前等级
  482. $data['level_name'] = $level_name; //当前等级名称
  483. $data['qinmi_sum'] = $qinmi_sum; //当前亲密度
  484. $data['next_level_diff'] = $next_level_diff; //距下一等级亲密度差值
  485. $data['next_level_name'] = $next_level_name; //下一等级名称
  486. $data['next_level_value'] = $next_level_value; //下一等级亲密度值
  487. $data['level_list'] = $list; //等级列表
  488. $this->success('亲密度等级信息', $data);
  489. }
  490. public function test(){
  491. $this->addintimacy(1,3,20);
  492. }
  493. //增加亲密度,顺带升级
  494. public function addintimacy($uid = 0, $other_uid = 0, $value = 0) {
  495. //增加亲密度
  496. $level_remark = ''; //亲密度等级是否变动: 0未变动 >0是亲密度等级
  497. $user_intimacy_info = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $other_uid])->find();
  498. if ($user_intimacy_info) {
  499. $user_intimacy_data['value'] = $user_intimacy_info['value'] + $value;
  500. $level = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_data['value']]])->order('level desc')->find();
  501. if ($level) {
  502. $user_intimacy_data['level'] = $level['level'];
  503. if ($level['level'] != $user_intimacy_info['level']) {
  504. $level_remark = "恭喜你们亲密度达到".$level['level']."级,获得称号'".$level['name']."'";
  505. }
  506. }
  507. $user_intimacy_rs = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $other_uid])->setField($user_intimacy_data);
  508. } else {
  509. $user_intimacy_data['uid'] = $uid;
  510. $user_intimacy_data['other_uid'] = $other_uid;
  511. $user_intimacy_data['value'] = $value;
  512. $level = Db::name('intimacy_level')->where(['value' => ['elt', $value]])->order('level desc')->find();
  513. if ($level) {
  514. $user_intimacy_data['level'] = $level['level'];
  515. $level_remark = "恭喜你们亲密度达到".$level['level']."级,获得称号'".$level['name']."'";
  516. }
  517. $user_intimacy_rs = Db::name('user_intimacy')->insertGetId($user_intimacy_data);
  518. }
  519. return ['status' => $user_intimacy_rs, 'level_remark' => $level_remark];
  520. }
  521. }