Uidsale.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 靓号
  7. */
  8. class Uidsale extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //靓号首页
  13. public function lists(){
  14. $keyword = input('keyword','');
  15. $typelist = Db::name('uidsale_type')->order('id asc')->select();
  16. $map = [
  17. 'status' => 0, //未卖出
  18. 'saletime' => 0, //卖出时间
  19. 'user_id' => 0, //目前所属用户
  20. 'is_show' => 1, //上架
  21. ];
  22. if(!empty($keyword)){
  23. $map['new_id'] = ['LIKE','%'.$keyword.'%'];
  24. }
  25. $uidlist = Db::name('uidsale')->field('id,new_id,type_id')->where($map)->order('id asc')->select();
  26. foreach($typelist as $key => &$val){
  27. $val['child'] = [];
  28. foreach($uidlist as $k => $v){
  29. if($v['type_id'] == $val['id']){
  30. $val['child'][] = $v;
  31. }
  32. }
  33. //去掉空的组
  34. if(empty($val['child'])){
  35. unset($typelist[$key]);
  36. }
  37. }
  38. //因为上面用了unset,数组有了键,前端不显示,重组一个新数组
  39. $result = [];
  40. foreach($typelist as $key => $val){
  41. $result[] = $val;
  42. }
  43. $this->success(1,$result);
  44. }
  45. //买靓号
  46. public function buy_one(){
  47. if(!$this->apiLimit()){
  48. $this->error('操作频繁');
  49. }
  50. $id = input('id',0);
  51. if(!$id){
  52. $this->error();
  53. }
  54. Db::startTrans();
  55. //检查
  56. $map = [
  57. 'status' => 0, //未卖出
  58. 'saletime' => 0, //卖出时间
  59. 'user_id' => 0, //目前所属用户
  60. 'is_show' => 1, //上架
  61. ];
  62. $info = Db::name('uidsale')->field('id,new_id')->where($map)->where('id',$id)->lock(true)->find();
  63. if(empty($info)){
  64. Db::rollback();
  65. $this->error('此靓号不存在,请刷新重试');
  66. }
  67. //卖出
  68. $update = [
  69. 'status' => 1, //卖出
  70. 'saletime' => time(), //卖出时间
  71. 'user_id' => $this->auth->id, //目前所属用户
  72. 'is_show' => 0, //下架
  73. ];
  74. $rs = Db::name('uidsale')->where('id',$id)->update($update);
  75. if($rs === false){
  76. Db::rollback();
  77. $this->error('购买失败');
  78. }
  79. //插入用户背包
  80. $nownum = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->find();
  81. if(empty($nownum)){
  82. $data = [
  83. 'user_id' => $this->auth->id,
  84. 'u_id' => $this->auth->u_id,
  85. 'is_cool' => 0,
  86. 'is_using' => 1,
  87. ];
  88. $rs1 = Db::name('uidsale_bag')->insertGetId($data);
  89. if(!$rs1){
  90. Db::rollback();
  91. $this->error('购买失败');
  92. }
  93. }
  94. //插入用户靓号
  95. $data = [
  96. 'user_id' => $this->auth->id,
  97. 'u_id' => $info['new_id'],
  98. 'is_cool' => 1,
  99. ];
  100. $rs2 = Db::name('uidsale_bag')->insertGetId($data);
  101. if(!$rs2){
  102. Db::rollback();
  103. $this->error('购买失败');
  104. }
  105. Db::commit();
  106. $this->success('操作成功',$id);
  107. }
  108. //用户当前靓号背包
  109. public function my_bag(){
  110. $list = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->order('is_using desc,is_cool desc,id desc')->select();
  111. $this->success(1,$list);
  112. }
  113. //切换使用靓号
  114. public function use_newone(){
  115. if(!$this->apiLimit()){
  116. $this->error('操作频繁');
  117. }
  118. $id = input('id',0);
  119. if(!$id){
  120. $this->error();
  121. }
  122. Db::startTrans();
  123. $info = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->where('id',$id)->find();
  124. if(empty($info)){
  125. Db::rollback();
  126. $this->error('操作有误,请刷新重试');
  127. }
  128. //全部卸下
  129. $rs = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->update(['is_using'=>0]);
  130. if($rs === false){
  131. Db::rollback();
  132. $this->error('切换失败');
  133. }
  134. //此靓号切换
  135. $rs = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->where('id',$id)->update(['is_using'=>1]);
  136. if($rs === false){
  137. Db::rollback();
  138. $this->error('切换失败');
  139. }
  140. //更新用户
  141. $rs = Db::name('user')->where('id',$this->auth->id)->update(['u_id'=>$info['u_id'],'is_cool'=>$info['is_cool']]);
  142. if($rs === false){
  143. Db::rollback();
  144. $this->error('切换失败');
  145. }
  146. Db::commit();
  147. $this->success();
  148. }
  149. }