HomeController.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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('sort', '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. $order->paytype = request('paytype');
  727. $order->out_trade_no = 'xxg' . date('Ymdhms');
  728. $order->save();
  729. if (request('couponid')) {
  730. $coupon = CouponInfoModel::find(request('couponid'));
  731. if ($coupon->type == 1) {
  732. $money = $money * ($coupon->discount / 10);
  733. } else {
  734. $money = $money - $coupon->discount_price;
  735. }
  736. }
  737. if (request('cardid')) {
  738. $discount = CardInfoModel::find(request('cardid'))->price;
  739. $money = $money - $discount;
  740. }
  741. $app = Factory::payment($this->options());
  742. $result = $app->order->unify([
  743. 'body' => '小相馆 -' . $order->out_trade_no,
  744. 'out_trade_no' => $order->out_trade_no,
  745. 'total_fee' => $money * 100,
  746. 'trade_type' => 'JSAPI',
  747. 'notify_url' => url('/api/home/notify'),
  748. 'openid' => $user->openid
  749. ]);
  750. \Log::info($result);
  751. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  752. $payment = Factory::payment($this->options());
  753. $jssdk = $payment->jssdk;
  754. $json = $jssdk->bridgeConfig($result['prepay_id']);
  755. \Log::info($json);
  756. return $this->api(compact('json'));
  757. } else {
  758. $msg = "签名失败,请稍后再试!";
  759. return $this->api(compact('msg'));
  760. }
  761. }
  762. //下面是回调函数
  763. public function notify()
  764. {
  765. $app = Factory::payment($this->options());
  766. \Log::info("wechat notify start!");
  767. return $app->handlePaidNotify(function ($notify, $successful) {
  768. \Log::info($notify);
  769. if ($notify['result_code'] == 'SUCCESS') {
  770. $order = OrderInfoModel::where('out_trade_no', $notify['out_trade_no'])->first();
  771. $order->status = 1;
  772. $order->save();
  773. $data['order_no'] = $notify['out_trade_no'];
  774. $data['order_id'] = $order->id;
  775. $data['order_price'] = $order->price;
  776. $data['paid_price'] = $notify['total_fee'] / 100;
  777. $data['type'] = $order->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. $mycoupons = $mycoupons->toArray();
  945. $mycoupons = array_values($mycoupons);
  946. $mycards = CardInfoModel::where('user_id', $user->id)->where('status', 1)->where('end_time', '>', now())->get();
  947. foreach ($mycards as $card) {
  948. $card->product = $card->product();
  949. }
  950. return $this->api(compact('mycards', 'mycoupons'));
  951. }
  952. /**
  953. * @api {get} /api/home/getcoupon 领取优惠券
  954. * @apiDescription 领取优惠券
  955. * @apiGroup Coupon
  956. * @apiPermission Passport
  957. * @apiVersion 0.1.0
  958. * @apiParam {int} coupon_id 优惠券ID
  959. * @apiSuccessExample {json} Success-Response:
  960. * HTTP/1.1 200 OK
  961. * {
  962. * "status": true,
  963. * "status_code": 0,
  964. * "message": "",
  965. * "data": {
  966. * "res": true //领取成功
  967. * }
  968. * }
  969. * @apiErrorExample {json} Error-Response:
  970. * HTTP/1.1 400 Bad Request
  971. * {
  972. * "state": false,
  973. * "code": 1000,
  974. * "message": "传入参数不正确",
  975. * "data": null or []
  976. * }
  977. *
  978. * {
  979. * "status": true,
  980. * "status_code": 0,
  981. * "message": "",
  982. * "data": {
  983. * "code": 10001,
  984. * "msg": "你已经领取过改优惠券,不能重复领取!"
  985. * }
  986. * }
  987. * 可能出现的错误代码:
  988. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  989. * 1001 CLIENT_WRONG_PARAMS 你已经领取过改优惠券,不能重复领取
  990. */
  991. public function getCoupon(Request $request)
  992. {
  993. $validator = Validator::make($request->all(),
  994. [
  995. 'coupon_id' => 'required'
  996. ],
  997. [
  998. 'coupon_id.required' => 'coupon_id不能为空!'
  999. ]
  1000. );
  1001. if ($validator->fails()) {
  1002. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1003. }
  1004. $user = Auth('api')->user();
  1005. if (!$user) {
  1006. $data = [
  1007. 'code' => '401',
  1008. 'msg' => 'token已过期,请重新登录',
  1009. ];
  1010. return $this->api($data);
  1011. }
  1012. $had = UserCouponRelationModel::where('coupon_id', request('coupon_id'))->where('user_id', $user->id)->count();
  1013. if ($had) {
  1014. $data = [
  1015. 'code' => 10001,
  1016. 'msg' => '你已经领取过改优惠券,不能重复领取!'
  1017. ];
  1018. return $this->api($data);
  1019. }
  1020. $coupon = CouponInfoModel::find(request('coupon_id'));
  1021. if (!$coupon) {
  1022. $data = [
  1023. 'code' => 10002,
  1024. 'msg' => '改优惠券已被领取完毕!'
  1025. ];
  1026. return $this->api($data);
  1027. }
  1028. if ($coupon->count > 0) {
  1029. $coupon->count = $coupon->count - 1;
  1030. $res = $coupon->save();
  1031. $data = $request->all();
  1032. $data['user_id'] = $user->id;
  1033. UserCouponRelationModel::create($data);
  1034. return $this->api(compact('res'));
  1035. } else {
  1036. $data = [
  1037. 'code' => 10002,
  1038. 'msg' => '改优惠券已被领取完毕!'
  1039. ];
  1040. return $this->api($data);
  1041. }
  1042. }
  1043. /**
  1044. * @api {get} /api/home/activecard 激活兑换卡
  1045. * @apiDescription 激活兑换卡
  1046. * @apiGroup Coupon
  1047. * @apiPermission Passport
  1048. * @apiVersion 0.1.0
  1049. * @apiParam {int} card_no 兑换卡编号
  1050. * @apiSuccessExample {json} Success-Response:
  1051. * HTTP/1.1 200 OK
  1052. * {
  1053. * "status": true,
  1054. * "status_code": 0,
  1055. * "message": "",
  1056. * "data": true
  1057. * }
  1058. * @apiErrorExample {json} Error-Response:
  1059. * HTTP/1.1 400 Bad Request
  1060. * {
  1061. * "state": false,
  1062. * "code": 1000,
  1063. * "message": "传入参数不正确",
  1064. * "data": null or []
  1065. * }
  1066. *
  1067. * {
  1068. * "status": true,
  1069. * "status_code": 0,
  1070. * "message": "",
  1071. * "data": {
  1072. * "code": "10006",
  1073. * "msg": "该兑换卡已经失效"
  1074. * }
  1075. * }
  1076. * 可能出现的错误代码:
  1077. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1078. * 1005 CLIENT_WRONG_PARAMS 该兑换卡不存在
  1079. * 1006 CLIENT_WRONG_PARAMS 该兑换卡已经失效
  1080. */
  1081. public function activeCard(Request $request)
  1082. {
  1083. $validator = Validator::make($request->all(),
  1084. [
  1085. 'card_no' => 'required',
  1086. ],
  1087. [
  1088. 'card_id.required' => 'card_no不能为空!'
  1089. ]
  1090. );
  1091. if ($validator->fails()) {
  1092. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1093. }
  1094. $user = Auth('api')->user();
  1095. if (!$user) {
  1096. $data = [
  1097. 'code' => '401',
  1098. 'msg' => 'token已过期,请重新登录',
  1099. ];
  1100. return $this->api($data);
  1101. }
  1102. $isExisted = CardInfoModel::where('card_no', request('card_no'))->count();
  1103. if (!$isExisted) {
  1104. $data = [
  1105. 'code' => '10005',
  1106. 'msg' => '该兑换卡不存在',
  1107. ];
  1108. return $this->api($data);
  1109. }
  1110. $isActived = CardInfoModel::where('card_no', request('card_no'))->whereIn('status', [1, 2])->first();
  1111. $end_time = CardInfoModel::where('card_no', request('card_no'))->first()->end_time;
  1112. if ($isActived || ($end_time < now())) {
  1113. $data = [
  1114. 'code' => '10006',
  1115. 'msg' => '该兑换卡已经失效',
  1116. ];
  1117. return $this->api($data);
  1118. }
  1119. $card = CardInfoModel::where('card_no', request('card_no'))->first();
  1120. $card->status = 1;
  1121. $card->user_id = $user->id;
  1122. $ok = $card->save();
  1123. return $this->api($ok);
  1124. }
  1125. /**
  1126. * @api {get} /api/home/usablecoupon 获取订单可用的优惠券/兑换卡
  1127. * @apiDescription 获取订单可用的优惠券/兑换卡
  1128. * @apiGroup Coupon
  1129. * @apiPermission Passport
  1130. * @apiVersion 0.1.0
  1131. * @apiParam {int} product_id 产品规格ID,订单中获取
  1132. * @apiSuccessExample {json} Success-Response:
  1133. * HTTP/1.1 200 OK
  1134. * {
  1135. * "status": true,
  1136. * "status_code": 0,
  1137. * "message": "",
  1138. * "data":{
  1139. * "usablecoupons":[ //可用的优惠券
  1140. * ...
  1141. * {
  1142. * "id": 1,
  1143. * "coupon_id": 1,
  1144. * "user_id": 1,
  1145. * "coupon": {
  1146. * "id": 1,
  1147. * "name": "证件照七折优惠",
  1148. * "type": "1",
  1149. * "min_price": 100,
  1150. * "discount": "7",
  1151. * "discount_price": null,
  1152. * "product_id": "1,2",
  1153. * "product":[ //可使用产品名字
  1154. * "证件照",
  1155. * "履历照"
  1156. * ],
  1157. * "count": 7,
  1158. * "end_time": "2018-07-31",
  1159. * "created_at": "2018-07-13 07:29:27",
  1160. * "updated_at": "2018-07-21 07:41:59"
  1161. * }
  1162. * }
  1163. * ...
  1164. * ],
  1165. *
  1166. * "usableCards": [ //可用兑换卡
  1167. * ...
  1168. * {
  1169. * "id": 7,
  1170. * "card_no": "15319090241",
  1171. * "price": 50,
  1172. * "min_price": 100,
  1173. * "status": 1,
  1174. * "user_id": 1,
  1175. * "remark": "",
  1176. * "end_time": "2019-07-31",
  1177. * "product_id": "1,2",
  1178. * "product":[ //可使用产品名字
  1179. * "证件照",
  1180. * "履历照"
  1181. * ],
  1182. * "deleted_at": null,
  1183. * "created_at": "2018-07-18 10:17:04",
  1184. * "updated_at": "2018-08-09 02:11:36"
  1185. * }
  1186. * ]
  1187. * ...
  1188. * }
  1189. * }
  1190. * @apiErrorExample {json} Error-Response:
  1191. * HTTP/1.1 400 Bad Request
  1192. * {
  1193. * "state": false,
  1194. * "code": 1000,
  1195. * "message": "传入参数不正确",
  1196. * "data": null or []
  1197. * }
  1198. * 可能出现的错误代码:
  1199. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1200. */
  1201. public function usableCoupon(Request $request)
  1202. {
  1203. $validator = Validator::make($request->all(),
  1204. [
  1205. 'product_id' => 'required'
  1206. ],
  1207. [
  1208. 'product_id.required' => 'product_id不能为空!'
  1209. ]
  1210. );
  1211. $user = Auth('api')->user();
  1212. if (!$user) {
  1213. $data = [
  1214. 'code' => '401',
  1215. 'msg' => 'token已过期,请重新登录',
  1216. ];
  1217. return $this->api($data);
  1218. }
  1219. if ($validator->fails()) {
  1220. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1221. }
  1222. $mycoupons = UserCouponRelationModel::where('user_id', $user->id)->with('coupon')->orderBy('created_at', 'desc')->get();
  1223. $usablecoupons = $mycoupons->filter(function ($value) {
  1224. $flag = false;
  1225. $product = ProductInfoModel::find(request('product_id'));
  1226. if (!$value->coupon) {
  1227. $flag = false;
  1228. return $flag;
  1229. }
  1230. $products = explode(',', $value->coupon->product_id);
  1231. $price = $product->current_price ? $product->current_price : $product->origin_price;
  1232. if ((strtotime($value->coupon->end_time) > strtotime(Carbon::now('Asia/Shanghai'))) && in_array($product->category_id, $products) && ($price >= $value->coupon->min_price)) {
  1233. $value->coupon->product = CouponInfoModel::find($value->coupon->id) ? CouponInfoModel::find($value->coupon->id)->product() : '';
  1234. $flag = true;
  1235. } else {
  1236. $flag = false;
  1237. }
  1238. return $flag;
  1239. });
  1240. $usablecoupons = $usablecoupons->toArray();
  1241. $usablecoupons = array_values($usablecoupons);
  1242. $current_product = ProductInfoModel::find(request('product_id'));
  1243. $usableCards = CardInfoModel::where('user_id', $user->id)->where('status', 1)->where('end_time', '>=', now())->where('product_id', 'like', '%' . request('product_id') . '%')->where('min_price','<=',$current_product->origin_price)->get();
  1244. foreach ($usableCards as $card) {
  1245. $card->product = $card->product();
  1246. }
  1247. return $this->api(compact('usablecoupons', 'usableCards'));
  1248. }
  1249. /**
  1250. * @api {get} /api/home/refund 申请退款
  1251. * @apiDescription 申请退款
  1252. * @apiGroup Order
  1253. * @apiPermission Passport
  1254. * @apiVersion 0.1.0
  1255. * @apiParam {int} order_id 产品规格ID,订单中获取
  1256. * @apiSuccessExample {json} Success-Response:
  1257. * HTTP/1.1 200 OK
  1258. * {
  1259. * "status": true,
  1260. * "status_code": 0,
  1261. * "message": "",
  1262. * "data": {
  1263. * "code": 200,
  1264. * "msg": "退款已申请,请等待商家审核!"
  1265. * }
  1266. * }
  1267. * @apiErrorExample {json} Error-Response:
  1268. * HTTP/1.1 400 Bad Request
  1269. * {
  1270. * "state": false,
  1271. * "code": 1000,
  1272. * "message": "传入参数不正确",
  1273. * "data": null or []
  1274. * }
  1275. *
  1276. * {
  1277. * "status": true,
  1278. * "status_code": 0,
  1279. * "message": "",
  1280. * "data": {
  1281. * "code": 10003,
  1282. * "msg": "该订单不能退款,如有疑问,请联系商家!"
  1283. * }
  1284. * }
  1285. * 可能出现的错误代码:
  1286. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1287. */
  1288. public function refund(Request $request)
  1289. {
  1290. $validator = Validator::make($request->all(),
  1291. [
  1292. 'order_id' => 'required'
  1293. ],
  1294. [
  1295. 'order_id.required' => 'order_id不能为空!'
  1296. ]
  1297. );
  1298. $user = Auth('api')->user();
  1299. if (!$user) {
  1300. $data = [
  1301. 'code' => '401',
  1302. 'msg' => 'token已过期,请重新登录',
  1303. ];
  1304. return $this->api($data);
  1305. }
  1306. if ($validator->fails()) {
  1307. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1308. }
  1309. $order = OrderInfoModel::find(request('order_id'));
  1310. if ($order->status != 1) {
  1311. $data = [
  1312. 'code' => 10003,
  1313. 'msg' => '该订单不能退款,如有疑问,请联系商家!'
  1314. ];
  1315. return $this->api($data);
  1316. }
  1317. $order->status = 3;
  1318. $ok = $order->save();
  1319. if ($ok) {
  1320. $data = [
  1321. 'code' => 200,
  1322. 'msg' => '退款已申请,请等待商家审核!'
  1323. ];
  1324. return $this->api($data);
  1325. }
  1326. }
  1327. //格式化排期
  1328. function cut_time_part($start, $end, $min = 30, $format = true)
  1329. {
  1330. $start = strtotime($start);
  1331. $end = strtotime($end);
  1332. $nums = ($end - $start) / ($min * 60);
  1333. $parts = ($end - $start) / $nums;
  1334. $last = ($end - $start) % $nums;
  1335. if ($last > 0) {
  1336. $parts = ($end - $start - $last) / $nums;
  1337. }
  1338. for ($i = 1; $i <= $nums; $i++) {
  1339. $_end = $start + $parts * $i;
  1340. $arr[] = array($start + $parts * ($i - 1), $_end);
  1341. }
  1342. $len = count($arr) - 1;
  1343. $arr[$len][1] = $arr[$len][1] + $last;
  1344. if ($format) {
  1345. foreach ($arr as $key => $value) {
  1346. $arr[$key] = date("Y-m-d H:i:s", $value[0]);
  1347. }
  1348. }
  1349. return $arr;
  1350. }
  1351. //格式化排期
  1352. public function formatSchedule($arrs, $max, $storeid)
  1353. {
  1354. $arr = [];
  1355. foreach ($arrs as $k => $item) {
  1356. $count = UserScheduleModel::where('time', $item)->where('store_id', $storeid)->count();
  1357. $remain = $max - $count;
  1358. $arr[$k]['time'] = $item;
  1359. $arr[$k]['remain'] = $remain;
  1360. }
  1361. return $arr;
  1362. }
  1363. public function options()
  1364. {
  1365. return [
  1366. 'app_id' => $this->setting->app_id,
  1367. 'mch_id' => $this->setting->mch_id,
  1368. 'key' => $this->setting->api_key,
  1369. 'notify_url' => url('/api/home/notify'),
  1370. 'sandbox' => false,
  1371. ];
  1372. }
  1373. }