Box.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. use utils\RedisCache;
  23. class Box
  24. {
  25. protected $model = null;
  26. public function __construct(ChatMessageModel $model)
  27. {
  28. $this->model = $model;
  29. }
  30. // 跑马灯效果
  31. public function boxDownMessageList(Request $request){
  32. $messages = [];
  33. $boxs = [
  34. 10=>'普通',
  35. 20=>'稀有',
  36. 30=>'史诗',
  37. 40=>'传说',
  38. ];
  39. $list = Db::name('box_mid_handle')
  40. ->alias('m')
  41. ->where('m.box_type', '>', 10)
  42. ->leftJoin('user u', 'u.id = m.uid')
  43. ->limit(20)
  44. ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages){
  45. // $mobile = v;
  46. $mobile = '****'.substr($data['mobile'], 7);
  47. $messages[] = '恭喜'.$mobile.'拆出'.$boxs[$data['box_type']].'福袋';
  48. })
  49. ->orderRaw("rand() , m.id DESC")
  50. ->field('m.uid,u.mobile,u.nickname,m.box_type')
  51. ->select()->toArray();
  52. return api_succ_return(['msg'=>'成功', 'data'=>$messages]);
  53. // return api_succ_return(['msg'=>'请求成功', 'data'=>[
  54. // '恭喜****9567拆出稀有附带',
  55. // '恭喜****9567拆出稀有附带',
  56. // '恭喜****9567拆出稀有附带',
  57. // '恭喜****9567拆出稀有附带',
  58. // '恭喜****9567拆出稀有附带',
  59. // '恭喜****9567拆出稀有附带'
  60. // ]]);
  61. }
  62. // 福袋 泡泡商品
  63. public function boxRandGoodsList(){
  64. $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();
  65. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  66. }
  67. // 当前最新一期福袋预约信息
  68. public function getNewBoxInfo(){
  69. //$id = Db::name('box')->where(['status'=>1])->max('id');
  70. $id = Db::name('box')->where(['status'=>1,'open_status'=>1])->max('id');
  71. if (empty($id)){
  72. return api_error_return(['msg'=>'暂未开启', 'code'=>303]);
  73. }
  74. $info = Db::name('box')->where('id', $id)->find();
  75. $cur_time = time();
  76. $time = sr_getcurtime($cur_time, 'Y-m-d ');
  77. $times = explode('|', $info['time_set']);
  78. $appoint_status = 1;
  79. // 1 还未开始 2 进行中 3已结束
  80. $dis_time = 0;
  81. $cur_times = [];
  82. // 当前时间小于 第一场 开始时间
  83. // if ($appoint_status != 1){
  84. // $begin_times = explode('-', $times[0]);
  85. // $begin_randtime = strtotime($time . $begin_times[0]);
  86. // if ($cur_time < $begin_randtime){
  87. // $appoint_status = 1;
  88. // $dis_time = $begin_randtime-$cur_time;
  89. // return 33;
  90. // }
  91. // }
  92. // 当前时间大于 最后一场结束时间
  93. if ($appoint_status != 3){
  94. $end_timesarr = explode('-', $times[count($times)-1]);
  95. $end_randtime = strtotime($time . $end_timesarr[1]);
  96. if ($cur_time > $end_randtime){
  97. $appoint_status = 3;
  98. }
  99. }
  100. // 是否拿到最近的一场未开始距离时间
  101. if ($appoint_status != 3){
  102. foreach ($times as $key=>$val){
  103. $time_cur = explode('-', $val);
  104. $time_randbegin = strtotime($time.$time_cur[0]);
  105. $time_randend = strtotime($time.$time_cur[1]);
  106. if ($cur_time > $time_randbegin && $cur_time < $time_randend){
  107. // 正在这一场进行中
  108. $appoint_status = 2;
  109. }
  110. }
  111. if ($appoint_status!= 2){
  112. $has_get = false;
  113. foreach ($times as $key=>$val){
  114. $time_cur = explode('-', $val);
  115. $time_randbegin = strtotime($time.$time_cur[0]);
  116. $time_randend = strtotime($time.$time_cur[1]);
  117. if ($cur_time < $time_randbegin && !$has_get){
  118. // 正在这一场进行中
  119. $has_get = true;
  120. $dis_time = $time_randbegin - $cur_time;
  121. }
  122. }
  123. }
  124. }
  125. $info['appoint_status'] = $info['open_status'] ==1? $appoint_status: 1;
  126. $info['dis_time'] = $dis_time;
  127. return api_succ_return(['msg'=>'成功', 'data'=>$info]);
  128. }
  129. // 预约福袋
  130. public function beforeBuyBox(Request $request){
  131. $model = new BoxModel();
  132. $post = $request->post();
  133. // 这是测试 上线要干掉
  134. // return api_error_return('暂未开放');
  135. $user_info = $request->user_info;
  136. $userId = isset($user_info['id'])? $user_info['id'] : 0;
  137. $cacheKey = "caches:box:apply:u_{$userId}";
  138. if(RedisCache::get($cacheKey)){
  139. return api_succ_return('请不要频繁提交');
  140. }
  141. RedisCache::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. RedisCache::clear($cacheKey);
  149. if ($e->getMessage() == '预约失败,还未开始预约、或预约已结束'){
  150. return api_error_return(
  151. ['msg'=>'预约失败,还未开始预约、或预约已结束', 'code'=>302]);
  152. }
  153. return api_error_return($e->getMessage());
  154. }
  155. RedisCache::clear($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. }