Relation.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 关系
  7. */
  8. class Relation extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //发布一种新关系
  13. public function addone(){
  14. $relationname = input('relationname','','trim');
  15. $price = input('price',0,'intval');
  16. if(empty($relationname) || empty($price)){
  17. $this->error();
  18. }
  19. $data = [
  20. 'uid' => $this->auth->id,
  21. 'relationname' => $relationname,
  22. 'price' => $price,
  23. 'createtime' => time(),
  24. ];
  25. $id = Db::name('user_relation')->insertGetId($data);
  26. $this->success('添加成功',$id);
  27. }
  28. //关系墙,列表
  29. public function lists(){
  30. $userid = input('uid',0,'intval');
  31. $userid = $userid ? $userid : $this->auth->id;
  32. //此用户创建的
  33. $list = Db::name('user_relation')->alias('r')
  34. ->field('r.id,r.relationname,r.price,r.to_uid,user.username,user.nickname,user.avatar,user.id as user_id,ur.nickname_remark')
  35. ->join('user','r.to_uid = user.id','LEFT')
  36. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  37. ->where('r.uid',$userid)
  38. ->order('id desc')
  39. ->select();
  40. $list = list_domain_image($list,['avatar']);
  41. foreach ($list as &$val) {
  42. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  43. }
  44. //别人的关系,此用户加入
  45. /* $list2 = Db::name('user_relation')->alias('r')
  46. ->field('r.id,r.relationname,r.price,r.to_uid,user.username,user.nickname,user.avatar,user.id as user_id')
  47. ->join('user','r.uid = user.id','LEFT')
  48. ->where('r.to_uid',$userid)
  49. ->order('id desc')
  50. ->select();
  51. $list = list_domain_image($list,['avatar']);
  52. $list = array_merge($list,$list2);*/
  53. $this->success('success',$list);
  54. }
  55. //绑定关系
  56. public function joinin(){
  57. $id = input('id',0);
  58. Db::startTrans();
  59. $info = Db::name('user_relation')->where('id',$id)->lock(true)->find();
  60. //检查
  61. if(empty($info)){
  62. Db::rollback();
  63. $this->error('不存在的关系');
  64. }
  65. //检查
  66. if($info['to_uid'] > 0){
  67. Db::rollback();
  68. $this->error('该关系已被他人绑定');
  69. }
  70. //检查
  71. if($info['uid'] == $this->auth->id){
  72. Db::rollback();
  73. $this->error('不能绑定自己发布的关系');
  74. }
  75. //更新
  76. $update = [
  77. 'to_uid' => $this->auth->id,
  78. 'updatetime' => time(),
  79. ];
  80. $rs = Db::name('user_relation')->where('id',$id)->update($update);
  81. if($rs === false){
  82. Db::rollback();
  83. $this->error('操作失败');
  84. }
  85. //扣钱
  86. if($info['price'] > 0){
  87. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,0,'gold',$info['price'],71,'绑定关系消费','user_relation',$id);
  88. if($rs_wallet['status'] === false){
  89. Db::rollback();
  90. $this->error($rs_wallet['msg']);
  91. }
  92. //给发布关系的人钱
  93. $bili = config('site.money_to_gold');
  94. $money = bcdiv($info['price'],$bili,2);
  95. if($money > 0){
  96. $rs_wallet = model('wallet')->lockChangeAccountRemain($info['uid'],0,'money',$money,72,'发布关系被绑定','user_relation',$id,2);
  97. if($rs_wallet['status'] === false){
  98. Db::rollback();
  99. $this->error($rs_wallet['msg']);
  100. }
  101. }
  102. //增加赠送用户上级余额
  103. $touserinfo = Db::name('user')->field('intro_uid')->where('id',$info['uid'])->find();
  104. if ($touserinfo['intro_uid']) {
  105. //获取返利比率
  106. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  107. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  108. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  109. //上级获得金额
  110. $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
  111. if ($intro_uid_money > 0) {
  112. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$info['uid'],'money',$intro_uid_money,68, '邀请人被绑关系获赠奖励','user_relation',$id);
  113. if($intro_result['status']===false)
  114. {
  115. Db::rollback();
  116. $this->error($intro_result['msg']);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. //增加亲密度
  123. $user_intimacy_rs = addintimacy($this->auth->id, $info['uid'], $info['price']);
  124. if (!$user_intimacy_rs['status']) {
  125. Db::rollback();
  126. $this->error('您的网络开小差啦~');
  127. }
  128. //发送消息
  129. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  130. $tenim = new \app\api\controller\Tenim;
  131. $tenim->sendMessageToUser($this->auth->id, $info['uid'], $user_intimacy_rs['level_remark'], 1);
  132. }
  133. Db::commit();
  134. $this->success('绑定成功');
  135. }
  136. }