Box.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * 收货地址
  4. */
  5. namespace app\api\controller\v1;
  6. use app\common\model\AddressModel;
  7. use app\common\model\BoxHandleModel;
  8. use app\common\model\BoxMidHandleModel;
  9. use app\common\model\BoxModel;
  10. use app\common\model\BoxRecordModel;
  11. use app\common\model\ChatMessageModel;
  12. use app\common\model\JhGoodsModel;
  13. use app\common\model\ShopGoodsModel;
  14. use app\common\model\ShopOrderModel;
  15. use app\common\model\UserAddressModel;
  16. use app\common\model\UserModel;
  17. use think\db\Where;
  18. use think\exception\InvalidArgumentException;
  19. use think\cache\driver\Redis;
  20. use think\facade\Db;
  21. use think\Request;
  22. class Box
  23. {
  24. protected $model = null;
  25. public function __construct(ChatMessageModel $model)
  26. {
  27. $this->model = $model;
  28. }
  29. // 跑马灯效果
  30. public function boxDownMessageList(Request $request){
  31. $messages = [];
  32. $boxs = [
  33. 10=>'普通',
  34. 20=>'稀有',
  35. 30=>'史诗',
  36. 40=>'传说',
  37. ];
  38. $list = Db::name('box_mid_handle')
  39. ->alias('m')
  40. ->where('m.box_type', '>', 10)
  41. ->leftJoin('user u', 'u.id = m.uid')
  42. ->limit(20)
  43. ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages){
  44. // $mobile = v;
  45. $mobile = '****'.substr($data['mobile'], 7);
  46. $messages[] = '恭喜'.$mobile.'拆出'.$boxs[$data['box_type']].'福袋';
  47. })
  48. ->orderRaw("rand() , m.id DESC")
  49. ->field('m.uid,u.mobile,u.nickname,m.box_type')
  50. ->select()->toArray();
  51. return api_succ_return(['msg'=>'成功', 'data'=>$messages]);
  52. // return api_succ_return(['msg'=>'请求成功', 'data'=>[
  53. // '恭喜****9567拆出稀有附带',
  54. // '恭喜****9567拆出稀有附带',
  55. // '恭喜****9567拆出稀有附带',
  56. // '恭喜****9567拆出稀有附带',
  57. // '恭喜****9567拆出稀有附带',
  58. // '恭喜****9567拆出稀有附带'
  59. // ]]);
  60. }
  61. // 福袋 泡泡商品
  62. public function boxRandGoodsList(){
  63. $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();
  64. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  65. }
  66. // 当前最新一期福袋预约信息
  67. public function getNewBoxInfo(){
  68. //$id = Db::name('box')->where(['status'=>1])->max('id');
  69. $id = Db::name('box')->where(['status'=>1,'open_status'=>1])->max('id');
  70. if (empty($id)){
  71. return api_error_return(['msg'=>'暂未开启', 'code'=>303]);
  72. }
  73. $info = Db::name('box')->where('id', $id)->find();
  74. $cur_time = time();
  75. $time = sr_getcurtime($cur_time, 'Y-m-d ');
  76. $times = explode('|', $info['time_set']);
  77. $appoint_status = 1;
  78. // 1 还未开始 2 进行中 3已结束
  79. $dis_time = 0;
  80. $cur_times = [];
  81. // 当前时间小于 第一场 开始时间
  82. // if ($appoint_status != 1){
  83. // $begin_times = explode('-', $times[0]);
  84. // $begin_randtime = strtotime($time . $begin_times[0]);
  85. // if ($cur_time < $begin_randtime){
  86. // $appoint_status = 1;
  87. // $dis_time = $begin_randtime-$cur_time;
  88. // return 33;
  89. // }
  90. // }
  91. // 当前时间大于 最后一场结束时间
  92. if ($appoint_status != 3){
  93. $end_timesarr = explode('-', $times[count($times)-1]);
  94. $end_randtime = strtotime($time . $end_timesarr[1]);
  95. if ($cur_time > $end_randtime){
  96. $appoint_status = 3;
  97. }
  98. }
  99. // 是否拿到最近的一场未开始距离时间
  100. if ($appoint_status != 3){
  101. foreach ($times as $key=>$val){
  102. $time_cur = explode('-', $val);
  103. $time_randbegin = strtotime($time.$time_cur[0]);
  104. $time_randend = strtotime($time.$time_cur[1]);
  105. if ($cur_time > $time_randbegin && $cur_time < $time_randend){
  106. // 正在这一场进行中
  107. $appoint_status = 2;
  108. }
  109. }
  110. if ($appoint_status!= 2){
  111. $has_get = false;
  112. foreach ($times as $key=>$val){
  113. $time_cur = explode('-', $val);
  114. $time_randbegin = strtotime($time.$time_cur[0]);
  115. $time_randend = strtotime($time.$time_cur[1]);
  116. if ($cur_time < $time_randbegin && !$has_get){
  117. // 正在这一场进行中
  118. $has_get = true;
  119. $dis_time = $time_randbegin - $cur_time;
  120. }
  121. }
  122. }
  123. }
  124. $info['appoint_status'] = $info['open_status'] ==1? $appoint_status: 1;
  125. $info['dis_time'] = $dis_time;
  126. return api_succ_return(['msg'=>'成功', 'data'=>$info]);
  127. }
  128. // 预约福袋
  129. public function beforeBuyBox(Request $request){
  130. $model = new BoxModel();
  131. $post = $request->post();
  132. // 这是测试 上线要干掉
  133. // return api_error_return('暂未开放');
  134. $user_info = $request->user_info;
  135. $userId = isset($user_info['id'])? $user_info['id'] : 0;
  136. $cacheKey = "caches:box:apply:u_{$userId}";
  137. $redis = new Redis();
  138. if($redis->get($cacheKey)){
  139. return api_succ_return('请不要频繁提交');
  140. }
  141. $redis->set($cacheKey,['uid'=> $request->uid, 'data'=> $request->post()], rand(2,3));
  142. Db::startTrans();
  143. try {
  144. $model->beforeBuyBox($request->uid, $post, $request->user_info);
  145. Db::commit();
  146. }catch (\Exception $e){
  147. Db::rollback();
  148. $redis->del($cacheKey);
  149. if ($e->getMessage() == '预约失败,还未开始预约、或预约已结束'){
  150. return api_error_return(
  151. ['msg'=>'预约失败,还未开始预约、或预约已结束', 'code'=>302]);
  152. }
  153. return api_error_return($e->getMessage());
  154. }
  155. $redis->del($cacheKey);
  156. return api_succ_return('成功');
  157. }
  158. // 我的预约
  159. public function boxBuyRecord(Request $request, BoxRecordModel $model){
  160. try {
  161. return api_succ_return(['msg'=>'成功', 'data'=>$model->getLog($request)]);
  162. } catch (\Exception $e) {
  163. return api_error_return('失败');
  164. }
  165. }
  166. // 福袋商品列表
  167. public function boxOutGoodsList(Request $request){
  168. $box_type = $request->post('box_type');
  169. $model = new ShopGoodsModel();
  170. // $boxs =
  171. $where = '';
  172. if ($box_type > 0){
  173. $where = $box_type;
  174. }else{
  175. $where = '10,20,30,40';
  176. }
  177. $list = $model->where('box_type','in', $where)
  178. ->withAttr('box_img', function ($val, $data){
  179. return getWebUrl().'.'.$val;
  180. })
  181. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  182. ->order('box_type', 'desc')
  183. ->paginate($request->post('limit'))
  184. ->toArray();
  185. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  186. }
  187. // 福袋商品列表顶部banner
  188. public function boxOutBanner(Request $request){
  189. $model = new ShopGoodsModel();
  190. $list = $model->where('box_type','in', [30,40])
  191. ->withAttr('box_img', function ($val, $data){
  192. return getWebUrl().'.'.$val;
  193. })
  194. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  195. ->order('box_type', 'desc')
  196. ->paginate(15)
  197. ->toArray();
  198. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  199. }
  200. // 拆袋
  201. public function openBoxOnline(Request $request){
  202. return api_error_return('已拆开');
  203. $model = new BoxMidHandleModel();
  204. $list = [];
  205. Db::startTrans();
  206. try {
  207. $list = $model->openBoxOnline($request->uid, $request->post(), $request->user_info);
  208. Db::commit();
  209. }catch (\Exception $e){
  210. Db::rollback();
  211. return api_error_return($e->getMessage());
  212. }
  213. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  214. }
  215. public function boxHandleList(Request $request){
  216. // 这是测试 上线要干掉
  217. // sr_testDb($request->header('token'), 45); ;
  218. $type = $request->post('type');
  219. $keywords = trim($request->post('keywords'));
  220. $box_type = $request->post('box_type');
  221. if (!in_array($type, [1,2])){
  222. return api_error_return('参数错误');
  223. }
  224. $where = array();
  225. if (!empty($keywords)){
  226. $where = [['r.h_sn', 'LIKE', '%'.$keywords.'%']];
  227. }
  228. if (!empty($box_type)){
  229. if (!in_array($box_type, [10, 20, 30, 40])){
  230. sr_throw('参数错误');
  231. }
  232. }
  233. $where = [];
  234. $where[] = ['r.is_delete', '=', 2]; // 修改 by wes 2023-02-19
  235. $where[] = ['r.status', '=', $type];
  236. $where[] = ['r.uid', '=', $request->uid];
  237. if (!empty($box_type)){
  238. $where[] = ['r.box_type', '=', $box_type];
  239. }
  240. $list = Db::name('box_handle')
  241. ->alias('r')
  242. ->where($where)
  243. // ->leftJoin('shop_goods g', 'g.goods_id = r.goods_id')
  244. // ->withoutField('r.goods_type')
  245. // ->field('r.*,g.goods_name,g.goods_sn,g.goods_img,g.price,g.spec_name')
  246. ->field('r.*')
  247. ->paginate($request->post('limit'))->toArray();
  248. foreach ($list['data'] as $key =>&$val){
  249. $goods_info = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find();
  250. $val['goods_name'] = $goods_info['goods_name'];
  251. $val['goods_sn'] = $goods_info['goods_sn'];
  252. $val['goods_img'] = $goods_info['goods_img'];
  253. $val['goods_sn'] = $goods_info['goods_sn'];
  254. $val['price'] = $goods_info['price'];
  255. $val['spec_name'] = $goods_info['spec_name'];
  256. //
  257. if ($val['box_type'] == 10){
  258. $val['is_canrecycle'] = 0;
  259. }else{
  260. $val['is_canrecycle'] = 1;
  261. }
  262. //
  263. }
  264. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  265. }
  266. public function boxHandleUnread(Request $request){
  267. $data = [];
  268. $model = new BoxHandleModel();
  269. $data['un_handle'] = [
  270. '10'=>$model->where('box_type', 10)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  271. '20'=>$model->where('box_type', 20)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  272. '30'=>$model->where('box_type', 30)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  273. '40'=>$model->where('box_type', 40)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count()
  274. ];
  275. $data['handled'] = [
  276. '10'=>$model->where('box_type', 10)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  277. '20'=>$model->where('box_type', 20)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  278. '30'=>$model->where('box_type', 30)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  279. '40'=>$model->where('box_type', 40)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count()
  280. ];
  281. return api_succ_return(['msg'=>'成功', 'data'=>$data]);
  282. }
  283. public function boxGoodsSurePost(Request $request){
  284. $model = new ShopOrderModel();
  285. Db::startTrans();
  286. try {
  287. $model->boxGoodsSurePost($request->uid, $request->post());
  288. Db::commit();
  289. }catch (\Exception $e){
  290. Db::rollback();
  291. return api_error_return($e->getMessage());
  292. }
  293. return api_succ_return('回收成功');
  294. }
  295. // 一键回收
  296. public function boxGoodsReBuy(Request $request){
  297. $model = new ShopOrderModel();
  298. $user_info = $request->user_info;
  299. $userId = isset($user_info['id'])? $user_info['id'] : 0;
  300. $cacheKey = "caches:box:rebuy:u_{$userId}";
  301. $redis = new Redis();
  302. if($redis->get($cacheKey)){
  303. return api_succ_return('请不要频繁操作');
  304. }
  305. $redis->set($cacheKey,['uid'=> $request->uid, 'data'=> $request->post()], rand(2,3));
  306. Db::startTrans();
  307. try {
  308. $res = $model->boxGoodsReBuy($request->uid, $request->post(), $request->user_info);
  309. Db::commit();
  310. }catch (\Exception $e){
  311. Db::rollback();
  312. $redis->del($cacheKey);
  313. return api_error_return($e->getMessage());
  314. }
  315. $redis->del($cacheKey);
  316. if ($res){
  317. return api_succ_return(['msg'=>'成功', 'data'=>$res]);
  318. }
  319. return api_succ_return('成功');
  320. }
  321. }