Relation.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 lists(){
  14. $uid = input('uid',$this->auth->id);
  15. //所有
  16. $list = Db::name('relation')->where('is_show',1)->order('weigh desc')->select();
  17. //已有关系
  18. $user_relation = Db::name('user_relation')->alias('ur')
  19. ->field('ur.*,u.avatar,u.nickname,to.avatar as to_avatar,to.nickname as to_nickname')
  20. ->join('user u','ur.uid = u.id','LEFT')
  21. ->join('user to','ur.to_uid = to.id','LEFT')
  22. ->where('ur.uid|ur.to_uid',$uid)->where('ur.status',1)->select();
  23. $user_relation = list_domain_image($user_relation,['avatar','to_avatar']);
  24. $new_user_relation = array_column($user_relation,null,'relation_id');
  25. //处理,留对方的头像和昵称
  26. foreach($list as $key => &$val){
  27. $val['info'] = [];
  28. if(isset($new_user_relation[$val['id']])){
  29. $info = $new_user_relation[$val['id']];
  30. if($info['uid'] == $uid){
  31. $info['avatar'] = $info['to_avatar'];
  32. $info['nickname'] = $info['to_nickname'];
  33. unset($info['to_avatar']);
  34. unset($info['to_nickname']);
  35. }else{
  36. unset($info['to_avatar']);
  37. unset($info['to_nickname']);
  38. }
  39. //是否能取消
  40. $info['can_cancel'] = 0;
  41. if($info['uid'] == $this->auth->id || $info['to_uid'] == $this->auth->id){
  42. $info['can_cancel'] = 1;
  43. }
  44. $val['info'] = $info;
  45. }
  46. }
  47. $this->success('success',$list);
  48. }
  49. //新增
  50. public function addone(){
  51. $to_uid = input('to_uid',0);
  52. if($to_uid == $this->auth->id){
  53. $this->error('不能跟自己建立关系');
  54. }
  55. //关系检测
  56. $relation_id = input('relation_id','');
  57. $relation = Db::name('relation')->where('id',$relation_id)->find();
  58. if(empty($relation)){
  59. $this->error('不存在的关系');
  60. }
  61. //检查已有关系
  62. $where = '(uid = '.$this->auth->id.' and to_uid = '.$to_uid.') or (uid = '.$to_uid.' and to_uid = '.$this->auth->id.')';
  63. $check = Db::name('user_relation')->where($where)->find();
  64. if($check){
  65. $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');
  66. if($check['status'] == 1){
  67. if($check['uid'] = $this->auth->id){
  68. $this->error('您已经与该用户建立'.$now_relation.'关系');
  69. }else{
  70. $this->error('该用户已经与您建立'.$now_relation.'关系');
  71. }
  72. }elseif($check['status'] == 0){
  73. if($check['uid'] = $this->auth->id){
  74. $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
  75. }else{
  76. $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
  77. }
  78. }elseif($check['status'] == 2){
  79. $this->error($now_relation.'关系的申请已被拒绝,七日内不能申请');
  80. }else{
  81. //可以申请
  82. }
  83. }else{
  84. //可以申请
  85. }
  86. //扣钱或道具
  87. //新增
  88. $data = [
  89. 'uid' => $this->auth->id,
  90. 'relation_id' => $relation_id,
  91. 'to_uid' => $to_uid,
  92. 'status' => 0,
  93. 'createtime' => time(),
  94. 'updatetime' => time(),
  95. ];
  96. $id = Db::name('user_relation')->insertGetId($data);
  97. //给对方一条消息
  98. $message = [
  99. 'user_id' => $to_uid,
  100. 'title' => '关系审核',
  101. 'content' => '有人想和你成为'.$relation['name'],
  102. 'createtime' => time(),
  103. 'status' => 0,
  104. 'infotype' => 'newrelation',//建立关系
  105. 'infotype_id' => 0,
  106. ];
  107. Db::name('message')->insertGetId($message);
  108. $this->success('申请成功,等待对方同意',$id);
  109. }
  110. //待审核关系
  111. public function audit_lists(){
  112. $list = Db::name('user_relation')->alias('ur')
  113. ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  114. ->join('relation','ur.relation_id = relation.id','LEFT')
  115. ->join('user','ur.uid = user.id','LEFT')
  116. ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
  117. ->where('ur.to_uid',$this->auth->id)
  118. ->where('ur.status',0)
  119. ->order('ur.id desc')->autopage()->select();
  120. $list = list_domain_image($list,['avatar']);
  121. if(!empty($list)){
  122. foreach($list as $key => &$val){
  123. //用户年龄
  124. $val['age'] = birthtime_to_age($val['birthday']);
  125. unset($val['birthday']);
  126. //用户vip
  127. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  128. unset($val['vip_endtime']);
  129. }
  130. }
  131. $this->success('success',$list);
  132. }
  133. //审核
  134. public function audit(){
  135. $id = input('id',0);
  136. $status = input('status',1);
  137. //查询
  138. $info = Db::name('user_relation')->alias('ur')
  139. ->where('ur.id',$id)
  140. ->where('ur.to_uid',$this->auth->id)
  141. ->where('ur.status',0)
  142. ->find();
  143. if(!$info){
  144. $this->error('请刷新重试');
  145. }
  146. //更新
  147. $data = [
  148. 'status' => $status,
  149. 'updatetime' => time(),
  150. ];
  151. Db::name('user_relation')->where('id',$id)->update($data);
  152. if($status == 1){
  153. $remark = '您已同意建立关系';
  154. $remark2 = '已同意';
  155. }else{
  156. $remark = '已拒绝';
  157. $remark2 = '已拒绝';
  158. }
  159. //给发起方一条消息
  160. $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
  161. $message = [
  162. 'user_id' => $info['uid'],
  163. 'title' => '关系申请结果',
  164. 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
  165. 'createtime' => time(),
  166. 'status' => 0,
  167. 'infotype' => '',//建立关系
  168. 'infotype_id' => 0,
  169. ];
  170. Db::name('message')->insertGetId($message);
  171. //
  172. $this->success($remark);
  173. }
  174. //取消
  175. public function cancel(){
  176. $id = input('id',0);
  177. $relation_id = input('relation_id',0);
  178. $where = [
  179. 'id' => $id,
  180. 'relation_id' => $relation_id
  181. ];
  182. $where2 = 'uid = '.$this->auth->id.' or to_uid = '.$this->auth->id;
  183. $info = Db::name('user_relation')->where($where)->where($where2)->find();
  184. if(empty($info)){
  185. $this->error('不存在的关系');
  186. }
  187. Db::name('user_relation')->where($where)->delete();
  188. //给发起方一条消息
  189. $relation_name = Db::name('relation')->where('id',$relation_id)->value('name');
  190. $message = [
  191. 'user_id' => ($info['uid'] == $this->auth->id) ? $info['to_uid'] : $info['uid'],
  192. 'title' => '关系取消',
  193. 'content' => $this->auth->nickname.'已取消和你的'.$relation_name.'关系',
  194. 'createtime' => time(),
  195. 'status' => 0,
  196. 'infotype' => '',//建立关系
  197. 'infotype_id' => 0,
  198. ];
  199. Db::name('message')->insertGetId($message);
  200. //
  201. $this->success('关系已取消');
  202. }
  203. ///////////////////////////关系卡///////////////////////
  204. //关系卡列表
  205. public function decorate_list(){
  206. $list = Db::name('decorate_relation')->select();
  207. $list = list_domain_image($list,['base_image']);
  208. $relation = Db::name('relation')->where('is_show',1)->order('weigh desc')->column('name');
  209. $relation = implode(',',$relation);
  210. $result = [
  211. 'list' => $list,
  212. 'relation' => $relation
  213. ];
  214. $this->success(1,$result);
  215. }
  216. /**
  217. * 购买并加入我的背包
  218. */
  219. public function buy_one() {
  220. $number = input_post('number',1,'intval'); //数量
  221. if(!$number){
  222. $this->error();
  223. }
  224. $did = input_post('did',''); //装扮ID
  225. if (!$did) {
  226. $this->error();
  227. }
  228. // 判断用户金币余额是否充足
  229. $walletinfo = model('wallet')->getWallet($this->auth->id);
  230. // 获取购买装扮需要的价格
  231. $decorate = Db::name('decorate_relation')->where(['id'=>$did])->find();
  232. if(!$decorate) {
  233. $this->error("道具信息获取失败!");
  234. }
  235. if($walletinfo['gold'] < $decorate['price']) {
  236. $this->error("您的金币不足,请先充值!");
  237. }
  238. // 进行购买逻辑
  239. Db::startTrans();
  240. // 添加到背包
  241. for($i=1;$i<=$number;$i++){
  242. $data[] = [
  243. 'user_id' => $this->auth->id,
  244. 'decorate_id' => $decorate['id'],
  245. 'is_using' => 0,
  246. 'createtime' => time(),
  247. 'updatetime' => time(),
  248. ];
  249. }
  250. $log_id = Db::name('user_decorate_relation')->insertAll($data);
  251. if(!$log_id){
  252. Db::rollback();
  253. $this->error('购买失败');
  254. }
  255. //扣钱
  256. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'购买关系卡','user_decorate_relation',0);
  257. if($rs['status'] === false){
  258. Db::rollback();
  259. $this->error($rs['msg']);
  260. }
  261. Db::commit();
  262. $this->success("购买成功!");
  263. }
  264. //获取用户装扮
  265. public function my_decorate_list()
  266. {
  267. $uid = $this->auth->id;
  268. $map = [
  269. 'a.user_id' => $uid,
  270. 'a.is_using'=> 0,
  271. ];
  272. $list = Db::name('user_decorate_relation')
  273. ->alias('a')
  274. ->field('a.id,a.decorate_id,b.name,b.base_image,count(a.decorate_id) as number')
  275. ->join('decorate_relation b', 'a.decorate_id = b.id','LEFT')
  276. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  277. $list = list_domain_image($list,['base_image']);
  278. $this->success('success',$list);
  279. }
  280. }