HomeController.php 53 KB

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