MagicMarket.php 20 KB

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