123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 新人礼包接口
- */
- class Bag extends Api
- {
- protected $noNeedLogin = [''];
- protected $noNeedRight = ['*'];
- /**
- * 获取礼包列表
- */
- public function getBaglist() {
- $user_id = $this->auth->id;
- // 获取剩余天数
- $res = [];
- $have = \app\common\model\NewBagHave::where(["user_id"=>$user_id])->find();
- if(!$have) {
- $this->success("新人礼包活动已结束");
- }
- $redtime = time() - $have->createtime;
- if($redtime > 7*86400) {
- $this->success("新人礼包活动已结束");
- }
- $res["redtime"] = 7*86400 - $redtime;
- // 获取礼包
- $baglist = \app\common\model\NewBag::alias("a")->field("a.id,a.title,a.value,a.desc")->order("a.weight","desc")->select();
- if($baglist) foreach($baglist as $k => $v) {
- // 获取礼包礼物
- $baglist[$k]["gifts"] = \app\common\model\NewGift::where(["bag_id"=>$v["id"]])->select();
- // 获取礼包完成情况
- $bagstatus = \app\common\model\NewBagFinish::where(["user_id"=>$user_id,"bag_id"=>$v["id"]])->value("status");
- $baglist[$k]["status"] = $bagstatus?$bagstatus:0;// 状态:0=未完成,1=未领取,2=已领取
- }
- $res["bagInfo"] = $baglist;
- $this->success("获取成功!",$res);
- }
- /**
- * 领取礼包
- */
- public function getBagGift() {
- $bag_id = $this->request->request("bag_id"); // 礼包ID
- if($bag_id <= 0) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- // 判断是否可以领取
- $have = \app\common\model\NewBagHave::where(["user_id"=>$user_id])->find();
- if(!$have) {
- $this->error("新人礼包活动已结束无法领取",null,106);
- }
- $redtime = time() - $have->createtime;
- if($redtime > 7*86400) {
- $this->error("新人礼包活动已结束无法领取",null,106);
- }
- $finish = \app\common\model\NewBagFinish::where(["user_id"=>$user_id,"bag_id"=>$bag_id])->find();
- if(!isset($finish->status)) $this->error("未找到相应礼包任务!");
- if($finish->status != 1) $this->error("未达到领取条件!");
- // 可以领取
- Db::startTrans();
- try{
- $res1 = true;
- // 获取领取礼包内容
- $baggift = \app\common\model\NewGift::where(["bag_id"=>$bag_id])->select();
- if($baggift) foreach($baggift as $k => $v) {
- $data = [];
- if($v["gift_type"] == 5) { // 礼物
- $data["user_id"] = $user_id;
- $data["name"] = $v["gift_name"];
- $data["image"] = $v["gift_image"];
- $data["gif_image"] = $v["gift_gif"];
- $data["value"] = $v["price"];
- $data["number"] = $v["number"];
- $data["createtime"] = time();
- $res1 = \app\common\model\GiftBack::insert($data);
- } elseif(in_array($v["gift_type"],[1,2,3,4])) { // 装饰
- for($i=0;$i<$v["number"];$i++) {
- $data[$i]["user_id"] = $user_id;
- $data[$i]["type"] = $v["gift_type"];
- $data[$i]["attire_name"] = $v["gift_name"];
- $data[$i]["price"] = $v["price"];
- $data[$i]["file_image"] = $v["gift_image"];
- $data[$i]["gif_image"] = $v["gift_gif"];
- $data[$i]["limit_day"] = $v["limit_day"];
- $data[$i]["createtime"] = time();
- }
- $res1 = \app\common\model\AttireBack::insertAll($data);
- }
- }
- // 更新礼包状态
- $finish->status = 2;
- $finish->updatetime = time();
- $res2 = $finish->save();
- if($res1 && $res2) {
- Db::commit();
- $this->success("领取成功!");
- }
- }catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- }
|