Family.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 家族卡
  7. */
  8. class Userdecoratefamily extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. ///////////////////////////家族卡///////////////////////
  13. //关系卡列表
  14. public function decorate_list(){
  15. $list = Db::name('decorate_family')->select();
  16. $list = list_domain_image($list,['base_image']);
  17. $this->success(1,$list);
  18. }
  19. /**
  20. * 购买并加入我的背包
  21. */
  22. public function buy_one() {
  23. $number = input('number',1,'intval'); //数量
  24. if(!$number){
  25. $this->error();
  26. }
  27. $did = input('did',''); //装扮ID
  28. if (!$did) {
  29. $this->error();
  30. }
  31. // 判断用户金币余额是否充足
  32. $walletinfo = model('wallet')->getWallet($this->auth->id);
  33. // 获取购买装扮需要的价格
  34. $decorate = Db::name('decorate_family')->where(['id'=>$did])->find();
  35. if(!$decorate) {
  36. $this->error("道具信息获取失败!");
  37. }
  38. $total_price = bcmul($decorate['price'],$number,0);
  39. if($walletinfo['jewel'] < $total_price) {
  40. $this->error("您的金币不足,请先充值!");
  41. }
  42. // 进行购买逻辑
  43. Db::startTrans();
  44. // 添加到背包
  45. for($i=1;$i<=$number;$i++){
  46. $data[] = [
  47. 'user_id' => $this->auth->id,
  48. 'decorate_id' => $decorate['id'],
  49. 'is_using' => 0,
  50. 'createtime' => time(),
  51. 'updatetime' => time(),
  52. ];
  53. }
  54. $log_id = Db::name('user_decorate_family')->insertAll($data);
  55. if(!$log_id){
  56. Db::rollback();
  57. $this->error('购买失败');
  58. }
  59. //扣钱
  60. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$total_price,'-',0,'购买家族卡',32,'jewel');
  61. if($rs['status'] === false){
  62. Db::rollback();
  63. $this->error($rs['msg']);
  64. }
  65. Db::commit();
  66. $this->success("购买成功!");
  67. }
  68. //获取用户装扮
  69. public function my_decorate_list()
  70. {
  71. $uid = $this->auth->id;
  72. $map = [
  73. 'a.user_id' => $uid,
  74. 'a.is_using'=> 0,
  75. ];
  76. $list = Db::name('user_decorate_family')
  77. ->alias('a')
  78. ->field('a.id,a.decorate_id,b.name,b.base_image,count(a.decorate_id) as number')
  79. ->join('decorate_family b', 'a.decorate_id = b.id','LEFT')
  80. ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();
  81. $list = list_domain_image($list,['base_image']);
  82. $this->success('success',$list);
  83. }
  84. }