Userdecorate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 用户装扮接口
  7. */
  8. class Userdecorate extends Api
  9. {
  10. protected $noNeedLogin = ['decorate_list'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 礼物列表
  14. *
  15. */
  16. public function decorate_list()
  17. {
  18. $type = input_post('type',1); // 类型:1=座驾,2=头饰,3=尾灯,4=气泡
  19. // 获取基本信息
  20. $where = [];
  21. $where['type'] = $type;
  22. $where['status'] = 1;
  23. $list = Db::name('decorate')->where($where)->autopage()->order('sort desc')->select();
  24. $list = list_domain_image($list,['base_image,play_image']);
  25. $this->success("success",$list);
  26. }
  27. /**
  28. * 购买并加入我的背包
  29. */
  30. public function buy_one() {
  31. $did = input_post('did',''); //装扮ID
  32. if (!$did) {
  33. $this->error();
  34. }
  35. // 判断用户钻石余额是否充足
  36. $walletinfo = model('wallet')->getWallet($this->auth->id);
  37. // 获取购买装扮需要的价格
  38. $decorate = Db::name('decorate')->where(['id'=>$did])->find();
  39. if(!$decorate) {
  40. $this->error("装扮信息获取失败!");
  41. }
  42. if($walletinfo['gold'] < $decorate['price']) {
  43. $this->error("您的余额不足,请先充值!");
  44. }
  45. // 进行购买逻辑
  46. Db::startTrans();
  47. // 添加到背包
  48. $data = [
  49. 'user_id' => $this->auth->id,
  50. 'decorate_id' => $decorate['id'],
  51. 'decorate_type' => $decorate['type'],
  52. 'is_using' => 0,
  53. 'end_time' => time() + ($decorate['days'] * 86400),
  54. 'createtime' => time(),
  55. 'updatetime' => time(),
  56. ];
  57. $log_id = Db::name('user_decorate')->insertGetId($data);
  58. if(!$log_id){
  59. Db::rollback();
  60. $this->error('购买失败');
  61. }
  62. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'','user_decorate',$log_id);
  63. if($rs['status'] === false){
  64. Db::rollback();
  65. $this->error($rs['msg']);
  66. }
  67. Db::commit();
  68. $this->success("购买成功!");
  69. }
  70. //获取用户装扮
  71. public function my_decorate_list()
  72. {
  73. $uid = $this->auth->id;
  74. $type = input_post('type',1);
  75. $map = [
  76. 'a.end_time' => ['gt',time()],
  77. 'a.decorate_type' => $type,
  78. 'a.user_id' => $uid,
  79. ];
  80. $list = Db::name('user_decorate')
  81. ->alias('a')
  82. ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image')
  83. ->join('decorate b', 'a.decorate_id = b.id')
  84. ->where($map)->order('a.id desc')->autopage()->select();
  85. $list = list_domain_image($list,['base_image','play_image']);
  86. $this->success('success',$list);
  87. }
  88. //获得某用户的某类型正在使用的装扮
  89. public function get_user_onetype_decorate(){
  90. $uid = input_post('uid');
  91. $type = input_post('type',1);
  92. if(!$uid){
  93. $this->error();
  94. }
  95. //获取用户气泡
  96. $map = [
  97. 'a.user_id' => $uid,
  98. 'a.is_using' => 1,
  99. 'a.decorate_type' => $type,
  100. 'a.end_time' => ['gt',time()],
  101. ];
  102. $info = Db::name('user_decorate')->alias('a')
  103. ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image')
  104. ->join('decorate b', 'a.decorate_id = b.id')
  105. ->where($map)->order('a.id desc')->find();
  106. $this->success('success',$info);
  107. }
  108. //设置装扮
  109. public function set_user_decorate()
  110. {
  111. $did = input_post('did',''); //装扮ID
  112. if (!$did) {
  113. $this->error();
  114. }
  115. $map = [
  116. 'user_id' => $this->auth->id,
  117. 'decorate_id' => $did,
  118. ];
  119. $info = Db::name('user_decorate')->where($map)->find();
  120. if (empty($info)) {
  121. $this->error('装扮不存在');
  122. }
  123. if ($info['end_time'] < time()) {
  124. $this->error('装扮已过期');
  125. }
  126. Db::startTrans();
  127. //清理该类型装扮使用状态
  128. $map = [
  129. 'user_id' => $this->auth->id,
  130. 'decorate_type' => $info['decorate_type'],
  131. ];
  132. $data = [];
  133. $data['is_using'] = 0;
  134. $data['updatetime'] = time();
  135. $reslut = Db::name('user_decorate')->where($map)->update($data);
  136. if (!$reslut) {
  137. Db::rollback();
  138. $this->error('设置失败');
  139. }
  140. //设置使用中状态
  141. $map = [
  142. 'user_id' => $this->auth->id,
  143. 'decorate_id' => $did,
  144. ];
  145. $data = [];
  146. $data['is_using'] = 1;
  147. $data['updatetime'] = time();
  148. $reslut = Db::name('user_decorate')->where($map)->update($data);
  149. if (!$reslut) {
  150. Db::rollback();
  151. $this->error('设置失败');
  152. }
  153. // 提交事务
  154. Db::commit();
  155. $this->success('设置成功');
  156. }
  157. //取消装扮
  158. public function cancel_user_decorate()
  159. {
  160. $did = input_post('did',''); //装扮ID
  161. if (!$did) {
  162. $this->error();
  163. }
  164. $map = [
  165. 'user_id' => $this->auth->id,
  166. 'decorate_id' => $did,
  167. ];
  168. $data = [];
  169. $data['is_using'] = 0;
  170. $data['updatetime'] = time();
  171. $reslut = Db::name('user_decorate')->where($map)->update($data);
  172. $this->success('设置成功');
  173. }
  174. }