MagicCase.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. /**
  6. * @title 模玩柜
  7. * @controller MagicCase
  8. * @group base
  9. */
  10. class MagicCase extends Base
  11. {
  12. public function initialize()
  13. {
  14. parent::initialize();
  15. parent::check_login();
  16. //resolve_magic($this->uid);
  17. }
  18. /**
  19. * @title 获取模玩柜列表
  20. * @desc 获取模玩柜列表
  21. * @author qc
  22. * @url /api/Magic_case/getMagicCaseList
  23. * @method GET
  24. * @header name:Authorization require:1 desc:Token
  25. * @param name:page type:int : default:1 desc:页数
  26. * @param name:page_num type:int : default:20 desc:每页数
  27. * @return name:list type:array default:-- desc:数据列表
  28. * @return name:list.order_id type:int default:0 desc:关联的订单id
  29. * @return name:list.create_at type:string default:0 desc:抽中时间
  30. * @return name:list.case_name type:string default:0 desc:名称
  31. * @return name:list.case_cover type:string default:0 desc:封面
  32. * @return name:list.num type:string default:0 desc:数量
  33. * @return name:list.crystal type:float default:0.00 desc:每个明信片价值
  34. * @return name:list.past_at type:string default:0.00 desc:过期时间
  35. * @return name:will_past type:int default:-- desc:10分钟内将要到期的记录
  36. */
  37. public function getMagicCaseList()
  38. {
  39. $will_past = Db::table('magic_case')
  40. ->where('user_id','=',$this->uid)
  41. ->where('num','>',0)
  42. ->where('past_int','<',time()+600)
  43. ->count();
  44. $list = Db::table('magic_case')
  45. ->field('id,order_id,create_at,case_name,case_cover,num,crystal,past_at')
  46. ->where('user_id','=',$this->uid)
  47. ->where('num','>',0)
  48. ->order('id desc')
  49. ->limit($this->off_set,$this->page_num)
  50. ->select();
  51. $this->success('ok',['list'=>$list,'will_past'=>$will_past]);
  52. }
  53. /**
  54. * @title 分解模玩柜
  55. * @desc 分解模玩柜
  56. * @author qc
  57. * @url /api/Magic_case/resolveMagicCase
  58. * @method POST
  59. * @header name:Authorization require:1 desc:Token
  60. * @param name:json_str type:string : default:1 desc:分解的json串[{'id':'';'num':'数量'}] [{"id":11,"num":1}]
  61. * @return name:total_crystal type:float default:0.00 desc:分级模玩柜获得的明信片
  62. */
  63. public function resolveMagicCase()
  64. {
  65. $json_str =input('post.json_str');
  66. $json_data = json_decode($json_str,true);
  67. $log_data =[];
  68. $total_crystal = 0;
  69. $error = false;
  70. Db::startTrans();
  71. foreach ($json_data as $val) {
  72. $magic_case = Db::table('magic_case')
  73. ->field('id,num,crystal')
  74. ->where(['user_id'=>$this->uid,'id'=>$val['id']])
  75. ->find();
  76. if(empty($magic_case) || $magic_case['num'] < $val['num']){
  77. $error = true;break;
  78. }
  79. // 扣减模玩柜的数量
  80. Db::table('magic_case')->where('id',$val['id'])->setDec('num',$val['num']);
  81. // 扣减模玩柜变更记录
  82. $crystal = bcmul($val['num'],$magic_case['crystal'],2);
  83. $log_data[]= [
  84. 'user_id' => $this->uid,
  85. 'create_at' => date('Y-m-d H:i:s'),
  86. 'num' => $val['num'],
  87. 'case_id' => $val['id'],
  88. 'crystal' =>$crystal ,
  89. ];
  90. $total_crystal = bcadd($total_crystal,$crystal,2);
  91. }
  92. if($error){
  93. Db::rollback();
  94. $this->error('分解数据有误');
  95. }
  96. //更新明信片
  97. $member_info = Db::table('store_member')->field('id,crystal,crystal_cash')->find($this->uid);
  98. Db::table('store_member')->where('id',$this->uid)->update(['crystal'=>bcadd($member_info['crystal'],$total_crystal,2)]);
  99. Db::table('magic_resolve')->insertAll($log_data);// 分解日志
  100. // 余额日志
  101. balance_log($this->uid,$total_crystal,'模玩柜分解',3);
  102. Db::commit();
  103. $this->success('ok',['total_crystal'=>$total_crystal]);
  104. }
  105. /**
  106. * @title 订单确认
  107. * @desc 订单确认
  108. * @author qc
  109. * @url /api/Magic_case/orderConfirm
  110. * @method GET
  111. * @header name:Authorization require:1 desc:Token
  112. * @param name:json_str type:string : default:1 desc:选中的json串[{'id':'';'num':'数量'}]
  113. * @return name:add_info type:array require:0 default:-- desc:默认地址
  114. * @return name:add_info.id type:int default:1 desc:收货地址记录id
  115. * @return name:add_info.pro_id type:int default:-- desc:省id
  116. * @return name:add_info.city_id type:int default:-- desc:市id
  117. * @return name:add_info.county_id type:int default:-- desc:县区id
  118. * @return name:add_info.pro_name type:string default:-- desc:省名
  119. * @return name:add_info.city_name type:string default:-- desc:市名
  120. * @return name:add_info.county_name type:string default:-- desc:县区名
  121. * @return name:add_info.detail type:string default:-- desc:详细地址
  122. * @return name:add_info.phone type:string default:-- desc:联系电话
  123. * @return name:add_info.name type:string default:-- desc:联系人
  124. * @return name:goods_data type:array require:0 default:-- desc:商品详情
  125. * @return name:goods_data.id type:array require:0 default:-- desc:模玩柜记录id
  126. * @return name:goods_data.num type:string require:0 default:-- desc:剩余数量
  127. * @return name:goods_data.create_at type:string require:0 default:-- desc:时间
  128. * @return name:goods_data.buy_num type:string require:0 default:-- desc:剩余购买
  129. * @return name:goods_data.case_name type:string require:0 default:-- desc:名称
  130. * @return name:goods_data.case_cover type:string require:0 default:-- desc:图片
  131. * @return name:freight_data type:array default:-- desc:运费详情
  132. * @return name:freight_data.all_freight type:int default:-- desc:总运费
  133. * @return name:freight_data.first_freight type:int default:-- desc:首件运费
  134. * @return name:freight_data.later_freight type:int default:-- desc:续件运费
  135. * @return name:coupon_list type:array default:-- desc:可用运费券
  136. * @return name:coupon_list.id type:int default:-- desc:券id
  137. * @return name:coupon_list.past_at type:int default:-- desc:过期时间
  138. */
  139. public function orderConfirm(){
  140. $json_str =input('get.json_str');
  141. $json_data = json_decode($json_str,true);
  142. $all_ids = array_column($json_data,'id'); // 所要购买的记录id
  143. $total_num = array_sum(array_column($json_data,'num'));//购买数量
  144. $freight_data = get_freight($total_num);// 计算运费
  145. $cl_data = array_column($json_data,null,'id');
  146. // 验证模玩柜数量
  147. $magic_case = Db::table('magic_case')->field('id,num,case_name,case_cover,create_at')->where('id','in',$all_ids)->select();
  148. $is_ck = true;
  149. foreach ($magic_case as &$mv) {
  150. if($mv['num'] < $cl_data[$mv['id']]['num'])$is_ck=false;// 购买数量大于剩与数量
  151. $mv['buy_num'] = $cl_data[$mv['id']]['num'];
  152. }
  153. if(!$is_ck) $this->error('模玩柜数量不足');
  154. // 默认收货地址
  155. $add_info = Db::table('delivery_address')->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
  156. // 运费券
  157. $coupon_list = Db::table('user_coupon_list')
  158. ->field('id,past_at')
  159. ->where('user_id','=',$this->uid)
  160. ->where('status','=',1)
  161. ->where('past_at','> time',date('Y-m-d H:i:s'))
  162. ->order('id desc')
  163. ->select();
  164. $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$magic_case,'freight_data'=>$freight_data,'coupon_list'=>$coupon_list]);
  165. }
  166. /**
  167. * @title 生成订单
  168. * @desc 生成订单
  169. * @author qc
  170. * @url /api/Magic_case/createOrder
  171. * @method POST
  172. * @header name:Authorization require:1 desc:Token
  173. * @param name:json_str type:string : default:1 desc:选中的json串[{'id':'';'num':'数量'}]
  174. * @param name:price_total type:float require:1 default:-- desc:支付金额
  175. * @param name:add_id type:int require:1 default:0 desc:收货地址id
  176. * @param name:coupon_id type:int require:0 default:0 desc:运费券的id
  177. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  178. */
  179. public function createOrder()
  180. {
  181. $json_str = input('post.json_str','');
  182. $add_id = input('post.add_id',0);
  183. $price_total = input("post.price_total",0);
  184. $coupon_id = input('post.coupon_id',0);
  185. if(!$add_id) $this->error('请选择收货地址');
  186. $send_info = Db::table('delivery_address')->field('pro_name,city_name,county_name,mer_name,detail,phone,name')->find($add_id);
  187. $json_data = json_decode($json_str,true);
  188. $cl_data = array_column($json_data,null,'id');
  189. $all_ids = array_column($json_data,'id'); // 所要购买的记录id
  190. $pro_info = []; // 商品详情
  191. // 验证模玩柜数量
  192. $total_num = 0;
  193. $magic_case = Db::table('magic_case')->field('id,goods_id,num,crystal,case_name,case_cover,create_at')->where('id','in',$all_ids)->select();
  194. $is_ck = true;
  195. foreach ($magic_case as &$mv) {
  196. if($mv['num'] < $cl_data[$mv['id']]['num'])$is_ck=false;// 购买数量大于剩与数量
  197. $pro_info[] = [
  198. 'magic_id' => $mv['id'],
  199. 'goods_id' => $mv['goods_id'],
  200. 'num' => $cl_data[$mv['id']]['num'],
  201. 'case_name' => $mv['case_name'],
  202. 'case_cover'=> $mv['case_cover'],
  203. 'crystal' => $mv['crystal'],
  204. 'draw_time' => $mv['create_at'],
  205. ];
  206. $total_num += $cl_data[$mv['id']]['num'];
  207. }
  208. if(!$is_ck) $this->error('模玩柜数量不足');
  209. $freight = get_freight($total_num); // 运费
  210. $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
  211. $order_int =[
  212. 'uid'=>$this->uid,
  213. 'order_no' => get_order_sn(),
  214. 'price_total' => $price_total,
  215. 'goods_num' => $total_num,
  216. 'create_at' => date('Y-m-d H:i:s'),
  217. 'pro_info' => json_encode($pro_info),
  218. 'coupon_id' => $coupon_id,
  219. 'address_id' => $add_id,
  220. 'freight' =>$freight['all_freight'],
  221. 'send_info' =>json_encode($send_info),
  222. ];
  223. Db::startTrans();
  224. $msg= '';$com=true;
  225. try{
  226. // 扣减模玩柜库存
  227. foreach ($pro_info as $pv) {
  228. Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setDec('num',$pv['num']);
  229. }
  230. if($coupon_id) Db::table('user_coupon_list')->where(['id'=>$coupon_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  231. // 生成订单
  232. Db::table('magic_order')->insert($order_int);
  233. $order_id = Db::getLastInsID();
  234. Db::commit();
  235. }catch (\Exception $e){
  236. Db::rollback();
  237. $com=false;
  238. $msg = $e->getMessage();
  239. }
  240. if($com){
  241. $this->success('ok',['order_id'=>$order_id]);
  242. }else{
  243. $this->error($msg);
  244. }
  245. }
  246. /**
  247. * @title 取消订单
  248. * @desc 取消订单
  249. * @author qc
  250. * @url /api/Magic_case/cancelOrder
  251. * @method POST
  252. * @header name:Authorization require:1 desc:Token
  253. * @param name:id type:int require:1 default:-- desc:订单id
  254. */
  255. public function cancelOrder()
  256. {
  257. $order_id = input('post.id');
  258. $order_detail = Db::table('magic_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  259. if(empty($order_detail)) $this->error('订单不存在');
  260. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  261. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  262. Db::startTrans();
  263. try {
  264. //恢复库存
  265. $pro_info = json_decode($order_detail['pro_info'],true);
  266. foreach ($pro_info as $pv) {
  267. Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setInc('num',$pv['num']);
  268. }
  269. if($order_detail['coupon_id']) Db::table('user_coupon_list')->where('id',$order_detail['coupon_id'])->update(['status'=>1,'used_time'=>'']);
  270. // 取消订单
  271. Db::table('magic_order')
  272. ->where(['id'=>$order_id])
  273. ->update(['cancel_state'=>1,'status'=>9]);
  274. Db::commit();
  275. }catch (\Exception $e){
  276. Db::rollback();
  277. }
  278. $this->success('取消成功');
  279. }
  280. /**
  281. * @title 获取订单详情
  282. * @desc 获取订单详情
  283. * @author qc
  284. * @url /api/Magic_case/getOrderDetail
  285. * @method GET
  286. * @header name:Authorization require:1 desc:Token
  287. * @param name:id type:int require:1 default:-- desc:订单id
  288. * @return name:order_no type:string default:-- desc:订单号
  289. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  290. * @return name:price_total type:float default:-- desc:订单金额
  291. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  292. * @return name:pay_no type:string default:-- desc:支付单号
  293. * @return name:create_at type:string default:-- desc:订单创建时间
  294. * @return name:pay_time type:string default:-- desc:支付时间
  295. * @return name:cancel_state type:int default:-- desc:取消状态
  296. * @return name:pro_info type:array default:-- desc:商品详情
  297. * @return name:pro_info.num type:int default:-- desc:商品数量
  298. * @return name:pro_info.case_name type:int default:-- desc:名称
  299. * @return name:pro_info.case_cover type:int default:-- desc:图片
  300. * @return name:pro_info.draw_time type:int default:-- desc:抽中时间
  301. * @return name:express_company_title type:string default:-- desc:物流公司
  302. * @return name:express_send_no type:string default:-- desc:物流单号
  303. * @return name:express_send_at type:string default:-- desc:发货时间
  304. * @return name:express_company_id type:string default:-- desc:物流公司id
  305. * @return name:send_info type:array default:0 desc:收货详情
  306. */
  307. public function getOrderDetail()
  308. {
  309. $order_id = input('id',0);
  310. $detail = Db::table('magic_order')->find($order_id);
  311. $this->success('获取成功',$detail);
  312. }
  313. /**
  314. * @title 去支付
  315. * @desc 去支付
  316. * @author qc
  317. * @url /api/Magic_case/payOrder
  318. * @method POST
  319. * @header name:Authorization require:1 desc:Token
  320. * @param name:o_id type:int require:1 default:-- desc:订单id
  321. * @return name:config type:array default:0 desc:支付配置
  322. * @return name:config.appId type:string default:-- desc:公众账号ID
  323. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  324. * @return name:config.signType type:string default:-- desc:签名类型
  325. * @return name:config.paySign type:string default:-- desc:签名
  326. * @return name:config.timestamp type:string default:-- desc:时间戳
  327. */
  328. public function payOrder()
  329. {
  330. $o_id = input('post.o_id');
  331. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  332. if(!$user_info['openid']) $this->error('请先完善用户信息');
  333. $order_info = Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
  334. if(empty($order_info)) $this->error('订单不存在');
  335. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  336. if($order_info['status'] != 0) $this->error('订单已支付');
  337. if($order_info['price_total'] < 0) $this->error('订单金额错误');
  338. $config = false;$msg= '';
  339. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  340. if($order_info['price_total'] == 0 ) {
  341. Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])
  342. ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>2,'status'=>1,'pay_no'=>$pay_no]);
  343. $this->success('支付成功',['pay_type'=>2,'config' => '']);
  344. }else{
  345. $notify_url = $this->request->root(true) . '/api/Pay/magicOrderNotify';
  346. Db::startTrans();
  347. try{
  348. // 更新订单
  349. $price_total = $order_info['price_total'];
  350. Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
  351. $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  352. if($config){
  353. Db::commit();
  354. }else{
  355. Db::rollback();
  356. }
  357. }catch (\Exception $e){
  358. Db::rollback();
  359. $msg = $e->getMessage();
  360. }
  361. if($config){
  362. $this->success('ok',['pay_type'=>1,'config'=>$config]);
  363. }else{
  364. $this->error($msg);
  365. }
  366. }
  367. }
  368. /**
  369. * @title 获取订单列表
  370. * @desc 获取订单列表
  371. * @author qc
  372. * @url /api/Magic_case/getMyOrderList
  373. * @method GET
  374. * @header name:Authorization require:1 desc:Token
  375. * @return name:id type:string default:-- desc:订单id
  376. * @return name:order_no type:string default:-- desc:订单号
  377. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  378. * @return name:price_total type:float default:-- desc:订单金额
  379. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  380. * @return name:pay_no type:string default:-- desc:支付单号
  381. * @return name:create_at type:string default:-- desc:订单创建时间
  382. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信,2余额)
  383. * @return name:pay_time type:string default:-- desc:支付时间
  384. * @return name:cancel_state type:int default:-- desc:取消状态
  385. * @return name:pro_info type:array default:-- desc:商品详情
  386. * @return name:pro_info.num type:int default:-- desc:商品数量
  387. * @return name:pro_info.case_name type:int default:-- desc:名称
  388. * @return name:pro_info.case_cover type:int default:-- desc:图片
  389. */
  390. public function getMyOrderList()
  391. {
  392. $status = input('status',-1);
  393. $where=[];
  394. if($status > -1) $where['o.status'] = $status;
  395. $where['o.uid'] = $this->uid;
  396. $where['o.is_deleted'] = 0;
  397. $where['o.cancel_state'] = 0;
  398. $where['o.is_deleted'] = 0;
  399. $list = Db::table('magic_order o')
  400. ->field('o.id,o.order_no,o.pay_state,pay_type,pay_time,o.create_at,price_total,status,pro_info')
  401. ->where($where)
  402. ->order('o.id desc')
  403. ->limit($this->off_set,$this->page_num)
  404. ->select();
  405. $this->success('获取成功',['list'=>$list]);
  406. }
  407. /**
  408. * @title 删除订单
  409. * @desc 删除订单
  410. * @author qc
  411. * @url /api/Magic_case/delOrder
  412. * @method POST
  413. * @header name:Authorization require:1 desc:Token
  414. * @param name:id type:int require:1 default:1 desc:订单id
  415. */
  416. public function delOrder()
  417. {
  418. $order_id = input('post.id');
  419. $order_info = Db::table('magic_order')
  420. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  421. ->find();
  422. if(empty($order_info)) $this->error('订单不存在');
  423. Db::table('magic_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  424. $this->success('删除成功');
  425. }
  426. /**
  427. * @title 确认收货
  428. * @desc 确认收货
  429. * @author qc
  430. * @url /api/Magic_case/confirmReceipt
  431. * @method POST
  432. * @header name:Authorization require:1 desc:Token
  433. * @param name:id type:int require:1 default:1 desc:订单id
  434. */
  435. public function confirmReceipt(){
  436. $order_info = Db::table('magic_order')
  437. ->where(['uid'=>$this->uid,'id'=>input('post.id')])
  438. ->find();
  439. if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
  440. Db::table('magic_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
  441. $this->success('收货成功');
  442. }
  443. }