Relation.php 11 KB

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