setting = SettingInfoModel::first(); $config = [ 'app_id' => $this->setting->app_id, 'secret' => $this->setting->app_secret, // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 'response_type' => 'array', ]; $this->app = Factory::miniProgram($config); } /** * @api {post} /api/home/login 登陆(login) * @apiDescription 登陆(login) * @apiGroup Login * @apiPermission none * @apiVersion 0.1.0 * @apiParam {string} code 小程序登陆后返回的code * @apiParam {string} nickName 微信昵称 * @apiParam {string} avatar 微信头像 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjUzMzYwMzNlNzIwOTE3YjE0NDc4YjgxMWY5MmI4MjBmMThlNDgxNjJjZGFkYmEwNGI3ZjliZTNmZjRlMTY2YjU4NGU4MzJhN2RmYzY2ZTcxIn0.eyJhdWQiOiIxIiwianRpIjoiNTMzNjAzM2U3MjA5MTdiMTQ0NzhiODExZjkyYjgyMGYxOGU0ODE2MmNkYWRiYTA0YjdmOWJlM2ZmNGUxNjZiNTg0ZTgzMmE3ZGZjNjZlNzEiLCJpYXQiOjE1MzI0MjA3NDAsIm5iZiI6MTUzMjQyMDc0MCwiZXhwIjoxNTYzOTU2NzQwLCJzdWIiOiIyIiwic2NvcGVzIjpbXX0.YnuRiJI9jlt-KeQ480UEpLWCUU8FEJvlTRtAdjOlP0BWmcdo0E9rGS4hriYnpfJOn09Cw0aRYuc4dgQYL_JWD2fodlGg1LRIvPTOtvM5TiwM86kQJawvfFw7X7p9nOhxrFa5Tyir0cdTcV0SmQbq8KIptjdR8j7wUTByKhONexBXtNnlZSpw70ckTQrAstkn97IDwPo04hhGhf6eDPc8ler0HONiAVqbRvvNG6yHShJarP1hxyXrYN2BM0N7dtLD_8Sr8XaXL6ie4rRFVM4fNwpn74DkiDwXY6-5Xet6mzPvvzARAmU5vJ7JHhcL1N7m7poNp6YCx_mZAZ1z8PGDKrtQWoVeAmIxo7qtI6jvgvUpEFnJQ-KHCunXflNBL-vIYW4o3llnYqku1pcBdAUfkYLjYUgB3EZio280_8q-6Q24VAMiHZ58AjYvHHJJssdOa3dVHGD9Iq2z1dWR6gmZ4MgGCunCcAe9L_CbDm7VtMq3nKj4a1WScRiMD5nlKAHgy4O32rtNXqDr5T-eV-QNa4ZOv4VZ1AR-WE4RkO4ArKPaFxgSa9mak98PU8NHcPaJ_B3eDbvtwtMloTXWSQP08cmUPXKjEwXvszdkUt-ZWirw5Sd22h3qMdCI3gcvzT4_rnDKCEk37P09fUIK8LZrS4s2xOhueHziMzheAF0QekI", * "user": { * "id": 2, * "nickname": "roger", * "openid": "olAS94uwfTdsL3nDnvG67p_v5Vks", * "mobile": "13788765546", * "avatar": "https://wx.qlogo.cn/mmopen/vi_32/IOcxico8l4A7W3qxDeA53Id5kVAj2ibUGvQib9QibicTp5c1RNshDj6EKz2PKWp3reHfib0xxT4wa7jJzcb7a4EggkVw/132", * } * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 * 500 获取openid失败 */ public function login(Request $request) { $code = $request->get('code'); $session = $this->app->auth->session($code); \Log::info(json_encode($session)); $openid = $session['openid']; if (!$openid) { $data = [ 'code' => 500, 'msg' => '获取openid失败!' ]; return $this->api($data); } $userinfo = UserInfoModel::where('openid', $openid)->first(['id', 'nickname', 'openid']); if (!$userinfo) { $data['openid'] = $openid; $data['nickname'] = $request->get('nickName'); $data['avatar'] = $request->get('avatar'); $userinfo = UserInfoModel::create($data); } if (Auth::loginUsingId($userinfo->id)) { $user = Auth::user(); $token = $user->createToken($user->id . '-' . $user->openid)->accessToken; return $this->api(compact('token', 'user')); } else { return $this->error(ErrorCode::INCORRECT_USER_OR_PASS); } } /** * @api {get} /api/home/getproducts 获取产品列表 * @apiDescription 获取产品列表 * @apiGroup Product * @apiPermission None * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": [ * ... * { * "id": 2, * "name": "履历照", * "img": "/upload/images/20180712/ab83f15f31ba33e17e195a20d5307e37.jpg" * }, * ... * ] * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function getProducts() { $products = ProductCategoryModel::OrderBy('sort', 'desc')->get(['id', 'name', 'img']); return $this->api($products); } /** * @api {get} /api/home/getproduct 获取产品详情及规格 * @apiDescription 获取产品详情及规格 * @apiGroup Product * @apiPermission none * @apiVersion 0.1.0 * @apiParam {int} id 产品的id * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "id": 1, * "name": "证件照", * "detail": "", //产品详情 * "img": "/upload/images/20180712/696aa5b6187bd8b1e7eb4f7962a5a3cb.jpg", * "spec": [ //规格 * ... * { * "id": 1, * "name": "证件照", * "img": "", * "category_id": 1, * "origin_price": 299, //原价 * "current_price": 199, //折后价 * * }, * ... * ] * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function getProduct(Request $request) { $validator = Validator::make($request->all(), [ 'id' => 'required', ], [ 'id.required' => '产品id不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $id = $request->get('id'); $product = ProductCategoryModel::with(['spec'])->select(['id', 'name', 'img', 'detail'])->find($id); return $this->api($product); } /** * @api {get} /api/home/getstores 获取店铺列表 * @apiDescription 获取店铺列表 * @apiGroup Store * @apiPermission None * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": [ * ... * { * "id": 1, * "name": "青羊店", * "img": "/upload/images/20180712/696aa5b6187bd8b1e7eb4f7962a5a3cb.jpg", * "address": "四川省成都市青羊区新华西路街道江汉路33号新华宾馆", * "phone": "18380257014", * "lat": "30.675999", //店铺纬度 * "lon": "104.064465" //店铺经度 * }, * ... * ] * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function getStores() { $stores = StoreInfoModel::get(['id', 'name', 'img', 'address', 'phone', 'lat', 'lon']); return $this->api($stores); } /** * @api {get} /api/home/getschedule 获取店铺排期 * @apiDescription 获取店铺排期 * @apiGroup Store * @apiPermission None * @apiVersion 0.1.0 * @apiParam {string} day 要预约的日期,如:2018-7-19 * @apiParam {string} store_id 要预约的店铺ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "am_schedule": [ * { * "time": "2018-07-19 09:00:00", * "remain": 3 可预订人数,0表示该时间段人数已满 * }, * { * "time": "2018-07-19 10:00:00", * "remain": 0 * }, * { * "time": "2018-07-19 11:00:00", * "remain": 3 * } * ], * "pm_schedule": [ * { * "time": "2018-07-19 14:00:00", * "remain": 3 * }, * { * "time": "2018-07-19 15:00:00", * "remain": 0 * }, * { * "time": "2018-07-19 16:00:00", * "remain": 3 * } * ] * * * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "code": 10004, * "msg": "店铺休假,请改天再约" * } * } * */ public function getSchedule(Request $request) { $validator = Validator::make($request->all(), [ 'day' => 'required', 'store_id' => 'required', ], [ 'day.required' => '日期不能为空!', 'store_id.required' => '店铺id不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $day = $request->get('day'); $storeid = $request->get('store_id'); $isholiday = ProductScheduleModel::where('store_id', $storeid)->where('holiday_start', '<=', $day)->where('holiday_end', '>=', $day)->first(); if ($isholiday) { $data = ['code' => 10004, 'msg' => '店铺休假,请改天再约']; return $this->api($data); } $am_start = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->am_start; $am_end = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->am_end; $pm_start = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->pm_start; $pm_end = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->pm_end; $max = ProductScheduleModel::where('store_id', $storeid)->first()->max; $min = ProductScheduleModel::where('store_id', $storeid)->first()->interval; $ams = $this->cut_time_part($am_start, $am_end, $min); $am_schedule = $this->formatSchedule($ams, $max, $storeid); $pms = $this->cut_time_part($pm_start, $pm_end, $min); $pm_schedule = $this->formatSchedule($pms, $max, $storeid); return $this->api(compact('am_schedule', 'pm_schedule')); } /** * @api {post} /api/home/createorder 创建订单 * @apiDescription 创建订单 * @apiGroup Order * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} store_id 预约店铺ID * @apiParam {int} product_id 产品规格ID * @apiParam {string} schedule_time 预约时间 * @apiParam {string} username 到店人姓名 * @apiParam {string} phone 联系方式 * @apiParam {string} email 邮箱(接受照片用) * @apiParam {int} sex 性别 (0:男;1:女) * @apiParam {string} [comment] 备注 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "id": 27, * "out_trade_no": "xxg1531993028", //订单编号 * "username": "lee", //联系人 * "phone": "13407570876", //联系电话 * "email": "154@qq.com", //邮箱 * "schedule_time": "2018-07-19 11:00:00", //预约时间 * "status": 0, * "store_id": "1", * "storename": "青羊店", //店铺名称 * "product_name": "证件照", //产品名称 * "service_time": "约120分钟", //服务时长 * "price": 199, //应付金额 * "deposit": 100 //定金 * } * * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function createOrder(Request $request) { $validator = Validator::make($request->all(), [ 'store_id' => 'required', 'product_id' => 'required', 'schedule_time' => 'required', 'username' => 'required', 'phone' => 'required', 'email' => 'required', 'sex' => 'required', ], [ 'store_id.required' => '店铺id不能为空!', 'product_id.required' => '产品id不能为空!', 'schedule_time.required' => '预约时间不能为空!', 'username.required' => 'username不能为空!', 'phone.required' => 'phone不能为空!', 'email.required' => 'email不能为空!', 'sex.required' => 'sex不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => 401, 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $orderdata = $request->all(); $scheduledata = [ 'time' => $request->get('schedule_time'), 'user_id' => $user->id, 'username' => $request->get('username'), 'phone' => $request->get('phone'), 'store_id' => $request->get('store_id'), ]; $schedule = UserScheduleModel::create($scheduledata); $product = ProductInfoModel::find(request('product_id')); $orderdata['schedule_id'] = $schedule->id; $orderdata['out_trade_no'] = 'xxg' . date('Ymdhms'); $orderdata['price'] = $product->current_price ? $product->current_price : $product->origin_price; $orderdata['deposit'] = $product->deposit; $orderdata['user_id'] = $user->id; $res = OrderInfoModel::create($orderdata); $order = OrderInfoModel::select(['id', 'out_trade_no', 'username', 'phone', 'email', 'schedule_time', 'status', 'store_id', 'price', 'deposit'])->find($res->id); $store = $order->store(); $order['storename'] = $store; $order['product_name'] = $product->name; $order['service_time'] = $product->service_time; return $this->api($order); } /** * @api {get} /api/home/myorders 我的订单列表 * @apiDescription 我的订单列表 * @apiGroup Order * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} [status] 订单状态:1:已付款,未完成拍摄;2:拍摄完成;3:已取消 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "current_page": 1, //当前页 * "data": [ * ... * { * "id": 3, * "out_trade_no": "s10003", //订单编号 * "username": "roger", * "sex": 0, * "phone": "13407570876", * "email": "154@qq.com", * "store_id": "1", * "schedule_time": "2018-07-19 10:00:00", //预约时间 * "status": 2, * "user_id": 1, * "product_id": 1, * "schedule_id": null, * "comment": "", * "created_at": "2018-07-19 08:53:02", * "updated_at": "2018-07-19 08:53:02", * "price": null, * "deposit": null, * "storename": "青羊店", * "product_name": "证件照" * }, * ... * ], * "first_page_url": "http://dev.xxg.com/api/home/myorders?page=1", //首页地址 * "from": 1, * "last_page": 2, * "last_page_url": "http://dev.xxg.com/api/home/myorders?page=2", //最后一页地址 * "next_page_url": "http://dev.xxg.com/api/home/myorders?page=2", //下一页地址 * "path": "http://dev.xxg.com/api/home/myorders", * "per_page": 5, //每页显示数量 * "prev_page_url": null, //上一页地址 * "to": 5, * "total": 7 * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function myOrders(Request $request) { $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } if (request('status')) { $orders = OrderInfoModel::where('user_id', $user->id)->where('status', request('status'))->get(['id', 'out_trade_no', 'username', 'schedule_time', 'product_id', 'status', 'store_id']); } else { $orders = OrderInfoModel::where('user_id', $user->id)->where('status', '>', 0)->paginate(5); } foreach ($orders as $order) { $store = $order->store(); $product = ProductInfoModel::find($order->product_id); $order['storename'] = $store; $order['product_name'] = $product->name; } return $this->api($orders); } /** * @api {get} /api/home/orderdetail 获取订单详情 * @apiDescription 获取订单详情 * @apiGroup Order * @apiPermission None * @apiVersion 0.1.0 * @apiParam {int} order_id 订单ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "id": 27, * "out_trade_no": "xxg1531993028", //订单编号 * "username": "lee", //联系人 * "phone": "13407570876", //联系电话 * "email": "154@qq.com", //邮箱 * "schedule_time": "2018-07-19 11:00:00", //预约时间 * "status": 0, * "store_id": "1", * "storename": "青羊店", //店铺名称 * "product_name": "证件照", //产品名称 * "service_time": "约120分钟", //服务时长 * "price": 199, //应付金额 * "deposit": 100 //定金 * "photo": [ //照片 * ] * "paidinfo": { * "id": 3, * "order_id": "27", * "order_price": 199, //订单价格 * "type": 0, //付款类型 0:全款 1:定金 * "paid_price": 199, //支付金额 * } * } * * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function orderDetail(Request $request) { $validator = Validator::make($request->all(), [ 'order_id' => 'required', ], [ 'order_id.required' => 'order_id不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $order = OrderInfoModel::with(['paidinfo'])->select(['id', 'out_trade_no', 'username', 'phone', 'email', 'schedule_time', 'status', 'store_id', 'price', 'deposit', 'product_id', 'photo'])->find(request('order_id')); $photo = $order->photo; $order->photo = explode(',', $photo); $store = $order->store(); $product = ProductInfoModel::find($order->product_id); $order['storename'] = $store; $order['product_name'] = $product->name; $order['service_time'] = $product->service_time; return $this->api($order); } /** * @api {post} /api/home/createbusiness 创建企业咨询 * @apiDescription 创建企业咨询 * @apiGroup Order * @apiPermission None * @apiVersion 0.1.0 * @apiParam {string} company 公司名称 * @apiParam {string} username 联系人姓名 * @apiParam {string} phone 联系方式 * @apiParam {string} email 邮箱 * @apiParam {string} comment 拍摄要求 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "company": "斯尔克", * "username": "梁女士", * "phone": "13546554325", * "email": "sdfsa@163.com", * "comment": "sfaaafda", * "updated_at": "2018-08-13 06:09:07", * "created_at": "2018-08-13 06:09:07", * "id": 4 * } * * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function createBusiness(Request $request) { $validator = Validator::make($request->all(), [ 'company' => 'required', 'username' => 'required', 'phone' => 'required', 'email' => 'required', 'comment' => 'required', ], [ 'company.required' => 'company不能为空!', 'username.required' => 'username不能为空!', 'phone.required' => 'phone不能为空!', 'email.required' => 'email不能为空!', 'comment.required' => 'comment不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $data = $request->all(); $business = BusinessServiceModel::create($data); return $this->api($business); } /** * @api {post} /api/home/pay 订单支付 * @apiDescription 订单支付 * @apiGroup Order * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {string} orderid 订单号,创建订单后返回 * @apiParam {int} paytype 支付类型:0:全款支付;1:定金支付 * @apiParam {int} [couponid] 优惠券id * @apiParam {int} [cardid] 抵扣卡id(抵扣卡和优惠券不能同时使用) * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "appId":"wx1c2357232cd25f65", * "timeStamp":"1524907589", * "nonceStr":"5ae43e45eb499", * "package":"prepay_id=wx28172629917401724160128f0238805782", * "signType":"MD5", * "paySign":"8E9CF26B2B83C22471D023CBBDC36EDF" * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 */ public function pay(Request $request) { $validator = Validator::make($request->all(), [ 'orderid' => 'required', 'paytype' => 'required', ], [ 'orderid.required' => 'orderid不能为空!', 'paytype.required' => 'paytype不能为空!', ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $order = OrderInfoModel::find($request->get('orderid')); \Log::info($this->options()); if (request('paytype') == 0) { $money = $order->price; } else { $money = $order->deposit; } $order->paytype = request('paytype'); $order->out_trade_no = 'xxg' . date('Ymdhms'); $order->save(); if (request('couponid')) { $coupon = CouponInfoModel::find(request('couponid')); Cache::store('database')->put($order->out_trade_no, request('couponid'), 10); if ($coupon->type == 1) { $money = $money * ($coupon->discount / 10); } else { $money = $money - $coupon->discount_price; } } if (request('cardid')) { $money = 0; } if($money == 0){ $order->status = 1; $order->save(); if(request('cardid')){ } $data['order_no'] = $order->out_trade_no; $data['order_id'] = $order->id; $data['order_price'] = $order->price; $data['paid_price'] = 0; $data['type'] = $order->paytype; PaidInfoModel::create($data); $data = [ 'code' => 200, 'msg' => '支付成功' ]; $card = CardInfoModel::find(request('cardid')); $card->status =2; $card->save(); return $this->api(compact('data')); } $app = Factory::payment($this->options()); $result = $app->order->unify([ 'body' => '小相馆 -' . $order->out_trade_no, 'out_trade_no' => $order->out_trade_no, 'total_fee' => $money * 100, 'trade_type' => 'JSAPI', 'notify_url' => url('/api/home/notify'), 'openid' => $user->openid ]); \Log::info($result); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { $payment = Factory::payment($this->options()); $jssdk = $payment->jssdk; $json = $jssdk->bridgeConfig($result['prepay_id']); \Log::info($json); return $this->api(compact('json')); } else { $msg = "签名失败,请稍后再试!"; return $this->api(compact('msg')); } } //下面是回调函数 public function notify() { $app = Factory::payment($this->options()); \Log::info("wechat notify start!"); return $app->handlePaidNotify(function ($notify, $successful) { \Log::info($notify); if ($notify['result_code'] == 'SUCCESS') { $order = OrderInfoModel::where('out_trade_no', $notify['out_trade_no'])->first(); $order->status = 1; $order->save(); $data['order_no'] = $notify['out_trade_no']; $data['order_id'] = $order->id; $data['order_price'] = $order->price; $data['paid_price'] = $notify['total_fee'] / 100; $data['type'] = $order->paytype; PaidInfoModel::create($data); $couponid = Cache::store('database')->get($order->out_trade_no); if($couponid) { $user_id = $order->user_id; $coupon = UserCouponRelationModel::where('coupon_id',$couponid)->where('user_id',$user_id)->first(); $coupon->status =1; $coupon->save(); } } else { return $successful('通信失败,请稍后再通知我'); } return true; }); } /** * @api {get} /api/home/couponlist 可领取的优惠券列表 * @apiDescription 可领取的优惠券列表 * @apiGroup Coupon * @apiPermission Passport * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": [ * ... * { * "id": 1, * "name": "证件照七折优惠", //优惠券名字 * "type": "1", //优惠券类型 0:固定金额优惠,1:折扣优惠 * "min_price": 100, //最低使用金额 * "discount": "7", //折扣 * "discount_price": null, //优惠金额 * "product_id": "1,2", //可使用的产品id * "product":[ //可使用产品名字 * "证件照", * "履历照" * ] * "count": 10, //剩余数量 * "end_time": "2018-07-31", //有效期 * "created_at": "2018-07-13 07:29:27", * "updated_at": "2018-07-18 06:36:38"e * } * ... * ] * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 */ public function couponList(Request $request) { $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $couponlist = CouponInfoModel::where('end_time', '>', Carbon::now('Asia/Shanghai'))->where('count', '>', 0)->get(); foreach ($couponlist as $key => $item) { $item->product = $item->product(); $had = UserCouponRelationModel::where('coupon_id', $item->id)->where('user_id', $user->id)->count(); if ($had) { unset($couponlist[$key]); } } return $this->api($couponlist); } /** * @api {get} /api/home/mycoupon 我的优惠券/兑换卡列表 * @apiDescription 我的优惠券/兑换卡列表 * @apiGroup Coupon * @apiPermission Passport * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "mycoupons":[ * ... * { * "id": 1, * "coupon_id": 1, * "user_id": 1, * "coupon": { * "id": 1, * "name": "证件照七折优惠", //优惠券名字 * "type": "1", //优惠券类型 0:固定金额优惠,1:折扣优惠 * "min_price": 100, //最低使用金额 * "discount": "7", //折扣 * "discount_price": null, //优惠金额 * "product_id": "1,2", //可使用的产品id * "product":[ //可使用产品名字 * "证件照", * "履历照" * ] * "count": 10, * "end_time": "2018-07-31", //有效期 * "created_at": "2018-07-13 07:29:27", * "updated_at": "2018-07-18 06:36:38" * } * } * ... * ], * "mycards": [ * ... * { * "id": 7, * "card_no": "15319090241", * "price": 50, * "min_price": 100, * "status": 1, * "user_id": 1, * "remark": "梁源源--13407570861", * "end_time": "2019-07-31", * "qrcode": null, * "product_id": "1,2", * "product":[], * "deleted_at": null, * "created_at": "2018-07-18 10:17:04", * "updated_at": "2018-08-09 02:11:36" * } * ... * ], * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 */ public function myCoupon(Request $request) { $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $mycoupons = UserCouponRelationModel::where('user_id', $user->id)->with('coupon')->orderBy('created_at', 'desc')->get(); $mycoupons = $mycoupons->filter(function ($value) { if (!$value->coupon) { return false; } if (strtotime($value->coupon->end_time) < strtotime(Carbon::now('Asia/Shanghai'))) { $value->status = 2; return true; } else { $value->coupon->product = CouponInfoModel::find($value->coupon->id) ? CouponInfoModel::find($value->coupon->id)->product() : ''; return true; } }); $mycoupons = $mycoupons->toArray(); $mycoupons = array_values($mycoupons); $mycards = CardInfoModel::where('user_id', $user->id)->get(); foreach ($mycards as $card) { $card->product = $card->product(); if($card->end_time < now()){ $card->status = 2; } } return $this->api(compact('mycards', 'mycoupons')); } /** * @api {get} /api/home/getcoupon 领取优惠券 * @apiDescription 领取优惠券 * @apiGroup Coupon * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} coupon_id 优惠券ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "res": true //领取成功 * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "code": 10001, * "msg": "你已经领取过改优惠券,不能重复领取!" * } * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 * 1001 CLIENT_WRONG_PARAMS 你已经领取过改优惠券,不能重复领取 */ public function getCoupon(Request $request) { $validator = Validator::make($request->all(), [ 'coupon_id' => 'required' ], [ 'coupon_id.required' => 'coupon_id不能为空!' ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $had = UserCouponRelationModel::where('coupon_id', request('coupon_id'))->where('user_id', $user->id)->count(); if ($had) { $data = [ 'code' => 10001, 'msg' => '你已经领取过改优惠券,不能重复领取!' ]; return $this->api($data); } $coupon = CouponInfoModel::find(request('coupon_id')); if (!$coupon) { $data = [ 'code' => 10002, 'msg' => '该优惠券已被领取完毕!' ]; return $this->api($data); } if ($coupon->count > 0) { $coupon->count = $coupon->count - 1; $res = $coupon->save(); $data = $request->all(); $data['user_id'] = $user->id; UserCouponRelationModel::create($data); return $this->api(compact('res')); } else { $data = [ 'code' => 10002, 'msg' => '该优惠券已被领取完毕!' ]; return $this->api($data); } } /** * @api {get} /api/home/activecard 激活兑换卡 * @apiDescription 激活兑换卡 * @apiGroup Coupon * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} card_no 兑换卡编号 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": true * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "code": "10006", * "msg": "该兑换卡已经失效" * } * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 * 1005 CLIENT_WRONG_PARAMS 该兑换卡不存在 * 1006 CLIENT_WRONG_PARAMS 该兑换卡已经失效 */ public function activeCard(Request $request) { $validator = Validator::make($request->all(), [ 'card_no' => 'required', ], [ 'card_id.required' => 'card_no不能为空!' ] ); if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } $isExisted = CardInfoModel::where('card_no', request('card_no'))->count(); if (!$isExisted) { $data = [ 'code' => '10005', 'msg' => '该兑换卡不存在', ]; return $this->api($data); } $isActived = CardInfoModel::where('card_no', request('card_no'))->whereIn('status', [1, 2])->first(); $end_time = CardInfoModel::where('card_no', request('card_no'))->first()->end_time; if ($isActived || ($end_time < now())) { $data = [ 'code' => '10006', 'msg' => '该兑换卡已经失效', ]; return $this->api($data); } $card = CardInfoModel::where('card_no', request('card_no'))->first(); $card->status = 1; $card->user_id = $user->id; $ok = $card->save(); return $this->api($ok); } /** * @api {get} /api/home/usablecoupon 获取订单可用的优惠券/兑换卡 * @apiDescription 获取订单可用的优惠券/兑换卡 * @apiGroup Coupon * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} product_id 产品规格ID,订单中获取 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data":{ * "usablecoupons":[ //可用的优惠券 * ... * { * "id": 1, * "coupon_id": 1, * "user_id": 1, * "coupon": { * "id": 1, * "name": "证件照七折优惠", * "type": "1", * "min_price": 100, * "discount": "7", * "discount_price": null, * "product_id": "1,2", * "product":[ //可使用产品名字 * "证件照", * "履历照" * ], * "count": 7, * "end_time": "2018-07-31", * "created_at": "2018-07-13 07:29:27", * "updated_at": "2018-07-21 07:41:59" * } * } * ... * ], * * "usableCards": [ //可用兑换卡 * ... * { * "id": 7, * "card_no": "15319090241", * "status": 1, * "user_id": 1, * "remark": "", * "end_time": "2019-07-31", * "product_id": "1,2", * "product":[ //可使用产品名字 * "证件照", * "履历照" * ], * "deleted_at": null, * "created_at": "2018-07-18 10:17:04", * "updated_at": "2018-08-09 02:11:36" * } * ] * ... * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 */ public function usableCoupon(Request $request) { $validator = Validator::make($request->all(), [ 'product_id' => 'required' ], [ 'product_id.required' => 'product_id不能为空!' ] ); $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $mycoupons = UserCouponRelationModel::where('user_id', $user->id)->with('coupon')->orderBy('created_at', 'desc')->get(); $usablecoupons = $mycoupons->filter(function ($value) { $flag = false; $product = ProductInfoModel::find(request('product_id')); if (!$value->coupon) { $flag = false; return $flag; } $products = explode(',', $value->coupon->product_id); $price = $product->current_price ? $product->current_price : $product->origin_price; if ((strtotime($value->coupon->end_time) > strtotime(Carbon::now('Asia/Shanghai'))) && in_array($product->category_id, $products) && ($price >= $value->coupon->min_price)) { $value->coupon->product = CouponInfoModel::find($value->coupon->id) ? CouponInfoModel::find($value->coupon->id)->product() : ''; $flag = true; } else { $flag = false; } return $flag; }); $usablecoupons = $usablecoupons->toArray(); $usablecoupons = array_values($usablecoupons); $usableCards = CardInfoModel::where('user_id', $user->id)->where('status', 1)->where('end_time', '>=', now())->where('product_id', 'like', '%' . request('product_id') . '%')->get(); foreach ($usableCards as $card) { $card->product = $card->product(); } return $this->api(compact('usablecoupons', 'usableCards')); } /** * @api {get} /api/home/refund 申请退款 * @apiDescription 申请退款 * @apiGroup Order * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} order_id 产品规格ID,订单中获取 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "code": 200, * "msg": "退款已申请,请等待商家审核!" * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "code": 10003, * "msg": "该订单不能退款,如有疑问,请联系商家!" * } * } * 可能出现的错误代码: * 1000 CLIENT_WRONG_PARAMS 传入参数不正确 */ public function refund(Request $request) { $validator = Validator::make($request->all(), [ 'order_id' => 'required' ], [ 'order_id.required' => 'order_id不能为空!' ] ); $user = Auth('api')->user(); if (!$user) { $data = [ 'code' => '401', 'msg' => 'token已过期,请重新登录', ]; return $this->api($data); } if ($validator->fails()) { return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages()); } $order = OrderInfoModel::find(request('order_id')); if ($order->status != 1) { $data = [ 'code' => 10003, 'msg' => '该订单不能退款,如有疑问,请联系商家!' ]; return $this->api($data); } $order->status = 3; $ok = $order->save(); if ($ok) { $data = [ 'code' => 200, 'msg' => '退款已申请,请等待商家审核!' ]; return $this->api($data); } } //格式化排期 function cut_time_part($start, $end, $min = 30, $format = true) { $start = strtotime($start); $end = strtotime($end); $nums = ($end - $start) / ($min * 60); $parts = ($end - $start) / $nums; $last = ($end - $start) % $nums; if ($last > 0) { $parts = ($end - $start - $last) / $nums; } for ($i = 1; $i <= $nums; $i++) { $_end = $start + $parts * $i; $arr[] = array($start + $parts * ($i - 1), $_end); } $len = count($arr) - 1; $arr[$len][1] = $arr[$len][1] + $last; if ($format) { foreach ($arr as $key => $value) { $arr[$key] = date("Y-m-d H:i:s", $value[0]); } } return $arr; } //格式化排期 public function formatSchedule($arrs, $max, $storeid) { $arr = []; foreach ($arrs as $k => $item) { $count = UserScheduleModel::where('time', $item)->where('store_id', $storeid)->count(); $remain = $max - $count; $arr[$k]['time'] = $item; $arr[$k]['remain'] = $remain; } return $arr; } public function options() { return [ 'app_id' => $this->setting->app_id, 'mch_id' => $this->setting->mch_id, 'key' => $this->setting->api_key, 'notify_url' => url('/api/home/notify'), 'sandbox' => false, ]; } }