Box.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\facade\Db;
  20. use think\Request;
  21. class Box
  22. {
  23. protected $model = null;
  24. public function __construct(ChatMessageModel $model)
  25. {
  26. $this->model = $model;
  27. }
  28. // 跑马灯效果
  29. public function boxDownMessageList(Request $request){
  30. $messages = [];
  31. $boxs = [
  32. 10=>'普通',
  33. 20=>'稀有',
  34. 30=>'史诗',
  35. 40=>'传说',
  36. ];
  37. $list = Db::name('box_mid_handle')
  38. ->alias('m')
  39. ->where('m.box_type', '>', 10)
  40. ->leftJoin('user u', 'u.id = m.uid')
  41. ->limit(20)
  42. ->withAttr('box_type', function ($val, $data) use ($boxs, &$messages){
  43. // $mobile = v;
  44. $mobile = '****'.substr($data['mobile'], 7);
  45. $messages[] = '恭喜'.$mobile.'拆出'.$boxs[$data['box_type']].'福袋';
  46. })
  47. ->orderRaw("rand() , m.id DESC")
  48. ->field('m.uid,u.mobile,u.nickname,m.box_type')
  49. ->select()->toArray();
  50. return api_succ_return(['msg'=>'成功', 'data'=>$messages]);
  51. // return api_succ_return(['msg'=>'请求成功', 'data'=>[
  52. // '恭喜****9567拆出稀有附带',
  53. // '恭喜****9567拆出稀有附带',
  54. // '恭喜****9567拆出稀有附带',
  55. // '恭喜****9567拆出稀有附带',
  56. // '恭喜****9567拆出稀有附带',
  57. // '恭喜****9567拆出稀有附带'
  58. // ]]);
  59. }
  60. // 福袋 泡泡商品
  61. public function boxRandGoodsList(){
  62. $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();
  63. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  64. }
  65. // 当前最新一期福袋预约信息
  66. public function getNewBoxInfo(){
  67. $id = Db::name('box')->where('status', 1)->max('id');
  68. if (empty($id)){
  69. return api_error_return(['msg'=>'暂未开启', 'code'=>303]);
  70. }
  71. $info = Db::name('box')->where('id', $id)->find();
  72. $cur_time = time();
  73. $time = sr_getcurtime($cur_time, 'Y-m-d ');
  74. $times = explode('|', $info['time_set']);
  75. $appoint_status = 1;
  76. // 1 还未开始 2 进行中 3已结束
  77. $dis_time = 0;
  78. $cur_times = [];
  79. // 当前时间小于 第一场 开始时间
  80. // if ($appoint_status != 1){
  81. // $begin_times = explode('-', $times[0]);
  82. // $begin_randtime = strtotime($time . $begin_times[0]);
  83. // if ($cur_time < $begin_randtime){
  84. // $appoint_status = 1;
  85. // $dis_time = $begin_randtime-$cur_time;
  86. // return 33;
  87. // }
  88. // }
  89. // 当前时间大于 最后一场结束时间
  90. if ($appoint_status != 3){
  91. $end_timesarr = explode('-', $times[count($times)-1]);
  92. $end_randtime = strtotime($time . $end_timesarr[1]);
  93. if ($cur_time > $end_randtime){
  94. $appoint_status = 3;
  95. }
  96. }
  97. // 是否拿到最近的一场未开始距离时间
  98. if ($appoint_status != 3){
  99. foreach ($times as $key=>$val){
  100. $time_cur = explode('-', $val);
  101. $time_randbegin = strtotime($time.$time_cur[0]);
  102. $time_randend = strtotime($time.$time_cur[1]);
  103. if ($cur_time > $time_randbegin && $cur_time < $time_randend){
  104. // 正在这一场进行中
  105. $appoint_status = 2;
  106. }
  107. }
  108. if ($appoint_status!= 2){
  109. $has_get = false;
  110. foreach ($times as $key=>$val){
  111. $time_cur = explode('-', $val);
  112. $time_randbegin = strtotime($time.$time_cur[0]);
  113. $time_randend = strtotime($time.$time_cur[1]);
  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. $info['appoint_status'] = $appoint_status;
  123. $info['dis_time'] = $dis_time;
  124. return api_succ_return(['msg'=>'成功', 'data'=>$info]);
  125. }
  126. // 预约福袋
  127. public function beforeBuyBox(Request $request){
  128. $model = new BoxModel();
  129. $post = $request->post();
  130. // 这是测试 上线要干掉
  131. // return api_error_return('暂未开放');
  132. Db::startTrans();
  133. try {
  134. $model->beforeBuyBox($request->uid, $post, $request->user_info);
  135. Db::commit();
  136. }catch (\Exception $e){
  137. Db::rollback();
  138. if ($e->getMessage() == '预约失败,还未开始预约、或预约已结束'){
  139. return api_error_return(
  140. ['msg'=>'预约失败,还未开始预约、或预约已结束', 'code'=>302]);
  141. }
  142. return api_error_return($e->getMessage());
  143. }
  144. return api_succ_return('成功');
  145. }
  146. // 我的预约
  147. public function boxBuyRecord(Request $request, BoxRecordModel $model){
  148. try {
  149. return api_succ_return(['msg'=>'成功', 'data'=>$model->getLog($request)]);
  150. } catch (\Exception $e) {
  151. return api_error_return('失败');
  152. }
  153. }
  154. // 福袋商品列表
  155. public function boxOutGoodsList(Request $request){
  156. $box_type = $request->post('box_type');
  157. $model = new ShopGoodsModel();
  158. // $boxs =
  159. $where = '';
  160. if ($box_type > 0){
  161. $where = $box_type;
  162. }else{
  163. $where = '10,20,30,40';
  164. }
  165. $list = $model->where('box_type','in', $where)
  166. ->withAttr('box_img', function ($val, $data){
  167. return getWebUrl().'.'.$val;
  168. })
  169. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  170. ->order('box_type', 'desc')
  171. ->paginate($request->post('limit'))
  172. ->toArray();
  173. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  174. }
  175. // 福袋商品列表顶部banner
  176. public function boxOutBanner(Request $request){
  177. $model = new ShopGoodsModel();
  178. $list = $model->where('box_type','in', [30,40])
  179. ->withAttr('box_img', function ($val, $data){
  180. return getWebUrl().'.'.$val;
  181. })
  182. ->field('goods_id,box_type,goods_img,price,goods_name,goods_sn')
  183. ->order('box_type', 'desc')
  184. ->paginate(15)
  185. ->toArray();
  186. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  187. }
  188. // 拆袋
  189. public function openBoxOnline(Request $request){
  190. return api_error_return('已拆开');
  191. $model = new BoxMidHandleModel();
  192. $list = [];
  193. Db::startTrans();
  194. try {
  195. $list = $model->openBoxOnline($request->uid, $request->post(), $request->user_info);
  196. Db::commit();
  197. }catch (\Exception $e){
  198. Db::rollback();
  199. return api_error_return($e->getMessage());
  200. }
  201. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  202. }
  203. public function boxHandleList(Request $request){
  204. // 这是测试 上线要干掉
  205. // sr_testDb($request->header('token'), 45); ;
  206. $type = $request->post('type');
  207. $keywords = trim($request->post('keywords'));
  208. $box_type = $request->post('box_type');
  209. if (!in_array($type, [1,2])){
  210. return api_error_return('参数错误');
  211. }
  212. $where = array();
  213. if (!empty($keywords)){
  214. $where = [['r.h_sn', 'LIKE', '%'.$keywords.'%']];
  215. }
  216. if (!empty($box_type)){
  217. if (!in_array($box_type, [10, 20, 30, 40])){
  218. sr_throw('参数错误');
  219. }
  220. }
  221. $where = [];
  222. $where[] = ['r.status', '=', $type];
  223. $where[] = ['r.uid', '=', $request->uid];
  224. if (!empty($box_type)){
  225. $where[] = ['r.box_type', '=', $box_type];
  226. }
  227. $list = Db::name('box_handle')
  228. ->alias('r')
  229. ->where($where)
  230. // ->leftJoin('shop_goods g', 'g.goods_id = r.goods_id')
  231. // ->withoutField('r.goods_type')
  232. // ->field('r.*,g.goods_name,g.goods_sn,g.goods_img,g.price,g.spec_name')
  233. ->field('r.*')
  234. ->paginate($request->post('limit'))->toArray();
  235. foreach ($list['data'] as $key =>&$val){
  236. $goods_info = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find();
  237. $val['goods_name'] = $goods_info['goods_name'];
  238. $val['goods_sn'] = $goods_info['goods_sn'];
  239. $val['goods_img'] = $goods_info['goods_img'];
  240. $val['goods_sn'] = $goods_info['goods_sn'];
  241. $val['price'] = $goods_info['price'];
  242. $val['spec_name'] = $goods_info['spec_name'];
  243. //
  244. if ($val['box_type'] == 10){
  245. $val['is_canrecycle'] = 0;
  246. }else{
  247. $val['is_canrecycle'] = 1;
  248. }
  249. //
  250. }
  251. return api_succ_return(['msg'=>'成功', 'data'=>$list['data']]);
  252. }
  253. public function boxHandleUnread(Request $request){
  254. $data = [];
  255. $model = new BoxHandleModel();
  256. $data['un_handle'] = [
  257. '10'=>$model->where('box_type', 10)->where('uid', $request->uid)->where('status', 1)->count(),
  258. '20'=>$model->where('box_type', 20)->where('uid', $request->uid)->where('status', 1)->count(),
  259. '30'=>$model->where('box_type', 30)->where('uid', $request->uid)->where('status', 1)->count(),
  260. '40'=>$model->where('box_type', 40)->where('uid', $request->uid)->where('status', 1)->count()
  261. ];
  262. $data['handled'] = [
  263. '10'=>$model->where('box_type', 10)->where('uid', $request->uid)->where('status', 2)->count(),
  264. '20'=>$model->where('box_type', 20)->where('uid', $request->uid)->where('status', 2)->count(),
  265. '30'=>$model->where('box_type', 30)->where('uid', $request->uid)->where('status', 2)->count(),
  266. '40'=>$model->where('box_type', 40)->where('uid', $request->uid)->where('status', 2)->count()
  267. ];
  268. return api_succ_return(['msg'=>'成功', 'data'=>$data]);
  269. }
  270. public function boxGoodsSurePost(Request $request){
  271. $model = new ShopOrderModel();
  272. Db::startTrans();
  273. try {
  274. $model->boxGoodsSurePost($request->uid, $request->post());
  275. Db::commit();
  276. }catch (\Exception $e){
  277. Db::rollback();
  278. return api_error_return($e->getMessage());
  279. }
  280. return api_succ_return('回收成功');
  281. }
  282. // 一键回收
  283. public function boxGoodsReBuy(Request $request){
  284. $model = new ShopOrderModel();
  285. Db::startTrans();
  286. try {
  287. $res = $model->boxGoodsReBuy($request->uid, $request->post(), $request->user_info);
  288. Db::commit();
  289. }catch (\Exception $e){
  290. Db::rollback();
  291. return api_error_return($e->getMessage());
  292. }
  293. if ($res){
  294. return api_succ_return(['msg'=>'成功', 'data'=>$res]);
  295. }
  296. return api_succ_return('成功');
  297. }
  298. }