Match.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. //视频通话每分钟调用一次
  50. public function video_onemin(){
  51. if ($this->auth->gender == 0) { //女生不花钱
  52. $this->error('您的网络开小差啦~');
  53. }
  54. //检测用户
  55. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  56. $to_user_id = input_post('to_user_id');
  57. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_video_price')->where('id',$to_user_id)->find();
  58. if(!$to_user_info){
  59. $this->error('不存在的用户');
  60. }
  61. if ($to_user_info['gender'] != 0) {
  62. $this->error('同性不能聊天~');
  63. }
  64. //正常价格
  65. $price = $to_user_info['match_video_price']; //扣费金币
  66. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  67. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,2),100,2); //抽成后收益
  68. Db::startTrans();
  69. //检查剩余分钟数
  70. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  71. if($user_wallet['video_sec'] >= 1){
  72. //扣分钟数
  73. $price = 0;
  74. //补贴给对方0.1金币
  75. $money = 0.1;
  76. }else{
  77. //需要扣别人的钱,判断钱是否购
  78. if($price > 0){
  79. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  80. if(bccomp($price,$goldtotal) == 1){
  81. Db::rollback();
  82. $this->error('金币不足');
  83. }
  84. }
  85. }
  86. //查询是否有匹配记录
  87. $user_match_video_log_info = [];
  88. if ($request_id) {
  89. $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();
  90. }
  91. if ($user_match_video_log_info) {
  92. //修改记录日志
  93. $data = [
  94. 'price' => $user_match_video_log_info['price'] + $price,
  95. 'updatetime' => time(),
  96. 'money' => $user_match_video_log_info['money'] + $money,
  97. 'call_minutes' => $user_match_video_log_info['call_minutes'] + 1
  98. ];
  99. $log_id = Db::name('user_match_video_log')->where(['id' => $user_match_video_log_info['id']])->setField($data);
  100. if (!$log_id) {
  101. Db::rollback();
  102. $this->error('扣费失败');
  103. }
  104. } else {
  105. //添加记录日志
  106. $data = [
  107. 'user_id' => $this->auth->id,
  108. 'price' => $price,
  109. 'createtime' => time(),
  110. 'to_user_id' => $to_user_id,
  111. 'money' => $money,
  112. 'request_id' => $request_id,
  113. 'call_minutes' => 1
  114. ];
  115. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  116. if (!$log_id) {
  117. Db::rollback();
  118. $this->error('扣费失败');
  119. }
  120. }
  121. //检查剩余分钟数
  122. if($user_wallet['video_sec'] >= 1){
  123. //扣分钟数
  124. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['video_sec'=>$user_wallet['video_sec']-1]);
  125. if($rs_wallet === false){
  126. Db::rollback();
  127. $this->error('扣除分钟数失败');
  128. }
  129. //补贴给对方0.1金币
  130. $money = 0.1;
  131. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,21,$this->auth->username.'使用免费次数的补贴','user_match_video_log',$log_id);
  132. if($rs['status'] === false){
  133. Db::rollback();
  134. $this->error($rs['msg']);
  135. }
  136. }else{
  137. //有性别差,扣费
  138. if($price > 0){
  139. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'与'.$to_user_info['username'].'视频通话','user_match_video_log',$log_id);
  140. if($rs['status'] === false){
  141. Db::rollback();
  142. $this->error($rs['msg']);
  143. }
  144. }
  145. //另一方加钱,0收费
  146. if($money > 0){
  147. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,21,'与'.$this->auth->username.'视频通话','user_match_video_log',$log_id);
  148. if($rs['status'] === false){
  149. Db::rollback();
  150. $this->error($rs['msg']);
  151. }
  152. }
  153. }
  154. //tag任务赠送金币
  155. //与1名异性语音通话奖励
  156. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  157. if($task_rs === false){
  158. Db::rollback();
  159. $this->error('完成任务赠送奖励失败');
  160. }
  161. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  162. if($task_rs === false){
  163. Db::rollback();
  164. $this->error('完成任务赠送奖励失败');
  165. }
  166. Db::commit();
  167. $rs = [
  168. 'get_jewel_value' => $data['money'],
  169. ];
  170. $this->success('success',$rs);
  171. }
  172. //语音通话每分钟调用一次
  173. public function audio_onemin(){
  174. if ($this->auth->gender == 0) { //女生不花钱
  175. $this->error('您的网络开小差啦~');
  176. }
  177. //检测用户
  178. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  179. $to_user_id = input_post('to_user_id');
  180. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_audio_price')->where('id',$to_user_id)->find();
  181. if(!$to_user_info){
  182. $this->error('不存在的用户');
  183. }
  184. if ($to_user_info['gender'] != 0) {
  185. $this->error('同性不能聊天~');
  186. }
  187. //正常价格
  188. $price = $to_user_info['match_audio_price']; //扣费金币
  189. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  190. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,2),100,2); //抽成后收益
  191. Db::startTrans();
  192. //检查剩余分钟数
  193. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  194. if($user_wallet['audio_sec'] >= 1){
  195. //扣分钟数
  196. $price = 0;
  197. //补贴给对方0.1金币
  198. $money = 0.1;
  199. }else{
  200. //需要扣别人的钱,判断钱是否购
  201. if($price > 0){
  202. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  203. if(bccomp($price,$goldtotal) == 1){
  204. Db::rollback();
  205. $this->error('金币不足');
  206. }
  207. }
  208. }
  209. //查询是否有匹配记录
  210. $user_match_audio_log_info = [];
  211. if ($request_id) {
  212. $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();
  213. }
  214. if ($user_match_audio_log_info) {
  215. //修改记录日志
  216. $data = [
  217. 'price' => $user_match_audio_log_info['price'] + $price,
  218. 'updatetime' => time(),
  219. 'money' => $user_match_audio_log_info['money'] + $money,
  220. 'call_minutes' => $user_match_audio_log_info['call_minutes'] + 1
  221. ];
  222. $log_id = Db::name('user_match_audio_log')->where(['id' => $user_match_audio_log_info['id']])->setField($data);
  223. if (!$log_id) {
  224. Db::rollback();
  225. $this->error('扣费失败');
  226. }
  227. } else {
  228. //添加记录日志
  229. $data = [
  230. 'user_id' => $this->auth->id,
  231. 'price' => $price,
  232. 'createtime' => time(),
  233. 'to_user_id' => $to_user_id,
  234. 'money' => $money,
  235. 'request_id' => $request_id,
  236. 'call_minutes' => 1
  237. ];
  238. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  239. if (!$log_id) {
  240. Db::rollback();
  241. $this->error('扣费失败');
  242. }
  243. }
  244. //检查剩余分钟数
  245. if($user_wallet['audio_sec'] >= 1){
  246. //扣分钟数
  247. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['audio_sec'=>$user_wallet['audio_sec']-1]);
  248. if($rs_wallet === false){
  249. Db::rollback();
  250. $this->error('扣除分钟数失败');
  251. }
  252. //补贴给对方0.1金币
  253. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,22,$this->auth->username.'使用免费次数的补贴','user_match_audio_log',$log_id);
  254. if($rs['status'] === false){
  255. Db::rollback();
  256. $this->error($rs['msg']);
  257. }
  258. }else{
  259. //有性别差,扣费
  260. if($price > 0){
  261. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,12,'与'.$to_user_info['username'].'语音通话','user_match_audio_log',$log_id);
  262. if($rs['status'] === false){
  263. Db::rollback();
  264. $this->error($rs['msg']);
  265. }
  266. }
  267. //另一方加钱,0收费
  268. if($money > 0){
  269. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,22,'与'.$this->auth->username.'语音通话','user_match_audio_log',$log_id);
  270. if($rs['status'] === false){
  271. Db::rollback();
  272. $this->error($rs['msg']);
  273. }
  274. }
  275. }
  276. //tag任务赠送金币
  277. //与1名异性语音通话奖励
  278. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  279. if($task_rs === false){
  280. Db::rollback();
  281. $this->error('完成任务赠送奖励失败');
  282. }
  283. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,11);
  284. if($task_rs === false){
  285. Db::rollback();
  286. $this->error('完成任务赠送奖励失败');
  287. }
  288. Db::commit();
  289. $rs = [
  290. 'get_jewel_value' => $data['money'],
  291. ];
  292. $this->success('success',$rs);
  293. }
  294. //打字聊天每句话调用一次
  295. public function typing_once(){
  296. if ($this->auth->gender == 0) { //女生不花钱
  297. $this->error('您的网络开小差啦~');
  298. }
  299. //检测用户
  300. $to_user_id = input_post('to_user_id');
  301. $to_user_info = Db::name('user')->field('id,username,intro_uid,gender,match_typing_price')->where('id',$to_user_id)->find();
  302. if(!$to_user_info){
  303. $this->error('不存在的用户');
  304. }
  305. if ($to_user_info['gender'] != 0) {
  306. $this->error('同性不能聊天~');
  307. }
  308. //正常价格
  309. $price = $to_user_info['match_typing_price']; //扣费金币
  310. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  311. $money = bcdiv(bcmul($price,100 - $gift_plat_scale,2),100,2); //抽成后收益
  312. Db::startTrans();
  313. //检查剩余分钟数
  314. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->lock(true)->find();
  315. if($user_wallet['typing_times'] >= 1){
  316. //扣分钟数
  317. $price = 0;
  318. //补贴给对方0.1金币
  319. $money = 0.1;
  320. }else{
  321. //需要扣别人的钱,判断钱是否购
  322. if($price > 0){
  323. $goldtotal = model('wallet')->getWallettotal($this->auth->id);
  324. if(bccomp($price,$goldtotal) == 1){
  325. Db::rollback();
  326. $this->error('金币不足');
  327. }
  328. }
  329. }
  330. //添加记录日志
  331. $data = [
  332. 'user_id' => $this->auth->id,
  333. 'price' => $price,
  334. 'createtime' => time(),
  335. 'to_user_id' => $to_user_id,
  336. 'money' => $money,
  337. ];
  338. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  339. if (!$log_id) {
  340. Db::rollback();
  341. $this->error('扣费失败');
  342. }
  343. //检查剩余分钟数
  344. if($user_wallet['typing_times'] >= 1){
  345. //扣分钟数
  346. $rs_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['typing_times'=>$user_wallet['typing_times']-1]);
  347. if($rs_wallet === false){
  348. Db::rollback();
  349. $this->error('扣除免费次数失败');
  350. }
  351. //补贴给对方0.1金币
  352. $money = 0.1;
  353. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,23,$this->auth->username.'使用免费次数的补贴','user_match_typing_log',$log_id);
  354. if($rs['status'] === false){
  355. Db::rollback();
  356. $this->error($rs['msg']);
  357. }
  358. }else{
  359. //有性别差,扣费
  360. if($price > 0){
  361. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,13,'与'.$to_user_info['username'].'聊天','user_match_typing_log',$log_id);
  362. if($rs['status'] === false){
  363. Db::rollback();
  364. $this->error($rs['msg']);
  365. }
  366. }
  367. //另一方加钱,0收费
  368. if($money > 0){
  369. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,'jewel',$money,23,'与'.$this->auth->username.'聊天','user_match_typing_log',$log_id);
  370. if($rs['status'] === false){
  371. Db::rollback();
  372. $this->error($rs['msg']);
  373. }
  374. }
  375. }
  376. Db::commit();
  377. $rs = [
  378. 'get_jewel_value' => $money,
  379. ];
  380. $this->success('success',$rs);
  381. }
  382. //语音匹配
  383. public function getaudiouser(){
  384. //给出备选用户
  385. $map = [
  386. 'user.status' =>1, //未封禁用户
  387. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  388. 'user.is_active' => 1, //在线的
  389. 'user.open_match_audio' => 1, //打开语聊开关
  390. ];
  391. if($this->auth->gender == 0){
  392. //或者未首充用户,且还有免费分钟数
  393. $map2['user.is_shouchong'] = 0;
  394. $map2['uw.audio_sec'] = ['gt',0];
  395. //男性要有最少一分钟的钱
  396. $map3['uw.gold'] = ['egt',$this->auth->match_audio_price];
  397. }else{
  398. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  399. $map2['user.match_audio_price'] = ['elt',$my_gold];
  400. }
  401. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  402. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  403. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  404. if(empty($lists) && $this->auth->gender == 0){
  405. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_audio_price')
  406. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  407. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  408. }
  409. if(!empty($lists)){
  410. foreach($lists as $key => &$val){
  411. $val = info_domain_image($val,['avatar']);
  412. $val['age'] = birthtime_to_age($val['birthday']);
  413. unset($val['birthday']);
  414. $val['match_audio_price'] = $this->auth->gender == 0 ? $this->auth->match_audio_price : $val['match_audio_price'];
  415. }
  416. }
  417. $this->success('success',$lists);
  418. }
  419. //视频匹配
  420. public function getvideouser(){
  421. //给出备选用户
  422. $map = [
  423. 'user.status' =>1, //未封禁用户
  424. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  425. 'user.is_active' => 1, //在线的
  426. 'user.open_match_video' => 1, //打开语聊开关
  427. ];
  428. if($this->auth->gender == 0){
  429. //或者未首充用户,且还有免费分钟数
  430. $map2['user.is_shouchong'] = 0;
  431. $map2['uw.video_sec'] = ['gt',0];
  432. //男性要有最少一分钟的钱
  433. $map3['uw.gold'] = ['egt',$this->auth->match_video_price];
  434. }else{
  435. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  436. $map2['user.match_video_price'] = ['elt',$my_gold];
  437. }
  438. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_video_price')
  439. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  440. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  441. if(empty($lists) && $this->auth->gender == 0){
  442. $lists = Db::name('user')->alias('user')->field('user.id,user.gender,user.birthday,user.avatar,user.nickname,user.match_video_price')
  443. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  444. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  445. }
  446. if(!empty($lists)){
  447. foreach($lists as $key => &$val){
  448. $val = info_domain_image($val,['avatar']);
  449. $val['age'] = birthtime_to_age($val['birthday']);
  450. unset($val['birthday']);
  451. $val['match_video_price'] = $this->auth->gender == 0 ? $this->auth->match_video_price : $val['match_video_price'];
  452. }
  453. }
  454. $this->success('success',$lists);
  455. }
  456. //聊天匹配
  457. public function gettypinguser(){
  458. //给出备选用户
  459. $map = [
  460. 'user.status' =>1, //未封禁用户
  461. 'user.gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  462. ];
  463. if($this->auth->gender == 0){
  464. //或者未首充用户,且还有免费分钟数
  465. $map2['user.is_shouchong'] = 0;
  466. $map2['uw.typing_times'] = ['gt',0];
  467. //男性要有最少一分钟的钱
  468. $map3['uw.gold'] = ['egt',$this->auth->match_typing_price];
  469. }else{
  470. $my_gold = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('gold');
  471. $map2['user.match_typing_price'] = ['elt',$my_gold];
  472. }
  473. $lists = Db::name('user')->alias('user')->field('user.id')
  474. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  475. ->where($map)->where($map2)->order('user.logintime desc')->page($this->page,100)->select();
  476. if(empty($lists) && $this->auth->gender == 0){
  477. $lists = Db::name('user')->alias('user')->field('user.id')
  478. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  479. ->where($map)->where($map3)->order('user.logintime desc')->page($this->page,100)->select();
  480. }
  481. $this->success('success',$lists);
  482. }
  483. //过滤规则
  484. private function fliter_user($lists){
  485. if(empty($lists)){
  486. return $lists;
  487. }
  488. //过滤掉通话中的
  489. foreach($lists as $key => $val){
  490. if(redis_matching_get($val['id']) == 1){
  491. unset($lists[$key]);
  492. }
  493. }
  494. return $lists;
  495. }
  496. //亲密度等级信息
  497. public function intimacylevel() {
  498. $user_id = input('user_id', 0, 'intval'); //对方id
  499. if (!$user_id) {
  500. $this->error('参数缺失');
  501. }
  502. if ($this->auth->id > $user_id) { //大的在后
  503. $where['uid'] = $user_id;
  504. $where['other_uid'] = $this->auth->id;
  505. } else { //小的在前
  506. $where['uid'] = $this->auth->id;
  507. $where['other_uid'] = $user_id;
  508. }
  509. $level = 0; //当前等级
  510. $level_name = ''; //当前等级名称
  511. $qinmi_sum = 0; //当前亲密度
  512. $next_level_diff = 0; //距下一等级亲密度差值
  513. $next_level_name = 0; //下一等级名称
  514. $next_level_value = 0;//下一等级亲密度值
  515. //亲密度等级列表
  516. $list = Db::name('intimacy_level')->field('name,level,value')->order('value')->select();
  517. //当前亲密度信息
  518. $user_intimacy_info = Db::name('user_intimacy')->where($where)->find();
  519. if ($user_intimacy_info) {
  520. //当前亲密度
  521. $qinmi_sum = $user_intimacy_info['value'];
  522. if ($list) {
  523. //当前等级信息
  524. $level_info = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_info['value']]])->order('level desc')->find();
  525. if ($level_info) {
  526. $level = $level_info['level'];
  527. $level_name = $level_info['name'];
  528. }
  529. //下一等级信息
  530. $next_level_info = Db::name('intimacy_level')->where(['value' => ['gt', $user_intimacy_info['value']]])->order('value')->find();
  531. if ($next_level_info) {
  532. $next_level_name = $next_level_info['name'];
  533. $next_level_value = $next_level_info['value'];
  534. $next_level_diff = $next_level_info['value'] - $user_intimacy_info['value'];
  535. }
  536. }
  537. } else {
  538. $next_level = Db::name('intimacy_level')->order('value')->find();
  539. $next_level_diff = $next_level['value'];
  540. $next_level_name = $next_level['name'];
  541. $next_level_value = $next_level['value'];
  542. }
  543. if ($list) {
  544. foreach ($list as &$v) {
  545. if ($v['level'] < $level) {
  546. $v['is_unlock'] = 1; //当前等级是否解锁: 1已解锁 2当前等级 3未解锁
  547. } elseif ($v['level'] == $level) {
  548. $v['is_unlock'] = 2;
  549. } else {
  550. $v['is_unlock'] = 3;
  551. }
  552. }
  553. }
  554. $data['level'] = $level; //当前等级
  555. $data['level_name'] = $level_name; //当前等级名称
  556. $data['qinmi_sum'] = $qinmi_sum; //当前亲密度
  557. $data['next_level_diff'] = $next_level_diff; //距下一等级亲密度差值
  558. $data['next_level_name'] = $next_level_name; //下一等级名称
  559. $data['next_level_value'] = $next_level_value; //下一等级亲密度值
  560. $data['level_list'] = $list; //等级列表
  561. $data['my_avatar'] = localpath_to_netpath($this->auth->avatar);
  562. $data['my_nickname'] = $this->auth->nickname;
  563. $other_user = Db::name('user')->field('avatar,nickname')->where('id',$user_id)->find();
  564. $data['other_avatar'] = localpath_to_netpath($other_user['avatar']);
  565. $data['other_nickname'] = $other_user['nickname'];
  566. $data['intimacy_rule'] = config('site.intimacy_rule');
  567. $this->success('亲密度等级信息', $data);
  568. }
  569. public function test(){
  570. $this->addintimacy(1,3,20);
  571. }
  572. //增加亲密度,顺带升级
  573. public function addintimacy($uid = 0, $other_uid = 0, $value = 0) {
  574. //增加亲密度
  575. $level_remark = ''; //亲密度等级是否变动: 0未变动 >0是亲密度等级
  576. $user_intimacy_info = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $other_uid])->find();
  577. if ($user_intimacy_info) {
  578. $user_intimacy_data['value'] = $user_intimacy_info['value'] + $value;
  579. $level = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_data['value']]])->order('level desc')->find();
  580. if ($level) {
  581. $user_intimacy_data['level'] = $level['level'];
  582. if ($level['level'] != $user_intimacy_info['level']) {
  583. $level_remark = "恭喜你们亲密度达到".$level['level']."级,获得称号'".$level['name']."'";
  584. }
  585. }
  586. $user_intimacy_rs = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $other_uid])->setField($user_intimacy_data);
  587. } else {
  588. $user_intimacy_data['uid'] = $uid;
  589. $user_intimacy_data['other_uid'] = $other_uid;
  590. $user_intimacy_data['value'] = $value;
  591. $level = Db::name('intimacy_level')->where(['value' => ['elt', $value]])->order('level desc')->find();
  592. if ($level) {
  593. $user_intimacy_data['level'] = $level['level'];
  594. $level_remark = "恭喜你们亲密度达到".$level['level']."级,获得称号'".$level['name']."'";
  595. }
  596. $user_intimacy_rs = Db::name('user_intimacy')->insertGetId($user_intimacy_data);
  597. }
  598. return ['status' => $user_intimacy_rs, 'level_remark' => $level_remark];
  599. }
  600. }