Box.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. return api_succ_return(['msg' => '成功', 'data' => ShopGoodsService::make()->getRandomGoods()]);
  77. }
  78. /**
  79. * 当前最新一期福袋预约信息
  80. * @return \think\Response
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getNewBoxInfo()
  86. {
  87. try{
  88. return api_succ_return(['msg' => '成功', 'data' => BoxService::make()->getNewInfo()]);
  89. } catch (\Exception $exception){
  90. return api_error_return($exception->getMessage());
  91. }
  92. }
  93. /**
  94. * 预约福袋
  95. * @param Request $request
  96. * @return \think\Response
  97. */
  98. public function beforeBuyBox(Request $request)
  99. {
  100. $post = $request->post();
  101. $user_info = $request->user_info;
  102. $userId = isset($user_info['id']) ? $user_info['id'] : 0;
  103. $cacheKey = "caches:box:applyLock:u_{$userId}";
  104. if (RedisCache::get($cacheKey)) {
  105. return api_error_return('请不要频繁提交,3秒后再尝试');
  106. }
  107. // 加锁
  108. RedisCache::set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3));
  109. Db::startTrans();
  110. try {
  111. BoxRecordService::make()->applyBox($request->uid, $post);
  112. Db::commit();
  113. } catch (\Exception $e) {
  114. Db::rollback();
  115. $error = $e->getMessage();
  116. RedisCache::clear($cacheKey);
  117. return api_error_return($error == 'no_open'? ['msg'=>'预约失败,还未开始预约、或预约已结束','code'=>302]:$error);
  118. }
  119. RedisCache::clear($cacheKey);
  120. return api_succ_return('预约成功');
  121. }
  122. /**
  123. * 我的预约
  124. * @param Request $request
  125. * @param BoxRecordModel $model
  126. * @return \think\Response
  127. */
  128. public function boxBuyRecord(Request $request, BoxRecordModel $model)
  129. {
  130. try {
  131. return api_succ_return(['msg' => '成功', 'data' => $model->getLog($request)]);
  132. } catch (\Exception $e) {
  133. return api_error_return('失败');
  134. }
  135. }
  136. /**
  137. * 福袋商品列表
  138. * @param Request $request
  139. * @return \think\Response
  140. * @throws \think\db\exception\DbException
  141. */
  142. public function boxOutGoodsList(Request $request)
  143. {
  144. $boxType = $request->post('box_type',0);
  145. $pageSize = $request->post('limit',10);
  146. $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize);
  147. return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]);
  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. $result = ShopGoodsService::make()->getBoxGoodsListByType($boxType,$pageSize);
  160. return api_succ_return(['msg' => '成功', 'data' => isset($result['data'])? $result['data'] : []]);
  161. }
  162. /**
  163. * 拆袋
  164. * @param Request $request
  165. * @return \think\Response
  166. */
  167. public function openBoxOnline(Request $request)
  168. {
  169. if(RedisCache::get("caches:box:open:user_".$request->uid)){
  170. return api_error_return('福袋已拆开');
  171. }else{
  172. return api_error_return('福袋未开,请耐心等候');
  173. }
  174. }
  175. /**
  176. * 福袋中奖记录
  177. * @param Request $request
  178. * @return \think\Response
  179. * @throws \think\Exception
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function boxHandleList(Request $request)
  185. {
  186. try{
  187. $dateType = $request->post('date_type', 1);
  188. $pageSize = $request->post('limit',10);
  189. $post = $request->post();
  190. if($dateType==1){
  191. $post['time'] = date('Y-m-d');
  192. return api_succ_return(['msg' => '获取今日数据成功', 'data' => BoxHandleService::make()->getBoxListByUser($request->uid, $post,$pageSize)]);
  193. }else{
  194. $post['time1'] = date('Y-m-d');
  195. $list = BoxHandleService::make()->getBoxListByUser($request->uid,$post,$pageSize);
  196. // $list = BoxHandleService::make()->getHistoryBoxList($request->uid,$post,$pageSize);
  197. return api_succ_return(['msg' => '获取历史数据成功1', 'data' => $list]);
  198. }
  199. } catch (\Exception $exception){
  200. return api_error_return('获取失败:'.$exception->getMessage());
  201. }
  202. }
  203. /**
  204. * 福仓数据统计
  205. * @param Request $request
  206. * @return \think\Response
  207. * @throws \think\db\exception\DbException
  208. */
  209. public function boxHandleUnread(Request $request)
  210. {
  211. $data = [];
  212. $dateType = $request->post('date_type', 1);
  213. $counts = BoxHandleService::make()->getBoxCount($request->uid, 1, $dateType);
  214. $data['un_handle'] = $counts;
  215. $counts = BoxHandleService::make()->getBoxCount($request->uid, 2, $dateType);
  216. $data['handled'] = $counts;
  217. return api_succ_return(['msg' => '成功', 'data' => $data]);
  218. }
  219. /**
  220. * 福袋一键发货处理
  221. * @param Request $request
  222. * @return \think\Response
  223. */
  224. public function boxGoodsSurePost(Request $request)
  225. {
  226. $cacheKey = "caches:box:delivery:user_{$request->uid}";
  227. if(RedisCache::get($cacheKey)){
  228. return api_error_return('请不要频繁提交,稍后再试~');
  229. }
  230. RedisCache::setnx($cacheKey, 1, rand(3,5));
  231. Db::startTrans();
  232. try {
  233. BoxHandleService::make()->boxDelivery($request->uid, $request->post());
  234. Db::commit();
  235. } catch (\Exception $e) {
  236. Db::rollback();
  237. RedisCache::clear($cacheKey);
  238. RedisCache::clear("caches:box:deliveryCatch:user_{$request->uid}:lock");
  239. return api_error_return($e->getMessage());
  240. }
  241. return api_succ_return('发货成功');
  242. }
  243. /**
  244. * 福袋一键回收处理
  245. * @param Request $request
  246. * @return \think\Response
  247. */
  248. public function boxGoodsReBuy(Request $request)
  249. {
  250. $action = $request->post('action');
  251. $cacheKey = "caches:box:rebuy:user_{$request->uid}_{$action}";
  252. if (RedisCache::get($cacheKey) && $action != 'apply') {
  253. return api_error_return('请不要频繁操作,稍后再尝试');
  254. }
  255. RedisCache::setnx($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(3, 5));
  256. Db::startTrans();
  257. try {
  258. $res = BoxHandleService::make()->boxGoodsReBuy($request->uid, $request->post());
  259. Db::commit();
  260. return api_succ_return(['msg' => '处理成功', 'data' => $res]);
  261. } catch (\Exception $e) {
  262. Db::rollback();
  263. RedisCache::clear($cacheKey);
  264. return api_error_return($e->getMessage()?$e->getMessage():'回收失败');
  265. }
  266. }
  267. }