HomeController.php 21 KB

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