HomeController.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Helper\AttachmentHelper;
  4. use App\Models\BaseAttachmentModel;
  5. use App\Models\BusinessServiceModel;
  6. use App\Models\CardInfoModel;
  7. use App\Models\CouponInfoModel;
  8. use App\Models\OrderInfoModel;
  9. use App\Models\PaidInfoModel;
  10. use App\Models\ProductCategoryModel;
  11. use App\Models\ProductInfoModel;
  12. use App\Models\ProductScheduleModel;
  13. use App\Models\SettingInfoModel;
  14. use App\Models\StoreInfoModel;
  15. use App\Models\UserCouponRelationModel;
  16. use App\Models\UserInfoModel;
  17. use App\Models\UserScheduleModel;
  18. use Carbon\Carbon;
  19. use EasyWeChat\Factory;
  20. use function foo\func;
  21. use Illuminate\Http\Request;
  22. use App\Services\Base\Attachment;
  23. use App\Services\Base\ErrorCode;
  24. use Validator, Response, Auth, Cache;
  25. class HomeController extends Controller
  26. {
  27. protected $app;
  28. protected $setting;
  29. public function __construct()
  30. {
  31. $this->setting = SettingInfoModel::first();
  32. $config = [
  33. 'app_id' => $this->setting->app_id,
  34. 'secret' => $this->setting->app_secret,
  35. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  36. 'response_type' => 'array',
  37. ];
  38. $this->app = Factory::miniProgram($config);
  39. }
  40. /**
  41. * @api {post} /api/home/login 登陆(login)
  42. * @apiDescription 登陆(login)
  43. * @apiGroup Login
  44. * @apiPermission none
  45. * @apiVersion 0.1.0
  46. * @apiParam {string} code 小程序登陆后返回的code
  47. * @apiParam {string} nickName 微信昵称
  48. * @apiParam {string} avatar 微信头像
  49. * @apiSuccessExample {json} Success-Response:
  50. * HTTP/1.1 200 OK
  51. * {
  52. * "status": true,
  53. * "status_code": 0,
  54. * "message": "",
  55. * "data": {
  56. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjUzMzYwMzNlNzIwOTE3YjE0NDc4YjgxMWY5MmI4MjBmMThlNDgxNjJjZGFkYmEwNGI3ZjliZTNmZjRlMTY2YjU4NGU4MzJhN2RmYzY2ZTcxIn0.eyJhdWQiOiIxIiwianRpIjoiNTMzNjAzM2U3MjA5MTdiMTQ0NzhiODExZjkyYjgyMGYxOGU0ODE2MmNkYWRiYTA0YjdmOWJlM2ZmNGUxNjZiNTg0ZTgzMmE3ZGZjNjZlNzEiLCJpYXQiOjE1MzI0MjA3NDAsIm5iZiI6MTUzMjQyMDc0MCwiZXhwIjoxNTYzOTU2NzQwLCJzdWIiOiIyIiwic2NvcGVzIjpbXX0.YnuRiJI9jlt-KeQ480UEpLWCUU8FEJvlTRtAdjOlP0BWmcdo0E9rGS4hriYnpfJOn09Cw0aRYuc4dgQYL_JWD2fodlGg1LRIvPTOtvM5TiwM86kQJawvfFw7X7p9nOhxrFa5Tyir0cdTcV0SmQbq8KIptjdR8j7wUTByKhONexBXtNnlZSpw70ckTQrAstkn97IDwPo04hhGhf6eDPc8ler0HONiAVqbRvvNG6yHShJarP1hxyXrYN2BM0N7dtLD_8Sr8XaXL6ie4rRFVM4fNwpn74DkiDwXY6-5Xet6mzPvvzARAmU5vJ7JHhcL1N7m7poNp6YCx_mZAZ1z8PGDKrtQWoVeAmIxo7qtI6jvgvUpEFnJQ-KHCunXflNBL-vIYW4o3llnYqku1pcBdAUfkYLjYUgB3EZio280_8q-6Q24VAMiHZ58AjYvHHJJssdOa3dVHGD9Iq2z1dWR6gmZ4MgGCunCcAe9L_CbDm7VtMq3nKj4a1WScRiMD5nlKAHgy4O32rtNXqDr5T-eV-QNa4ZOv4VZ1AR-WE4RkO4ArKPaFxgSa9mak98PU8NHcPaJ_B3eDbvtwtMloTXWSQP08cmUPXKjEwXvszdkUt-ZWirw5Sd22h3qMdCI3gcvzT4_rnDKCEk37P09fUIK8LZrS4s2xOhueHziMzheAF0QekI",
  57. * "user": {
  58. * "id": 2,
  59. * "nickname": "roger",
  60. * "openid": "olAS94uwfTdsL3nDnvG67p_v5Vks",
  61. * "mobile": "13788765546",
  62. * "avatar": "https://wx.qlogo.cn/mmopen/vi_32/IOcxico8l4A7W3qxDeA53Id5kVAj2ibUGvQib9QibicTp5c1RNshDj6EKz2PKWp3reHfib0xxT4wa7jJzcb7a4EggkVw/132",
  63. * }
  64. * }
  65. * }
  66. * @apiErrorExample {json} Error-Response:
  67. * HTTP/1.1 400 Bad Request
  68. * {
  69. * "state": false,
  70. * "code": 1000,
  71. * "message": "传入参数不正确",
  72. * "data": null or []
  73. * }
  74. * 可能出现的错误代码:
  75. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  76. * 500 获取openid失败
  77. */
  78. public function login(Request $request)
  79. {
  80. $code = $request->get('code');
  81. $session = $this->app->auth->session($code);
  82. \Log::info(json_encode($session));
  83. $openid = $session['openid'];
  84. if (!$openid) {
  85. $data = [
  86. 'code' => 500,
  87. 'msg' => '获取openid失败!'
  88. ];
  89. return $this->api($data);
  90. }
  91. $userinfo = UserInfoModel::where('openid', $openid)->first(['id', 'nickname', 'openid']);
  92. if (!$userinfo) {
  93. $data['openid'] = $openid;
  94. $data['nickname'] = $request->get('nickName');
  95. $data['avatar'] = $request->get('avatar');
  96. $userinfo = UserInfoModel::create($data);
  97. }
  98. if (Auth::loginUsingId($userinfo->id)) {
  99. $user = Auth::user();
  100. $token = $user->createToken($user->id . '-' . $user->openid)->accessToken;
  101. return $this->api(compact('token', 'user'));
  102. } else {
  103. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  104. }
  105. }
  106. /**
  107. * @api {get} /api/home/getproducts 获取产品列表
  108. * @apiDescription 获取产品列表
  109. * @apiGroup Product
  110. * @apiPermission None
  111. * @apiVersion 0.1.0
  112. * @apiSuccessExample {json} Success-Response:
  113. * HTTP/1.1 200 OK
  114. * {
  115. * "state": true,
  116. * "code": 0,
  117. * "message": "",
  118. * "data": [
  119. * ...
  120. * {
  121. * "id": 2,
  122. * "name": "履历照",
  123. * "img": "/upload/images/20180712/ab83f15f31ba33e17e195a20d5307e37.jpg"
  124. * },
  125. * ...
  126. * ]
  127. * }
  128. * @apiErrorExample {json} Error-Response:
  129. * HTTP/1.1 400 Bad Request
  130. * {
  131. * "state": false,
  132. * "code": 1000,
  133. * "message": "传入参数不正确",
  134. * "data": null or []
  135. * }
  136. */
  137. public function getProducts()
  138. {
  139. $products = ProductCategoryModel::OrderBy('id', 'desc')->get(['id', 'name', 'img']);
  140. return $this->api($products);
  141. }
  142. /**
  143. * @api {get} /api/home/getproduct 获取产品详情及规格
  144. * @apiDescription 获取产品详情及规格
  145. * @apiGroup Product
  146. * @apiPermission none
  147. * @apiVersion 0.1.0
  148. * @apiParam {int} id 产品的id
  149. * @apiSuccessExample {json} Success-Response:
  150. * HTTP/1.1 200 OK
  151. * {
  152. * "state": true,
  153. * "code": 0,
  154. * "message": "",
  155. * "data": {
  156. * "id": 1,
  157. * "name": "证件照",
  158. * "detail": "", //产品详情
  159. * "img": "/upload/images/20180712/696aa5b6187bd8b1e7eb4f7962a5a3cb.jpg",
  160. * "spec": [ //规格
  161. * ...
  162. * {
  163. * "id": 1,
  164. * "name": "证件照",
  165. * "img": "",
  166. * "category_id": 1,
  167. * "origin_price": 299, //原价
  168. * "current_price": 199, //折后价
  169. *
  170. * },
  171. * ...
  172. * ]
  173. * }
  174. * }
  175. * @apiErrorExample {json} Error-Response:
  176. * HTTP/1.1 400 Bad Request
  177. * {
  178. * "state": false,
  179. * "code": 1000,
  180. * "message": "传入参数不正确",
  181. * "data": null or []
  182. * }
  183. */
  184. public function getProduct(Request $request)
  185. {
  186. $validator = Validator::make($request->all(),
  187. [
  188. 'id' => 'required',
  189. ],
  190. [
  191. 'id.required' => '产品id不能为空!',
  192. ]
  193. );
  194. if ($validator->fails()) {
  195. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  196. }
  197. $id = $request->get('id');
  198. $product = ProductCategoryModel::with(['spec'])->select(['id', 'name', 'img', 'detail'])->find($id);
  199. return $this->api($product);
  200. }
  201. /**
  202. * @api {get} /api/home/getstores 获取店铺列表
  203. * @apiDescription 获取店铺列表
  204. * @apiGroup Store
  205. * @apiPermission None
  206. * @apiVersion 0.1.0
  207. * @apiSuccessExample {json} Success-Response:
  208. * HTTP/1.1 200 OK
  209. * {
  210. * "state": true,
  211. * "code": 0,
  212. * "message": "",
  213. * "data": [
  214. * ...
  215. * {
  216. * "id": 1,
  217. * "name": "青羊店",
  218. * "img": "/upload/images/20180712/696aa5b6187bd8b1e7eb4f7962a5a3cb.jpg",
  219. * "address": "四川省成都市青羊区新华西路街道江汉路33号新华宾馆",
  220. * "phone": "18380257014",
  221. * "lat": "30.675999", //店铺纬度
  222. * "lon": "104.064465" //店铺经度
  223. * },
  224. * ...
  225. * ]
  226. * }
  227. * @apiErrorExample {json} Error-Response:
  228. * HTTP/1.1 400 Bad Request
  229. * {
  230. * "state": false,
  231. * "code": 1000,
  232. * "message": "传入参数不正确",
  233. * "data": null or []
  234. * }
  235. */
  236. public function getStores()
  237. {
  238. $stores = StoreInfoModel::get(['id', 'name', 'img', 'address', 'phone', 'lat', 'lon']);
  239. return $this->api($stores);
  240. }
  241. /**
  242. * @api {get} /api/home/getschedule 获取店铺排期
  243. * @apiDescription 获取店铺排期
  244. * @apiGroup Store
  245. * @apiPermission None
  246. * @apiVersion 0.1.0
  247. * @apiParam {string} day 要预约的日期,如:2018-7-19
  248. * @apiParam {string} store_id 要预约的店铺ID
  249. * @apiSuccessExample {json} Success-Response:
  250. * HTTP/1.1 200 OK
  251. * {
  252. * "state": true,
  253. * "code": 0,
  254. * "message": "",
  255. * "data": {
  256. * "am_schedule": [
  257. * {
  258. * "time": "2018-07-19 09:00:00",
  259. * "remain": 3 可预订人数,0表示该时间段人数已满
  260. * },
  261. * {
  262. * "time": "2018-07-19 10:00:00",
  263. * "remain": 0
  264. * },
  265. * {
  266. * "time": "2018-07-19 11:00:00",
  267. * "remain": 3
  268. * }
  269. * ],
  270. * "pm_schedule": [
  271. * {
  272. * "time": "2018-07-19 14:00:00",
  273. * "remain": 3
  274. * },
  275. * {
  276. * "time": "2018-07-19 15:00:00",
  277. * "remain": 0
  278. * },
  279. * {
  280. * "time": "2018-07-19 16:00:00",
  281. * "remain": 3
  282. * }
  283. * ]
  284. *
  285. *
  286. * }
  287. * @apiErrorExample {json} Error-Response:
  288. * HTTP/1.1 400 Bad Request
  289. * {
  290. * "state": false,
  291. * "code": 1000,
  292. * "message": "传入参数不正确",
  293. * "data": null or []
  294. * }
  295. * {
  296. * "status": true,
  297. * "status_code": 0,
  298. * "message": "",
  299. * "data": {
  300. * "code": 10004,
  301. * "msg": "店铺休假,请改天再约"
  302. * }
  303. * }
  304. *
  305. */
  306. public function getSchedule(Request $request)
  307. {
  308. $validator = Validator::make($request->all(),
  309. [
  310. 'day' => 'required',
  311. 'store_id' => 'required',
  312. ],
  313. [
  314. 'day.required' => '日期不能为空!',
  315. 'store_id.required' => '店铺id不能为空!',
  316. ]
  317. );
  318. if ($validator->fails()) {
  319. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  320. }
  321. $day = $request->get('day');
  322. $storeid = $request->get('store_id');
  323. $isholiday = ProductScheduleModel::where('store_id', $storeid)->where('holiday_start', '<=', $day)->where('holiday_end', '>=', $day)->first();
  324. if ($isholiday) {
  325. $data = ['code' => 10004, 'msg' => '店铺休假,请改天再约'];
  326. return $this->api($data);
  327. }
  328. $am_start = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->am_start;
  329. $am_end = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->am_end;
  330. $pm_start = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->pm_start;
  331. $pm_end = $day . ' ' . ProductScheduleModel::where('store_id', $storeid)->first()->pm_end;
  332. $max = ProductScheduleModel::where('store_id', $storeid)->first()->max;
  333. $min = ProductScheduleModel::where('store_id', $storeid)->first()->interval;
  334. $ams = $this->cut_time_part($am_start, $am_end, $min);
  335. $am_schedule = $this->formatSchedule($ams, $max, $storeid);
  336. $pms = $this->cut_time_part($pm_start, $pm_end, $min);
  337. $pm_schedule = $this->formatSchedule($pms, $max, $storeid);
  338. return $this->api(compact('am_schedule', 'pm_schedule'));
  339. }
  340. /**
  341. * @api {post} /api/home/createorder 创建订单
  342. * @apiDescription 创建订单
  343. * @apiGroup Order
  344. * @apiPermission Passport
  345. * @apiVersion 0.1.0
  346. * @apiParam {int} store_id 预约店铺ID
  347. * @apiParam {int} product_id 产品规格ID
  348. * @apiParam {string} schedule_time 预约时间
  349. * @apiParam {string} username 到店人姓名
  350. * @apiParam {string} phone 联系方式
  351. * @apiParam {string} email 邮箱(接受照片用)
  352. * @apiParam {int} sex 性别 (0:男;1:女)
  353. * @apiParam {string} [comment] 备注
  354. * @apiSuccessExample {json} Success-Response:
  355. * HTTP/1.1 200 OK
  356. * {
  357. * "state": true,
  358. * "code": 0,
  359. * "message": "",
  360. * "data": {
  361. * "id": 27,
  362. * "out_trade_no": "xxg1531993028", //订单编号
  363. * "username": "lee", //联系人
  364. * "phone": "13407570876", //联系电话
  365. * "email": "154@qq.com", //邮箱
  366. * "schedule_time": "2018-07-19 11:00:00", //预约时间
  367. * "status": 0,
  368. * "store_id": "1",
  369. * "storename": "青羊店", //店铺名称
  370. * "product_name": "证件照", //产品名称
  371. * "service_time": "约120分钟", //服务时长
  372. * "price": 199, //应付金额
  373. * "deposit": 100 //定金
  374. * }
  375. *
  376. * }
  377. * @apiErrorExample {json} Error-Response:
  378. * HTTP/1.1 400 Bad Request
  379. * {
  380. * "state": false,
  381. * "code": 1000,
  382. * "message": "传入参数不正确",
  383. * "data": null or []
  384. * }
  385. */
  386. public function createOrder(Request $request)
  387. {
  388. $validator = Validator::make($request->all(),
  389. [
  390. 'store_id' => 'required',
  391. 'product_id' => 'required',
  392. 'schedule_time' => 'required',
  393. 'username' => 'required',
  394. 'phone' => 'required',
  395. 'email' => 'required',
  396. 'sex' => 'required',
  397. ],
  398. [
  399. 'store_id.required' => '店铺id不能为空!',
  400. 'product_id.required' => '产品id不能为空!',
  401. 'schedule_time.required' => '预约时间不能为空!',
  402. 'username.required' => 'username不能为空!',
  403. 'phone.required' => 'phone不能为空!',
  404. 'email.required' => 'email不能为空!',
  405. 'sex.required' => 'sex不能为空!',
  406. ]
  407. );
  408. if ($validator->fails()) {
  409. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  410. }
  411. $user = Auth('api')->user();
  412. if (!$user) {
  413. $data = [
  414. 'code' => 401,
  415. 'msg' => 'token已过期,请重新登录',
  416. ];
  417. return $this->api($data);
  418. }
  419. $orderdata = $request->all();
  420. $scheduledata = [
  421. 'time' => $request->get('schedule_time'),
  422. 'user_id' => $user->id,
  423. 'username' => $request->get('username'),
  424. 'phone' => $request->get('phone'),
  425. 'store_id' => $request->get('store_id'),
  426. ];
  427. $schedule = UserScheduleModel::create($scheduledata);
  428. $product = ProductInfoModel::find(request('product_id'));
  429. $orderdata['schedule_id'] = $schedule->id;
  430. $orderdata['out_trade_no'] = 'xxg' . date('Ymdhms');
  431. $orderdata['price'] = $product->current_price ? $product->current_price : $product->origin_price;
  432. $orderdata['deposit'] = $product->deposit;
  433. $orderdata['user_id'] = $user->id;
  434. $res = OrderInfoModel::create($orderdata);
  435. $order = OrderInfoModel::select(['id', 'out_trade_no', 'username', 'phone', 'email', 'schedule_time', 'status', 'store_id', 'price', 'deposit'])->find($res->id);
  436. $store = $order->store();
  437. $order['storename'] = $store;
  438. $order['product_name'] = $product->name;
  439. $order['service_time'] = $product->service_time;
  440. return $this->api($order);
  441. }
  442. /**
  443. * @api {get} /api/home/myorders 我的订单列表
  444. * @apiDescription 我的订单列表
  445. * @apiGroup Order
  446. * @apiPermission Passport
  447. * @apiVersion 0.1.0
  448. * @apiParam {int} [status] 订单状态:1:已付款,未完成拍摄;2:拍摄完成;3:已取消
  449. * @apiSuccessExample {json} Success-Response:
  450. * HTTP/1.1 200 OK
  451. * {
  452. * "state": true,
  453. * "code": 0,
  454. * "message": "",
  455. * "data": {
  456. * "current_page": 1, //当前页
  457. * "data": [
  458. * ...
  459. * {
  460. * "id": 3,
  461. * "out_trade_no": "s10003", //订单编号
  462. * "username": "roger",
  463. * "sex": 0,
  464. * "phone": "13407570876",
  465. * "email": "154@qq.com",
  466. * "store_id": "1",
  467. * "schedule_time": "2018-07-19 10:00:00", //预约时间
  468. * "status": 2,
  469. * "user_id": 1,
  470. * "product_id": 1,
  471. * "schedule_id": null,
  472. * "comment": "",
  473. * "created_at": "2018-07-19 08:53:02",
  474. * "updated_at": "2018-07-19 08:53:02",
  475. * "price": null,
  476. * "deposit": null,
  477. * "storename": "青羊店",
  478. * "product_name": "证件照"
  479. * },
  480. * ...
  481. * ],
  482. * "first_page_url": "http://dev.xxg.com/api/home/myorders?page=1", //首页地址
  483. * "from": 1,
  484. * "last_page": 2,
  485. * "last_page_url": "http://dev.xxg.com/api/home/myorders?page=2", //最后一页地址
  486. * "next_page_url": "http://dev.xxg.com/api/home/myorders?page=2", //下一页地址
  487. * "path": "http://dev.xxg.com/api/home/myorders",
  488. * "per_page": 5, //每页显示数量
  489. * "prev_page_url": null, //上一页地址
  490. * "to": 5,
  491. * "total": 7
  492. * }
  493. * }
  494. * @apiErrorExample {json} Error-Response:
  495. * HTTP/1.1 400 Bad Request
  496. * {
  497. * "state": false,
  498. * "code": 1000,
  499. * "message": "传入参数不正确",
  500. * "data": null or []
  501. * }
  502. */
  503. public function myOrders(Request $request)
  504. {
  505. $user = Auth('api')->user();
  506. if (!$user) {
  507. $data = [
  508. 'code' => '401',
  509. 'msg' => 'token已过期,请重新登录',
  510. ];
  511. return $this->api($data);
  512. }
  513. if (request('status')) {
  514. $orders = OrderInfoModel::where('user_id', $user->id)->where('status', request('status'))->get(['id', 'out_trade_no', 'username', 'schedule_time', 'product_id', 'status', 'store_id']);
  515. } else {
  516. $orders = OrderInfoModel::where('user_id', $user->id)->where('status', '>', 0)->paginate(5);
  517. }
  518. foreach ($orders as $order) {
  519. $store = $order->store();
  520. $product = ProductInfoModel::find($order->product_id);
  521. $order['storename'] = $store;
  522. $order['product_name'] = $product->name;
  523. }
  524. return $this->api($orders);
  525. }
  526. /**
  527. * @api {get} /api/home/orderdetail 获取订单详情
  528. * @apiDescription 获取订单详情
  529. * @apiGroup Order
  530. * @apiPermission None
  531. * @apiVersion 0.1.0
  532. * @apiParam {int} order_id 订单ID
  533. * @apiSuccessExample {json} Success-Response:
  534. * HTTP/1.1 200 OK
  535. * {
  536. * "state": true,
  537. * "code": 0,
  538. * "message": "",
  539. * "data": {
  540. * "id": 27,
  541. * "out_trade_no": "xxg1531993028", //订单编号
  542. * "username": "lee", //联系人
  543. * "phone": "13407570876", //联系电话
  544. * "email": "154@qq.com", //邮箱
  545. * "schedule_time": "2018-07-19 11:00:00", //预约时间
  546. * "status": 0,
  547. * "store_id": "1",
  548. * "storename": "青羊店", //店铺名称
  549. * "product_name": "证件照", //产品名称
  550. * "service_time": "约120分钟", //服务时长
  551. * "price": 199, //应付金额
  552. * "deposit": 100 //定金
  553. * "photo": [ //照片
  554. * ]
  555. * "paidinfo": {
  556. * "id": 3,
  557. * "order_id": "27",
  558. * "order_price": 199, //订单价格
  559. * "type": 0, //付款类型 0:全款 1:定金
  560. * "paid_price": 199, //支付金额
  561. * }
  562. * }
  563. *
  564. * }
  565. * @apiErrorExample {json} Error-Response:
  566. * HTTP/1.1 400 Bad Request
  567. * {
  568. * "state": false,
  569. * "code": 1000,
  570. * "message": "传入参数不正确",
  571. * "data": null or []
  572. * }
  573. */
  574. public function orderDetail(Request $request)
  575. {
  576. $validator = Validator::make($request->all(),
  577. [
  578. 'order_id' => 'required',
  579. ],
  580. [
  581. 'order_id.required' => 'order_id不能为空!',
  582. ]
  583. );
  584. if ($validator->fails()) {
  585. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  586. }
  587. $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'));
  588. $photo = $order->photo;
  589. $order->photo = explode(',', $photo);
  590. $store = $order->store();
  591. $product = ProductInfoModel::find($order->product_id);
  592. $order['storename'] = $store;
  593. $order['product_name'] = $product->name;
  594. $order['service_time'] = $product->service_time;
  595. return $this->api($order);
  596. }
  597. /**
  598. * @api {post} /api/home/createbusiness 创建企业咨询
  599. * @apiDescription 创建企业咨询
  600. * @apiGroup Order
  601. * @apiPermission None
  602. * @apiVersion 0.1.0
  603. * @apiParam {string} company 公司名称
  604. * @apiParam {string} username 联系人姓名
  605. * @apiParam {string} phone 联系方式
  606. * @apiParam {string} email 邮箱
  607. * @apiParam {string} comment 拍摄要求
  608. * @apiSuccessExample {json} Success-Response:
  609. * HTTP/1.1 200 OK
  610. * {
  611. * "state": true,
  612. * "code": 0,
  613. * "message": "",
  614. * "data": {
  615. * "company": "斯尔克",
  616. * "username": "梁女士",
  617. * "phone": "13546554325",
  618. * "email": "sdfsa@163.com",
  619. * "comment": "sfaaafda",
  620. * "updated_at": "2018-08-13 06:09:07",
  621. * "created_at": "2018-08-13 06:09:07",
  622. * "id": 4
  623. * }
  624. *
  625. * }
  626. * @apiErrorExample {json} Error-Response:
  627. * HTTP/1.1 400 Bad Request
  628. * {
  629. * "state": false,
  630. * "code": 1000,
  631. * "message": "传入参数不正确",
  632. * "data": null or []
  633. * }
  634. */
  635. public function createBusiness(Request $request)
  636. {
  637. $validator = Validator::make($request->all(),
  638. [
  639. 'company' => 'required',
  640. 'username' => 'required',
  641. 'phone' => 'required',
  642. 'email' => 'required',
  643. 'comment' => 'required',
  644. ],
  645. [
  646. 'company.required' => 'company不能为空!',
  647. 'username.required' => 'username不能为空!',
  648. 'phone.required' => 'phone不能为空!',
  649. 'email.required' => 'email不能为空!',
  650. 'comment.required' => 'comment不能为空!',
  651. ]
  652. );
  653. if ($validator->fails()) {
  654. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  655. }
  656. $data = $request->all();
  657. $business = BusinessServiceModel::create($data);
  658. return $this->api($business);
  659. }
  660. /**
  661. * @api {post} /api/home/pay 订单支付
  662. * @apiDescription 订单支付
  663. * @apiGroup Order
  664. * @apiPermission Passport
  665. * @apiVersion 0.1.0
  666. * @apiParam {string} orderid 订单号,创建订单后返回
  667. * @apiParam {int} paytype 支付类型:0:全款支付;1:定金支付
  668. * @apiParam {int} [couponid] 优惠券id
  669. * @apiParam {int} [cardid] 抵扣卡id(抵扣卡和优惠券不能同时使用)
  670. * @apiSuccessExample {json} Success-Response:
  671. * HTTP/1.1 200 OK
  672. * {
  673. * "status": true,
  674. * "status_code": 0,
  675. * "message": "",
  676. * "data": {
  677. * "appId":"wx1c2357232cd25f65",
  678. * "timeStamp":"1524907589",
  679. * "nonceStr":"5ae43e45eb499",
  680. * "package":"prepay_id=wx28172629917401724160128f0238805782",
  681. * "signType":"MD5",
  682. * "paySign":"8E9CF26B2B83C22471D023CBBDC36EDF"
  683. * }
  684. * }
  685. * @apiErrorExample {json} Error-Response:
  686. * HTTP/1.1 400 Bad Request
  687. * {
  688. * "state": false,
  689. * "code": 1000,
  690. * "message": "传入参数不正确",
  691. * "data": null or []
  692. * }
  693. * 可能出现的错误代码:
  694. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  695. */
  696. public function pay(Request $request)
  697. {
  698. $validator = Validator::make($request->all(),
  699. [
  700. 'orderid' => 'required',
  701. 'paytype' => 'required',
  702. ],
  703. [
  704. 'orderid.required' => 'orderid不能为空!',
  705. 'paytype.required' => 'paytype不能为空!',
  706. ]
  707. );
  708. if ($validator->fails()) {
  709. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  710. }
  711. $user = Auth('api')->user();
  712. if (!$user) {
  713. $data = [
  714. 'code' => '401',
  715. 'msg' => 'token已过期,请重新登录',
  716. ];
  717. return $this->api($data);
  718. }
  719. $order = OrderInfoModel::find($request->get('orderid'));
  720. \Log::info($this->options());
  721. if (request('paytype') == 0) {
  722. $money = $order->price;
  723. } else {
  724. $money = $order->deposit;
  725. }
  726. session()->put($order->id . 'paytype', request('paytype'));
  727. if (request('couponid')) {
  728. $coupon = CouponInfoModel::find(request('couponid'));
  729. if ($coupon->type == 1) {
  730. $money = $money * ($coupon->discount / 10);
  731. } else {
  732. $money = $money - $coupon->discount_price;
  733. }
  734. }
  735. if (request('cardid')) {
  736. $discount = CardInfoModel::find(request('cardid'))->price;
  737. $money = $money - $discount;
  738. }
  739. $app = Factory::payment($this->options());
  740. $result = $app->order->unify([
  741. 'body' => '小相馆 -' . $order->out_trade_no,
  742. 'out_trade_no' => $order->out_trade_no,
  743. 'total_fee' => $money * 100,
  744. 'trade_type' => 'JSAPI',
  745. 'notify_url' => url('/api/home/notify'),
  746. 'openid' => $user->openid
  747. ]);
  748. \Log::info($result);
  749. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  750. $payment = Factory::payment($this->options());
  751. $jssdk = $payment->jssdk;
  752. $json = $jssdk->bridgeConfig($result['prepay_id']);
  753. \Log::info($json);
  754. return $this->api(compact('json'));
  755. } else {
  756. $msg = "签名失败,请稍后再试!";
  757. return $this->api(compact('msg'));
  758. }
  759. }
  760. //下面是回调函数
  761. public function notify()
  762. {
  763. $app = Factory::payment($this->options());
  764. \Log::info("wechat notify start!");
  765. return $app->handlePaidNotify(function ($notify, $successful) {
  766. \Log::info($notify);
  767. if ($notify['result_code'] == 'SUCCESS') {
  768. $order = OrderInfoModel::where('out_trade_no', $notify['out_trade_no'])->first();
  769. $order->status = 1;
  770. $order->save();
  771. $data['order_no'] = $notify['out_trade_no'];
  772. $data['order_id'] = $order->id;
  773. $data['order_price'] = $order->price;
  774. $data['paid_price'] = $notify['total_fee'] / 100;
  775. $data['type'] = 0;
  776. \Log::info($order->id . 'paytype');
  777. \Log::info(session()->get($order->id . 'paytype'));
  778. PaidInfoModel::create($data);
  779. } else {
  780. return $successful('通信失败,请稍后再通知我');
  781. }
  782. return true;
  783. });
  784. }
  785. /**
  786. * @api {get} /api/home/couponlist 可领取的优惠券列表
  787. * @apiDescription 可领取的优惠券列表
  788. * @apiGroup Coupon
  789. * @apiPermission Passport
  790. * @apiVersion 0.1.0
  791. * @apiSuccessExample {json} Success-Response:
  792. * HTTP/1.1 200 OK
  793. * {
  794. * "status": true,
  795. * "status_code": 0,
  796. * "message": "",
  797. * "data": [
  798. * ...
  799. * {
  800. * "id": 1,
  801. * "name": "证件照七折优惠", //优惠券名字
  802. * "type": "1", //优惠券类型 0:固定金额优惠,1:折扣优惠
  803. * "min_price": 100, //最低使用金额
  804. * "discount": "7", //折扣
  805. * "discount_price": null, //优惠金额
  806. * "product_id": "1,2", //可使用的产品id
  807. * "product":[ //可使用产品名字
  808. * "证件照",
  809. * "履历照"
  810. * ]
  811. * "count": 10, //剩余数量
  812. * "end_time": "2018-07-31", //有效期
  813. * "created_at": "2018-07-13 07:29:27",
  814. * "updated_at": "2018-07-18 06:36:38"e
  815. * }
  816. * ...
  817. * ]
  818. * }
  819. * @apiErrorExample {json} Error-Response:
  820. * HTTP/1.1 400 Bad Request
  821. * {
  822. * "state": false,
  823. * "code": 1000,
  824. * "message": "传入参数不正确",
  825. * "data": null or []
  826. * }
  827. * 可能出现的错误代码:
  828. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  829. */
  830. public function couponList(Request $request)
  831. {
  832. $user = Auth('api')->user();
  833. if (!$user) {
  834. $data = [
  835. 'code' => '401',
  836. 'msg' => 'token已过期,请重新登录',
  837. ];
  838. return $this->api($data);
  839. }
  840. $couponlist = CouponInfoModel::where('end_time', '>', Carbon::now('Asia/Shanghai'))->where('count', '>', 0)->get();
  841. foreach ($couponlist as $key => $item) {
  842. $item->product = $item->product();
  843. $had = UserCouponRelationModel::where('coupon_id', $item->id)->where('user_id', $user->id)->count();
  844. if ($had) {
  845. unset($couponlist[$key]);
  846. }
  847. }
  848. return $this->api($couponlist);
  849. }
  850. /**
  851. * @api {get} /api/home/mycoupon 我的优惠券/兑换卡列表
  852. * @apiDescription 我的优惠券/兑换卡列表
  853. * @apiGroup Coupon
  854. * @apiPermission Passport
  855. * @apiVersion 0.1.0
  856. * @apiSuccessExample {json} Success-Response:
  857. * HTTP/1.1 200 OK
  858. * {
  859. * "status": true,
  860. * "status_code": 0,
  861. * "message": "",
  862. * "data": {
  863. * "mycoupons":[
  864. * ...
  865. * {
  866. * "id": 1,
  867. * "coupon_id": 1,
  868. * "user_id": 1,
  869. * "coupon": {
  870. * "id": 1,
  871. * "name": "证件照七折优惠", //优惠券名字
  872. * "type": "1", //优惠券类型 0:固定金额优惠,1:折扣优惠
  873. * "min_price": 100, //最低使用金额
  874. * "discount": "7", //折扣
  875. * "discount_price": null, //优惠金额
  876. * "product_id": "1,2", //可使用的产品id
  877. * "product":[ //可使用产品名字
  878. * "证件照",
  879. * "履历照"
  880. * ]
  881. * "count": 10,
  882. * "end_time": "2018-07-31", //有效期
  883. * "created_at": "2018-07-13 07:29:27",
  884. * "updated_at": "2018-07-18 06:36:38"
  885. * }
  886. * }
  887. * ...
  888. * ],
  889. * "mycards": [
  890. * ...
  891. * {
  892. * "id": 7,
  893. * "card_no": "15319090241",
  894. * "price": 50,
  895. * "min_price": 100,
  896. * "status": 1,
  897. * "user_id": 1,
  898. * "remark": "梁源源--13407570861",
  899. * "end_time": "2019-07-31",
  900. * "qrcode": null,
  901. * "product_id": "1,2",
  902. * "product":[],
  903. * "deleted_at": null,
  904. * "created_at": "2018-07-18 10:17:04",
  905. * "updated_at": "2018-08-09 02:11:36"
  906. * }
  907. * ...
  908. * ],
  909. * }
  910. * }
  911. * @apiErrorExample {json} Error-Response:
  912. * HTTP/1.1 400 Bad Request
  913. * {
  914. * "state": false,
  915. * "code": 1000,
  916. * "message": "传入参数不正确",
  917. * "data": null or []
  918. * }
  919. * 可能出现的错误代码:
  920. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  921. */
  922. public function myCoupon(Request $request)
  923. {
  924. $user = Auth('api')->user();
  925. if (!$user) {
  926. $data = [
  927. 'code' => '401',
  928. 'msg' => 'token已过期,请重新登录',
  929. ];
  930. return $this->api($data);
  931. }
  932. $mycoupons = UserCouponRelationModel::where('user_id', $user->id)->with('coupon')->orderBy('created_at', 'desc')->get();
  933. $mycoupons = $mycoupons->filter(function ($value) {
  934. if (!$value->coupon) {
  935. return false;
  936. }
  937. if (strtotime($value->coupon->end_time) < strtotime(Carbon::now('Asia/Shanghai'))) {
  938. return false;
  939. } else {
  940. $value->coupon->product = CouponInfoModel::find($value->coupon->id) ? CouponInfoModel::find($value->coupon->id)->product() : '';
  941. return true;
  942. }
  943. });
  944. $mycards = CardInfoModel::where('user_id', $user->id)->where('status', 1)->where('end_time', '>', now())->get();
  945. foreach ($mycards as $card) {
  946. $card->product = $card->product();
  947. }
  948. return $this->api(compact('mycards', 'mycoupons'));
  949. }
  950. /**
  951. * @api {get} /api/home/getcoupon 领取优惠券
  952. * @apiDescription 领取优惠券
  953. * @apiGroup Coupon
  954. * @apiPermission Passport
  955. * @apiVersion 0.1.0
  956. * @apiParam {int} coupon_id 优惠券ID
  957. * @apiSuccessExample {json} Success-Response:
  958. * HTTP/1.1 200 OK
  959. * {
  960. * "status": true,
  961. * "status_code": 0,
  962. * "message": "",
  963. * "data": {
  964. * "res": true //领取成功
  965. * }
  966. * }
  967. * @apiErrorExample {json} Error-Response:
  968. * HTTP/1.1 400 Bad Request
  969. * {
  970. * "state": false,
  971. * "code": 1000,
  972. * "message": "传入参数不正确",
  973. * "data": null or []
  974. * }
  975. *
  976. * {
  977. * "status": true,
  978. * "status_code": 0,
  979. * "message": "",
  980. * "data": {
  981. * "code": 10001,
  982. * "msg": "你已经领取过改优惠券,不能重复领取!"
  983. * }
  984. * }
  985. * 可能出现的错误代码:
  986. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  987. * 1001 CLIENT_WRONG_PARAMS 你已经领取过改优惠券,不能重复领取
  988. */
  989. public function getCoupon(Request $request)
  990. {
  991. $validator = Validator::make($request->all(),
  992. [
  993. 'coupon_id' => 'required'
  994. ],
  995. [
  996. 'coupon_id.required' => 'coupon_id不能为空!'
  997. ]
  998. );
  999. if ($validator->fails()) {
  1000. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1001. }
  1002. $user = Auth('api')->user();
  1003. if (!$user) {
  1004. $data = [
  1005. 'code' => '401',
  1006. 'msg' => 'token已过期,请重新登录',
  1007. ];
  1008. return $this->api($data);
  1009. }
  1010. $had = UserCouponRelationModel::where('coupon_id', request('coupon_id'))->where('user_id', $user->id)->count();
  1011. if ($had) {
  1012. $data = [
  1013. 'code' => 10001,
  1014. 'msg' => '你已经领取过改优惠券,不能重复领取!'
  1015. ];
  1016. return $this->api($data);
  1017. }
  1018. $coupon = CouponInfoModel::find(request('coupon_id'));
  1019. if (!$coupon) {
  1020. $data = [
  1021. 'code' => 10002,
  1022. 'msg' => '改优惠券已被领取完毕!'
  1023. ];
  1024. return $this->api($data);
  1025. }
  1026. if ($coupon->count > 0) {
  1027. $coupon->count = $coupon->count - 1;
  1028. $res = $coupon->save();
  1029. $data = $request->all();
  1030. $data['user_id'] = $user->id;
  1031. UserCouponRelationModel::create($data);
  1032. return $this->api(compact('res'));
  1033. } else {
  1034. $data = [
  1035. 'code' => 10002,
  1036. 'msg' => '改优惠券已被领取完毕!'
  1037. ];
  1038. return $this->api($data);
  1039. }
  1040. }
  1041. /**
  1042. * @api {get} /api/home/activecard 激活兑换卡
  1043. * @apiDescription 激活兑换卡
  1044. * @apiGroup Coupon
  1045. * @apiPermission Passport
  1046. * @apiVersion 0.1.0
  1047. * @apiParam {int} card_no 兑换卡编号
  1048. * @apiSuccessExample {json} Success-Response:
  1049. * HTTP/1.1 200 OK
  1050. * {
  1051. * "status": true,
  1052. * "status_code": 0,
  1053. * "message": "",
  1054. * "data": true
  1055. * }
  1056. * @apiErrorExample {json} Error-Response:
  1057. * HTTP/1.1 400 Bad Request
  1058. * {
  1059. * "state": false,
  1060. * "code": 1000,
  1061. * "message": "传入参数不正确",
  1062. * "data": null or []
  1063. * }
  1064. *
  1065. * {
  1066. * "status": true,
  1067. * "status_code": 0,
  1068. * "message": "",
  1069. * "data": {
  1070. * "code": "10006",
  1071. * "msg": "该兑换卡已经失效"
  1072. * }
  1073. * }
  1074. * 可能出现的错误代码:
  1075. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1076. * 1005 CLIENT_WRONG_PARAMS 该兑换卡不存在
  1077. * 1006 CLIENT_WRONG_PARAMS 该兑换卡已经失效
  1078. */
  1079. public function activeCard(Request $request)
  1080. {
  1081. $validator = Validator::make($request->all(),
  1082. [
  1083. 'card_no' => 'required',
  1084. ],
  1085. [
  1086. 'card_id.required' => 'card_no不能为空!'
  1087. ]
  1088. );
  1089. if ($validator->fails()) {
  1090. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1091. }
  1092. $user = Auth('api')->user();
  1093. if (!$user) {
  1094. $data = [
  1095. 'code' => '401',
  1096. 'msg' => 'token已过期,请重新登录',
  1097. ];
  1098. return $this->api($data);
  1099. }
  1100. $isExisted = CardInfoModel::where('card_no', request('card_no'))->count();
  1101. if (!$isExisted) {
  1102. $data = [
  1103. 'code' => '10005',
  1104. 'msg' => '该兑换卡不存在',
  1105. ];
  1106. return $this->api($data);
  1107. }
  1108. $isActived = CardInfoModel::where('card_no', request('card_no'))->whereIn('status', [1, 2])->first();
  1109. $end_time = CardInfoModel::where('card_no', request('card_no'))->first()->end_time;
  1110. if ($isActived || ($end_time < now())) {
  1111. $data = [
  1112. 'code' => '10006',
  1113. 'msg' => '该兑换卡已经失效',
  1114. ];
  1115. return $this->api($data);
  1116. }
  1117. $card = CardInfoModel::where('card_no', request('card_no'))->first();
  1118. $card->status = 1;
  1119. $card->user_id = $user->id;
  1120. $ok = $card->save();
  1121. return $this->api($ok);
  1122. }
  1123. /**
  1124. * @api {get} /api/home/usablecoupon 获取订单可用的优惠券/兑换卡
  1125. * @apiDescription 获取订单可用的优惠券/兑换卡
  1126. * @apiGroup Coupon
  1127. * @apiPermission Passport
  1128. * @apiVersion 0.1.0
  1129. * @apiParam {int} product_id 产品规格ID,订单中获取
  1130. * @apiSuccessExample {json} Success-Response:
  1131. * HTTP/1.1 200 OK
  1132. * {
  1133. * "status": true,
  1134. * "status_code": 0,
  1135. * "message": "",
  1136. * "data":{
  1137. * "usablecoupons":[ //可用的优惠券
  1138. * ...
  1139. * {
  1140. * "id": 1,
  1141. * "coupon_id": 1,
  1142. * "user_id": 1,
  1143. * "coupon": {
  1144. * "id": 1,
  1145. * "name": "证件照七折优惠",
  1146. * "type": "1",
  1147. * "min_price": 100,
  1148. * "discount": "7",
  1149. * "discount_price": null,
  1150. * "product_id": "1,2",
  1151. * "product":[ //可使用产品名字
  1152. * "证件照",
  1153. * "履历照"
  1154. * ],
  1155. * "count": 7,
  1156. * "end_time": "2018-07-31",
  1157. * "created_at": "2018-07-13 07:29:27",
  1158. * "updated_at": "2018-07-21 07:41:59"
  1159. * }
  1160. * }
  1161. * ...
  1162. * ],
  1163. *
  1164. * "usableCards": [ //可用兑换卡
  1165. * ...
  1166. * {
  1167. * "id": 7,
  1168. * "card_no": "15319090241",
  1169. * "price": 50,
  1170. * "min_price": 100,
  1171. * "status": 1,
  1172. * "user_id": 1,
  1173. * "remark": "",
  1174. * "end_time": "2019-07-31",
  1175. * "product_id": "1,2",
  1176. * "product":[ //可使用产品名字
  1177. * "证件照",
  1178. * "履历照"
  1179. * ],
  1180. * "deleted_at": null,
  1181. * "created_at": "2018-07-18 10:17:04",
  1182. * "updated_at": "2018-08-09 02:11:36"
  1183. * }
  1184. * ]
  1185. * ...
  1186. * }
  1187. * }
  1188. * @apiErrorExample {json} Error-Response:
  1189. * HTTP/1.1 400 Bad Request
  1190. * {
  1191. * "state": false,
  1192. * "code": 1000,
  1193. * "message": "传入参数不正确",
  1194. * "data": null or []
  1195. * }
  1196. * 可能出现的错误代码:
  1197. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1198. */
  1199. public function usableCoupon(Request $request)
  1200. {
  1201. $validator = Validator::make($request->all(),
  1202. [
  1203. 'product_id' => 'required'
  1204. ],
  1205. [
  1206. 'product_id.required' => 'product_id不能为空!'
  1207. ]
  1208. );
  1209. $user = Auth('api')->user();
  1210. if (!$user) {
  1211. $data = [
  1212. 'code' => '401',
  1213. 'msg' => 'token已过期,请重新登录',
  1214. ];
  1215. return $this->api($data);
  1216. }
  1217. if ($validator->fails()) {
  1218. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1219. }
  1220. $mycoupons = UserCouponRelationModel::where('user_id', $user->id)->with('coupon')->orderBy('created_at', 'desc')->get();
  1221. $usablecoupons = $mycoupons->filter(function ($value) {
  1222. $flag = false;
  1223. $product = ProductInfoModel::find(request('product_id'));
  1224. if (!$value->coupon) {
  1225. $flag = false;
  1226. return $flag;
  1227. }
  1228. $products = explode(',', $value->coupon->product_id);
  1229. $price = $product->current_price ? $product->current_price : $product->origin_price;
  1230. if ((strtotime($value->coupon->end_time) > strtotime(Carbon::now('Asia/Shanghai'))) && in_array($product->category_id, $products) && ($price >= $value->coupon->min_price)) {
  1231. $value->coupon->product = CouponInfoModel::find($value->coupon->id) ? CouponInfoModel::find($value->coupon->id)->product() : '';
  1232. $flag = true;
  1233. } else {
  1234. $flag = false;
  1235. }
  1236. return $flag;
  1237. });
  1238. $usableCards = CardInfoModel::where('user_id', $user->id)->where('status', 1)->where('end_time', '>=', now())->where('product_id', 'like', '%' . request('product_id') . '%')->get();
  1239. foreach ($usableCards as $card) {
  1240. $card->product = $card->product();
  1241. }
  1242. return $this->api(compact('usablecoupons', 'usableCards'));
  1243. }
  1244. /**
  1245. * @api {get} /api/home/refund 申请退款
  1246. * @apiDescription 申请退款
  1247. * @apiGroup Order
  1248. * @apiPermission Passport
  1249. * @apiVersion 0.1.0
  1250. * @apiParam {int} order_id 产品规格ID,订单中获取
  1251. * @apiSuccessExample {json} Success-Response:
  1252. * HTTP/1.1 200 OK
  1253. * {
  1254. * "status": true,
  1255. * "status_code": 0,
  1256. * "message": "",
  1257. * "data": {
  1258. * "code": 200,
  1259. * "msg": "退款已申请,请等待商家审核!"
  1260. * }
  1261. * }
  1262. * @apiErrorExample {json} Error-Response:
  1263. * HTTP/1.1 400 Bad Request
  1264. * {
  1265. * "state": false,
  1266. * "code": 1000,
  1267. * "message": "传入参数不正确",
  1268. * "data": null or []
  1269. * }
  1270. *
  1271. * {
  1272. * "status": true,
  1273. * "status_code": 0,
  1274. * "message": "",
  1275. * "data": {
  1276. * "code": 10003,
  1277. * "msg": "该订单不能退款,如有疑问,请联系商家!"
  1278. * }
  1279. * }
  1280. * 可能出现的错误代码:
  1281. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1282. */
  1283. public function refund(Request $request)
  1284. {
  1285. $validator = Validator::make($request->all(),
  1286. [
  1287. 'order_id' => 'required'
  1288. ],
  1289. [
  1290. 'order_id.required' => 'order_id不能为空!'
  1291. ]
  1292. );
  1293. $user = Auth('api')->user();
  1294. if (!$user) {
  1295. $data = [
  1296. 'code' => '401',
  1297. 'msg' => 'token已过期,请重新登录',
  1298. ];
  1299. return $this->api($data);
  1300. }
  1301. if ($validator->fails()) {
  1302. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1303. }
  1304. $order = OrderInfoModel::find(request('order_id'));
  1305. if ($order->status != 1) {
  1306. $data = [
  1307. 'code' => 10003,
  1308. 'msg' => '该订单不能退款,如有疑问,请联系商家!'
  1309. ];
  1310. return $this->api($data);
  1311. }
  1312. $order->status = 3;
  1313. $ok = $order->save();
  1314. if ($ok) {
  1315. $data = [
  1316. 'code' => 200,
  1317. 'msg' => '退款已申请,请等待商家审核!'
  1318. ];
  1319. return $this->api($data);
  1320. }
  1321. }
  1322. //格式化排期
  1323. function cut_time_part($start, $end, $min = 30, $format = true)
  1324. {
  1325. $start = strtotime($start);
  1326. $end = strtotime($end);
  1327. $nums = ($end - $start) / ($min * 60);
  1328. $parts = ($end - $start) / $nums;
  1329. $last = ($end - $start) % $nums;
  1330. if ($last > 0) {
  1331. $parts = ($end - $start - $last) / $nums;
  1332. }
  1333. for ($i = 1; $i <= $nums; $i++) {
  1334. $_end = $start + $parts * $i;
  1335. $arr[] = array($start + $parts * ($i - 1), $_end);
  1336. }
  1337. $len = count($arr) - 1;
  1338. $arr[$len][1] = $arr[$len][1] + $last;
  1339. if ($format) {
  1340. foreach ($arr as $key => $value) {
  1341. $arr[$key] = date("Y-m-d H:i:s", $value[0]);
  1342. }
  1343. }
  1344. return $arr;
  1345. }
  1346. //格式化排期
  1347. public function formatSchedule($arrs, $max, $storeid)
  1348. {
  1349. $arr = [];
  1350. foreach ($arrs as $k => $item) {
  1351. $count = UserScheduleModel::where('time', $item)->where('store_id', $storeid)->count();
  1352. $remain = $max - $count;
  1353. $arr[$k]['time'] = $item;
  1354. $arr[$k]['remain'] = $remain;
  1355. }
  1356. return $arr;
  1357. }
  1358. public function options()
  1359. {
  1360. return [
  1361. 'app_id' => $this->setting->app_id,
  1362. 'mch_id' => $this->setting->mch_id,
  1363. 'key' => $this->setting->api_key,
  1364. 'notify_url' => url('/api/home/notify'),
  1365. 'sandbox' => false,
  1366. ];
  1367. }
  1368. }