HomeController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\IntroductionInfoModel;
  4. use App\Models\MajorInfoModel;
  5. use App\Models\OrderInfoModel;
  6. use App\Models\PaidSettingModel;
  7. use App\Models\QueryInfoModel;
  8. use App\Models\StudentCountModel;
  9. use App\Models\UserInfoModel;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. use App\Services\Base\ErrorCode;
  13. use Validator, Response;
  14. use EasyWeChat\Factory;
  15. class HomeController extends Controller
  16. {
  17. protected $app;
  18. public function __construct()
  19. {
  20. $config = [
  21. 'app_id' => 'wxea7a26e5da5b46f2',
  22. 'secret' => '769802a6f23aac585e355ce0e326aa03',
  23. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  24. 'response_type' => 'array',
  25. ];
  26. $this->app = Factory::miniProgram($config);
  27. }
  28. /**
  29. * @api {post} /api/home/login 登陆(login)
  30. * @apiDescription 登陆(login)
  31. * @apiGroup 高考助手
  32. * @apiPermission none
  33. * @apiVersion 0.1.0
  34. * @apiParam {string} [code](必填)
  35. * @apiParam {string} [nickName]
  36. * @apiParam {string} [avatar]
  37. * @apiSuccessExample {json} Success-Response:
  38. * HTTP/1.1 200 OK
  39. * {
  40. * "status": true,
  41. * "status_code": 0,
  42. * "message": "",
  43. * "data": {
  44. * "userinfo": {
  45. * "id": "",
  46. * "nickname": "",
  47. * "openid": "",
  48. * "has_agreed": "" //1:已同意说明,进入主页面;0:为同意协议,进入协议说明页面
  49. * }
  50. *
  51. * }
  52. * }
  53. * @apiErrorExample {json} Error-Response:
  54. * HTTP/1.1 400 Bad Request
  55. * {
  56. * "state": false,
  57. * "code": 1000,
  58. * "message": "传入参数不正确",
  59. * "data": null or []
  60. * }
  61. * 可能出现的错误代码:
  62. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  63. */
  64. public function login(Request $request)
  65. {
  66. $code = $request->get('code');
  67. $session = $this->app->auth->session($code);
  68. $openid = $session['openid'];
  69. $userinfo = UserInfoModel::where('openid', $openid)->first(['id', 'nickname', 'openid', 'has_agreed']);
  70. if ($userinfo) {
  71. return $this->api(compact('userinfo'));
  72. } else {
  73. $data['openid'] = $openid;
  74. $data['nickname'] = $request->get('nickName');
  75. $data['avatar'] = $request->get('avatar');
  76. $data['has_agreed'] = 0;
  77. $userinfo = UserInfoModel::create($data);
  78. return $this->api(compact('userinfo'));
  79. }
  80. }
  81. /**
  82. * @api {get} /api/home/getintroduction 获取使用说用及协议
  83. * @apiDescription 获取使用说用及协议
  84. * @apiGroup 高考助手
  85. * @apiPermission none
  86. * @apiVersion 0.1.0
  87. * @apiSuccessExample {json} Success-Response:
  88. * HTTP/1.1 200 OK
  89. * {
  90. * "status": true,
  91. * "status_code": 0,
  92. * "message": "",
  93. * "data": {
  94. * "list": [
  95. *
  96. * ]
  97. *
  98. * }
  99. * }
  100. * @apiErrorExample {json} Error-Response:
  101. * HTTP/1.1 400 Bad Request
  102. * {
  103. * "state": false,
  104. * "code": 1000,
  105. * "message": "传入参数不正确",
  106. * "data": null or []
  107. * }
  108. * 可能出现的错误代码:
  109. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  110. */
  111. public function getIntroduction()
  112. {
  113. $list = IntroductionInfoModel::get(['title', 'content', 'type']);
  114. return $this->api(compact('list'));
  115. }
  116. /**
  117. * @api {post} /api/home/agreeintroduction 同意使用说用及协议
  118. * @apiDescription 同意使用说用及协议
  119. * @apiGroup 高考助手
  120. * @apiPermission none
  121. * @apiVersion 0.1.0
  122. * @apiParam {int} [userid] 用户ID(必填)
  123. * @apiSuccessExample {json} Success-Response:
  124. * HTTP/1.1 200 OK
  125. * {
  126. * "status": true,
  127. * "status_code": 0,
  128. * "message": "",
  129. * "data": {
  130. * "userinfo": [
  131. *
  132. * ]
  133. *
  134. * }
  135. * }
  136. * @apiErrorExample {json} Error-Response:
  137. * HTTP/1.1 400 Bad Request
  138. * {
  139. * "state": false,
  140. * "code": 1000,
  141. * "message": "传入参数不正确",
  142. * "data": null or []
  143. * }
  144. * 可能出现的错误代码:
  145. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  146. */
  147. public function agreeIntroduction(Request $request)
  148. {
  149. $userid = $request->get('userid');
  150. $data['has_agreed'] = 1;
  151. $res = UserInfoModel::where('id', $userid)->update($data);
  152. if ($res) {
  153. $userinfo = UserInfoModel::where('id', $userid)->first(['id', 'nickname', 'openid', 'has_agreed']);
  154. }
  155. return $this->api(compact('userinfo'));
  156. }
  157. /**
  158. * @api {get} /api/home/getbasedata 获取可选批次及省份
  159. * @apiDescription 获取可选批次及省份
  160. * @apiGroup 高考助手
  161. * @apiPermission none
  162. * @apiVersion 0.1.0
  163. * @apiSuccessExample {json} Success-Response:
  164. * HTTP/1.1 200 OK
  165. * {
  166. * "status": true,
  167. * "status_code": 0,
  168. * "message": "",
  169. * "data": {
  170. * "batchs": [
  171. *
  172. * ],
  173. * "provinces":[
  174. *
  175. * ]
  176. *
  177. * }
  178. * }
  179. * @apiErrorExample {json} Error-Response:
  180. * HTTP/1.1 400 Bad Request
  181. * {
  182. * "state": false,
  183. * "code": 1000,
  184. * "message": "传入参数不正确",
  185. * "data": null or []
  186. * }
  187. * 可能出现的错误代码:
  188. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  189. */
  190. public function getBaseData()
  191. {
  192. $batchs = MajorInfoModel::groupBy('batch')->pluck('batch');
  193. $provinces = [
  194. "全部省份", "安徽", "澳门", "北京", "重庆", "福建", "甘肃", "广东", "广西", "贵州", "海南", "河北", "河南", "黑龙江", "湖北", "湖南", "吉林", "江苏", "江西", "辽宁", "内蒙古", "宁夏", "青海", "山东", "山西", "陕西", "上海", "四川", "台湾", "天津", "西藏", "香港", "新疆", "云南", "浙江"
  195. ];
  196. return $this->api(compact('batchs', 'provinces'));
  197. }
  198. /**
  199. * @api {post} /api/home/getphonenumber 获取手机号
  200. * @apiDescription 获取手机号
  201. * @apiGroup 高考助手
  202. * @apiPermission none
  203. * @apiVersion 0.1.0
  204. * @apiParam {string} [code] code(必填)
  205. * @apiParam {string} [iv] iv(必填)
  206. * @apiParam {string} [encryptData] encryptData(必填)
  207. * @apiSuccessExample {json} Success-Response:
  208. * HTTP/1.1 200 OK
  209. * {
  210. * "status": true,
  211. * "status_code": 0,
  212. * "message": "",
  213. * "data": {
  214. * "decryptedData": [
  215. *
  216. * ]
  217. *
  218. * }
  219. *
  220. * }
  221. * @apiErrorExample {json} Error-Response:
  222. * HTTP/1.1 400 Bad Request
  223. * {
  224. * "state": false,
  225. * "code": 1000,
  226. * "message": "传入参数不正确",
  227. * "data": null or []
  228. * }
  229. * 可能出现的错误代码:
  230. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  231. */
  232. public function getPhoneNumber(Request $request){
  233. $code = $request->get('code');
  234. $iv = $request->get('iv');
  235. $encryptData = $request->get('encryptData');
  236. $session = $this->app->auth->session($code);
  237. \Log::info($session);
  238. $decryptedData = $this->app->encryptor->decryptData($session['session_key'], $iv, $encryptData);
  239. return $this->api(compact('decryptedData'));
  240. }
  241. /**
  242. * @api {post} /api/home/getqueried 查询免费信息
  243. * @apiDescription 查询免费信息
  244. * @apiGroup 高考助手
  245. * @apiPermission none
  246. * @apiVersion 0.1.0
  247. * @apiParam {int} [userid] 用户ID(必填)
  248. * @apiParam {string} [username] 考生姓名(必填)
  249. * @apiParam {string} [cnumber] 考号(必填)
  250. * @apiParam {string} [class] 科类(必填)
  251. * @apiParam {int} [grade] 高考成绩(必填)
  252. * @apiParam {int} [mobile] 手机号(必填)
  253. * @apiParam {string} [batch] 批次(必填)
  254. * @apiParam {string} [province] 省份(必填)
  255. * @apiParam {string} [code] 推荐码(选填)
  256. * @apiSuccessExample {json} Success-Response:
  257. * HTTP/1.1 200 OK
  258. * {
  259. * "status": true,
  260. * "status_code": 0,
  261. * "message": "",
  262. * "data": {
  263. * "rank": ""
  264. * "grade": ""
  265. * "college_count": ""
  266. *
  267. * }
  268. * }
  269. * @apiErrorExample {json} Error-Response:
  270. * HTTP/1.1 400 Bad Request
  271. * {
  272. * "state": false,
  273. * "code": 1000,
  274. * "message": "传入参数不正确",
  275. * "data": null or []
  276. * }
  277. * 可能出现的错误代码:
  278. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  279. */
  280. public function getQueried(Request $request)
  281. {
  282. $validator = Validator::make($request->all(),
  283. [
  284. 'cnumber' => 'required',
  285. 'username' => 'required',
  286. 'grade' => 'required|integer',
  287. 'mobile' => 'required'
  288. ],
  289. [
  290. 'cnumber.required' => '考号不能为空!',
  291. 'username.required' => '姓名不能为空!',
  292. 'username.required' => '姓名不能为空!',
  293. 'mobile.required' => '手机号不能为空!',
  294. 'grade.required' => '成绩不能为空!',
  295. 'grade.integer' => '请输入正确格式的成绩!',
  296. ]
  297. );
  298. if ($validator->fails()) {
  299. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  300. }
  301. /*更新用户的信息*/
  302. $this->updateUserinfo();
  303. $year = date('Y');
  304. $grade = $request->get('grade');
  305. $batch = $request->get('batch');
  306. $class = $request->get('class');
  307. $province = $request->get('province');
  308. $maxgrade = StudentCountModel::where('year', $year)->orderBy('grade', 'desc')->first()->grade;
  309. if ($grade > $maxgrade) {
  310. $rank = "前10";
  311. } else {
  312. $rank = StudentCountModel::where('year', $year)->where('grade', $grade)->first(['total'])->total;
  313. }
  314. if ($province == "全部省份") {
  315. $college = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('min_grade', "<=", $grade)->groupBy('college')->get();
  316. } else {
  317. $college = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('province', 'like', '%' . $province . '%')->where('min_grade', "<=", $grade)->groupBy('college')->get();
  318. }
  319. $college_count = count($college);
  320. /*创建查询记录*/
  321. $user = UserInfoModel::find(request('userid'));
  322. $this->createQueryInfo($user);
  323. return $this->api(compact('rank', 'grade', 'province', 'college_count', 'batch'));
  324. }
  325. /**
  326. * @api {post} /api/home/getpaidmajors 查询付费信息
  327. * @apiDescription 查询付费信息
  328. * @apiGroup 高考助手
  329. * @apiPermission none
  330. * @apiVersion 0.1.0
  331. * @apiParam {int} [userid] 用户ID(必填)
  332. * @apiParam {string} [username] 考生姓名(必填)
  333. * @apiParam {string} [batch] 批次(必填)
  334. * @apiParam {string} [province] 省份(必填)
  335. * @apiParam {string} [class] 科类(必填)
  336. * @apiSuccessExample {json} Success-Response:
  337. * HTTP/1.1 200 OK 已付费查看过,显示匹配信息
  338. * {
  339. * "status": true,
  340. * "status_code": 0,
  341. * "message": "",
  342. * "data": {
  343. * "count": ""
  344. * "major":[
  345. * ]
  346. *
  347. * }
  348. * }
  349. *
  350. *HTTP/1.1 200 OK 未曾付费,跳转到支付页面
  351. * {
  352. * "status": true,
  353. * "status_code": 0,
  354. * "message": "",
  355. * "data": {
  356. * "msg": "need to pay",
  357. * "price": ""
  358. * }
  359. * }
  360. * @apiErrorExample {json} Error-Response:
  361. * HTTP/1.1 400 Bad Request
  362. * {
  363. * "state": false,
  364. * "code": 1000,
  365. * "message": "传入参数不正确",
  366. * "data": null or []
  367. * }
  368. * 可能出现的错误代码:
  369. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  370. */
  371. public function getPaidMajors(Request $request)
  372. {
  373. $validator = Validator::make($request->all(),
  374. [
  375. 'userid' => 'required',
  376. 'batch' => 'required',
  377. 'province' => 'required',
  378. 'class' => 'required'
  379. ],
  380. [
  381. 'userid.required' => 'userid不能为空!',
  382. 'batch.required' => 'batch不能为空!',
  383. 'province.required' => 'province不能为空!',
  384. 'class.required' => 'class不能为空!',
  385. ]
  386. );
  387. if ($validator->fails()) {
  388. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  389. }
  390. if ($this->checkPaid()) {
  391. $year = date('Y');
  392. $batch = $request->get('batch');
  393. $province = $request->get('province');
  394. $class = request('class');
  395. $userid = request('userid');
  396. $_user = UserInfoModel::find($userid);
  397. $grade = $_user->grade;
  398. if ($province == "全部省份") {
  399. $major = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('min_grade', "<=", $grade)->get();
  400. } else {
  401. $major = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('province', 'like', '%' . $province . '%')->where('min_grade', "<=", $grade)->get();
  402. }
  403. $count = count($major);
  404. $this->createQueryInfo($_user, 1);
  405. return $this->api(compact('count', 'major'));
  406. } else {
  407. $price = PaidSettingModel::first();
  408. $msg = 'need to pay';
  409. $price = $price->price;
  410. return $this->api(compact('msg', 'price'));
  411. }
  412. }
  413. /**
  414. * @api {post} /api/home/pay 获取微信支付签名信息
  415. * @apiDescription 获取微信支付签名信息
  416. * @apiGroup 高考助手
  417. * @apiPermission none
  418. * @apiVersion 0.1.0
  419. * @apiParam {int} [userid] 用户ID(必填)
  420. * @apiParam {string} [price] 付费金额(必填)
  421. * @apiSuccessExample {json} Success-Response:
  422. * HTTP/1.1 200 OK
  423. * {
  424. * "status": true,
  425. * "status_code": 0,
  426. * "message": "",
  427. * "data": {
  428. * "appId":"wx1c2357232cd25f65",
  429. * "timeStamp":"1524907589",
  430. * "nonceStr":"5ae43e45eb499",
  431. * "package":"prepay_id=wx28172629917401724160128f0238805782",
  432. * "signType":"MD5",
  433. * "paySign":"8E9CF26B2B83C22471D023CBBDC36EDF"
  434. * }
  435. * }
  436. * @apiErrorExample {json} Error-Response:
  437. * HTTP/1.1 400 Bad Request
  438. * {
  439. * "state": false,
  440. * "code": 1000,
  441. * "message": "传入参数不正确",
  442. * "data": null or []
  443. * }
  444. * 可能出现的错误代码:
  445. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  446. */
  447. public function pay(Request $request)
  448. {
  449. $validator = Validator::make($request->all(),
  450. [
  451. 'userid' => 'required',
  452. 'price' => 'required',
  453. ],
  454. [
  455. 'userid.required' => 'userid不能为空!',
  456. 'price.required' => 'price不能为空!',
  457. ]
  458. );
  459. if ($validator->fails()) {
  460. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  461. }
  462. $money = $request->input('price');
  463. $user = UserInfoModel::find(request('userid'));
  464. $trade_no = date("YmdHis");
  465. \Log::info($this->options());
  466. $app = Factory::payment($this->options());
  467. $result = $app->order->unify([
  468. 'body' => '高考志愿助手 - 付费查询',
  469. 'out_trade_no' => $trade_no,
  470. 'total_fee' => $money * 100,
  471. 'trade_type' => 'JSAPI',
  472. 'notify_url' => url('/api/home/notify'),
  473. 'openid' => $user->openid
  474. ]);
  475. \Log::info($result);
  476. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  477. $payment = Factory::payment($this->options());
  478. $jssdk = $payment->jssdk;
  479. $json = $jssdk->bridgeConfig($result['prepay_id']);
  480. \Log::info($json);
  481. return $this->api(compact('json'));
  482. }else{
  483. $msg = "签名失败,请稍后再试!";
  484. return $this->api(compact('msg'));
  485. }
  486. }
  487. //下面是回调函数
  488. public function notify()
  489. {
  490. $app = Factory::payment($this->options());
  491. \Log::info("wechat notify start!");
  492. return $app->handlePaidNotify(function ($notify, $successful) {
  493. \Log::info($notify);
  494. if ($notify['result_code'] == 'SUCCESS') {
  495. $user = UserInfoModel::where('openid',$notify['openid'])->first();
  496. $data['order_no'] = $notify['out_trade_no'];
  497. $data['price'] = $notify['total_fee'] / 100;
  498. $data['user_id'] = $user->id;
  499. $data['code'] = $user->code;
  500. \Log::info($data);
  501. $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
  502. $this->createQueryInfo($user,1);
  503. OrderInfoModel::create($data);
  504. $user->save();
  505. } else {
  506. return $successful('通信失败,请稍后再通知我');
  507. }
  508. return true;
  509. });
  510. }
  511. public function checkPaid()
  512. {
  513. $userid = request('userid');
  514. $_user = UserInfoModel::find($userid);
  515. $hasPaid = QueryInfoModel::where('user_id', $_user->id)->where('grade', $_user->grade)->where('is_paid', 1)->count();
  516. $not_expire = Carbon::now() < $_user->paid_end_time;
  517. if($hasPaid && $not_expire){
  518. return 1;
  519. }else{
  520. return 0;
  521. }
  522. }
  523. public function updateUserinfo()
  524. {
  525. $userid = request('userid');
  526. $data['username'] = request('username');
  527. $data['cnumber'] = request('cnumber');
  528. $data['mobile'] = request('mobile');
  529. $data['grade'] = request('grade');
  530. $data['code'] = request('code');
  531. $res = UserInfoModel::where('id', $userid)->update($data);
  532. }
  533. public function createQueryInfo($userinfo, $ispaid = 0)
  534. {
  535. $data['is_paid'] = $ispaid;
  536. $data['user_id'] = $userinfo->id;
  537. $data['cnumber'] = $userinfo->cnumber;
  538. $data['grade'] = $userinfo->grade;
  539. $data['code'] = $userinfo->code;
  540. $data['mobile'] = $userinfo->mobile;
  541. $data['class'] = $userinfo->class;
  542. \Log::info($data);
  543. QueryInfoModel::create($data);
  544. }
  545. public function options()
  546. {
  547. return [
  548. 'app_id' => "wxea7a26e5da5b46f2",
  549. 'mch_id' => "1398823402",
  550. 'key' => "c1891122765718911227657189112276",
  551. 'notify_url' => url('/api/home/pay'),
  552. 'sandbox' => false,
  553. ];
  554. }
  555. }