Relation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $list = Db::name('user_relation')->alias('r')
  33. ->field('r.id,r.relationname,r.price,r.to_uid,user.u_id,user.nickname,user.avatar,user.id as user_id')
  34. ->join('user','r.to_uid = user.id','LEFT')
  35. ->where('r.uid',$userid)
  36. ->order('id desc')
  37. ->select();
  38. $list = list_domain_image($list,['avatar']);
  39. $this->success(1,$list);
  40. }
  41. //绑定关系
  42. public function joinin(){
  43. $id = input('id',0);
  44. Db::startTrans();
  45. $info = Db::name('user_relation')->where('id',$id)->lock(true)->find();
  46. //检查
  47. if(empty($info)){
  48. Db::rollback();
  49. $this->error('不存在的关系');
  50. }
  51. //检查
  52. if($info['to_uid'] > 0){
  53. Db::rollback();
  54. $this->error('该关系已被他人绑定');
  55. }
  56. //检查
  57. if($info['uid'] == $this->auth->id){
  58. Db::rollback();
  59. $this->error('不能绑定自己发布的关系');
  60. }
  61. //更新
  62. $update = [
  63. 'to_uid' => $this->auth->id,
  64. 'updatetime' => time(),
  65. ];
  66. $rs = Db::name('user_relation')->where('id',$id)->update($update);
  67. if($rs === false){
  68. Db::rollback();
  69. $this->error('操作失败');
  70. }
  71. //扣钱
  72. if($info['price'] > 0){
  73. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$info['price'],'-',0,'绑定关系消费',19,'jewel');
  74. if($rs_wallet['status'] === false){
  75. Db::rollback();
  76. $this->error($rs_wallet['msg']);
  77. }
  78. }
  79. Db::commit();
  80. $this->success('绑定成功');
  81. }
  82. }