| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- /**
- * 收货地址
- */
- namespace app\api\controller\v1;
- use app\common\model\AddressModel;
- 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 think\db\Where;
- use think\exception\InvalidArgumentException;
- use think\cache\driver\Redis;
- use think\facade\Db;
- use think\Request;
- class Box
- {
- protected $model = null;
- public function __construct(ChatMessageModel $model)
- {
- $this->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]);
- }
- // 当前最新一期福袋预约信息
- public function getNewBoxInfo(){
-
-
- //$id = Db::name('box')->where(['status'=>1])->max('id');
- $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;
- $cur_times = [];
- // 当前时间小于 第一场 开始时间
- // if ($appoint_status != 1){
- // $begin_times = explode('-', $times[0]);
- // $begin_randtime = strtotime($time . $begin_times[0]);
- // if ($cur_time < $begin_randtime){
- // $appoint_status = 1;
- // $dis_time = $begin_randtime-$cur_time;
- // return 33;
- // }
- // }
- // 当前时间大于 最后一场结束时间
- 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]);
- $time_randend = strtotime($time.$time_cur[1]);
- if ($cur_time < $time_randbegin && !$has_get){
- // 正在这一场进行中
- $has_get = true;
- $dis_time = $time_randbegin - $cur_time;
- }
- }
- }
- }
- $info['appoint_status'] = $info['open_status'] ==1? $appoint_status: 1;
- $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}";
- $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 {
- $model->beforeBuyBox($request->uid, $post, $request->user_info);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $redis->del($cacheKey);
- if ($e->getMessage() == '预约失败,还未开始预约、或预约已结束'){
- return api_error_return(
- ['msg'=>'预约失败,还未开始预约、或预约已结束', 'code'=>302]);
- }
- return api_error_return($e->getMessage());
- }
- $redis->del($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){
- // 这是测试 上线要干掉
- // sr_testDb($request->header('token'), 45); ;
- $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)
- // ->leftJoin('shop_goods g', 'g.goods_id = r.goods_id')
- // ->withoutField('r.goods_type')
- // ->field('r.*,g.goods_name,g.goods_sn,g.goods_img,g.price,g.spec_name')
- ->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('成功');
- }
- }
|