Box.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 app\common\service\SystemConfigService;
  18. use think\db\Where;
  19. use think\exception\InvalidArgumentException;
  20. use think\cache\driver\Redis;
  21. use think\facade\Db;
  22. use think\Request;
  23. use utils\RedisCache;
  24. class Box
  25. {
  26. protected $model = null;
  27. public function __construct(ChatMessageModel $model)
  28. {
  29. $this->model = $model;
  30. }
  31. // 跑马灯效果
  32. public function boxDownMessageList(Request $request)
  33. {
  34. $messages = [];
  35. $boxs = [
  36. 10 => '普通',
  37. 20 => '稀有',
  38. 30 => '史诗',
  39. 40 => '传说',
  40. ];
  41. $list = Db::name('box_mid_handle')
  42. ->alias('m')
  43. ->where('m.box_type', '>', 10)
  44. ->leftJoin('user u', 'u.id = m.uid')
  45. ->limit(20)
  46. ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages) {
  47. // $mobile = v;
  48. $mobile = '****' . substr($data['mobile'], 7);
  49. $messages[] = '恭喜' . $mobile . '拆出' . $boxs[$data['box_type']] . '福袋';
  50. })
  51. ->orderRaw("rand() , m.id DESC")
  52. ->field('m.uid,u.mobile,u.nickname,m.box_type')
  53. ->select()->toArray();
  54. return api_succ_return(['msg' => '成功', 'data' => $messages]);
  55. // return api_succ_return(['msg'=>'请求成功', 'data'=>[
  56. // '恭喜****9567拆出稀有附带',
  57. // '恭喜****9567拆出稀有附带',
  58. // '恭喜****9567拆出稀有附带',
  59. // '恭喜****9567拆出稀有附带',
  60. // '恭喜****9567拆出稀有附带',
  61. // '恭喜****9567拆出稀有附带'
  62. // ]]);
  63. }
  64. // 福袋 泡泡商品
  65. public function boxRandGoodsList()
  66. {
  67. $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();
  68. return api_succ_return(['msg' => '成功', 'data' => $list]);
  69. }
  70. /**
  71. * 当前最新一期福袋预约信息
  72. * @return \think\Response
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getNewBoxInfo()
  78. {
  79. $id = Db::name('box')->where(['status' => 1, 'open_status' => 1])->max('id');
  80. if (empty($id)) {
  81. return api_error_return(['msg' => '暂未开启', 'code' => 303]);
  82. }
  83. $info = Db::name('box')->where('id', $id)->find();
  84. $cur_time = time();
  85. $time = sr_getcurtime($cur_time, 'Y-m-d ');
  86. $times = explode('|', $info['time_set']);
  87. $appoint_status = 1;
  88. // 1 还未开始 2 进行中 3已结束
  89. $dis_time = 0;
  90. // 当前时间大于 最后一场结束时间
  91. if ($appoint_status != 3) {
  92. $end_timesarr = explode('-', $times[count($times) - 1]);
  93. $end_randtime = strtotime($time . $end_timesarr[1]);
  94. if ($cur_time > $end_randtime) {
  95. $appoint_status = 3;
  96. }
  97. }
  98. // 是否拿到最近的一场未开始距离时间
  99. if ($appoint_status != 3) {
  100. foreach ($times as $key => $val) {
  101. $time_cur = explode('-', $val);
  102. $time_randbegin = strtotime($time . $time_cur[0]);
  103. $time_randend = strtotime($time . $time_cur[1]);
  104. if ($cur_time > $time_randbegin && $cur_time < $time_randend) {
  105. // 正在这一场进行中
  106. $appoint_status = 2;
  107. }
  108. }
  109. if ($appoint_status != 2) {
  110. $has_get = false;
  111. foreach ($times as $key => $val) {
  112. $time_cur = explode('-', $val);
  113. $time_randbegin = strtotime($time . $time_cur[0]);
  114. if ($cur_time < $time_randbegin && !$has_get) {
  115. // 正在这一场进行中
  116. $has_get = true;
  117. $dis_time = $time_randbegin - $cur_time;
  118. }
  119. }
  120. }
  121. }
  122. $isOpen = (int)SystemConfigService::make()->getConfigByName('fudai_is_apply',1,'fudai');
  123. $info['is_open'] = $isOpen;
  124. $info['appoint_status'] = $info['open_status'] == 1 && $isOpen==1? $appoint_status : 4;
  125. $info['dis_time'] = $dis_time;
  126. return api_succ_return(['msg' => '成功', 'data' => $info]);
  127. }
  128. // 预约福袋
  129. public function beforeBuyBox(Request $request)
  130. {
  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. {
  161. try {
  162. return api_succ_return(['msg' => '成功', 'data' => $model->getLog($request)]);
  163. } catch (\Exception $e) {
  164. return api_error_return('失败');
  165. }
  166. }
  167. // 福袋商品列表
  168. public function boxOutGoodsList(Request $request)
  169. {
  170. $box_type = $request->post('box_type');
  171. $model = new ShopGoodsModel();
  172. // $boxs =
  173. $where = '';
  174. if ($box_type > 0) {
  175. $where = $box_type;
  176. } else {
  177. $where = '10,20,30,40';
  178. }
  179. $list = $model->where('box_type', 'in', $where)
  180. ->withAttr('box_img', function ($val, $data) {
  181. return getWebUrl() . '.' . $val;
  182. })
  183. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  184. ->order('box_type', 'desc')
  185. ->paginate($request->post('limit'))
  186. ->toArray();
  187. return api_succ_return(['msg' => '成功', 'data' => $list['data']]);
  188. }
  189. // 福袋商品列表顶部banner
  190. public function boxOutBanner(Request $request)
  191. {
  192. $model = new ShopGoodsModel();
  193. $list = $model->where('box_type', 'in', [30, 40])
  194. ->withAttr('box_img', function ($val, $data) {
  195. return getWebUrl() . '.' . $val;
  196. })
  197. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  198. ->order('box_type', 'desc')
  199. ->paginate(15)
  200. ->toArray();
  201. return api_succ_return(['msg' => '成功', 'data' => $list['data']]);
  202. }
  203. // 拆袋
  204. public function openBoxOnline(Request $request)
  205. {
  206. return api_error_return('已拆开');
  207. $model = new BoxMidHandleModel();
  208. $list = [];
  209. Db::startTrans();
  210. try {
  211. $list = $model->openBoxOnline($request->uid, $request->post(), $request->user_info);
  212. Db::commit();
  213. } catch (\Exception $e) {
  214. Db::rollback();
  215. return api_error_return($e->getMessage());
  216. }
  217. return api_succ_return(['msg' => '成功', 'data' => $list]);
  218. }
  219. public function boxHandleList(Request $request)
  220. {
  221. $type = $request->post('type');
  222. $keywords = trim($request->post('keywords'));
  223. $box_type = $request->post('box_type');
  224. if (!in_array($type, [1, 2])) {
  225. return api_error_return('订单类型参数错误');
  226. }
  227. $where = array();
  228. if (!empty($keywords)) {
  229. $where = [['r.h_sn', 'LIKE', '%' . $keywords . '%']];
  230. }
  231. if (!empty($box_type)) {
  232. if (!in_array($box_type, [10, 20, 30, 40])) {
  233. sr_throw('盒子类型参数错误');
  234. }
  235. }
  236. $where = [];
  237. $where[] = ['r.is_delete', '=', 2]; // 修改 by wes 2023-02-19
  238. $where[] = ['r.status', '=', $type];
  239. $where[] = ['r.uid', '=', $request->uid];
  240. if (!empty($box_type)) {
  241. $where[] = ['r.box_type', '=', $box_type];
  242. }
  243. $list = Db::name('box_handle')
  244. ->alias('r')
  245. ->where($where)
  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. {
  268. $data = [];
  269. $model = new BoxHandleModel();
  270. $data['un_handle'] = [
  271. '10' => $model->where('box_type', 10)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  272. '20' => $model->where('box_type', 20)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  273. '30' => $model->where('box_type', 30)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count(),
  274. '40' => $model->where('box_type', 40)->where('uid', $request->uid)->where('status', 1)->where('is_delete', 2)->count()
  275. ];
  276. $data['handled'] = [
  277. '10' => $model->where('box_type', 10)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  278. '20' => $model->where('box_type', 20)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  279. '30' => $model->where('box_type', 30)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count(),
  280. '40' => $model->where('box_type', 40)->where('uid', $request->uid)->where('status', 2)->where('is_delete', 2)->count()
  281. ];
  282. return api_succ_return(['msg' => '成功', 'data' => $data]);
  283. }
  284. public function boxGoodsSurePost(Request $request)
  285. {
  286. $model = new ShopOrderModel();
  287. Db::startTrans();
  288. try {
  289. $model->boxGoodsSurePost($request->uid, $request->post());
  290. Db::commit();
  291. } catch (\Exception $e) {
  292. Db::rollback();
  293. return api_error_return($e->getMessage());
  294. }
  295. return api_succ_return('回收成功');
  296. }
  297. // 一键回收
  298. public function boxGoodsReBuy(Request $request)
  299. {
  300. $model = new ShopOrderModel();
  301. $user_info = $request->user_info;
  302. $userId = isset($user_info['id']) ? $user_info['id'] : 0;
  303. $cacheKey = "caches:box:rebuy:u_{$userId}";
  304. $redis = new Redis();
  305. if ($redis->get($cacheKey)) {
  306. return api_succ_return('请不要频繁操作');
  307. }
  308. $redis->set($cacheKey, ['uid' => $request->uid, 'data' => $request->post()], rand(2, 3));
  309. Db::startTrans();
  310. try {
  311. $res = $model->boxGoodsReBuy($request->uid, $request->post(), $request->user_info);
  312. Db::commit();
  313. } catch (\Exception $e) {
  314. Db::rollback();
  315. $redis->del($cacheKey);
  316. return api_error_return($e->getMessage());
  317. }
  318. $redis->del($cacheKey);
  319. if ($res) {
  320. return api_succ_return(['msg' => '成功', 'data' => $res]);
  321. }
  322. return api_succ_return('成功');
  323. }
  324. }