Lianmeng.php 5.6 KB

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