Box.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. return api_succ_return(['msg' => '成功', 'data' => ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize)]);
  148. }
  149. /**
  150. * 福袋商品列表顶部banner
  151. * @param Request $request
  152. * @return \think\Response
  153. * @throws \think\db\exception\DbException
  154. */
  155. public function boxOutBanner(Request $request)
  156. {
  157. $boxType = [30,40];
  158. $pageSize = $request->post('limit',10);
  159. return api_succ_return(['msg' => '成功', 'data' => ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize)]);
  160. }
  161. /**
  162. * 拆袋
  163. * @param Request $request
  164. * @return \think\Response
  165. */
  166. public function openBoxOnline(Request $request)
  167. {
  168. if(RedisCache::get("caches:box:open:user_".$request->uid)){
  169. return api_error_return('福袋已拆开');
  170. }else{
  171. return api_error_return('福袋未开,请耐心等候');
  172. }
  173. }
  174. /**
  175. * 福袋中奖记录
  176. * @param Request $request
  177. * @return \think\Response
  178. * @throws \think\Exception
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\DbException
  181. * @throws \think\db\exception\ModelNotFoundException
  182. */
  183. public function boxHandleList(Request $request)
  184. {
  185. try{
  186. $dateType = $request->post('date_type', 1);
  187. $pageSize = $request->post('limit',10);
  188. if($dateType==1){
  189. return api_succ_return(['msg' => '获取今日数据成功', 'data' => BoxHandleService::make()->getBoxListByUser($request->uid,$request->post(),$pageSize)]);
  190. }else{
  191. return api_succ_return(['msg' => '获取历史数据成功', 'data' => BoxHandleService::make()->getHistoryBoxList($request->uid,$request->post(),$pageSize)]);
  192. }
  193. } catch (\Exception $exception){
  194. return api_error_return('获取失败:'.$exception->getMessage());
  195. }
  196. }
  197. /**
  198. * 福仓数据统计
  199. * @param Request $request
  200. * @return \think\Response
  201. * @throws \think\db\exception\DbException
  202. */
  203. public function boxHandleUnread(Request $request)
  204. {
  205. $data = [];
  206. $dateType = $request->post('date_type', 1);
  207. $counts = BoxHandleService::make()->getBoxCount($request->uid, 1, $dateType);
  208. $data['un_handle'] = $counts;
  209. $counts = BoxHandleService::make()->getBoxCount($request->uid, 2, $dateType);
  210. $data['handled'] = $counts;
  211. return api_succ_return(['msg' => '成功', 'data' => $data]);
  212. }
  213. /**
  214. * 福袋一键发货处理
  215. * @param Request $request
  216. * @return \think\Response
  217. */
  218. public function boxGoodsSurePost(Request $request)
  219. {
  220. $cacheKey = "caches:box:delivery:user_{$request->uid}";
  221. if(RedisCache::get($cacheKey)){
  222. return api_error_return('请不要频繁提交,稍后再试~');
  223. }
  224. RedisCache::setnx($cacheKey, 1, rand(3,5));
  225. Db::startTrans();
  226. try {
  227. BoxHandleService::make()->boxDelivery($request->uid, $request->post());
  228. Db::commit();
  229. } catch (\Exception $e) {
  230. Db::rollback();
  231. RedisCache::clear($cacheKey);
  232. RedisCache::clear("caches:box:deliveryCatch:user_{$request->uid}:lock");
  233. return api_error_return($e->getMessage());
  234. }
  235. return api_succ_return('发货成功');
  236. }
  237. /**
  238. * 福袋一键回收处理
  239. * @param Request $request
  240. * @return \think\Response
  241. */
  242. public function boxGoodsReBuy(Request $request)
  243. {
  244. $action = $request->post('action');
  245. $cacheKey = "caches:box:rebuy:user_{$request->uid}_{$action}";
  246. if (RedisCache::get($cacheKey)) {
  247. return api_error_return('请不要频繁操作,稍后再尝试');
  248. }
  249. RedisCache::setnx($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(3, 5));
  250. Db::startTrans();
  251. try {
  252. $res = BoxHandleService::make()->boxGoodsReBuy($request->uid, $request->post());
  253. Db::commit();
  254. return api_succ_return(['msg' => '处理成功', 'data' => $res]);
  255. } catch (\Exception $e) {
  256. Db::rollback();
  257. RedisCache::clear($cacheKey);
  258. return api_error_return($e->getMessage()?$e->getMessage():'回收失败');
  259. }
  260. }
  261. }