Uidsale.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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,price,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 $key1 => $val1){
  41. $result[] = $val1;
  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,price,new_id')->where($map)->where('id',$id)->lock(true)->find();
  63. if(empty($info)){
  64. Db::rollback();
  65. $this->error('此靓号不存在,请刷新重试');
  66. }
  67. //扣金币
  68. if($info['price'] > 0){
  69. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$info['price'],'-',0,'购买靓号',36,'jewel');
  70. if($rs_wallet['status'] === false){
  71. Db::rollback();
  72. $this->error('购买靓号付费失败');
  73. }
  74. }
  75. //卖出
  76. $update = [
  77. 'status' => 1, //卖出
  78. 'saletime' => time(), //卖出时间
  79. 'user_id' => $this->auth->id, //目前所属用户
  80. 'is_show' => 0, //下架
  81. ];
  82. $rs = Db::name('uidsale')->where('id',$id)->update($update);
  83. if($rs === false){
  84. Db::rollback();
  85. $this->error('购买失败');
  86. }
  87. //自带id插入用户背包
  88. $nownum = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->find();
  89. if(empty($nownum)){
  90. $data = [
  91. 'user_id' => $this->auth->id,
  92. 'u_id' => $this->auth->u_id,
  93. 'is_cool' => 0,
  94. 'is_using' => 1,
  95. ];
  96. $rs1 = Db::name('uidsale_bag')->insertGetId($data);
  97. if(!$rs1){
  98. Db::rollback();
  99. $this->error('购买失败');
  100. }
  101. }
  102. //插入用户靓号
  103. $data = [
  104. 'user_id' => $this->auth->id,
  105. 'u_id' => $info['new_id'],
  106. 'is_cool' => 1,
  107. ];
  108. $rs2 = Db::name('uidsale_bag')->insertGetId($data);
  109. if(!$rs2){
  110. Db::rollback();
  111. $this->error('购买失败');
  112. }
  113. Db::commit();
  114. $this->success('操作成功',$id);
  115. }
  116. //用户当前靓号背包
  117. public function my_bag(){
  118. $list = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->order('is_using desc,is_cool desc,id desc')->select();
  119. $this->success(1,$list);
  120. }
  121. //切换使用靓号
  122. public function use_newone(){
  123. if(!$this->apiLimit()){
  124. $this->error('操作频繁');
  125. }
  126. $id = input('id',0);
  127. if(!$id){
  128. $this->error();
  129. }
  130. Db::startTrans();
  131. $info = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->where('id',$id)->find();
  132. if(empty($info)){
  133. Db::rollback();
  134. $this->error('操作有误,请刷新重试');
  135. }
  136. //全部卸下
  137. $rs = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->update(['is_using'=>0]);
  138. if($rs === false){
  139. Db::rollback();
  140. $this->error('切换失败');
  141. }
  142. //此靓号切换
  143. $rs = Db::name('uidsale_bag')->where('user_id',$this->auth->id)->where('id',$id)->update(['is_using'=>1]);
  144. if($rs === false){
  145. Db::rollback();
  146. $this->error('切换失败');
  147. }
  148. //更新用户
  149. $rs = Db::name('user')->where('id',$this->auth->id)->update(['u_id'=>$info['u_id'],'is_cool'=>$info['is_cool']]);
  150. if($rs === false){
  151. Db::rollback();
  152. $this->error('切换失败');
  153. }
  154. Db::commit();
  155. $this->success();
  156. }
  157. }