Userdecorate.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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',2); // 类型:1=座驾,2=头饰,3=尾灯,4=气泡,5=关系卡,6=联盟道具
  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. $this->apiLimit();
  32. $did = input_post('did',''); //装扮ID
  33. if (!$did) {
  34. $this->error();
  35. }
  36. // 判断用户金币余额是否充足
  37. $walletinfo = model('wallet')->getWallet($this->auth->id);
  38. // 获取购买装扮需要的价格
  39. $decorate = Db::name('decorate')->where(['id'=>$did])->find();
  40. if(!$decorate) {
  41. $this->error("装扮信息获取失败!");
  42. }
  43. if($walletinfo['gold'] < $decorate['price']) {
  44. $this->error("您的金币不足,请先充值!");
  45. }
  46. // 进行购买逻辑
  47. Db::startTrans();
  48. // 添加到背包
  49. $check = Db::name('user_decorate')->where('user_id',$this->auth->id)->where('decorate_id',$decorate['id'])->order('id desc')->lock(true)->find();
  50. if(!$check){
  51. $data = [
  52. 'user_id' => $this->auth->id,
  53. 'decorate_id' => $decorate['id'],
  54. 'decorate_type' => $decorate['type'],
  55. 'is_using' => 0,
  56. 'end_time' => time() + (intval($decorate['days']) * 86400),
  57. 'createtime' => time(),
  58. 'updatetime' => time(),
  59. ];
  60. $log_id = Db::name('user_decorate')->insertGetId($data);
  61. if(!$log_id){
  62. Db::rollback();
  63. $this->error('购买失败');
  64. }
  65. }else{
  66. $update = [
  67. 'updatetime' => time(),
  68. ];
  69. if($check['endtime'] < time()){
  70. //过期了
  71. $update['endtime'] = time() + (intval($decorate['days']) * 86400);
  72. }else{
  73. //追加日期
  74. $update['endtime'] = $check['endtime'] + (intval($decorate['days']) * 86400);
  75. }
  76. $rs_update = Db::name('user_decorate')->where('id',$check['id'])->update($update);
  77. if($rs_update === false){
  78. Db::rollback();
  79. $this->error('购买失败');
  80. }
  81. }
  82. //扣钱
  83. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'购买装扮','user_decorate',$log_id);
  84. if($rs['status'] === false){
  85. Db::rollback();
  86. $this->error($rs['msg']);
  87. }
  88. Db::commit();
  89. $this->success("购买成功!");
  90. }
  91. //获取用户装扮
  92. public function my_decorate_list()
  93. {
  94. $uid = $this->auth->id;
  95. $type = input_post('type',2);
  96. $map = [
  97. 'a.end_time' => ['gt',time()],
  98. 'a.decorate_type' => $type,
  99. 'a.user_id' => $uid,
  100. ];
  101. $list = Db::name('user_decorate')
  102. ->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')->autopage()->select();
  106. $list = list_domain_image($list,['base_image','play_image']);
  107. $this->success('success',$list);
  108. }
  109. //获得某用户的某类型正在使用的装扮
  110. public function get_user_onetype_decorate(){
  111. $uid = input_post('uid');
  112. $type = input_post('type',2);
  113. if(!$uid){
  114. $this->error();
  115. }
  116. //获取用户气泡
  117. $map = [
  118. 'a.user_id' => $uid,
  119. 'a.is_using' => 1,
  120. 'a.decorate_type' => $type,
  121. 'a.end_time' => ['gt',time()],
  122. ];
  123. $info = Db::name('user_decorate')->alias('a')
  124. ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image')
  125. ->join('decorate b', 'a.decorate_id = b.id')
  126. ->where($map)->order('a.id desc')->find();
  127. $info = info_domain_image($info,['base_image','play_image']);
  128. $this->success('success',$info);
  129. }
  130. //设置装扮
  131. public function set_user_decorate()
  132. {
  133. $did = input_post('did',''); //用户装扮ID
  134. if (!$did) {
  135. $this->error();
  136. }
  137. $map = [
  138. 'user_id' => $this->auth->id,
  139. 'id' => $did,
  140. ];
  141. $info = Db::name('user_decorate')->where($map)->find();
  142. if (empty($info)) {
  143. $this->error('装扮不存在');
  144. }
  145. if ($info['end_time'] < time()) {
  146. $this->error('装扮已过期');
  147. }
  148. Db::startTrans();
  149. //清理该类型装扮使用状态
  150. $map = [
  151. 'user_id' => $this->auth->id,
  152. 'decorate_type' => $info['decorate_type'],
  153. ];
  154. $data = [];
  155. $data['is_using'] = 0;
  156. $data['updatetime'] = time();
  157. $reslut = Db::name('user_decorate')->where($map)->update($data);
  158. if (!$reslut) {
  159. Db::rollback();
  160. $this->error('设置失败');
  161. }
  162. //设置使用中状态
  163. $map = [
  164. // 'user_id' => $this->auth->id,
  165. 'id' => $did,
  166. ];
  167. $data = [];
  168. $data['is_using'] = 1;
  169. $data['updatetime'] = time();
  170. $reslut = Db::name('user_decorate')->where($map)->update($data);
  171. if (!$reslut) {
  172. Db::rollback();
  173. $this->error('设置失败');
  174. }
  175. // 提交事务
  176. Db::commit();
  177. $this->success('设置成功');
  178. }
  179. //取消装扮
  180. public function cancel_user_decorate()
  181. {
  182. $did = input_post('did',''); //装扮ID
  183. if (!$did) {
  184. $this->error();
  185. }
  186. $map = [
  187. 'user_id' => $this->auth->id,
  188. 'id' => $did,
  189. ];
  190. $data = [];
  191. $data['is_using'] = 0;
  192. $data['updatetime'] = time();
  193. $reslut = Db::name('user_decorate')->where($map)->update($data);
  194. $this->success('设置成功');
  195. }
  196. }