Relation.php 2.7 KB

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