Relation.php 14 KB

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