model = $model; } // 跑马灯效果 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 = v; $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 api_succ_return(['msg'=>'请求成功', 'data'=>[ // '恭喜****9567拆出稀有附带', // '恭喜****9567拆出稀有附带', // '恭喜****9567拆出稀有附带', // '恭喜****9567拆出稀有附带', // '恭喜****9567拆出稀有附带', // '恭喜****9567拆出稀有附带' // ]]); } // 福袋 泡泡商品 public function boxRandGoodsList() { $list = Db::name('shop_goods')->where('on_sale', 1)->limit(12)->orderRaw("rand() , goods_id DESC")->field('goods_id,box_pic,goods_sn')->select()->toArray(); return api_succ_return(['msg' => '成功', 'data' => $list]); } /** * 当前最新一期福袋预约信息 * @return \think\Response * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getNewBoxInfo() { $id = Db::name('box')->where(['status' => 1, 'open_status' => 1])->max('id'); if (empty($id)) { return api_error_return(['msg' => '暂未开启', 'code' => 303]); } $info = Db::name('box')->where('id', $id)->find(); $cur_time = time(); $time = sr_getcurtime($cur_time, 'Y-m-d '); $times = explode('|', $info['time_set']); $appoint_status = 1; // 1 还未开始 2 进行中 3已结束 $dis_time = 0; // 当前时间大于 最后一场结束时间 if ($appoint_status != 3) { $end_timesarr = explode('-', $times[count($times) - 1]); $end_randtime = strtotime($time . $end_timesarr[1]); if ($cur_time > $end_randtime) { $appoint_status = 3; } } // 是否拿到最近的一场未开始距离时间 if ($appoint_status != 3) { foreach ($times as $key => $val) { $time_cur = explode('-', $val); $time_randbegin = strtotime($time . $time_cur[0]); $time_randend = strtotime($time . $time_cur[1]); if ($cur_time > $time_randbegin && $cur_time < $time_randend) { // 正在这一场进行中 $appoint_status = 2; } } if ($appoint_status != 2) { $has_get = false; foreach ($times as $key => $val) { $time_cur = explode('-', $val); $time_randbegin = strtotime($time . $time_cur[0]); if ($cur_time < $time_randbegin && !$has_get) { // 正在这一场进行中 $has_get = true; $dis_time = $time_randbegin - $cur_time; } } } } $isOpen = (int)SystemConfigService::make()->getConfigByName('fudai_is_apply',1,'fudai'); $info['is_open'] = $isOpen; $info['appoint_status'] = $info['open_status'] == 1 && $isOpen==1? $appoint_status : 4; $info['dis_time'] = $dis_time; return api_succ_return(['msg' => '成功', 'data' => $info]); } // 预约福袋 public function beforeBuyBox(Request $request) { $model = new BoxModel(); $post = $request->post(); // 这是测试 上线要干掉 // return api_error_return('暂未开放'); $user_info = $request->user_info; $userId = isset($user_info['id']) ? $user_info['id'] : 0; $cacheKey = "caches:box:apply:u_{$userId}"; if (RedisCache::get($cacheKey)) { return api_succ_return('请不要频繁提交'); } RedisCache::set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3)); Db::startTrans(); try { $model->beforeBuyBox($request->uid, $post, $request->user_info); Db::commit(); } catch (\Exception $e) { Db::rollback(); RedisCache::clear($cacheKey); if ($e->getMessage() == '预约失败,还未开始预约、或预约已结束') { return api_error_return( ['msg' => '预约失败,还未开始预约、或预约已结束', 'code' => 302]); } return api_error_return($e->getMessage()); } RedisCache::clear($cacheKey); return api_succ_return('成功'); } // 我的预约 public function boxBuyRecord(Request $request, BoxRecordModel $model) { try { return api_succ_return(['msg' => '成功', 'data' => $model->getLog($request)]); } catch (\Exception $e) { return api_error_return('失败'); } } // 福袋商品列表 public function boxOutGoodsList(Request $request) { $box_type = $request->post('box_type'); $model = new ShopGoodsModel(); // $boxs = $where = ''; if ($box_type > 0) { $where = $box_type; } else { $where = '10,20,30,40'; } $list = $model->where('box_type', 'in', $where) ->withAttr('box_img', function ($val, $data) { return getWebUrl() . '.' . $val; }) ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn') ->order('box_type', 'desc') ->paginate($request->post('limit')) ->toArray(); return api_succ_return(['msg' => '成功', 'data' => $list['data']]); } // 福袋商品列表顶部banner public function boxOutBanner(Request $request) { $model = new ShopGoodsModel(); $list = $model->where('box_type', 'in', [30, 40]) ->withAttr('box_img', function ($val, $data) { return getWebUrl() . '.' . $val; }) ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn') ->order('box_type', 'desc') ->paginate(15) ->toArray(); return api_succ_return(['msg' => '成功', 'data' => $list['data']]); } // 拆袋 public function openBoxOnline(Request $request) { return api_error_return('已拆开'); $model = new BoxMidHandleModel(); $list = []; Db::startTrans(); try { $list = $model->openBoxOnline($request->uid, $request->post(), $request->user_info); Db::commit(); } catch (\Exception $e) { Db::rollback(); return api_error_return($e->getMessage()); } return api_succ_return(['msg' => '成功', 'data' => $list]); } public function boxHandleList(Request $request) { $type = $request->post('type'); $keywords = trim($request->post('keywords')); $box_type = $request->post('box_type'); if (!in_array($type, [1, 2])) { return api_error_return('订单类型参数错误'); } $where = array(); if (!empty($keywords)) { $where = [['r.h_sn', 'LIKE', '%' . $keywords . '%']]; } if (!empty($box_type)) { if (!in_array($box_type, [10, 20, 30, 40])) { sr_throw('盒子类型参数错误'); } } $where = []; $where[] = ['r.is_delete', '=', 2]; // 修改 by wes 2023-02-19 $where[] = ['r.status', '=', $type]; $where[] = ['r.uid', '=', $request->uid]; if (!empty($box_type)) { $where[] = ['r.box_type', '=', $box_type]; } $list = Db::name('box_handle') ->alias('r') ->where($where) ->field('r.*') ->paginate($request->post('limit'))->toArray(); foreach ($list['data'] as $key => &$val) { $goods_info = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find(); $val['goods_name'] = $goods_info['goods_name']; $val['goods_sn'] = $goods_info['goods_sn']; $val['goods_img'] = $goods_info['goods_img']; $val['goods_sn'] = $goods_info['goods_sn']; $val['price'] = $goods_info['price']; $val['spec_name'] = $goods_info['spec_name']; // if ($val['box_type'] == 10) { $val['is_canrecycle'] = 0; } else { $val['is_canrecycle'] = 1; } // } return api_succ_return(['msg' => '成功', 'data' => $list['data']]); } public function boxHandleUnread(Request $request) { $data = []; $model = new BoxHandleModel(); $data['un_handle'] = [ '10' => $model->where('box_type', 10)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(), '20' => $model->where('box_type', 20)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(), '30' => $model->where('box_type', 30)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(), '40' => $model->where('box_type', 40)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count() ]; $data['handled'] = [ '10' => $model->where('box_type', 10)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(), '20' => $model->where('box_type', 20)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(), '30' => $model->where('box_type', 30)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(), '40' => $model->where('box_type', 40)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count() ]; return api_succ_return(['msg' => '成功', 'data' => $data]); } public function boxGoodsSurePost(Request $request) { $model = new ShopOrderModel(); Db::startTrans(); try { $model->boxGoodsSurePost($request->uid, $request->post()); Db::commit(); } catch (\Exception $e) { Db::rollback(); return api_error_return($e->getMessage()); } return api_succ_return('回收成功'); } // 一键回收 public function boxGoodsReBuy(Request $request) { $model = new ShopOrderModel(); $user_info = $request->user_info; $userId = isset($user_info['id']) ? $user_info['id'] : 0; $cacheKey = "caches:box:rebuy:u_{$userId}"; $redis = new Redis(); if ($redis->get($cacheKey)) { return api_succ_return('请不要频繁操作'); } $redis->set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3)); Db::startTrans(); try { $res = $model->boxGoodsReBuy($request->uid, $request->post(), $request->user_info); Db::commit(); } catch (\Exception $e) { Db::rollback(); $redis->del($cacheKey); return api_error_return($e->getMessage()); } $redis->del($cacheKey); if ($res) { return api_succ_return(['msg' => '成功', 'data' => $res]); } return api_succ_return('成功'); } }