Match.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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_log(){
  14. if($this->auth->gender == 0){
  15. $list = Db::name('user_match_video_log')->alias('log')
  16. ->field('log.id,log.user_id as the_user_id,log.call_minutes,log.createtime,user.avatar,user.nickname')
  17. ->join('user','log.user_id = user.id','LEFT')
  18. ->where('log.to_user_id' , $this->auth->id)
  19. ->order('log.id desc')->autopage()->select();
  20. }else{
  21. $list = Db::name('user_match_video_log')->alias('log')
  22. ->field('log.id,log.to_user_id as the_user_id,log.call_minutes,log.createtime,user.avatar,user.nickname')
  23. ->join('user','log.to_user_id = user.id','LEFT')
  24. ->where('log.user_id' , $this->auth->id)
  25. ->order('log.id desc')->autopage()->select();
  26. }
  27. $list = list_domain_image($list,['avatar']);
  28. $this->success(1,$list);
  29. }
  30. //语音通话记录
  31. public function audio_log(){
  32. if($this->auth->gender == 0){
  33. $list = Db::name('user_match_audio_log')->alias('log')
  34. ->field('log.id,log.user_id as the_user_id,log.call_minutes,log.createtime,user.avatar,user.nickname')
  35. ->join('user','log.user_id = user.id','LEFT')
  36. ->where('log.to_user_id' , $this->auth->id)
  37. ->order('log.id desc')->autopage()->select();
  38. }else{
  39. $list = Db::name('user_match_audio_log')->alias('log')
  40. ->field('log.id,log.to_user_id as the_user_id,log.call_minutes,log.createtime,user.avatar,user.nickname')
  41. ->join('user','log.to_user_id = user.id','LEFT')
  42. ->where('log.user_id' , $this->auth->id)
  43. ->order('log.id desc')->autopage()->select();
  44. }
  45. $list = list_domain_image($list,['avatar']);
  46. $this->success(1,$list);
  47. }
  48. //三个通话前的配置
  49. public function match_config(){
  50. $user_id = input('user_id',0);
  51. if(!$user_id){$this->error();}
  52. if($this->auth->gender == 1){
  53. //拿男用户的三个免费额度
  54. $man_wallet = Db::name('user_wallet')->field('gold,jewel,audio_sec,video_sec,typing_times')->where('user_id',$this->auth->id)->find();
  55. $man_wallet['goldtotal'] = bcadd($man_wallet['gold'],$man_wallet['jewel'],1);
  56. unset($man_wallet['gold']);
  57. unset($man_wallet['jewel']);
  58. //女用户的三个价格
  59. $woman_price = Db::name('user')->field('match_video_price,match_audio_price,match_typing_price')->where('id',$user_id)->find();
  60. }else{
  61. //拿男用户的三个免费额度
  62. $man_wallet = Db::name('user_wallet')->field('gold,jewel,audio_sec,video_sec,typing_times')->where('user_id',$user_id)->find();
  63. $man_wallet['goldtotal'] = bcadd($man_wallet['gold'],$man_wallet['jewel'],1);
  64. unset($man_wallet['gold']);
  65. unset($man_wallet['jewel']);
  66. //女用户的三个价格
  67. $woman_price = Db::name('user')->field('match_video_price,match_audio_price,match_typing_price')->where('id',$this->auth->id)->find();
  68. }
  69. $result = [
  70. 'avatar' => localpath_to_netpath(Db::name('user')->where('id',$user_id)->value('avatar')),
  71. 'man_wallet' => $man_wallet,
  72. 'woman_price' => $woman_price,
  73. 'video_butie' => '10', //视频补贴价格
  74. 'audio_butie' => '10', //语音补贴价格
  75. 'typing_butie' => '0.1',//私聊补贴价格
  76. ];
  77. $this->success(1,$result);
  78. }
  79. //视频通话每分钟调用一次
  80. public function video_onemin(){
  81. if ($this->auth->gender == 0) { //女生不花钱
  82. $this->error('您的网络开小差啦~');
  83. }
  84. //检测用户
  85. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  86. $to_user_id = input_post('to_user_id');
  87. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_video_price')->where('id',$to_user_id)->find();
  88. if(!$to_user_info){
  89. $this->error('不存在的用户');
  90. }
  91. //客服不收钱
  92. $kefu_ids = config('site.kefu_user_ids');
  93. if(in_array($to_user_id,explode(',',$kefu_ids)) || in_array($this->auth->id,explode(',',$kefu_ids))){
  94. $rs = [
  95. 'get_jewel_value' => 0,
  96. 'next_minute' => 1, //1=足够下一分钟,0=不足够下一分钟
  97. ];
  98. $this->success('success',$rs);
  99. }
  100. if ($to_user_info['gender'] != 0) {
  101. $this->error('同性不能聊天~');
  102. }
  103. //正常价格
  104. $price = $to_user_info['match_video_price']; //扣费金币
  105. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  106. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,1),100,1); //抽成后收益
  107. Db::startTrans();
  108. //检查剩余分钟数
  109. $task_status = 0;//任务状态
  110. $next_minute = 0;//默认不足下一分钟
  111. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  112. if($user_wallet['video_sec'] >= 1){
  113. //扣分钟数
  114. $price = 0;
  115. //补贴给对方0.1金币
  116. $money = '10';
  117. //下一分钟
  118. if($user_wallet['video_sec'] > 1){
  119. $next_minute = 1;
  120. }else{
  121. //最后一分钟的情况,查询钱包
  122. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  123. if($goldtotal >= $to_user_info['match_video_price']){
  124. $next_minute = 1;
  125. }
  126. }
  127. }else{
  128. $task_status = 1;
  129. //需要扣别人的钱,判断钱是否足够
  130. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  131. if($price > 0){
  132. if(bccomp($price,$goldtotal,1) == 1){
  133. Db::rollback();
  134. $this->error('金币不足');
  135. }
  136. }
  137. if($goldtotal - $price >= $price){
  138. $next_minute = 1;
  139. }
  140. }
  141. //查询是否有匹配记录
  142. $user_match_video_log_info = [];
  143. if ($request_id) {
  144. $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();
  145. }
  146. if ($user_match_video_log_info) {
  147. //修改记录日志
  148. $data = [
  149. 'price' => $user_match_video_log_info['price'] + $price,
  150. 'updatetime' => time(),
  151. 'money' => bcadd($user_match_video_log_info['money'],$money,1),
  152. 'call_minutes' => $user_match_video_log_info['call_minutes'] + 1
  153. ];
  154. //免费的,补贴
  155. if($task_status == 0){
  156. $data['free_times'] = $user_match_video_log_info['free_times'] + 1;
  157. $data['free_money'] = $user_match_video_log_info['free_money'] + $money;
  158. }else{
  159. $data['free_times'] = $user_match_video_log_info['free_times'];
  160. $data['free_money'] = $user_match_video_log_info['free_money'];
  161. }
  162. $log_rs = Db::name('user_match_video_log')->where(['id' => $user_match_video_log_info['id']])->update($data);
  163. if (!$log_rs) {
  164. Db::rollback();
  165. $this->error('扣费失败');
  166. }
  167. $log_id = $user_match_video_log_info['id'];
  168. } else {
  169. //添加记录日志
  170. $data = [
  171. 'user_id' => $this->auth->id,
  172. 'price' => $price,
  173. 'createtime' => time(),
  174. 'updatetime' => time(),
  175. 'to_user_id' => $to_user_id,
  176. 'money' => $money,
  177. 'request_id' => $request_id,
  178. 'call_minutes' => 1,
  179. 'free_times' => $task_status == 0 ? 1 : 0, //免费的,补贴
  180. 'free_money' => $task_status == 0 ? $money : 0, //免费的,补贴
  181. ];
  182. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  183. if (!$log_id) {
  184. Db::rollback();
  185. $this->error('扣费失败');
  186. }
  187. }
  188. //检查剩余分钟数
  189. if($user_wallet['video_sec'] >= 1){
  190. //扣分钟数
  191. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['video_sec'=>$user_wallet['video_sec']-1]);
  192. if($rs_wallet === false){
  193. Db::rollback();
  194. $this->error('扣除分钟数失败');
  195. }
  196. //补贴给对方0.1金币
  197. $money = '10';
  198. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,21,'与'.$this->auth->username.'视频通话,新人补贴','user_match_video_log',$log_id,$log_id);
  199. if($rs['status'] === false){
  200. Db::rollback();
  201. $this->error($rs['msg']);
  202. }
  203. }else{
  204. //有性别差,扣费
  205. if($price > 0){
  206. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'与'.$to_user_info['username'].'视频通话','user_match_video_log',$log_id,$log_id);
  207. if($rs['status'] === false){
  208. Db::rollback();
  209. $this->error($rs['msg']);
  210. }
  211. }
  212. //另一方加钱,0收费
  213. if($money > 0){
  214. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,21,'与'.$this->auth->username.'视频通话','user_match_video_log',$log_id,$log_id);
  215. if($rs['status'] === false){
  216. Db::rollback();
  217. $this->error($rs['msg']);
  218. }
  219. }
  220. //增加送礼用户的财富等级
  221. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$price);
  222. //增加获赠用户的魅力等级
  223. $res_wealth = \app\common\model\User::add_charm_level($to_user_id,$price);
  224. //增加亲密度
  225. if ($this->auth->id > $to_user_id) { //大的在后
  226. \app\common\model\User::add_intimacy($to_user_id,$this->auth->id,$price);
  227. } else { //小的在前
  228. \app\common\model\User::add_intimacy($this->auth->id,$to_user_id,$price);
  229. }
  230. }
  231. //tag任务赠送金币
  232. //与1名异性语音通话奖励
  233. if($task_status == 1){
  234. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  235. if($task_rs === false){
  236. Db::rollback();
  237. $this->error('完成任务赠送奖励失败');
  238. }
  239. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  240. if($task_rs === false){
  241. Db::rollback();
  242. $this->error('完成任务赠送奖励失败');
  243. }
  244. }
  245. Db::commit();
  246. $remark = '';
  247. if($data['money'] > $data['free_money']){
  248. $remark .= ('通话+'.($data['money'] - $data['free_money']).'金币');
  249. }
  250. if($data['free_money'] > 0){
  251. $remark .= !empty($remark) ? ',' : '';
  252. $remark .= ('新人补贴+'.$data['free_money'].'金币');
  253. }
  254. $rs = [
  255. 'get_jewel_value' => $data['money'],
  256. 'remark' => $remark,
  257. 'next_minute' => $next_minute,
  258. ];
  259. $this->success('success',$rs);
  260. }
  261. //语音通话每分钟调用一次
  262. public function audio_onemin(){
  263. if ($this->auth->gender == 0) { //女生不花钱
  264. $this->error('您的网络开小差啦~');
  265. }
  266. //检测用户
  267. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  268. $to_user_id = input_post('to_user_id');
  269. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_audio_price')->where('id',$to_user_id)->find();
  270. if(!$to_user_info){
  271. $this->error('不存在的用户');
  272. }
  273. //客服不收钱
  274. $kefu_ids = config('site.kefu_user_ids');
  275. if(in_array($to_user_id,explode(',',$kefu_ids)) || in_array($this->auth->id,explode(',',$kefu_ids))){
  276. $rs = [
  277. 'get_jewel_value' => 0,
  278. 'next_minute' => 1, //1=足够下一分钟,0=不足够下一分钟
  279. ];
  280. $this->success('success',$rs);
  281. }
  282. if ($to_user_info['gender'] != 0) {
  283. $this->error('同性不能聊天~');
  284. }
  285. //正常价格
  286. $price = $to_user_info['match_audio_price']; //扣费金币
  287. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  288. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,1),100,1); //抽成后收益
  289. Db::startTrans();
  290. //检查剩余分钟数
  291. $task_status = 0;//任务状态
  292. $next_minute = 0;//默认不足下一分钟
  293. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  294. if($user_wallet['audio_sec'] >= 1){
  295. //扣分钟数
  296. $price = 0;
  297. //补贴给对方0.1金币
  298. $money = '10';
  299. //下一分钟
  300. if($user_wallet['audio_sec'] > 1){
  301. $next_minute = 1;
  302. }else{
  303. //最后一分钟的情况,查询钱包
  304. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  305. if($goldtotal >= $to_user_info['match_audio_price']){
  306. $next_minute = 1;
  307. }
  308. }
  309. }else{
  310. $task_status = 1;
  311. //需要扣别人的钱,判断钱是否购
  312. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  313. if($price > 0){
  314. if(bccomp($price,$goldtotal,1) == 1){
  315. Db::rollback();
  316. $this->error('金币不足');
  317. }
  318. }
  319. if($goldtotal - $price >= $price){
  320. $next_minute = 1;
  321. }
  322. }
  323. //查询是否有匹配记录
  324. $user_match_audio_log_info = [];
  325. if ($request_id) {
  326. $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();
  327. }
  328. if ($user_match_audio_log_info) {
  329. //修改记录日志
  330. $data = [
  331. 'price' => $user_match_audio_log_info['price'] + $price,
  332. 'updatetime' => time(),
  333. 'money' => bcadd($user_match_audio_log_info['money'],$money,1),
  334. 'call_minutes' => $user_match_audio_log_info['call_minutes'] + 1
  335. ];
  336. //免费的,补贴
  337. if($task_status == 0){
  338. $data['free_times'] = $user_match_audio_log_info['free_times'] + 1;
  339. $data['free_money'] = $user_match_audio_log_info['free_money'] + $money;
  340. }else{
  341. $data['free_times'] = $user_match_audio_log_info['free_times'];
  342. $data['free_money'] = $user_match_audio_log_info['free_money'];
  343. }
  344. $log_rs = Db::name('user_match_audio_log')->where(['id' => $user_match_audio_log_info['id']])->update($data);
  345. if (!$log_rs) {
  346. Db::rollback();
  347. $this->error('扣费失败');
  348. }
  349. $log_id = $user_match_audio_log_info['id'];
  350. } else {
  351. //添加记录日志
  352. $data = [
  353. 'user_id' => $this->auth->id,
  354. 'price' => $price,
  355. 'createtime' => time(),
  356. 'updatetime' => time(),
  357. 'to_user_id' => $to_user_id,
  358. 'money' => $money,
  359. 'request_id' => $request_id,
  360. 'call_minutes' => 1,
  361. 'free_times' => $task_status == 0 ? 1 : 0, //免费的,补贴
  362. 'free_money' => $task_status == 0 ? $money : 0, //免费的,补贴
  363. ];
  364. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  365. if (!$log_id) {
  366. Db::rollback();
  367. $this->error('扣费失败');
  368. }
  369. }
  370. //检查剩余分钟数
  371. if($user_wallet['audio_sec'] >= 1){
  372. //扣分钟数
  373. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['audio_sec'=>$user_wallet['audio_sec']-1]);
  374. if($rs_wallet === false){
  375. Db::rollback();
  376. $this->error('扣除分钟数失败');
  377. }
  378. //补贴给对方0.1金币
  379. $money = '10';
  380. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,22,'与'.$this->auth->username.'语音通话,新人补贴','user_match_audio_log',$log_id,$log_id);
  381. if($rs['status'] === false){
  382. Db::rollback();
  383. $this->error($rs['msg']);
  384. }
  385. }else{
  386. //有性别差,扣费
  387. if($price > 0){
  388. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,12,'与'.$to_user_info['username'].'语音通话','user_match_audio_log',$log_id,$log_id);
  389. if($rs['status'] === false){
  390. Db::rollback();
  391. $this->error($rs['msg']);
  392. }
  393. }
  394. //另一方加钱,0收费
  395. if($money > 0){
  396. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,22,'与'.$this->auth->username.'语音通话','user_match_audio_log',$log_id,$log_id);
  397. if($rs['status'] === false){
  398. Db::rollback();
  399. $this->error($rs['msg']);
  400. }
  401. }
  402. //增加送礼用户的财富等级
  403. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$price);
  404. //增加获赠用户的魅力等级
  405. $res_wealth = \app\common\model\User::add_charm_level($to_user_id,$price);
  406. //增加亲密度
  407. if ($this->auth->id > $to_user_id) { //大的在后
  408. \app\common\model\User::add_intimacy($to_user_id,$this->auth->id,$price);
  409. } else { //小的在前
  410. \app\common\model\User::add_intimacy($this->auth->id,$to_user_id,$price);
  411. }
  412. }
  413. //tag任务赠送金币
  414. //与1名异性语音通话奖励
  415. if($task_status == 1){
  416. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  417. if($task_rs === false){
  418. Db::rollback();
  419. $this->error('完成任务赠送奖励失败');
  420. }
  421. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  422. if($task_rs === false){
  423. Db::rollback();
  424. $this->error('完成任务赠送奖励失败');
  425. }
  426. }
  427. Db::commit();
  428. $remark = '';
  429. if($data['money'] > $data['free_money']){
  430. $remark .= ('通话+'.($data['money'] - $data['free_money']).'金币');
  431. }
  432. if($data['free_money'] > 0){
  433. $remark .= !empty($remark) ? ',' : '';
  434. $remark .= ('新人补贴+'.$data['free_money'].'金币');
  435. }
  436. $rs = [
  437. 'get_jewel_value' => $data['money'],
  438. 'remark' => $remark,
  439. 'next_minute' => $next_minute,
  440. ];
  441. $this->success('success',$rs);
  442. }
  443. //打字聊天每句话调用一次
  444. public function typing_once(){
  445. if ($this->auth->gender == 0) { //女生不花钱
  446. $this->error('您的网络开小差啦~');
  447. }
  448. //检测用户
  449. $to_user_id = input_post('to_user_id');
  450. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_typing_price')->where('id',$to_user_id)->find();
  451. if(!$to_user_info){
  452. $this->error('不存在的用户');
  453. }
  454. //客服不收钱
  455. $kefu_ids = config('site.kefu_user_ids');
  456. if(in_array($to_user_id,explode(',',$kefu_ids)) || in_array($this->auth->id,explode(',',$kefu_ids))){
  457. $rs = [
  458. 'get_jewel_value' => 0,
  459. 'next_minute' => 1, //1=足够下一分钟,0=不足够下一分钟
  460. ];
  461. $this->success('success',$rs);
  462. }
  463. if ($to_user_info['gender'] != 0) {
  464. $this->error('同性不能聊天~');
  465. }
  466. //正常价格
  467. $price = $to_user_info['match_typing_price']; //扣费金币
  468. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  469. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,1),100,1); //抽成后收益
  470. Db::startTrans();
  471. //检查剩余分钟数
  472. $task_status = 0;//任务状态
  473. $next_minute = 0;//默认不足下一分钟
  474. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  475. if($user_wallet['typing_times'] >= 1){
  476. //扣分钟数
  477. $price = 0;
  478. //补贴给对方0.1金币
  479. $money = '0.1';
  480. //下一分钟
  481. if($user_wallet['typing_times'] > 1){
  482. $next_minute = 1;
  483. }else{
  484. //最后一分钟的情况,查询钱包
  485. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  486. if($goldtotal >= $to_user_info['match_typing_price']){
  487. $next_minute = 1;
  488. }
  489. }
  490. }else{
  491. $task_status = 1;
  492. //需要扣别人的钱,判断钱是否购
  493. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  494. if($price > 0){
  495. if(bccomp($price,$goldtotal,1) == 1){
  496. Db::rollback();
  497. $this->error('金币不足');
  498. }
  499. }
  500. if($goldtotal - $price >= $price){
  501. $next_minute = 1;
  502. }
  503. }
  504. //添加记录日志
  505. $data = [
  506. 'user_id' => $this->auth->id,
  507. 'price' => $price,
  508. 'createtime' => time(),
  509. 'to_user_id' => $to_user_id,
  510. 'money' => $money,
  511. 'is_free' => $task_status == 0 ? 1 : 0, //免费的,补贴
  512. ];
  513. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  514. if (!$log_id) {
  515. Db::rollback();
  516. $this->error('扣费失败');
  517. }
  518. //检查剩余分钟数
  519. if($user_wallet['typing_times'] >= 1){
  520. //扣分钟数
  521. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['typing_times'=>$user_wallet['typing_times']-1]);
  522. if($rs_wallet === false){
  523. Db::rollback();
  524. $this->error('扣除免费次数失败');
  525. }
  526. //补贴给对方0.1金币
  527. $money = '0.1';
  528. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,23,'与'.$this->auth->username.'聊天,新人补贴','user_match_typing_log',$log_id);
  529. if($rs['status'] === false){
  530. Db::rollback();
  531. $this->error($rs['msg']);
  532. }
  533. }else{
  534. //有性别差,扣费
  535. if($price > 0){
  536. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,13,'与'.$to_user_info['username'].'聊天','user_match_typing_log',$log_id);
  537. if($rs['status'] === false){
  538. Db::rollback();
  539. $this->error($rs['msg']);
  540. }
  541. }
  542. //另一方加钱,0收费
  543. if($money > 0){
  544. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,23,'与'.$this->auth->username.'聊天','user_match_typing_log',$log_id);
  545. if($rs['status'] === false){
  546. Db::rollback();
  547. $this->error($rs['msg']);
  548. }
  549. }
  550. //增加送礼用户的财富等级
  551. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$price);
  552. //增加获赠用户的魅力等级
  553. $res_wealth = \app\common\model\User::add_charm_level($to_user_id,$price);
  554. //增加亲密度
  555. if ($this->auth->id > $to_user_id) { //大的在后
  556. \app\common\model\User::add_intimacy($to_user_id,$this->auth->id,$price);
  557. } else { //小的在前
  558. \app\common\model\User::add_intimacy($this->auth->id,$to_user_id,$price);
  559. }
  560. }
  561. Db::commit();
  562. if($task_status == 0){
  563. $remark = '新人补贴+'.$data['money'].'金币';
  564. }else{
  565. $remark = '+'.$data['money'].'金币';
  566. }
  567. $rs = [
  568. 'get_jewel_value' => $money,
  569. 'remark' => $remark,
  570. 'next_minute' => $next_minute,
  571. ];
  572. $this->success('success',$rs);
  573. }
  574. //语音匹配
  575. public function getaudiouser(){
  576. if(config('site.index_match_audio_switch') != 1){
  577. $this->error('该功能暂未开启');
  578. }
  579. //客服
  580. $kefu_ids = config('site.kefu_user_ids');
  581. //给出备选用户
  582. $map = [
  583. 'user.status' =>1, //未封禁用户
  584. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  585. 'user.is_active' => 1, //在线的
  586. 'user.open_match_audio' => 1, //打开语聊开关
  587. 'user.id' => ['NOTIN',$kefu_ids], //排除客服
  588. ];
  589. if($this->auth->gender == 0){
  590. //或者未首充用户,且还有免费分钟数
  591. $map2['user.is_shouchong'] = 0;
  592. $map2['uw.audio_sec'] = ['gt',0];
  593. //男性要有最少一分钟的钱
  594. $map3['uw.gold'] = ['egt',$this->auth->match_audio_price];
  595. }else{
  596. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  597. $map2['user.match_audio_price'] = ['elt',$my_gold];
  598. }
  599. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  600. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  601. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  602. if(empty($lists) && $this->auth->gender == 0){
  603. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  604. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  605. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  606. }
  607. if(!empty($lists)){
  608. foreach($lists as $key => &$val){
  609. $val = info_domain_image($val,['avatar']);
  610. $val['age'] = birthtime_to_age($val['birthday']);
  611. unset($val['birthday']);
  612. $val['match_audio_price'] = $this->auth->gender == 0 ? $this->auth->match_audio_price : $val['match_audio_price'];
  613. }
  614. }
  615. $this->success('success',$lists);
  616. }
  617. //视频匹配
  618. public function getvideouser(){
  619. if(config('site.index_match_video_switch') != 1){
  620. $this->error('该功能暂未开启');
  621. }
  622. //客服
  623. $kefu_ids = config('site.kefu_user_ids');
  624. //给出备选用户
  625. $map = [
  626. 'user.status' =>1, //未封禁用户
  627. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  628. 'user.is_active' => 1, //在线的
  629. 'user.open_match_video' => 1, //打开语聊开关
  630. 'user.id' => ['NOTIN',$kefu_ids], //排除客服
  631. ];
  632. if($this->auth->gender == 0){
  633. //或者未首充用户,且还有免费分钟数
  634. $map2['user.is_shouchong'] = 0;
  635. $map2['uw.video_sec'] = ['gt',0];
  636. //男性要有最少一分钟的钱
  637. $map3['uw.gold'] = ['egt',$this->auth->match_video_price];
  638. }else{
  639. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  640. $map2['user.match_video_price'] = ['elt',$my_gold];
  641. }
  642. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_video_price')
  643. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  644. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  645. if(empty($lists) && $this->auth->gender == 0){
  646. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_video_price')
  647. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  648. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  649. }
  650. if(!empty($lists)){
  651. foreach($lists as $key => &$val){
  652. $val = info_domain_image($val,['avatar']);
  653. $val['age'] = birthtime_to_age($val['birthday']);
  654. unset($val['birthday']);
  655. $val['match_video_price'] = $this->auth->gender == 0 ? $this->auth->match_video_price : $val['match_video_price'];
  656. }
  657. }
  658. $this->success('success',$lists);
  659. }
  660. //亲密度等级信息
  661. public function intimacylevel() {
  662. $user_id = input('user_id', 0, 'intval'); //对方id
  663. if (!$user_id) {
  664. $this->error('参数缺失');
  665. }
  666. if ($this->auth->id > $user_id) { //大的在后
  667. $where['uid'] = $user_id;
  668. $where['other_uid'] = $this->auth->id;
  669. } else { //小的在前
  670. $where['uid'] = $this->auth->id;
  671. $where['other_uid'] = $user_id;
  672. }
  673. $level = 0; //当前等级
  674. $level_name = ''; //当前等级名称
  675. $qinmi_sum = 0; //当前亲密度
  676. $next_level_diff = 0; //距下一等级亲密度差值
  677. $next_level_name = 0; //下一等级名称
  678. $next_level_value = 0;//下一等级亲密度值
  679. //亲密度等级列表
  680. $list = Db::name('intimacy_level')->field('name,info,level,value')->order('value')->select();
  681. //当前亲密度信息
  682. $user_intimacy_info = Db::name('user_intimacy')->where($where)->find();
  683. if ($user_intimacy_info) {
  684. //当前亲密度
  685. $qinmi_sum = $user_intimacy_info['value'];
  686. //当前等级信息
  687. $level_info = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_info['value']]])->order('level desc')->find();
  688. if ($level_info) {
  689. $level = $level_info['level'];
  690. $level_name = $level_info['name'];
  691. }
  692. //下一等级信息
  693. $next_level_info = Db::name('intimacy_level')->where(['value' => ['gt', $user_intimacy_info['value']]])->order('value')->find();
  694. if ($next_level_info) {
  695. $next_level_name = $next_level_info['name'];
  696. $next_level_value = $next_level_info['value'];
  697. $next_level_diff = bcsub($next_level_info['value'],$user_intimacy_info['value'],1);
  698. }
  699. } else {
  700. $level = 1; //当前等级
  701. $level_name = '初级'; //当前等级名称
  702. $qinmi_sum = 0; //当前亲密度
  703. $next_level_info = Db::name('intimacy_level')->where('level',2)->find();
  704. $next_level_diff = $next_level_info['value'];
  705. $next_level_name = $next_level_info['name'];
  706. $next_level_value = $next_level_info['value'];
  707. }
  708. if ($list) {
  709. foreach ($list as &$v) {
  710. if ($v['level'] < $level) {
  711. $v['is_unlock'] = 1; //当前等级是否解锁: 1已解锁 2当前等级 3未解锁
  712. } elseif ($v['level'] == $level) {
  713. $v['is_unlock'] = 2;
  714. } else {
  715. $v['is_unlock'] = 3;
  716. }
  717. }
  718. }
  719. $data['level'] = $level; //当前等级
  720. $data['level_name'] = $level_name; //当前等级名称
  721. $data['qinmi_sum'] = $qinmi_sum; //当前亲密度
  722. $data['next_level_diff'] = $next_level_diff; //距下一等级亲密度差值
  723. $data['next_level_name'] = $next_level_name; //下一等级名称
  724. $data['next_level_value'] = $next_level_value; //下一等级亲密度值
  725. $data['level_list'] = $list; //等级列表
  726. $data['my_avatar'] = localpath_to_netpath($this->auth->avatar);
  727. $data['my_nickname'] = $this->auth->nickname;
  728. $other_user = Db::name('user')->field('avatar,nickname')->where('id',$user_id)->find();
  729. $data['other_avatar'] = localpath_to_netpath($other_user['avatar']);
  730. $data['other_nickname'] = $other_user['nickname'];
  731. $data['intimacy_rule'] = config('site.intimacy_rule');
  732. $this->success('亲密度等级信息', $data);
  733. }
  734. //与你有缘 推送搭讪人
  735. public function getindex_dashan(){
  736. //客服
  737. $kefu_ids = config('site.kefu_user_ids');
  738. //强制条件
  739. $map = [
  740. 'user.status' =>1, //未封禁用户
  741. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  742. 'user.id' => ['NOTIN',$kefu_ids], //排除客服
  743. ];
  744. //找到没聊过的人
  745. if ($this->auth->gender == 1) {
  746. $where = [
  747. 'user_id' => $this->auth->id,
  748. ];
  749. $uids = Db::name('user_match_typing_log')->where($where)->column('to_user_id');
  750. }else{
  751. $where = [
  752. 'to_user_id' => $this->auth->id,
  753. ];
  754. $uids = Db::name('user_match_typing_log')->where($where)->column('user_id');
  755. }
  756. $map2 = [
  757. 'user.id' => ['NOTIN',$uids],
  758. ];
  759. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname')
  760. ->where($map)->where($map2)->order('user.logintime desc')->limit(6)->select();
  761. /*if(empty($lists)){
  762. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname')
  763. ->where($map)->order('user.logintime desc')->limit(6)->select();
  764. }*/
  765. if(!empty($lists)){
  766. foreach($lists as $key => &$val){
  767. $val = info_domain_image($val,['avatar']);
  768. $val['age'] = birthtime_to_age($val['birthday']);
  769. unset($val['birthday']);
  770. }
  771. }
  772. $this->success('success',$lists);
  773. }
  774. //聊天匹配
  775. public function gettypinguser(){
  776. exit;//不需要这个接口
  777. //给出备选用户
  778. $map = [
  779. 'user.status' =>1, //未封禁用户
  780. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  781. ];
  782. if($this->auth->gender == 0){
  783. //或者未首充用户,且还有免费分钟数
  784. $map2['user.is_shouchong'] = 0;
  785. $map2['uw.typing_times'] = ['gt',0];
  786. //男性要有最少一分钟的钱
  787. $map3['uw.gold'] = ['egt',$this->auth->match_typing_price];
  788. }else{
  789. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  790. $map2['user.match_typing_price'] = ['elt',$my_gold];
  791. }
  792. $lists = Db::name('user')->alias('user')->field('user.id')
  793. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  794. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  795. if(empty($lists) && $this->auth->gender == 0){
  796. $lists = Db::name('user')->alias('user')->field('user.id')
  797. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  798. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  799. }
  800. $this->success('success',$lists);
  801. }
  802. //////////////////////////////////////////////////////////////
  803. //过滤规则
  804. private function fliter_user($lists){
  805. if(empty($lists)){
  806. return $lists;
  807. }
  808. //过滤掉通话中的
  809. foreach($lists as $key => $val){
  810. if(redis_matching_get($val['id']) == 1){
  811. unset($lists[$key]);
  812. }
  813. }
  814. return $lists;
  815. }
  816. }