Box.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * 收货地址
  4. */
  5. namespace app\api\controller\v1;
  6. use app\common\model\AddressModel;
  7. use app\common\model\BoxHandleAllModel;
  8. use app\common\model\BoxHandleModel;
  9. use app\common\model\BoxMidHandleModel;
  10. use app\common\model\BoxModel;
  11. use app\common\model\BoxRecordModel;
  12. use app\common\model\ChatMessageModel;
  13. use app\common\model\JhGoodsModel;
  14. use app\common\model\ShopGoodsModel;
  15. use app\common\model\ShopOrderModel;
  16. use app\common\model\UserAddressModel;
  17. use app\common\model\UserModel;
  18. use app\common\service\BoxHandleService;
  19. use app\common\service\BoxRecordService;
  20. use app\common\service\BoxService;
  21. use app\common\service\ShopGoodsService;
  22. use app\common\service\SystemConfigService;
  23. use think\db\Where;
  24. use think\exception\InvalidArgumentException;
  25. use think\cache\driver\Redis;
  26. use think\facade\Db;
  27. use think\Request;
  28. use utils\RedisCache;
  29. class Box
  30. {
  31. protected $model = null;
  32. public function __construct(ChatMessageModel $model)
  33. {
  34. $this->model = $model;
  35. }
  36. /**
  37. * 跑马灯效果
  38. * @param Request $request
  39. * @return \think\Response
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function boxDownMessageList(Request $request)
  45. {
  46. $messages = [];
  47. $boxs = [
  48. 10 => '普通',
  49. 20 => '稀有',
  50. 30 => '史诗',
  51. 40 => '传说',
  52. ];
  53. $list = Db::name('box_mid_handle')
  54. ->alias('m')
  55. ->where('m.box_type', '>', 10)
  56. ->leftJoin('user u', 'u.id = m.uid')
  57. ->limit(20)
  58. ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages) {
  59. $mobile = '****' . substr($data['mobile'], 7);
  60. $messages[] = '恭喜' . $mobile . '拆出' . $boxs[$data['box_type']] . '福袋';
  61. })
  62. ->orderRaw("rand() , m.id DESC")
  63. ->field('m.uid,u.mobile,u.nickname,m.box_type')
  64. ->select()->toArray();
  65. return api_succ_return(['msg' => '成功', 'data' => $messages]);
  66. }
  67. /**
  68. * 福袋 泡泡商品
  69. * @return \think\Response
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException\
  73. */
  74. public function boxRandGoodsList()
  75. {
  76. $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();
  77. return api_succ_return(['msg' => '成功', 'data' => $list]);
  78. }
  79. /**
  80. * 当前最新一期福袋预约信息
  81. * @return \think\Response
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function getNewBoxInfo()
  87. {
  88. try{
  89. return api_succ_return(['msg' => '成功', 'data' => BoxService::make()->getNewInfo()]);
  90. } catch (\Exception $exception){
  91. return api_error_return($exception->getMessage());
  92. }
  93. }
  94. /**
  95. * 预约福袋
  96. * @param Request $request
  97. * @return \think\Response
  98. */
  99. public function beforeBuyBox(Request $request)
  100. {
  101. $post = $request->post();
  102. $user_info = $request->user_info;
  103. $userId = isset($user_info['id']) ? $user_info['id'] : 0;
  104. $cacheKey = "caches:box:applyLock:u_{$userId}";
  105. if (RedisCache::get($cacheKey)) {
  106. return api_error_return('请不要频繁提交,3秒后再尝试');
  107. }
  108. // 加锁
  109. RedisCache::set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3));
  110. Db::startTrans();
  111. try {
  112. BoxRecordService::make()->applyBox($request->uid, $post);
  113. Db::commit();
  114. } catch (\Exception $e) {
  115. Db::rollback();
  116. $error = $e->getMessage();
  117. RedisCache::clear($cacheKey);
  118. return api_error_return($error == 'no_open'? ['msg'=>'预约失败,还未开始预约、或预约已结束','code'=>302]:$error);
  119. }
  120. RedisCache::clear($cacheKey);
  121. return api_succ_return('预约成功');
  122. }
  123. /**
  124. * 我的预约
  125. * @param Request $request
  126. * @param BoxRecordModel $model
  127. * @return \think\Response
  128. */
  129. public function boxBuyRecord(Request $request, BoxRecordModel $model)
  130. {
  131. try {
  132. return api_succ_return(['msg' => '成功', 'data' => $model->getLog($request)]);
  133. } catch (\Exception $e) {
  134. return api_error_return('失败');
  135. }
  136. }
  137. /**
  138. * 福袋商品列表
  139. * @param Request $request
  140. * @return \think\Response
  141. * @throws \think\db\exception\DbException
  142. */
  143. public function boxOutGoodsList(Request $request)
  144. {
  145. $boxType = $request->post('box_type',0);
  146. $pageSize = $request->post('limit',10);
  147. $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize);
  148. return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]);
  149. }
  150. /**
  151. * 福袋商品列表顶部banner
  152. * @param Request $request
  153. * @return \think\Response
  154. * @throws \think\db\exception\DbException
  155. */
  156. public function boxOutBanner(Request $request)
  157. {
  158. $boxType = [30,40];
  159. $pageSize = $request->post('limit',10);
  160. $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize);
  161. return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]);
  162. }
  163. /**
  164. * 拆袋
  165. * @param Request $request
  166. * @return \think\Response
  167. */
  168. public function openBoxOnline(Request $request)
  169. {
  170. if(RedisCache::get("caches:box:open:user_".$request->uid)){
  171. return api_error_return('福袋已拆开');
  172. }else{
  173. return api_error_return('福袋未开,请耐心等候');
  174. }
  175. }
  176. /**
  177. * 福袋中奖记录
  178. * @param Request $request
  179. * @return \think\Response
  180. * @throws \think\Exception
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\DbException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. */
  185. public function boxHandleList(Request $request)
  186. {
  187. try{
  188. $dateType = $request->post('date_type', 1);
  189. $pageSize = $request->post('limit',10);
  190. $post = $request->post();
  191. if($dateType==1){
  192. $post['time'] = date('Y-m-d');
  193. return api_succ_return(['msg' => '获取今日数据成功', 'data' => BoxHandleService::make()->getBoxListByUser($request->uid, $post,$pageSize)]);
  194. }else{
  195. $post['time1'] = date('Y-m-d');
  196. $list = BoxHandleService::make()->getBoxListByUser($request->uid,$post,$pageSize);
  197. // $list = BoxHandleService::make()->getHistoryBoxList($request->uid,$post,$pageSize);
  198. return api_succ_return(['msg' => '获取历史数据成功1', 'data' => $list]);
  199. }
  200. } catch (\Exception $exception){
  201. return api_error_return('获取失败:'.$exception->getMessage());
  202. }
  203. }
  204. /**
  205. * 福仓数据统计
  206. * @param Request $request
  207. * @return \think\Response
  208. * @throws \think\db\exception\DbException
  209. */
  210. public function boxHandleUnread(Request $request)
  211. {
  212. $data = [];
  213. $dateType = $request->post('date_type', 1);
  214. $counts = BoxHandleService::make()->getBoxCount($request->uid, 1, $dateType);
  215. $data['un_handle'] = $counts;
  216. $counts = BoxHandleService::make()->getBoxCount($request->uid, 2, $dateType);
  217. $data['handled'] = $counts;
  218. return api_succ_return(['msg' => '成功', 'data' => $data]);
  219. }
  220. /**
  221. * 福袋一键发货处理
  222. * @param Request $request
  223. * @return \think\Response
  224. */
  225. public function boxGoodsSurePost(Request $request)
  226. {
  227. $cacheKey = "caches:box:delivery:user_{$request->uid}";
  228. if(RedisCache::get($cacheKey)){
  229. return api_error_return('请不要频繁提交,稍后再试~');
  230. }
  231. RedisCache::setnx($cacheKey, 1, rand(3,5));
  232. Db::startTrans();
  233. try {
  234. BoxHandleService::make()->boxDelivery($request->uid, $request->post());
  235. Db::commit();
  236. } catch (\Exception $e) {
  237. Db::rollback();
  238. RedisCache::clear($cacheKey);
  239. RedisCache::clear("caches:box:deliveryCatch:user_{$request->uid}:lock");
  240. return api_error_return($e->getMessage());
  241. }
  242. return api_succ_return('发货成功');
  243. }
  244. /**
  245. * 福袋一键回收处理
  246. * @param Request $request
  247. * @return \think\Response
  248. */
  249. public function boxGoodsReBuy(Request $request)
  250. {
  251. $action = $request->post('action');
  252. $cacheKey = "caches:box:rebuy:user_{$request->uid}_{$action}";
  253. if (RedisCache::get($cacheKey) && $action != 'apply') {
  254. return api_error_return('请不要频繁操作,稍后再尝试');
  255. }
  256. RedisCache::setnx($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(3, 5));
  257. Db::startTrans();
  258. try {
  259. $res = BoxHandleService::make()->boxGoodsReBuy($request->uid, $request->post());
  260. Db::commit();
  261. return api_succ_return(['msg' => '处理成功', 'data' => $res]);
  262. } catch (\Exception $e) {
  263. Db::rollback();
  264. RedisCache::clear($cacheKey);
  265. return api_error_return($e->getMessage()?$e->getMessage():'回收失败');
  266. }
  267. }
  268. }