Userdecorate.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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['end_time'] < time()){
  70. //过期了
  71. $update['end_time'] = time() + (intval($decorate['days']) * 86400);
  72. }else{
  73. //追加日期
  74. $update['end_time'] = $check['end_time'] + (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. $log_id = $check['id'];
  82. }
  83. //扣钱
  84. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'购买装扮','user_decorate',$log_id);
  85. if($rs['status'] === false){
  86. Db::rollback();
  87. $this->error($rs['msg']);
  88. }
  89. Db::commit();
  90. $this->success("购买成功!");
  91. }
  92. //获取用户装扮
  93. public function my_decorate_list()
  94. {
  95. $uid = $this->auth->id;
  96. $type = input_post('type',2);
  97. $map = [
  98. 'a.end_time' => ['gt',time()],
  99. 'a.decorate_type' => $type,
  100. 'a.user_id' => $uid,
  101. ];
  102. $list = Db::name('user_decorate')
  103. ->alias('a')
  104. ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image')
  105. ->join('decorate b', 'a.decorate_id = b.id')
  106. ->where($map)->order('a.id desc')->autopage()->select();
  107. $list = list_domain_image($list,['base_image','play_image']);
  108. $this->success('success',$list);
  109. }
  110. //获得某用户的某类型正在使用的装扮
  111. public function get_user_onetype_decorate(){
  112. $uid = input_post('uid');
  113. $type = input_post('type',2);
  114. if(!$uid){
  115. $this->error();
  116. }
  117. //获取用户气泡
  118. $map = [
  119. 'a.user_id' => $uid,
  120. 'a.is_using' => 1,
  121. 'a.decorate_type' => $type,
  122. 'a.end_time' => ['gt',time()],
  123. ];
  124. $info = Db::name('user_decorate')->alias('a')
  125. ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image')
  126. ->join('decorate b', 'a.decorate_id = b.id')
  127. ->where($map)->order('a.id desc')->find();
  128. $info = info_domain_image($info,['base_image','play_image']);
  129. $this->success('success',$info);
  130. }
  131. //设置装扮
  132. public function set_user_decorate()
  133. {
  134. $did = input_post('did',''); //用户装扮ID
  135. if (!$did) {
  136. $this->error();
  137. }
  138. $map = [
  139. 'user_id' => $this->auth->id,
  140. 'id' => $did,
  141. ];
  142. $info = Db::name('user_decorate')->where($map)->find();
  143. if (empty($info)) {
  144. $this->error('装扮不存在');
  145. }
  146. if ($info['end_time'] < time()) {
  147. $this->error('装扮已过期');
  148. }
  149. Db::startTrans();
  150. //清理该类型装扮使用状态
  151. $map = [
  152. 'user_id' => $this->auth->id,
  153. 'decorate_type' => $info['decorate_type'],
  154. ];
  155. $data = [];
  156. $data['is_using'] = 0;
  157. $data['updatetime'] = time();
  158. $reslut = Db::name('user_decorate')->where($map)->update($data);
  159. if (!$reslut) {
  160. Db::rollback();
  161. $this->error('设置失败');
  162. }
  163. //设置使用中状态
  164. $map = [
  165. // 'user_id' => $this->auth->id,
  166. 'id' => $did,
  167. ];
  168. $data = [];
  169. $data['is_using'] = 1;
  170. $data['updatetime'] = time();
  171. $reslut = Db::name('user_decorate')->where($map)->update($data);
  172. if (!$reslut) {
  173. Db::rollback();
  174. $this->error('设置失败');
  175. }
  176. // 提交事务
  177. Db::commit();
  178. $this->success('设置成功');
  179. }
  180. //取消装扮
  181. public function cancel_user_decorate()
  182. {
  183. $did = input_post('did',''); //装扮ID
  184. if (!$did) {
  185. $this->error();
  186. }
  187. $map = [
  188. 'user_id' => $this->auth->id,
  189. 'id' => $did,
  190. ];
  191. $data = [];
  192. $data['is_using'] = 0;
  193. $data['updatetime'] = time();
  194. $reslut = Db::name('user_decorate')->where($map)->update($data);
  195. $this->success('设置成功');
  196. }
  197. }