Relation.php 14 KB

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