OrderController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\BaseAreaModel;
  4. use App\Models\S1CartInfoModel;
  5. use App\Models\S1GoodsInfoModel;
  6. use App\Models\S1OrderGoodsModel;
  7. use App\Models\S1OrderInfoModel;
  8. use App\Models\S1OrderRefundModel;
  9. use App\Models\WxUserAddressModel;
  10. use App\Services\Base\ErrorCode;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Validator;
  16. class OrderController extends Controller
  17. {
  18. /**
  19. * @api {get} /api/order/sure 立即购买(或是购物车结算)
  20. * @apiDescription 立即购买(或是购物车结算)
  21. * @apiGroup Order
  22. * @apiParam {string} appid appid
  23. * @apiParam {int} [id] 商品id
  24. * @apiParam {int} [number] 商品数量
  25. * @apiParam {array} [cart_id] 购物车id
  26. * @apiPermission none
  27. * @apiVersion 0.1.0
  28. * @apiSuccessExample {json} Success-Response:
  29. * HTTP/1.1 200 OK
  30. {
  31. "status": true,
  32. "status_code": 0,
  33. "message": "",
  34. "data": {
  35. "goods": { 购买商品
  36. "name": "商品1",
  37. "sure_number": "12", 确认数量
  38. "attr_info": "属性", 已选属性
  39. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  40. },
  41. "address": { 默认地址
  42. "name": "hahahahaha",
  43. "tel": "134324",
  44. "area": "23423423",
  45. "address": "address",
  46. }
  47. }
  48. }
  49. * @apiErrorExample {json} Error-Response:
  50. * HTTP/1.1 400 Bad Request
  51. */
  52. public function sure(Request $request)
  53. {
  54. $user = Auth::guard('api')->user();
  55. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  56. $validator = Validator::make($request->all(),
  57. [
  58. 'appid' => 'required',
  59. ],
  60. [
  61. 'appid.required' => 'appid不存在',
  62. ]
  63. );
  64. if ($validator->fails()) {
  65. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  66. }
  67. $goods = [];
  68. $id = $request->input('id');
  69. if ($id) { //商品id存在就是在商品页立即购买
  70. $goodsinfo = S1GoodsInfoModel::find($id);
  71. \Log::info(Cache::get('attr_info'));
  72. if (Cache::get('attr_info') && Cache::get('attr_info')['user_id']==$user->id && Cache::get('attr_info')['goods_id']==$id) {
  73. $goodsinfo->attr_info = Cache::get('attr_info')['attr_info'];
  74. }else{
  75. $goodsinfo->attr_info = null;
  76. }
  77. $goodsinfo->sure_number = $request->input('number');
  78. $goods[] = $goodsinfo;
  79. }else{ //购物车购买
  80. $cart_ids = json_decode($request->input('cart_id'),true);
  81. if (is_array($cart_ids)) {
  82. foreach ($cart_ids as $value) {
  83. $car = S1CartInfoModel::find($value);
  84. $goods_info = S1GoodsInfoModel::find($car->s1_goods_id);
  85. $goods_info->sure_number = $car->goods_num;
  86. $goods_info->attr_info = $car->attr_info;
  87. $goods[] = $goods_info;
  88. }
  89. }
  90. }
  91. $address = WxUserAddressModel::where('wx_user_id',$user->id)->where('status',1)->first();
  92. return $this->api(compact('goods','address'));
  93. }
  94. /**
  95. * @api {get} /api/order/create 创建订单
  96. * @apiDescription 创建订单
  97. * @apiGroup Order
  98. * @apiParam {string} appid appid
  99. * @apiParam {int} address_id 收货地址id
  100. * @apiParam {int} [id] 商品id
  101. * @apiParam {int} [number] 商品数量
  102. * @apiParam {array} [attr] 商品属性
  103. * //购物车id列表或是商品id
  104. * @apiParam {array} [cart_id_list] 购物车id
  105. * @apiPermission none
  106. * @apiVersion 0.1.0
  107. * @apiSuccessExample {json} Success-Response:
  108. * HTTP/1.1 200 OK
  109. {
  110. "status": true,
  111. "status_code": 0,
  112. "message": "",
  113. "data": {
  114. "s1_order_id": 15
  115. }
  116. }
  117. * @apiErrorExample {json} Error-Response:
  118. * HTTP/1.1 400 Bad Request
  119. {
  120. "status": false,
  121. "status_code": 2004,
  122. "message": "数据异常",
  123. "data": null
  124. }
  125. */
  126. public function create(Request $request)
  127. {
  128. $user = Auth::guard('api')->user();
  129. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  130. $validator = Validator::make($request->all(),
  131. [
  132. 'appid' => 'required',
  133. 'address_id' => 'required',
  134. ],
  135. [
  136. 'appid.required' => 'appid不存在',
  137. 'address_id.required' => '请选择收货地址',
  138. ]
  139. );
  140. if ($validator->fails()) {
  141. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  142. }
  143. $attr = json_decode($request->input('attr'),true);
  144. $appid = $request->input('appid');
  145. $wx_address_id = $request->input('address_id');
  146. $wx_user_id = $user->id;
  147. $id = $request->input('id');
  148. $number = $request->input('number');
  149. $cart_id_list =json_decode($request->input('cart_id_list'),true);
  150. $last_id = S1OrderInfoModel::orderBy('id','desc')->first();
  151. if(empty($last_id)) {
  152. $next_id = 1;
  153. }else{
  154. $next_id = $last_id->id + 1;
  155. }
  156. $trade_no = 'WX_'.date('YmdHis').$next_id;
  157. $order_time = date('Y-m-d H:i:s');
  158. if (!empty($id)) { //商品详情处创建订单
  159. $attr_info = '';
  160. if (is_array($attr)) {
  161. foreach ($attr as $k => $v){
  162. $attr_info .= $v['attr_group_name'].':'.$v['attr_name'].',';
  163. }
  164. }
  165. $goods = S1GoodsInfoModel::find($id);
  166. $amount = $goods->price * $number;
  167. $arr = compact('appid','wx_user_id','wx_address_id','trade_no','amount','order_time');
  168. $order = S1OrderInfoModel::create($arr);
  169. $s1_order_id = $order->id;
  170. $order_info = compact('s1_order_id','s1_goods_id','number','attr_info');
  171. S1OrderGoodsModel::create($order_info);
  172. return $this->api(compact('s1_order_id'));
  173. }elseif(is_array($cart_id_list)){ //购物车创建订单(购物车id)
  174. $carts = S1CartInfoModel::whereIn('id',$cart_id_list)->with(['goods'=> function ($query) {
  175. $query->select('id','price');
  176. }])->get()->toArray();
  177. $amount = collect($carts)->sum(function ($item) {
  178. return $item['goods_num']*$item['goods']['price'];
  179. });
  180. $arr = compact('appid','wx_user_id','wx_address_id','trade_no','amount','order_time');
  181. $order = S1OrderInfoModel::create($arr);
  182. $s1_order_id = $order->id;
  183. foreach ($carts as $cart) {
  184. $order_info[] = ['s1_order_id'=>$s1_order_id,'s1_goods_id'=>$cart['s1_goods_id'],
  185. 'number'=>$cart['goods_num'],'attr_info'=>$cart['attr_info'],'created_at'=>date('Y-m-d H:i:s')
  186. ];
  187. }
  188. S1OrderGoodsModel::insert($order_info);
  189. return $this->api(compact('s1_order_id'));
  190. }else{ //参数错误
  191. return $this->error(ErrorCode::CAT_ERROR);
  192. }
  193. }
  194. /**
  195. * @api {post} /api/order/pay 支付
  196. * @apiDescription 支付
  197. * @apiGroup Order
  198. * @apiParam {int} id 订单id
  199. * @apiPermission none
  200. * @apiVersion 0.1.0
  201. * @apiSuccessExample {json} Success-Response:
  202. * HTTP/1.1 200 OK
  203. {
  204. "status": true,
  205. "status_code": 0,
  206. "message": "",
  207. "data": ""
  208. }
  209. * @apiErrorExample {json} Error-Response:
  210. * HTTP/1.1 400 Bad Request
  211. {
  212. "status": false,
  213. "status_code": 2006,
  214. "message": "操作失败",
  215. "data": null
  216. }
  217. */
  218. public function pay(Request $request)
  219. {
  220. $user = Auth::guard('api')->user();
  221. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  222. $validator = Validator::make($request->all(),
  223. [
  224. 'id' => 'required',
  225. ],
  226. [
  227. 'id.required' => '订单不存在',
  228. ]
  229. );
  230. if ($validator->fails()) {
  231. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  232. }
  233. $id = $request->input('id');
  234. $order = S1OrderInfoModel::find($id);
  235. if (empty($order)) return $this->error(ErrorCode::ORDER_NOT_EXIST);
  236. // 支付逻辑
  237. $order->status = 2;
  238. $order->pay_time = date('Y-m-d H:i:s');
  239. $ok = $order->save();
  240. if ($ok) return $this->api('');
  241. return $this->error(ErrorCode::OP_ERROR);
  242. }
  243. /**
  244. * @api {post} /api/order/refund 申请售后
  245. * @apiDescription 申请售后
  246. * @apiGroup Order
  247. * @apiParam {int} order_id 订单id
  248. * @apiParam {int} refund_type 售后类型
  249. * @apiParam {int} [refund_price] 价格
  250. * @apiParam {string} refund_desc 原因
  251. * @apiParam {array} pic_list 凭证
  252. * @apiPermission none
  253. * @apiVersion 0.1.0
  254. * @apiSuccessExample {json} Success-Response:
  255. * HTTP/1.1 200 OK
  256. {
  257. "status": true,
  258. "status_code": 0,
  259. "message": "",
  260. "data": ""
  261. }
  262. * @apiErrorExample {json} Error-Response:
  263. * HTTP/1.1 400 Bad Request
  264. {
  265. "status": false,
  266. "status_code": 2006,
  267. "message": "操作失败",
  268. "data": null
  269. }
  270. */
  271. public function refund(Request $request)
  272. {
  273. $user = Auth::guard('api')->user();
  274. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  275. $validator = Validator::make($request->all(),
  276. [
  277. 'order_id' => 'required',
  278. 'refund_desc' => 'required',
  279. 'pic_list' => 'required',
  280. 'refund_type' => 'required',
  281. ],
  282. [
  283. 'order_id.required' => '订单不存在',
  284. 'refund_desc.required' => '请填写原因',
  285. 'pic_list.required' => '请上传凭证',
  286. 'refund_type.required' => '请选择售后类型',
  287. ]
  288. );
  289. if ($validator->fails()) {
  290. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  291. }
  292. $order_id = $request->input('order_id');
  293. $refund_desc = $request->input('refund_desc');
  294. $refund_type = $request->input('refund_type');
  295. $pic_list = json_encode($request->input('pic_list'));
  296. $order = S1OrderInfoModel::find($order_id);
  297. if(empty($order)) return $this->error(ErrorCode::ORDER_NOT_EXIST);
  298. $arr = compact('order_id','refund_desc','refund_type','pic_list');
  299. $ok = S1OrderRefundModel::firstOrCreate($arr);
  300. if ($ok) {
  301. $order->status = 4;
  302. $order->apply_time = date('Y-m-d H:i:s');
  303. $order->save();
  304. return $this->api('');
  305. }else{
  306. return $this->error(ErrorCode::OP_ERROR);
  307. }
  308. }
  309. /**
  310. * @api {get} /api/order/show 订单详情
  311. * @apiDescription 订单详情
  312. * @apiGroup Order
  313. * @apiParam {int} id 订单id
  314. * @apiPermission none
  315. * @apiVersion 0.1.0
  316. * @apiSuccessExample {json} Success-Response:
  317. * HTTP/1.1 200 OK
  318. {
  319. "status": true,
  320. "status_code": 0,
  321. "message": "",
  322. "data": {
  323. "user_name": "hahahahaha",
  324. "address": "中国,浙江省,杭州市,江干区三泰魔方123",
  325. "trade_no": "123123",
  326. "order_time": "2017-10-13 09:55:00",
  327. "amount": "10000",
  328. "goods_number": 2
  329. }
  330. }
  331. * @apiErrorExample {json} Error-Response:
  332. * HTTP/1.1 400 Bad Request
  333. {
  334. "status": false,
  335. "status_code": 2009,
  336. "message": "订单没有找到",
  337. "data": null
  338. }
  339. */
  340. public function show(Request $request)
  341. {
  342. $user = Auth::guard('api')->user();
  343. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  344. $validator = Validator::make($request->all(),
  345. [
  346. 'id' => 'required',
  347. ],
  348. [
  349. 'id.required' => '订单不存在',
  350. ]
  351. );
  352. if ($validator->fails()) {
  353. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  354. }
  355. $id = ($request->input('id'));
  356. $order = S1OrderInfoModel::with('address','goods')->find($id);
  357. if(empty($order)) return $this->error(ErrorCode::ORDER_NOT_EXIST);
  358. $arr = [];
  359. $area = BaseAreaModel::find($order['address']['area']);
  360. if (!empty($area)){
  361. $info = explode(',',$area['merger_name']);
  362. unset($info[0]);
  363. $info = join('',$info);
  364. }else{
  365. $info = '';
  366. }
  367. $address = empty($area) ? $order['address']['address'] : $info.$order['address']['address'];
  368. /* $arr['user_name'] = $order['address']['name'];
  369. $arr['trade_no'] = $order['trade_no'];
  370. $arr['order_time'] = $order->order_time;
  371. $arr['amount'] = $order['amount'];
  372. $arr['goods_number'] = count($order['goods']);*/
  373. $order->address_info = $address;
  374. return $this->api($order);
  375. }
  376. }