Relation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. if(!$this->user_auth_limit()){
  56. $this->error('请先完成实名认证');
  57. }
  58. //关系检测
  59. $relation_id = input('relation_id','');
  60. $relation = Db::name('relation')->where('id',$relation_id)->find();
  61. if(empty($relation)){
  62. $this->error('不存在的关系');
  63. }
  64. $relation_name = $relation['name'];
  65. //检查这两个人的已有关系
  66. $where = '(uid = '.$this->auth->id.' and to_uid = '.$to_uid.') or (uid = '.$to_uid.' and to_uid = '.$this->auth->id.')';
  67. $check = Db::name('user_relation')->where($where)->find();
  68. if($check){
  69. $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');
  70. if($check['status'] == 1){
  71. if($check['uid'] == $this->auth->id){
  72. $this->error('您已经与该用户建立'.$now_relation.'关系');
  73. }else{
  74. $this->error('该用户已经与您建立'.$now_relation.'关系');
  75. }
  76. }elseif($check['status'] == 0){
  77. if($check['uid'] == $this->auth->id){
  78. $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
  79. }else{
  80. $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
  81. }
  82. }elseif($check['status'] == 2){
  83. $this->error($now_relation.'关系的申请已被拒绝,七日内不能再像TA申请');
  84. }else{
  85. //可以申请
  86. }
  87. }else{
  88. //可以申请
  89. }
  90. //检查我 关系的唯一性
  91. $check_map1 = 'relation_id = '.$relation_id.' and (uid = '.$this->auth->id.' or to_uid = '.$this->auth->id.')';
  92. $check_res1 = Db::name('user_relation')->where($check_map1)->find();
  93. if($check_res1){
  94. if($check_res1['status'] == 1){
  95. if($check_res1['uid'] == $this->auth->id){
  96. $this->error('您已经与其他用户建立'.$relation_name.'关系');
  97. }else{
  98. $this->error('其他用户已经与您建立'.$relation_name.'关系');
  99. }
  100. }elseif($check_res1['status'] == 0){
  101. if($check_res1['uid'] == $this->auth->id){
  102. $this->error('您已经与其他用户申请建立'.$relation_name.'关系,待对方审核中');
  103. }else{
  104. $this->error('其他用户已经与您申请建立'.$relation_name.'关系,待您审核中');
  105. }
  106. }
  107. }
  108. //检查对方 关系的唯一性
  109. $check_map2 = 'relation_id = '.$relation_id.' and (uid = '.$to_uid.' or to_uid = '.$to_uid.')';
  110. $check_res2 = Db::name('user_relation')->where($check_map2)->find();
  111. if($check_res2){
  112. if($check_res2['status'] == 1){
  113. if($check_res2['uid'] == $to_uid){
  114. $this->error('TA已经与其他用户建立'.$relation_name.'关系');
  115. }else{
  116. $this->error('其他用户已经与TA建立'.$relation_name.'关系');
  117. }
  118. }elseif($check_res2['status'] == 0){
  119. if($check_res2['uid'] == $to_uid){
  120. $this->error('TA已经与其他用户申请建立'.$relation_name.'关系');
  121. }else{
  122. $this->error('其他用户已经与TA申请建立'.$relation_name.'关系');
  123. }
  124. }
  125. }
  126. Db::startTrans();
  127. //检查关系卡数量
  128. $cardnum = Db::name('user_decorate_relation')->where('user_id',$this->auth->id)->where('is_using',0)->order('id desc')->lock(true)->find();
  129. if(empty($cardnum)){
  130. Db::rollback();
  131. $this->error('关系卡数量不足');
  132. }
  133. //扣掉一个关系卡
  134. $use_card = Db::name('user_decorate_relation')->where('id',$cardnum['id'])->update(['is_using'=>1,'updatetime'=>time()]);
  135. if(!$use_card){
  136. Db::rollback();
  137. $this->error('关系申请失败');
  138. }
  139. //新增
  140. $data = [
  141. 'uid' => $this->auth->id,
  142. 'relation_id' => $relation_id,
  143. 'to_uid' => $to_uid,
  144. 'status' => 0,
  145. 'createtime' => time(),
  146. 'updatetime' => time(),
  147. ];
  148. $id = Db::name('user_relation')->insertGetId($data);
  149. if(!$id){
  150. Db::rollback();
  151. $this->error('关系申请失败');
  152. }
  153. //给对方一条消息
  154. $message = [
  155. 'content' => $this->auth->nickname . '申请与你成为'.$relation['name'],
  156. ];
  157. $msg_id = \app\common\model\Message::addMessage($to_uid,'关系审核',$message['content'],'newrelation',0);
  158. //发起人也要发消息
  159. //系统消息
  160. $to_nickname = Db::name('user')->where('id',$to_uid)->value('nickname');
  161. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'申请关系','您正在申请与'.$to_nickname.'成为'.$relation_name.'关系,请等待对方同意','applyrelation');
  162. Db::commit();
  163. $this->success('申请成功,请等待对方同意',$id);
  164. }
  165. //待审核关系
  166. public function audit_lists(){
  167. $list = Db::name('user_relation')->alias('ur')
  168. ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  169. ->join('relation','ur.relation_id = relation.id','LEFT')
  170. ->join('user','ur.uid = user.id','LEFT')
  171. ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
  172. ->where('ur.to_uid',$this->auth->id)
  173. ->where('ur.status',0)
  174. ->order('ur.id desc')->autopage()->select();
  175. $list = list_domain_image($list,['avatar']);
  176. if(!empty($list)){
  177. foreach($list as $key => &$val){
  178. //用户年龄
  179. $val['age'] = birthtime_to_age($val['birthday']);
  180. unset($val['birthday']);
  181. //用户vip
  182. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  183. unset($val['vip_endtime']);
  184. }
  185. }
  186. $this->success('success',$list);
  187. }
  188. //审核
  189. public function audit(){
  190. $id = input('id',0);
  191. $status = input('status',1);
  192. //查询
  193. $info = Db::name('user_relation')->alias('ur')
  194. ->where('ur.id',$id)
  195. ->where('ur.to_uid',$this->auth->id)
  196. ->where('ur.status',0)
  197. ->find();
  198. if(!$info){
  199. $this->error('请刷新重试');
  200. }
  201. //更新
  202. $data = [
  203. 'status' => $status,
  204. 'updatetime' => time(),
  205. ];
  206. $rs = Db::name('user_relation')->where('id',$id)->update($data);
  207. if($rs === false){
  208. $this->error('操作失败');
  209. }
  210. if($status == 1){
  211. $remark = '您已同意建立关系';
  212. $remark2 = '已同意';
  213. }else{
  214. $remark = '已拒绝';
  215. $remark2 = '已拒绝';
  216. //退回关系卡
  217. $use_card = Db::name('user_decorate_relation')->where('user_id',$info['uid'])->where('is_using',1)->order('id desc')->find();
  218. if($use_card){
  219. $rs2 = Db::name('user_decorate_relation')->where('id',$use_card['id'])->update(['is_using'=>0,'updatetime'=>time()]);
  220. if($rs2 === false){
  221. Db::rollback();
  222. $this->error('操作失败');
  223. }
  224. }
  225. }
  226. //给发起方一条消息
  227. $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
  228. $message = [
  229. 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
  230. ];
  231. $msg_id = \app\common\model\Message::addMessage($info['uid'],'关系申请结果',$message['content']);
  232. //
  233. $this->success($remark);
  234. }
  235. //取消
  236. public function cancel(){
  237. $id = input('id',0);
  238. $relation_id = input('relation_id',0);
  239. $where = [
  240. 'id' => $id,
  241. 'relation_id' => $relation_id
  242. ];
  243. $where2 = 'uid = '.$this->auth->id.' or to_uid = '.$this->auth->id;
  244. $info = Db::name('user_relation')->where($where)->where($where2)->find();
  245. if(empty($info)){
  246. $this->error('不存在的关系');
  247. }
  248. Db::name('user_relation')->where($where)->delete();
  249. //给发起方一条消息
  250. $relation_name = Db::name('relation')->where('id',$relation_id)->value('name');
  251. $message = [
  252. 'user_id' => ($info['uid'] == $this->auth->id) ? $info['to_uid'] : $info['uid'],
  253. 'content' => $this->auth->nickname.'已取消和你的'.$relation_name.'关系',
  254. ];
  255. $msg_id = \app\common\model\Message::addMessage($message['user_id'],'关系取消',$message['content']);
  256. //
  257. $this->success('关系已取消');
  258. }
  259. ///////////////////////////关系卡///////////////////////
  260. //关系卡列表
  261. public function decorate_list(){
  262. $list = Db::name('decorate_relation')->autopage()->select();
  263. $list = list_domain_image($list,['base_image']);
  264. $relation = Db::name('relation')->where('is_show',1)->order('weigh desc')->column('name');
  265. $relation = implode(',',$relation);
  266. $result = [
  267. 'list' => $list,
  268. 'relation' => $relation
  269. ];
  270. $this->success(1,$result);
  271. }
  272. /**
  273. * 购买并加入我的背包
  274. */
  275. public function buy_one() {
  276. $number = input_post('number',1,'intval'); //数量
  277. if(!$number){
  278. $this->error();
  279. }
  280. $did = input_post('did',''); //装扮ID
  281. if (!$did) {
  282. $this->error();
  283. }
  284. // 判断用户金币余额是否充足
  285. $walletinfo = model('wallet')->getWallet($this->auth->id);
  286. // 获取购买装扮需要的价格
  287. $decorate = Db::name('decorate_relation')->where(['id'=>$did])->find();
  288. if(!$decorate) {
  289. $this->error("道具信息获取失败!");
  290. }
  291. $total_price = bcmul($decorate['price'],$number,0);
  292. if($walletinfo['gold'] < $total_price) {
  293. $this->error("您的金币不足,请先充值!");
  294. }
  295. // 进行购买逻辑
  296. Db::startTrans();
  297. // 添加到背包
  298. for($i=1;$i<=$number;$i++){
  299. $data[] = [
  300. 'user_id' => $this->auth->id,
  301. 'decorate_id' => $decorate['id'],
  302. 'is_using' => 0,
  303. 'createtime' => time(),
  304. 'updatetime' => time(),
  305. ];
  306. }
  307. $log_id = Db::name('user_decorate_relation')->insertAll($data);
  308. if(!$log_id){
  309. Db::rollback();
  310. $this->error('购买失败');
  311. }
  312. //扣钱
  313. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$total_price,32,'购买关系卡','user_decorate_relation',0);
  314. if($rs['status'] === false){
  315. Db::rollback();
  316. $this->error($rs['msg']);
  317. }
  318. Db::commit();
  319. $this->success("购买成功!");
  320. }
  321. //获取用户装扮
  322. public function my_decorate_list()
  323. {
  324. $uid = $this->auth->id;
  325. $map = [
  326. 'a.user_id' => $uid,
  327. 'a.is_using'=> 0,
  328. ];
  329. $list = Db::name('user_decorate_relation')
  330. ->alias('a')
  331. ->field('a.id,a.decorate_id,b.name,b.base_image,count(a.decorate_id) as number')
  332. ->join('decorate_relation b', 'a.decorate_id = b.id','LEFT')
  333. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  334. $list = list_domain_image($list,['base_image']);
  335. $this->success('success',$list);
  336. }
  337. }