Userdecorate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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','android_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,b.android_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','android_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,b.android_image')
  104. ->join('decorate b', 'a.decorate_id = b.id')
  105. ->where($map)->order('a.id desc')->find();
  106. $info = info_domain_image($info,['base_image','play_image','android_image']);
  107. $this->success('success',$info);
  108. }
  109. //设置装扮
  110. public function set_user_decorate()
  111. {
  112. $did = input_post('did',''); //用户装扮ID
  113. if (!$did) {
  114. $this->error();
  115. }
  116. $map = [
  117. 'user_id' => $this->auth->id,
  118. 'id' => $did,
  119. ];
  120. $info = Db::name('user_decorate')->where($map)->find();
  121. if (empty($info)) {
  122. $this->error('装扮不存在');
  123. }
  124. if ($info['end_time'] < time()) {
  125. $this->error('装扮已过期');
  126. }
  127. Db::startTrans();
  128. //清理该类型装扮使用状态
  129. $map = [
  130. 'user_id' => $this->auth->id,
  131. 'decorate_type' => $info['decorate_type'],
  132. ];
  133. $data = [];
  134. $data['is_using'] = 0;
  135. $data['updatetime'] = time();
  136. $reslut = Db::name('user_decorate')->where($map)->update($data);
  137. if (!$reslut) {
  138. Db::rollback();
  139. $this->error('设置失败');
  140. }
  141. //设置使用中状态
  142. $map = [
  143. // 'user_id' => $this->auth->id,
  144. 'id' => $did,
  145. ];
  146. $data = [];
  147. $data['is_using'] = 1;
  148. $data['updatetime'] = time();
  149. $reslut = Db::name('user_decorate')->where($map)->update($data);
  150. if (!$reslut) {
  151. Db::rollback();
  152. $this->error('设置失败');
  153. }
  154. // 提交事务
  155. Db::commit();
  156. $this->success('设置成功');
  157. }
  158. //取消装扮
  159. public function cancel_user_decorate()
  160. {
  161. $did = input_post('did',''); //装扮ID
  162. if (!$did) {
  163. $this->error();
  164. }
  165. $map = [
  166. 'user_id' => $this->auth->id,
  167. 'decorate_id' => $did,
  168. ];
  169. $data = [];
  170. $data['is_using'] = 0;
  171. $data['updatetime'] = time();
  172. $reslut = Db::name('user_decorate')->where($map)->update($data);
  173. $this->success('设置成功');
  174. }
  175. }