MagicCase.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_cash')->find($this->uid);
  98. Db::table('store_member')->where('id',$this->uid)->update(['crystal_cash'=>bcadd($member_info['crystal_cash'],$total_crystal,2)]);
  99. Db::table('magic_resolve')->insertAll($log_data);// 分解日志
  100. // 余额日志
  101. crystal_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. $json_data = json_decode($json_str,true);
  187. $cl_data = array_column($json_data,null,'id');
  188. $all_ids = array_column($json_data,'id'); // 所要购买的记录id
  189. $pro_info = []; // 商品详情
  190. // 验证魔玩柜数量
  191. $total_num = 0;
  192. $magic_case = Db::table('magic_case')->field('id,goods_id,num,crystal,case_name,case_cover,create_at')->where('id','in',$all_ids)->select();
  193. $is_ck = true;
  194. foreach ($magic_case as &$mv) {
  195. if($mv['num'] < $cl_data[$mv['id']]['num'])$is_ck=false;// 购买数量大于剩与数量
  196. $pro_info[] = [
  197. 'magic_id' => $mv['id'],
  198. 'goods_id' => $mv['goods_id'],
  199. 'num' => $cl_data[$mv['id']]['num'],
  200. 'case_name' => $mv['case_name'],
  201. 'case_cover'=> $mv['case_cover'],
  202. 'crystal' => $mv['crystal'],
  203. 'draw_time' => $mv['create_at'],
  204. ];
  205. $total_num += $cl_data[$mv['id']]['num'];
  206. }
  207. if(!$is_ck) $this->error('魔玩柜数量不足');
  208. $freight = get_freight($total_num); // 运费
  209. $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
  210. $order_int =[
  211. 'uid'=>$this->uid,
  212. 'order_no' => get_order_sn(),
  213. 'price_total' => $price_total,
  214. 'goods_num' => $total_num,
  215. 'create_at' => date('Y-m-d H:i:s'),
  216. 'pro_info' => json_encode($pro_info),
  217. 'coupon_id' => $coupon_id,
  218. 'address_id' => $add_id,
  219. 'freight' =>$freight['all_freight'],
  220. ];
  221. Db::startTrans();
  222. $msg= '';$com=true;
  223. try{
  224. // 扣减魔玩柜库存
  225. foreach ($pro_info as $pv) {
  226. Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setDec('num',$pv['num']);
  227. }
  228. if($coupon_id) Db::table('user_coupon_list')->where(['id'=>$coupon_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
  229. // 生成订单
  230. Db::table('magic_order')->insert($order_int);
  231. $order_id = Db::getLastInsID();
  232. Db::commit();
  233. }catch (\Exception $e){
  234. Db::rollback();
  235. $com=false;
  236. $msg = $e->getMessage();
  237. }
  238. if($com){
  239. $this->success('ok',['order_id'=>$order_id]);
  240. }else{
  241. $this->error($msg);
  242. }
  243. }
  244. /**
  245. * @title 取消订单
  246. * @desc 取消订单
  247. * @author qc
  248. * @url /api/Magic_case/cancelOrder
  249. * @method POST
  250. * @header name:Authorization require:1 desc:Token
  251. * @param name:id type:int require:1 default:-- desc:订单id
  252. */
  253. public function cancelOrder()
  254. {
  255. $order_id = input('post.id');
  256. $order_detail = Db::table('magic_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  257. if(empty($order_detail)) $this->error('订单不存在');
  258. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  259. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  260. Db::startTrans();
  261. try {
  262. //恢复库存
  263. $pro_info = json_decode($order_detail['pro_info'],true);
  264. foreach ($pro_info as $pv) {
  265. Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setInc('num',$pv['num']);
  266. }
  267. if($order_detail['coupon_id']) Db::table('user_coupon_list')->where('id',$order_detail['coupon_id'])->update(['status'=>1,'used_time'=>'']);
  268. // 取消订单
  269. Db::table('magic_order')
  270. ->where(['id'=>$order_id])
  271. ->update(['cancel_state'=>1,'status'=>9]);
  272. Db::commit();
  273. }catch (\Exception $e){
  274. Db::rollback();
  275. }
  276. $this->success('取消成功');
  277. }
  278. /**
  279. * @title 获取订单详情
  280. * @desc 获取订单详情
  281. * @author qc
  282. * @url /api/Magic_case/getOrderDetail
  283. * @method GET
  284. * @header name:Authorization require:1 desc:Token
  285. * @param name:id type:int require:1 default:-- desc:订单id
  286. * @return name:order_no type:string default:-- desc:订单号
  287. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  288. * @return name:price_total type:float default:-- desc:订单金额
  289. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  290. * @return name:pay_no type:string default:-- desc:支付单号
  291. * @return name:create_at type:string default:-- desc:订单创建时间
  292. * @return name:pay_time type:string default:-- desc:支付时间
  293. * @return name:cancel_state type:int default:-- desc:取消状态
  294. * @return name:pro_info type:array default:-- desc:商品详情
  295. * @return name:pro_info.num type:int default:-- desc:商品数量
  296. * @return name:pro_info.case_name type:int default:-- desc:名称
  297. * @return name:pro_info.case_cover type:int default:-- desc:图片
  298. * @return name:pro_info.draw_time type:int default:-- desc:抽中时间
  299. * @return name:express_company_title type:string default:-- desc:物流公司
  300. * @return name:express_send_no type:string default:-- desc:物流单号
  301. * @return name:express_send_at type:string default:-- desc:发货时间
  302. * @return name:express_company_id type:string default:-- desc:物流公司id
  303. * @return name:address_area type:string default:0 desc:收货地区(精确到县区)
  304. * @return name:address_detail type:string default:0 desc:详细地址
  305. * @return name:address_user type:string default:0 desc:收货人姓名
  306. * @return name:address_tel type:string default:0 desc:收货人电话
  307. *
  308. */
  309. public function getOrderDetail()
  310. {
  311. $order_id = input('id',0);
  312. $detail = Db::table('magic_order')->find($order_id);
  313. $add_info = Db::table('delivery_address')->find($detail['address_id']);
  314. $detail['address_area'] = $add_info['mer_name'];
  315. $detail['address_detail'] = $add_info['detail'];
  316. $detail['address_user'] = $add_info['name'];
  317. $detail['address_tel'] = $add_info['phone'];
  318. $this->success('获取成功',$detail);
  319. }
  320. /**
  321. * @title 去支付
  322. * @desc 去支付
  323. * @author qc
  324. * @url /api/Magic_case/payOrder
  325. * @method POST
  326. * @header name:Authorization require:1 desc:Token
  327. * @param name:o_id type:int require:1 default:-- desc:订单id
  328. * @return name:config type:array default:0 desc:支付配置
  329. * @return name:config.appId type:string default:-- desc:公众账号ID
  330. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  331. * @return name:config.signType type:string default:-- desc:签名类型
  332. * @return name:config.paySign type:string default:-- desc:签名
  333. * @return name:config.timestamp type:string default:-- desc:时间戳
  334. */
  335. public function payOrder()
  336. {
  337. $o_id = input('post.o_id');
  338. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  339. if(!$user_info['openid']) $this->error('请先完善用户信息');
  340. $order_info = Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
  341. if(empty($order_info)) $this->error('订单不存在');
  342. if($order_info['cancel_state'] != 0) $this->error('订单已取消');
  343. if($order_info['status'] != 0) $this->error('订单已支付');
  344. if($order_info['price_total'] < 0) $this->error('订单金额错误');
  345. $config = false;$msg= '';
  346. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  347. if($order_info['price_total'] == 0 ) {
  348. Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])
  349. ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>2,'status'=>1,'pay_no'=>$pay_no]);
  350. $this->success('支付成功',['pay_type'=>2,'config' => '']);
  351. }else{
  352. $notify_url = $this->request->root(true) . '/api/Pay/magicOrderNotify';
  353. Db::startTrans();
  354. try{
  355. // 更新订单
  356. $price_total = $order_info['price_total'];
  357. Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
  358. $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
  359. if($config){
  360. Db::commit();
  361. }else{
  362. Db::rollback();
  363. }
  364. }catch (\Exception $e){
  365. Db::rollback();
  366. $msg = $e->getMessage();
  367. }
  368. if($config){
  369. $this->success('ok',['pay_type'=>1,'config'=>$config]);
  370. }else{
  371. $this->error($msg);
  372. }
  373. }
  374. }
  375. /**
  376. * @title 获取订单列表
  377. * @desc 获取订单列表
  378. * @author qc
  379. * @url /api/Magic_case/getMyOrderList
  380. * @method GET
  381. * @header name:Authorization require:1 desc:Token
  382. * @return name:id type:string default:-- desc:订单id
  383. * @return name:order_no type:string default:-- desc:订单号
  384. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  385. * @return name:price_total type:float default:-- desc:订单金额
  386. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  387. * @return name:pay_no type:string default:-- desc:支付单号
  388. * @return name:create_at type:string default:-- desc:订单创建时间
  389. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信,2余额)
  390. * @return name:pay_time type:string default:-- desc:支付时间
  391. * @return name:cancel_state type:int default:-- desc:取消状态
  392. * @return name:pro_info type:array default:-- desc:商品详情
  393. * @return name:pro_info.num type:int default:-- desc:商品数量
  394. * @return name:pro_info.case_name type:int default:-- desc:名称
  395. * @return name:pro_info.case_cover type:int default:-- desc:图片
  396. */
  397. public function getMyOrderList()
  398. {
  399. $status = input('status',-1);
  400. $where=[];
  401. if($status > -1) $where['o.status'] = $status;
  402. $where['o.uid'] = $this->uid;
  403. $where['o.is_deleted'] = 0;
  404. $where['o.cancel_state'] = 0;
  405. $where['o.is_deleted'] = 0;
  406. $list = Db::table('magic_order o')
  407. ->field('o.id,o.order_no,o.pay_state,pay_type,pay_time,o.create_at,price_total,status,pro_info')
  408. ->where($where)
  409. ->order('o.id desc')
  410. ->limit($this->off_set,$this->page_num)
  411. ->select();
  412. $this->success('获取成功',['list'=>$list]);
  413. }
  414. /**
  415. * @title 删除订单
  416. * @desc 删除订单
  417. * @author qc
  418. * @url /api/Magic_case/delOrder
  419. * @method POST
  420. * @header name:Authorization require:1 desc:Token
  421. * @param name:id type:int require:1 default:1 desc:订单id
  422. */
  423. public function delOrder()
  424. {
  425. $order_id = input('post.id');
  426. $order_info = Db::table('magic_order')
  427. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  428. ->find();
  429. if(empty($order_info)) $this->error('订单不存在');
  430. Db::table('magic_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  431. $this->success('删除成功');
  432. }
  433. /**
  434. * @title 确认收货
  435. * @desc 确认收货
  436. * @author qc
  437. * @url /api/Magic_case/confirmReceipt
  438. * @method POST
  439. * @header name:Authorization require:1 desc:Token
  440. * @param name:id type:int require:1 default:1 desc:订单id
  441. */
  442. public function confirmReceipt(){
  443. $order_info = Db::table('magic_order')
  444. ->where(['uid'=>$this->uid,'id'=>input('post.id')])
  445. ->find();
  446. if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
  447. Db::table('magic_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
  448. $this->success('收货成功');
  449. }
  450. }