Lianmeng.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 道具商城,联盟
  7. */
  8. class Lianmeng extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //道具列表
  13. public function decorate_list(){
  14. $type = input_post('type',1);
  15. // 获取基本信息
  16. $where = [];
  17. $where['type'] = $type;
  18. $where['status'] = 1;
  19. $list = Db::name('decorate_lianmeng')->where($where)->autopage()->order('sort desc')->select();
  20. $list = list_domain_image($list,['base_image']);
  21. $this->success("success",$list);
  22. }
  23. /**
  24. * 购买并加入我的背包
  25. */
  26. public function buy_one() {
  27. $number = input_post('number',1,'intval'); //数量
  28. if(!$number){
  29. $this->error();
  30. }
  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_lianmeng')->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. for($i=1;$i<=$number;$i++){
  49. $data[] = [
  50. 'user_id' => $this->auth->id,
  51. 'decorate_id' => $decorate['id'],
  52. 'is_using' => 0,
  53. 'createtime' => time(),
  54. 'updatetime' => time(),
  55. ];
  56. }
  57. $log_id = Db::name('user_decorate_lianmeng')->insertAll($data);
  58. if(!$log_id){
  59. Db::rollback();
  60. $this->error('购买失败');
  61. }
  62. //扣钱
  63. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'购买联盟道具','user_decorate_lianmeng',0);
  64. if($rs['status'] === false){
  65. Db::rollback();
  66. $this->error($rs['msg']);
  67. }
  68. Db::commit();
  69. $this->success("购买成功!");
  70. }
  71. //获取用户装扮
  72. public function my_decorate_list()
  73. {
  74. $uid = $this->auth->id;
  75. $type = input_post('type',1);
  76. $map = [
  77. 'a.user_id' => $uid,
  78. 'a.is_using' => 0,
  79. 'b.type' => $type,
  80. ];
  81. $list = Db::name('user_decorate_lianmeng')
  82. ->alias('a')
  83. ->field('a.id,a.decorate_id,b.name,b.type,b.base_image,count(a.decorate_id) as number')
  84. ->join('decorate_lianmeng b', 'a.decorate_id = b.id','LEFT')
  85. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  86. $list = list_domain_image($list,['base_image']);
  87. $this->success('success',$list);
  88. }
  89. //使用道具
  90. public function use_decorate()
  91. {
  92. $plat_name = input_post('plat_name',''); //平台用户名
  93. if (!$plat_name) {
  94. $this->error();
  95. }
  96. $id = input_post('id',0); //用户装扮ID
  97. if (!$id) {
  98. $this->error();
  99. }
  100. $map = [
  101. 'user_id' => $this->auth->id,
  102. 'id' => $id,
  103. ];
  104. $info = Db::name('user_decorate_lianmeng')->where($map)->find();
  105. if (empty($info)) {
  106. $this->error('道具不存在');
  107. }
  108. if ($info['is_using'] != 0) {
  109. $this->error('道具已使用');
  110. }
  111. Db::startTrans();
  112. //设置使用中状态
  113. $map = [
  114. 'id' => $id,
  115. ];
  116. $data = [];
  117. $data['is_using'] = 1;
  118. $data['plat_name'] = $plat_name;
  119. $data['updatetime'] = time();
  120. $reslut = Db::name('user_decorate_lianmeng')->where($map)->update($data);
  121. if (!$reslut) {
  122. Db::rollback();
  123. $this->error('使用失败');
  124. }
  125. // 提交事务
  126. Db::commit();
  127. $this->success('使用完成');
  128. }
  129. }