Lianmeng.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $total_price = bcmul($decorate['price'],$number,0);
  43. if($walletinfo['gold'] < $total_price) {
  44. $this->error("您的金币不足,请先充值!");
  45. }
  46. // 进行购买逻辑
  47. Db::startTrans();
  48. // 添加到背包
  49. for($i=1;$i<=$number;$i++){
  50. $data[] = [
  51. 'user_id' => $this->auth->id,
  52. 'decorate_id' => $decorate['id'],
  53. 'is_using' => 0,
  54. 'createtime' => time(),
  55. 'updatetime' => time(),
  56. ];
  57. }
  58. $log_id = Db::name('user_decorate_lianmeng')->insertAll($data);
  59. if(!$log_id){
  60. Db::rollback();
  61. $this->error('购买失败');
  62. }
  63. //扣钱
  64. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$total_price,33,'购买联盟道具','user_decorate_lianmeng',0);
  65. if($rs['status'] === false){
  66. Db::rollback();
  67. $this->error($rs['msg']);
  68. }
  69. Db::commit();
  70. $this->success("购买成功!");
  71. }
  72. //获取用户装扮
  73. public function my_decorate_list()
  74. {
  75. $uid = $this->auth->id;
  76. $type = input_post('type',1);
  77. $map = [
  78. 'a.user_id' => $uid,
  79. 'a.is_using' => 0,
  80. 'b.type' => $type,
  81. ];
  82. $list = Db::name('user_decorate_lianmeng')
  83. ->alias('a')
  84. ->field('a.id,a.decorate_id,b.name,b.type,b.base_image,count(a.decorate_id) as number')
  85. ->join('decorate_lianmeng b', 'a.decorate_id = b.id','LEFT')
  86. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  87. $list = list_domain_image($list,['base_image']);
  88. $this->success('success',$list);
  89. }
  90. //使用道具
  91. public function use_decorate()
  92. {
  93. $plat_name = input_post('plat_name',''); //平台用户名
  94. if (!$plat_name) {
  95. $this->error();
  96. }
  97. $id = input_post('id',0); //用户装扮ID
  98. if (!$id) {
  99. $this->error();
  100. }
  101. $map = [
  102. 'user_id' => $this->auth->id,
  103. 'id' => $id,
  104. ];
  105. $info = Db::name('user_decorate_lianmeng')->where($map)->find();
  106. if (empty($info)) {
  107. $this->error('道具不存在');
  108. }
  109. if ($info['is_using'] != 0) {
  110. $this->error('道具已使用');
  111. }
  112. Db::startTrans();
  113. //设置使用中状态
  114. $map = [
  115. 'id' => $id,
  116. ];
  117. $data = [];
  118. $data['is_using'] = 1;
  119. $data['plat_name'] = $plat_name;
  120. $data['updatetime'] = time();
  121. $reslut = Db::name('user_decorate_lianmeng')->where($map)->update($data);
  122. if (!$reslut) {
  123. Db::rollback();
  124. $this->error('使用失败');
  125. }
  126. //通知管理员
  127. $admin_uids = config('site.lianmeng_msg_uids');
  128. $admin_uids = explode(',',$admin_uids);
  129. if(is_array($admin_uids) && !empty($admin_uids)){
  130. foreach($admin_uids as $key => $admin_uid){
  131. $msg_id = \app\common\model\Message::addMessage($admin_uid,'联盟道具核销',$this->auth->username.'刚刚使用了联盟道具,请进入后台核销');
  132. }
  133. }
  134. // 提交事务
  135. Db::commit();
  136. $this->success('使用完成');
  137. }
  138. //用户道具使用记录
  139. public function my_decorate_uselog()
  140. {
  141. $uid = $this->auth->id;
  142. $map = [
  143. 'a.user_id' => $uid,
  144. 'a.is_using' => 1,
  145. ];
  146. $list = Db::name('user_decorate_lianmeng')
  147. ->alias('a')
  148. ->field('a.*,b.name,b.price,b.base_image,b.type')
  149. ->join('decorate_lianmeng b', 'a.decorate_id = b.id','LEFT')
  150. ->where($map)
  151. ->order('a.id desc')
  152. ->autopage()->select();
  153. $list = list_domain_image($list,['base_image']);
  154. $type_arr = [
  155. 1 => '吉傲T币',
  156. 2 => '吉傲会员',
  157. 3 => '联盟商家币',
  158. 4 => '联盟商家会员'
  159. ];
  160. if(!empty($list)){
  161. foreach($list as $key => &$val){
  162. $val['type_text'] = $type_arr[$val['type']];
  163. }
  164. }
  165. $this->success('success',$list);
  166. }
  167. }