Relation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. 'user_id' => $to_uid,
  156. 'title' => '关系审核',
  157. 'content' => $this->auth->nickname . '申请与你成为'.$relation['name'],
  158. 'createtime' => time(),
  159. 'status' => 0,
  160. 'infotype' => 'newrelation',//建立关系
  161. 'infotype_id' => 0,
  162. ];
  163. Db::name('message')->insertGetId($message);
  164. //发起人也要发消息
  165. //系统消息
  166. $to_nickname = Db::name('user')->where('id',$to_uid)->value('nickname');
  167. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'申请关系','您正在申请与'.$to_nickname.'成为'.$relation_name.'关系,请等待对方同意','applyrelation');
  168. Db::commit();
  169. $this->success('申请成功,请等待对方同意',$id);
  170. }
  171. //待审核关系
  172. public function audit_lists(){
  173. $list = Db::name('user_relation')->alias('ur')
  174. ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  175. ->join('relation','ur.relation_id = relation.id','LEFT')
  176. ->join('user','ur.uid = user.id','LEFT')
  177. ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
  178. ->where('ur.to_uid',$this->auth->id)
  179. ->where('ur.status',0)
  180. ->order('ur.id desc')->autopage()->select();
  181. $list = list_domain_image($list,['avatar']);
  182. if(!empty($list)){
  183. foreach($list as $key => &$val){
  184. //用户年龄
  185. $val['age'] = birthtime_to_age($val['birthday']);
  186. unset($val['birthday']);
  187. //用户vip
  188. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  189. unset($val['vip_endtime']);
  190. }
  191. }
  192. $this->success('success',$list);
  193. }
  194. //审核
  195. public function audit(){
  196. $id = input('id',0);
  197. $status = input('status',1);
  198. //查询
  199. $info = Db::name('user_relation')->alias('ur')
  200. ->where('ur.id',$id)
  201. ->where('ur.to_uid',$this->auth->id)
  202. ->where('ur.status',0)
  203. ->find();
  204. if(!$info){
  205. $this->error('请刷新重试');
  206. }
  207. //更新
  208. $data = [
  209. 'status' => $status,
  210. 'updatetime' => time(),
  211. ];
  212. $rs = Db::name('user_relation')->where('id',$id)->update($data);
  213. if($rs === false){
  214. $this->error('操作失败');
  215. }
  216. if($status == 1){
  217. $remark = '您已同意建立关系';
  218. $remark2 = '已同意';
  219. }else{
  220. $remark = '已拒绝';
  221. $remark2 = '已拒绝';
  222. //退回关系卡
  223. $use_card = Db::name('user_decorate_relation')->where('user_id',$info['uid'])->where('is_using',1)->order('id desc')->find();
  224. if($use_card){
  225. $rs2 = Db::name('user_decorate_relation')->where('id',$use_card['id'])->update(['is_using'=>0,'updatetime'=>time()]);
  226. if($rs2 === false){
  227. Db::rollback();
  228. $this->error('操作失败');
  229. }
  230. }
  231. }
  232. //给发起方一条消息
  233. $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
  234. $message = [
  235. 'user_id' => $info['uid'],
  236. 'title' => '关系申请结果',
  237. 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
  238. 'createtime' => time(),
  239. 'status' => 0,
  240. 'infotype' => '',//建立关系
  241. 'infotype_id' => 0,
  242. ];
  243. Db::name('message')->insertGetId($message);
  244. //
  245. $this->success($remark);
  246. }
  247. //取消
  248. public function cancel(){
  249. $id = input('id',0);
  250. $relation_id = input('relation_id',0);
  251. $where = [
  252. 'id' => $id,
  253. 'relation_id' => $relation_id
  254. ];
  255. $where2 = 'uid = '.$this->auth->id.' or to_uid = '.$this->auth->id;
  256. $info = Db::name('user_relation')->where($where)->where($where2)->find();
  257. if(empty($info)){
  258. $this->error('不存在的关系');
  259. }
  260. Db::name('user_relation')->where($where)->delete();
  261. //给发起方一条消息
  262. $relation_name = Db::name('relation')->where('id',$relation_id)->value('name');
  263. $message = [
  264. 'user_id' => ($info['uid'] == $this->auth->id) ? $info['to_uid'] : $info['uid'],
  265. 'title' => '关系取消',
  266. 'content' => $this->auth->nickname.'已取消和你的'.$relation_name.'关系',
  267. 'createtime' => time(),
  268. 'status' => 0,
  269. 'infotype' => '',//建立关系
  270. 'infotype_id' => 0,
  271. ];
  272. Db::name('message')->insertGetId($message);
  273. //
  274. $this->success('关系已取消');
  275. }
  276. ///////////////////////////关系卡///////////////////////
  277. //关系卡列表
  278. public function decorate_list(){
  279. $list = Db::name('decorate_relation')->select();
  280. $list = list_domain_image($list,['base_image']);
  281. $relation = Db::name('relation')->where('is_show',1)->order('weigh desc')->column('name');
  282. $relation = implode(',',$relation);
  283. $result = [
  284. 'list' => $list,
  285. 'relation' => $relation
  286. ];
  287. $this->success(1,$result);
  288. }
  289. /**
  290. * 购买并加入我的背包
  291. */
  292. public function buy_one() {
  293. $number = input_post('number',1,'intval'); //数量
  294. if(!$number){
  295. $this->error();
  296. }
  297. $did = input_post('did',''); //装扮ID
  298. if (!$did) {
  299. $this->error();
  300. }
  301. // 判断用户金币余额是否充足
  302. $walletinfo = model('wallet')->getWallet($this->auth->id);
  303. // 获取购买装扮需要的价格
  304. $decorate = Db::name('decorate_relation')->where(['id'=>$did])->find();
  305. if(!$decorate) {
  306. $this->error("道具信息获取失败!");
  307. }
  308. $total_price = bcmul($decorate['price'],$number,0);
  309. if($walletinfo['gold'] < $total_price) {
  310. $this->error("您的金币不足,请先充值!");
  311. }
  312. // 进行购买逻辑
  313. Db::startTrans();
  314. // 添加到背包
  315. for($i=1;$i<=$number;$i++){
  316. $data[] = [
  317. 'user_id' => $this->auth->id,
  318. 'decorate_id' => $decorate['id'],
  319. 'is_using' => 0,
  320. 'createtime' => time(),
  321. 'updatetime' => time(),
  322. ];
  323. }
  324. $log_id = Db::name('user_decorate_relation')->insertAll($data);
  325. if(!$log_id){
  326. Db::rollback();
  327. $this->error('购买失败');
  328. }
  329. //扣钱
  330. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$total_price,32,'购买关系卡','user_decorate_relation',0);
  331. if($rs['status'] === false){
  332. Db::rollback();
  333. $this->error($rs['msg']);
  334. }
  335. Db::commit();
  336. $this->success("购买成功!");
  337. }
  338. //获取用户装扮
  339. public function my_decorate_list()
  340. {
  341. $uid = $this->auth->id;
  342. $map = [
  343. 'a.user_id' => $uid,
  344. 'a.is_using'=> 0,
  345. ];
  346. $list = Db::name('user_decorate_relation')
  347. ->alias('a')
  348. ->field('a.id,a.decorate_id,b.name,b.base_image,count(a.decorate_id) as number')
  349. ->join('decorate_relation b', 'a.decorate_id = b.id','LEFT')
  350. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  351. $list = list_domain_image($list,['base_image']);
  352. $this->success('success',$list);
  353. }
  354. }