| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- /**
- * 收货地址
- */
- namespace app\api\controller\v1;
- use app\common\model\AddressModel;
- use app\common\model\BoxHandleAllModel;
- use app\common\model\BoxHandleModel;
- use app\common\model\BoxMidHandleModel;
- use app\common\model\BoxModel;
- use app\common\model\BoxRecordModel;
- use app\common\model\ChatMessageModel;
- use app\common\model\JhGoodsModel;
- use app\common\model\ShopGoodsModel;
- use app\common\model\ShopOrderModel;
- use app\common\model\UserAddressModel;
- use app\common\model\UserModel;
- use app\common\service\BoxHandleService;
- use app\common\service\BoxRecordService;
- use app\common\service\BoxService;
- use app\common\service\ShopGoodsService;
- use app\common\service\SystemConfigService;
- use think\db\Where;
- use think\exception\InvalidArgumentException;
- use think\cache\driver\Redis;
- use think\facade\Db;
- use think\Request;
- use utils\RedisCache;
- class Box
- {
- protected $model = null;
- public function __construct(ChatMessageModel $model)
- {
- $this->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():'回收失败');
- }
- }
- }
|