Bag.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 新人礼包接口
  7. */
  8. class Bag extends Api
  9. {
  10. protected $noNeedLogin = [''];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 获取礼包列表
  14. */
  15. public function getBaglist() {
  16. $user_id = $this->auth->id;
  17. // 获取剩余天数
  18. $res = [];
  19. $have = \app\common\model\NewBagHave::where(["user_id"=>$user_id])->find();
  20. if(!$have) {
  21. $this->success("新人礼包活动已结束");
  22. }
  23. $redtime = time() - $have->createtime;
  24. if($redtime > 7*86400) {
  25. $this->success("新人礼包活动已结束");
  26. }
  27. $res["redtime"] = 7*86400 - $redtime;
  28. // 获取礼包
  29. $baglist = \app\common\model\NewBag::alias("a")->field("a.id,a.title,a.value,a.desc")->order("a.weight","desc")->select();
  30. if($baglist) foreach($baglist as $k => $v) {
  31. // 获取礼包礼物
  32. $baglist[$k]["gifts"] = \app\common\model\NewGift::where(["bag_id"=>$v["id"]])->select();
  33. // 获取礼包完成情况
  34. $bagstatus = \app\common\model\NewBagFinish::where(["user_id"=>$user_id,"bag_id"=>$v["id"]])->value("status");
  35. $baglist[$k]["status"] = $bagstatus?$bagstatus:0;// 状态:0=未完成,1=未领取,2=已领取
  36. }
  37. $res["bagInfo"] = $baglist;
  38. $this->success("获取成功!",$res);
  39. }
  40. /**
  41. * 领取礼包
  42. */
  43. public function getBagGift() {
  44. $bag_id = $this->request->request("bag_id"); // 礼包ID
  45. if($bag_id <= 0) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. $user_id = $this->auth->id;
  49. // 判断是否可以领取
  50. $have = \app\common\model\NewBagHave::where(["user_id"=>$user_id])->find();
  51. if(!$have) {
  52. $this->error("新人礼包活动已结束无法领取",null,106);
  53. }
  54. $redtime = time() - $have->createtime;
  55. if($redtime > 7*86400) {
  56. $this->error("新人礼包活动已结束无法领取",null,106);
  57. }
  58. $finish = \app\common\model\NewBagFinish::where(["user_id"=>$user_id,"bag_id"=>$bag_id])->find();
  59. if(!isset($finish->status)) $this->error("未找到相应礼包任务!");
  60. if($finish->status != 1) $this->error("未达到领取条件!");
  61. // 可以领取
  62. Db::startTrans();
  63. try{
  64. $res1 = true;
  65. // 获取领取礼包内容
  66. $baggift = \app\common\model\NewGift::where(["bag_id"=>$bag_id])->select();
  67. if($baggift) foreach($baggift as $k => $v) {
  68. $data = [];
  69. if($v["gift_type"] == 5) { // 礼物
  70. $data["user_id"] = $user_id;
  71. $data["name"] = $v["gift_name"];
  72. $data["image"] = $v["gift_image"];
  73. $data["gif_image"] = $v["gift_gif"];
  74. $data["value"] = $v["price"];
  75. $data["number"] = $v["number"];
  76. $data["createtime"] = time();
  77. $res1 = \app\common\model\GiftBack::insert($data);
  78. } elseif(in_array($v["gift_type"],[1,2,3,4])) { // 装饰
  79. for($i=0;$i<$v["number"];$i++) {
  80. $data[$i]["user_id"] = $user_id;
  81. $data[$i]["type"] = $v["gift_type"];
  82. $data[$i]["attire_name"] = $v["gift_name"];
  83. $data[$i]["price"] = $v["price"];
  84. $data[$i]["file_image"] = $v["gift_image"];
  85. $data[$i]["gif_image"] = $v["gift_gif"];
  86. $data[$i]["limit_day"] = $v["limit_day"];
  87. $data[$i]["createtime"] = time();
  88. }
  89. $res1 = \app\common\model\AttireBack::insertAll($data);
  90. }
  91. }
  92. // 更新礼包状态
  93. $finish->status = 2;
  94. $finish->updatetime = time();
  95. $res2 = $finish->save();
  96. if($res1 && $res2) {
  97. Db::commit();
  98. $this->success("领取成功!");
  99. }
  100. }catch (ValidateException $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. }
  105. }