123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 关系
- */
- class Relation extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- //发布一种新关系
- public function addone(){
- $relationname = input('relationname','','trim');
- $price = input('price',0,'intval');
- if(empty($relationname) || empty($price)){
- $this->error();
- }
- $data = [
- 'uid' => $this->auth->id,
- 'relationname' => $relationname,
- 'price' => $price,
- 'createtime' => time(),
- ];
- $id = Db::name('user_relation')->insertGetId($data);
- $this->success('添加成功',$id);
- }
- //关系墙,列表
- public function lists(){
- $userid = input('uid',0,'intval');
- $userid = $userid ? $userid : $this->auth->id;
- //此用户创建的
- $list = Db::name('user_relation')->alias('r')
- ->field('r.id,r.relationname,r.price,r.to_uid,user.username,user.nickname,user.avatar,user.id as user_id,ur.nickname_remark')
- ->join('user','r.to_uid = user.id','LEFT')
- ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
- ->where('r.uid',$userid)
- ->order('id desc')
- ->select();
- $list = list_domain_image($list,['avatar']);
- foreach ($list as &$val) {
- $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
- }
- //别人的关系,此用户加入
- /* $list2 = Db::name('user_relation')->alias('r')
- ->field('r.id,r.relationname,r.price,r.to_uid,user.username,user.nickname,user.avatar,user.id as user_id')
- ->join('user','r.uid = user.id','LEFT')
- ->where('r.to_uid',$userid)
- ->order('id desc')
- ->select();
- $list = list_domain_image($list,['avatar']);
- $list = array_merge($list,$list2);*/
- $this->success('success',$list);
- }
- //绑定关系
- public function joinin(){
- $id = input('id',0);
- Db::startTrans();
- $info = Db::name('user_relation')->where('id',$id)->lock(true)->find();
- //检查
- if(empty($info)){
- Db::rollback();
- $this->error('不存在的关系');
- }
- //检查
- if($info['to_uid'] > 0){
- Db::rollback();
- $this->error('该关系已被他人绑定');
- }
- //检查
- if($info['uid'] == $this->auth->id){
- Db::rollback();
- $this->error('不能绑定自己发布的关系');
- }
- //更新
- $update = [
- 'to_uid' => $this->auth->id,
- 'updatetime' => time(),
- ];
- $rs = Db::name('user_relation')->where('id',$id)->update($update);
- if($rs === false){
- Db::rollback();
- $this->error('操作失败');
- }
- //扣钱
- if($info['price'] > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,0,'gold',$info['price'],71,'绑定关系消费','user_relation',$id);
- if($rs_wallet['status'] === false){
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- //给发布关系的人钱
- $bili = config('site.money_to_gold');
- $money = bcdiv($info['price'],$bili,2);
- if($money > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($info['uid'],0,'money',$money,72,'发布关系被绑定','user_relation',$id,2);
- if($rs_wallet['status'] === false){
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- }
- //增加赠送用户上级余额
- $touserinfo = Db::name('user')->field('intro_uid')->where('id',$info['uid'])->find();
- if ($touserinfo['intro_uid']) {
- //获取返利比率
- $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
- $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
- if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
- //上级获得金额
- $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
- if ($intro_uid_money > 0) {
- $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$info['uid'],'money',$intro_uid_money,68, '邀请人被绑关系获赠奖励','user_relation',$id);
- if($intro_result['status']===false)
- {
- Db::rollback();
- $this->error($intro_result['msg']);
- }
- }
- }
- }
- }
- //增加亲密度
- $user_intimacy_rs = addintimacy($this->auth->id, $info['uid'], $info['price']);
- if (!$user_intimacy_rs['status']) {
- Db::rollback();
- $this->error('您的网络开小差啦~');
- }
- //发送消息
- if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
- $tenim = new \app\api\controller\Tenim;
- $tenim->sendMessageToUser($this->auth->id, $info['uid'], $user_intimacy_rs['level_remark'], 1);
- }
- Db::commit();
- $this->success('绑定成功');
- }
- }
|