Relation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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)->update(['is_using'=>0,'updatetime'=>time()]);
  223. if(!$use_card){
  224. Db::rollback();
  225. $this->error('操作失败');
  226. }
  227. }
  228. //给发起方一条消息
  229. $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
  230. $message = [
  231. 'user_id' => $info['uid'],
  232. 'title' => '关系申请结果',
  233. 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
  234. 'createtime' => time(),
  235. 'status' => 0,
  236. 'infotype' => '',//建立关系
  237. 'infotype_id' => 0,
  238. ];
  239. Db::name('message')->insertGetId($message);
  240. //
  241. $this->success($remark);
  242. }
  243. //取消
  244. public function cancel(){
  245. $id = input('id',0);
  246. $relation_id = input('relation_id',0);
  247. $where = [
  248. 'id' => $id,
  249. 'relation_id' => $relation_id
  250. ];
  251. $where2 = 'uid = '.$this->auth->id.' or to_uid = '.$this->auth->id;
  252. $info = Db::name('user_relation')->where($where)->where($where2)->find();
  253. if(empty($info)){
  254. $this->error('不存在的关系');
  255. }
  256. Db::name('user_relation')->where($where)->delete();
  257. //给发起方一条消息
  258. $relation_name = Db::name('relation')->where('id',$relation_id)->value('name');
  259. $message = [
  260. 'user_id' => ($info['uid'] == $this->auth->id) ? $info['to_uid'] : $info['uid'],
  261. 'title' => '关系取消',
  262. 'content' => $this->auth->nickname.'已取消和你的'.$relation_name.'关系',
  263. 'createtime' => time(),
  264. 'status' => 0,
  265. 'infotype' => '',//建立关系
  266. 'infotype_id' => 0,
  267. ];
  268. Db::name('message')->insertGetId($message);
  269. //
  270. $this->success('关系已取消');
  271. }
  272. ///////////////////////////关系卡///////////////////////
  273. //关系卡列表
  274. public function decorate_list(){
  275. $list = Db::name('decorate_relation')->select();
  276. $list = list_domain_image($list,['base_image']);
  277. $relation = Db::name('relation')->where('is_show',1)->order('weigh desc')->column('name');
  278. $relation = implode(',',$relation);
  279. $result = [
  280. 'list' => $list,
  281. 'relation' => $relation
  282. ];
  283. $this->success(1,$result);
  284. }
  285. /**
  286. * 购买并加入我的背包
  287. */
  288. public function buy_one() {
  289. $number = input_post('number',1,'intval'); //数量
  290. if(!$number){
  291. $this->error();
  292. }
  293. $did = input_post('did',''); //装扮ID
  294. if (!$did) {
  295. $this->error();
  296. }
  297. // 判断用户金币余额是否充足
  298. $walletinfo = model('wallet')->getWallet($this->auth->id);
  299. // 获取购买装扮需要的价格
  300. $decorate = Db::name('decorate_relation')->where(['id'=>$did])->find();
  301. if(!$decorate) {
  302. $this->error("道具信息获取失败!");
  303. }
  304. $total_price = bcmul($decorate['price'],$number,0);
  305. if($walletinfo['gold'] < $total_price) {
  306. $this->error("您的金币不足,请先充值!");
  307. }
  308. // 进行购买逻辑
  309. Db::startTrans();
  310. // 添加到背包
  311. for($i=1;$i<=$number;$i++){
  312. $data[] = [
  313. 'user_id' => $this->auth->id,
  314. 'decorate_id' => $decorate['id'],
  315. 'is_using' => 0,
  316. 'createtime' => time(),
  317. 'updatetime' => time(),
  318. ];
  319. }
  320. $log_id = Db::name('user_decorate_relation')->insertAll($data);
  321. if(!$log_id){
  322. Db::rollback();
  323. $this->error('购买失败');
  324. }
  325. //扣钱
  326. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$total_price,32,'购买关系卡','user_decorate_relation',0);
  327. if($rs['status'] === false){
  328. Db::rollback();
  329. $this->error($rs['msg']);
  330. }
  331. Db::commit();
  332. $this->success("购买成功!");
  333. }
  334. //获取用户装扮
  335. public function my_decorate_list()
  336. {
  337. $uid = $this->auth->id;
  338. $map = [
  339. 'a.user_id' => $uid,
  340. 'a.is_using'=> 0,
  341. ];
  342. $list = Db::name('user_decorate_relation')
  343. ->alias('a')
  344. ->field('a.id,a.decorate_id,b.name,b.base_image,count(a.decorate_id) as number')
  345. ->join('decorate_relation b', 'a.decorate_id = b.id','LEFT')
  346. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  347. $list = list_domain_image($list,['base_image']);
  348. $this->success('success',$list);
  349. }
  350. }