model = $model; } /** * 跑马灯效果 * @param Request $request * @return \think\Response * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function boxDownMessageList(Request $request) { $messages = []; $boxs = [ 10 => '普通', 20 => '稀有', 30 => '史诗', 40 => '传说', ]; $list = Db::name('box_mid_handle') ->alias('m') ->where('m.box_type', '>', 10) ->leftJoin('user u', 'u.id = m.uid') ->limit(20) ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages) { $mobile = '****' . substr($data['mobile'], 7); $messages[] = '恭喜' . $mobile . '拆出' . $boxs[$data['box_type']] . '福袋'; }) ->orderRaw("rand() , m.id DESC") ->field('m.uid,u.mobile,u.nickname,m.box_type') ->select()->toArray(); return api_succ_return(['msg' => '成功', 'data' => $messages]); } /** * 福袋 泡泡商品 * @return \think\Response * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException\ */ public function boxRandGoodsList() { return api_succ_return(['msg' => '成功', 'data' => ShopGoodsService::make()->getRandomGoods()]); } /** * 当前最新一期福袋预约信息 * @return \think\Response * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getNewBoxInfo() { try{ return api_succ_return(['msg' => '成功', 'data' => BoxService::make()->getNewInfo()]); } catch (\Exception $exception){ return api_error_return($exception->getMessage()); } } /** * 预约福袋 * @param Request $request * @return \think\Response */ public function beforeBuyBox(Request $request) { $post = $request->post(); $user_info = $request->user_info; $userId = isset($user_info['id']) ? $user_info['id'] : 0; $cacheKey = "caches:box:applyLock:u_{$userId}"; if (RedisCache::get($cacheKey)) { return api_error_return('请不要频繁提交,3秒后再尝试'); } // 加锁 RedisCache::set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3)); Db::startTrans(); try { BoxRecordService::make()->applyBox($request->uid, $post); Db::commit(); } catch (\Exception $e) { Db::rollback(); $error = $e->getMessage(); RedisCache::clear($cacheKey); return api_error_return($error == 'no_open'? ['msg'=>'预约失败,还未开始预约、或预约已结束','code'=>302]:$error); } RedisCache::clear($cacheKey); return api_succ_return('预约成功'); } /** * 我的预约 * @param Request $request * @param BoxRecordModel $model * @return \think\Response */ public function boxBuyRecord(Request $request, BoxRecordModel $model) { try { return api_succ_return(['msg' => '成功', 'data' => $model->getLog($request)]); } catch (\Exception $e) { return api_error_return('失败'); } } /** * 福袋商品列表 * @param Request $request * @return \think\Response * @throws \think\db\exception\DbException */ public function boxOutGoodsList(Request $request) { $boxType = $request->post('box_type',0); $pageSize = $request->post('limit',10); $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize); return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]); } /** * 福袋商品列表顶部banner * @param Request $request * @return \think\Response * @throws \think\db\exception\DbException */ public function boxOutBanner(Request $request) { $boxType = [30,40]; $pageSize = $request->post('limit',10); $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize); return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]); } /** * 拆袋 * @param Request $request * @return \think\Response */ public function openBoxOnline(Request $request) { if(RedisCache::get("caches:box:open:user_".$request->uid)){ return api_error_return('福袋已拆开'); }else{ return api_error_return('福袋未开,请耐心等候'); } } /** * 福袋中奖记录 * @param Request $request * @return \think\Response * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function boxHandleList(Request $request) { try{ $dateType = $request->post('date_type', 1); $pageSize = $request->post('limit',10); $post = $request->post(); if($dateType==1){ $post['time'] = date('Y-m-d'); return api_succ_return(['msg' => '获取今日数据成功', 'data' => BoxHandleService::make()->getBoxListByUser($request->uid, $post,$pageSize)]); }else{ $post['time1'] = date('Y-m-d'); $list = BoxHandleService::make()->getBoxListByUser($request->uid,$post,$pageSize); // $list = BoxHandleService::make()->getHistoryBoxList($request->uid,$post,$pageSize); return api_succ_return(['msg' => '获取历史数据成功1', 'data' => $list]); } } catch (\Exception $exception){ return api_error_return('获取失败:'.$exception->getMessage()); } } /** * 福仓数据统计 * @param Request $request * @return \think\Response * @throws \think\db\exception\DbException */ public function boxHandleUnread(Request $request) { $data = []; $dateType = $request->post('date_type', 1); $counts = BoxHandleService::make()->getBoxCount($request->uid, 1, $dateType); $data['un_handle'] = $counts; $counts = BoxHandleService::make()->getBoxCount($request->uid, 2, $dateType); $data['handled'] = $counts; return api_succ_return(['msg' => '成功', 'data' => $data]); } /** * 福袋一键发货处理 * @param Request $request * @return \think\Response */ public function boxGoodsSurePost(Request $request) { $cacheKey = "caches:box:delivery:user_{$request->uid}"; if(RedisCache::get($cacheKey)){ return api_error_return('请不要频繁提交,稍后再试~'); } RedisCache::setnx($cacheKey, 1, rand(3,5)); Db::startTrans(); try { BoxHandleService::make()->boxDelivery($request->uid, $request->post()); Db::commit(); } catch (\Exception $e) { Db::rollback(); RedisCache::clear($cacheKey); RedisCache::clear("caches:box:deliveryCatch:user_{$request->uid}:lock"); return api_error_return($e->getMessage()); } return api_succ_return('发货成功'); } /** * 福袋一键回收处理 * @param Request $request * @return \think\Response */ public function boxGoodsReBuy(Request $request) { $action = $request->post('action'); $cacheKey = "caches:box:rebuy:user_{$request->uid}_{$action}"; if (RedisCache::get($cacheKey) && $action != 'apply') { return api_error_return('请不要频繁操作,稍后再尝试'); } RedisCache::setnx($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(3, 5)); Db::startTrans(); try { $res = BoxHandleService::make()->boxGoodsReBuy($request->uid, $request->post()); Db::commit(); return api_succ_return(['msg' => '处理成功', 'data' => $res]); } catch (\Exception $e) { Db::rollback(); RedisCache::clear($cacheKey); return api_error_return($e->getMessage()?$e->getMessage():'回收失败'); } } }