|
|
@@ -0,0 +1,283 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+use Illuminate\Http\Request;
|
|
|
+
|
|
|
+class BoxController extends Controller
|
|
|
+{
|
|
|
+ // 获取用户信息
|
|
|
+ public function open(Request $request) {
|
|
|
+ //
|
|
|
+ $user = unserialize($request->get("account"));
|
|
|
+ // 校验参数
|
|
|
+ $num = $request->input("num", 1);
|
|
|
+ if($num !=1 && $num !=5) {
|
|
|
+ return [
|
|
|
+ 'errno'=> 10001,
|
|
|
+ "errmsg" => '参数错误',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $perCoupon = 1;
|
|
|
+ if($perCoupon * $num > $user->coupon) {
|
|
|
+ return [
|
|
|
+ 'errno'=> 10002,
|
|
|
+ "errmsg" => '请求失败,礼劵不够了',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // 获取基本数据
|
|
|
+ $levelPercent = $this->getLevelPercent();
|
|
|
+ $levelPaster = $this->getLevelPaster();
|
|
|
+ $hitCnt = $this->getBoxHit()['hit_cnt'];
|
|
|
+ // 循环执行
|
|
|
+ $result = [];
|
|
|
+ $ids = [];
|
|
|
+ $mapBoxId = [];
|
|
|
+ // dd($hitCnt);
|
|
|
+ $user_last_hit_cnt = $user->last_hit_cnt;
|
|
|
+ for($i=0;$i<$num;$i++) {
|
|
|
+ $tmp = $this->getOpenResult($user_last_hit_cnt, $levelPercent, $hitCnt);
|
|
|
+ if($tmp['level'] == 'SSR') {
|
|
|
+ $user_last_hit_cnt = 0;
|
|
|
+ } else {
|
|
|
+ $user_last_hit_cnt++;
|
|
|
+ }
|
|
|
+ $result[] = $tmp;
|
|
|
+ $ids[] = $tmp['attire_id'];
|
|
|
+ $mapBoxId[$tmp['attire_id']] = $tmp['id']; // 获取box_id 和 attire_id的映射关系
|
|
|
+ }
|
|
|
+ // 获取装扮的数据 名称/正面图片
|
|
|
+ $attires = \App\Models\Attire::whereIn("id", $ids)
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+ $mapAttire = array_column($attires, null,"id");
|
|
|
+ //
|
|
|
+ $existAttire = \App\Models\FittingRoom::where("account_id", $user->id)
|
|
|
+ ->whereIn("attire_id", $ids)
|
|
|
+ ->pluck("attire_id")
|
|
|
+ ->toArray();
|
|
|
+ unset($ids);
|
|
|
+ //
|
|
|
+ $gainIds = [];
|
|
|
+ $gainAttireIds = [];
|
|
|
+ $retrieveIds = [];
|
|
|
+ $retrievePaster = 0;
|
|
|
+ foreach($result as &$v) {
|
|
|
+ $tmpAttireId = $v["attire_id"];
|
|
|
+ $v["name"] = $mapAttire[$tmpAttireId]["name"];
|
|
|
+ $v["img_1"] = Storage::disk('cosv5')->url($mapAttire[$tmpAttireId]["img_1"]);
|
|
|
+ //
|
|
|
+ if($existAttire && in_array($tmpAttireId, $existAttire)) {
|
|
|
+ $retrieveIds[] = $mapBoxId[$tmpAttireId];
|
|
|
+ $retrievePaster += (int)$levelPaster[$v["level"]];
|
|
|
+ $v["is_retrieve"] = 1;
|
|
|
+ $v["retrieve_paster"] = (int)$levelPaster[$v["level"]];
|
|
|
+ } else {
|
|
|
+ $gainIds[] = $mapBoxId[$tmpAttireId];
|
|
|
+ $gainAttireIds[] = $tmpAttireId;
|
|
|
+ $v["is_retrieve"] = 0;
|
|
|
+ $v["retrieve_paster"] = 0;
|
|
|
+ $existAttire[] = $tmpAttireId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unset($v); // 解决php的引用bug
|
|
|
+ // 使用事务进行处理
|
|
|
+ DB::transaction(function () use($gainIds, $retrieveIds, $user, $perCoupon, $num, $retrievePaster, $user_last_hit_cnt,$gainAttireIds) {
|
|
|
+ // 更新未抽中大赏的数量
|
|
|
+ DB::table('accounts')
|
|
|
+ ->where('id', $user->id)
|
|
|
+ ->update(['last_hit_cnt' => $user_last_hit_cnt]);
|
|
|
+ // 删除花费的礼劵
|
|
|
+ DB::table('accounts')
|
|
|
+ ->where("id", $user->id)
|
|
|
+ ->decrement("coupon", $perCoupon * $num);
|
|
|
+ // 回收的贴纸数
|
|
|
+ if($retrievePaster) {
|
|
|
+ DB::table('accounts')
|
|
|
+ ->where("id", $user->id)
|
|
|
+ ->increment("paster", $retrievePaster);
|
|
|
+ }
|
|
|
+ // 未回收的加入试衣间
|
|
|
+ $fittimgRooms = [];
|
|
|
+ foreach($gainAttireIds as &$v) {
|
|
|
+ $fittimgRooms[$v] = [
|
|
|
+ "account_id" => $user->id,
|
|
|
+ "attire_id" => $v,
|
|
|
+ 'created_at' =>date("Y-m-d H:i:s"),
|
|
|
+ 'updated_at' =>date("Y-m-d H:i:s"),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ if($fittimgRooms) {
|
|
|
+ \App\Models\FittingRoom::insertOrIgnore(array_values($fittimgRooms));
|
|
|
+ }
|
|
|
+ // 礼盒记录
|
|
|
+ $objBoxRecord = new \App\Models\BoxRecord();
|
|
|
+ $objBoxRecord->account_id = $user->id;
|
|
|
+ $objBoxRecord->num = $num;
|
|
|
+ $objBoxRecord->box_ids = implode(",", $gainIds);
|
|
|
+ $objBoxRecord->retrieve_pasters = $retrievePaster;
|
|
|
+ $objBoxRecord->retrieve_box_ids = implode(",", $retrieveIds);
|
|
|
+ $objBoxRecord->cost_coupons = $perCoupon * $num;
|
|
|
+ $objBoxRecord->save();
|
|
|
+ // 礼劵明细记录
|
|
|
+ $objCouponRecord = new \App\Models\CouponRecord();
|
|
|
+ $objCouponRecord->account_id = $user->id;
|
|
|
+ $objCouponRecord->add_cnt = -1 * $perCoupon * $num;
|
|
|
+ $objCouponRecord->type = 1;
|
|
|
+ $objCouponRecord->box_record_id = $objBoxRecord->id;
|
|
|
+ $objCouponRecord->day = date("Y-m-d");
|
|
|
+ $objCouponRecord->save();
|
|
|
+ // 贴纸回收记录
|
|
|
+ if($retrievePaster) {
|
|
|
+ $objPasterRecord = new \App\Models\PasterRecord();
|
|
|
+ $objPasterRecord->account_id = $user->id;
|
|
|
+ $objPasterRecord->add_cnt = $retrievePaster;
|
|
|
+ $objPasterRecord->type = 1;
|
|
|
+ $objPasterRecord->box_record_id = $objBoxRecord->id;
|
|
|
+ $objPasterRecord->day = date("Y-m-d");
|
|
|
+ $objPasterRecord->save();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //
|
|
|
+ return [
|
|
|
+ 'errno'=> 10000,
|
|
|
+ "errmsg" => 'ok',
|
|
|
+ 'data' => $result,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // 一次得开礼盒计算
|
|
|
+ private function getOpenResult($user_last_hit_cnt, $levelPercent, $hitCnt) {
|
|
|
+ // 获取所有的ssr随机取一个
|
|
|
+ if($user_last_hit_cnt >= $hitCnt-1) {
|
|
|
+ $level = 'SSR';
|
|
|
+ } else {
|
|
|
+ //
|
|
|
+ $levels = [];
|
|
|
+ foreach($levelPercent as $k=>$v) {
|
|
|
+ for($i=0;$i<$v;$i++) {
|
|
|
+ $levels[] = $k;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ shuffle($levels);
|
|
|
+ $idx = array_rand($levels);
|
|
|
+ $level = $levels[$idx];
|
|
|
+ }
|
|
|
+ // 随机取一个
|
|
|
+ $gifts = \App\Models\GiftBox::whereHas("attire", function($q) use ($level) {
|
|
|
+ $q->where("level", $level);
|
|
|
+ })->get(["id","attire_id"])->toArray();
|
|
|
+ //
|
|
|
+ foreach($gifts as &$v) {
|
|
|
+ $v['level'] = $level;
|
|
|
+ }
|
|
|
+ shuffle($gifts);
|
|
|
+ $idx = array_rand($gifts);
|
|
|
+ $hitGifts = $gifts[$idx];
|
|
|
+ //
|
|
|
+ return $hitGifts;
|
|
|
+ }
|
|
|
+ //
|
|
|
+ public function getLevelPercent() {
|
|
|
+ $obj = \App\Models\Config::where("key", "box_level_percent")->first();
|
|
|
+ if ($obj && $obj->val) {
|
|
|
+ $data = json_decode($obj->val, true);
|
|
|
+ if ($data) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //
|
|
|
+ return config("avatar.box_level_percent");
|
|
|
+ }
|
|
|
+ //
|
|
|
+ public function getLevelPaster() {
|
|
|
+ //
|
|
|
+ $obj = \App\Models\Config::where("key", "box_level_paster")->first();
|
|
|
+ if ($obj && $obj->val) {
|
|
|
+ $data = json_decode($obj->val, true);
|
|
|
+ if ($data) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //
|
|
|
+ return config("avatar.box_level_paster");
|
|
|
+ }
|
|
|
+ //
|
|
|
+ public function getBoxHit() {
|
|
|
+ //
|
|
|
+ $obj = \App\Models\Config::where("key", "box_hit_cnt")->first();
|
|
|
+ if ($obj && $obj->val) {
|
|
|
+ return ['hit_cnt'=>(int)$obj->val];
|
|
|
+ }
|
|
|
+ //
|
|
|
+ return config("avatar.box_hit_cnt");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取礼盒装扮列表
|
|
|
+ public function gifts(Request $request) {
|
|
|
+ // 随机取一个
|
|
|
+ $gifts = \App\Models\GiftBox::with(["attire"])->orderBy("sort","asc")->orderBy("id","asc")->get(["attire_id"])->toArray();
|
|
|
+ $result = [];
|
|
|
+ $ret = [];
|
|
|
+ foreach($gifts as $v) {
|
|
|
+ $result[$v["attire"]["level"]][] = [
|
|
|
+ "name" => $v["attire"]["name"],
|
|
|
+ "img_1" => Storage::disk('cosv5')->url($v["attire"]["img_1"]),
|
|
|
+ "level" => $v["attire"]["level"],
|
|
|
+ ];
|
|
|
+ $ret[] = [
|
|
|
+ "name" => $v["attire"]["name"],
|
|
|
+ "img_1" => Storage::disk('cosv5')->url($v["attire"]["img_1"]),
|
|
|
+ "level" => $v["attire"]["level"],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ //
|
|
|
+ // $ret = [];
|
|
|
+ // $ret = array_merge($ret, $result['SSR']);
|
|
|
+ // $ret = array_merge($ret, $result['SR']);
|
|
|
+ // $ret = array_merge($ret, $result['R']);
|
|
|
+ // $ret = array_merge($ret, $result['N']);
|
|
|
+ // 获取级别的概率
|
|
|
+ $levelPercent = \App\Models\Config::where("key", 'box_level_percent')->value("val");
|
|
|
+ $levelPercent = json_decode($levelPercent, true);
|
|
|
+ //
|
|
|
+ $t = [];
|
|
|
+ $t['SSR'] = sprintf("%g%%", $levelPercent['SSR']);
|
|
|
+ $t['SR'] = sprintf("%g%%", $levelPercent['SR']);
|
|
|
+ $t['R'] = sprintf("%g%%", $levelPercent['R']);
|
|
|
+ $t['N'] = sprintf("%g%%", $levelPercent['N']);
|
|
|
+ $levelPercent = $t;
|
|
|
+ //
|
|
|
+ $hitCnt = \App\Models\Config::where("key", 'box_hit_cnt')->value("val");
|
|
|
+ $levels = [
|
|
|
+ [
|
|
|
+ 'level_name' => 'SSR',
|
|
|
+ 'percent' => $levelPercent['SSR'],
|
|
|
+ 'relations' => $result['SSR']
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'level_name' => 'SR',
|
|
|
+ 'percent' => $levelPercent['SR'],
|
|
|
+ 'relations' => $result['SR']
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'level_name' => 'R',
|
|
|
+ 'percent' => $levelPercent['R'],
|
|
|
+ 'relations' => $result['R']
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'level_name' => 'N',
|
|
|
+ 'percent' => $levelPercent['N'],
|
|
|
+ 'relations' => $result['N']
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ //
|
|
|
+ return [
|
|
|
+ 'errno'=> 10000,
|
|
|
+ "errmsg" => 'ok',
|
|
|
+ 'data' => $ret,
|
|
|
+ 'percent_data' => compact('levelPercent','hitCnt','levels'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|