123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\IntroductionInfoModel;
- use App\Models\MajorInfoModel;
- use App\Models\OrderInfoModel;
- use App\Models\PaidSettingModel;
- use App\Models\QueryInfoModel;
- use App\Models\StudentCountModel;
- use App\Models\UserInfoModel;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Services\Base\ErrorCode;
- use Validator, Response;
- use EasyWeChat\Factory;
- class HomeController extends Controller
- {
- protected $app;
- public function __construct()
- {
- $config = [
- 'app_id' => 'wxdeff0d78ebe3e744',
- 'secret' => '44cf7fc6e0d4012f180c7f04d47d344d',
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- ];
- $this->app = Factory::miniProgram($config);
- }
- /**
- * @api {post} /api/home/login 登陆(login)
- * @apiDescription 登陆(login)
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} [code](必填)
- * @apiParam {string} [nickName]
- * @apiParam {string} [avatar]
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "userinfo": {
- * "id": "",
- * "nickname": "",
- * "openid": "",
- * "has_agreed": "" //1:已同意说明,进入主页面;0:为同意协议,进入协议说明页面
- * }
- *
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function login(Request $request)
- {
- $code = $request->get('code');
- $session = $this->app->auth->session($code);
- \Log::info(json_encode($session));
- $openid = $session['openid'];
- $userinfo = UserInfoModel::where('openid', $openid)->first(['id', 'nickname', 'openid', 'has_agreed']);
- if ($userinfo) {
- return $this->api(compact('userinfo'));
- } else {
- $data['openid'] = $openid;
- $data['nickname'] = $request->get('nickName');
- $data['avatar'] = $request->get('avatar');
- $data['has_agreed'] = 0;
- $userinfo = UserInfoModel::create($data);
- return $this->api(compact('userinfo'));
- }
- }
- /**
- * @api {get} /api/home/getintroduction 获取使用说用及协议
- * @apiDescription 获取使用说用及协议
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "list": [
- *
- * ]
- *
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function getIntroduction()
- {
- $list = IntroductionInfoModel::get(['title', 'content', 'type']);
- return $this->api(compact('list'));
- }
- /**
- * @api {post} /api/home/agreeintroduction 同意使用说用及协议
- * @apiDescription 同意使用说用及协议
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} [userid] 用户ID(必填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "userinfo": [
- *
- * ]
- *
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function agreeIntroduction(Request $request)
- {
- $userid = $request->get('userid');
- $data['has_agreed'] = 1;
- $res = UserInfoModel::where('id', $userid)->update($data);
- if ($res) {
- $userinfo = UserInfoModel::where('id', $userid)->first(['id', 'nickname', 'openid', 'has_agreed']);
- }
- return $this->api(compact('userinfo'));
- }
- /**
- * @api {post} /api/home/getbasedata 获取可基础数据
- * @apiDescription 获取基础数据
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} [userid] 用户ID(必填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "userinfo":[
- *
- * ]
- * "batchs": [
- *
- * ],
- * "provinces":[
- *
- * ]
- *
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function getBaseData(Request $request)
- {
- $validator = Validator::make($request->all(),
- [
- 'userid' => 'required',
- ],
- [
- 'userid.required' => 'userid不能为空!',
- ]
- );
- if ($validator->fails()) {
- return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
- }
- $batchs = MajorInfoModel::groupBy('batch')->pluck('batch');
- $provinces = [
- "全部省份", "安徽", "澳门", "北京", "重庆", "福建", "甘肃", "广东", "广西", "贵州", "海南", "河北", "河南", "黑龙江", "湖北", "湖南", "吉林", "江苏", "江西", "辽宁", "内蒙古", "宁夏", "青海", "山东", "山西", "陕西", "上海", "四川", "台湾", "天津", "西藏", "香港", "新疆", "云南", "浙江"
- ];
- $userinfo = UserInfoModel::select(['id','username','code','cnumber','class','grade','mobile'])->find(request('userid'));
- return $this->api(compact('userinfo','batchs', 'provinces'));
- }
- /**
- * @api {post} /api/home/getphonenumber 获取手机号
- * @apiDescription 获取手机号
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} [code] code(必填)
- * @apiParam {string} [iv] iv(必填)
- * @apiParam {string} [encryptData] encryptData(必填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "decryptedData": [
- *
- * ]
- *
- * }
- *
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function getPhoneNumber(Request $request){
- $code = $request->get('code');
- $iv = $request->get('iv');
- $encryptData = $request->get('encryptData');
- $session = $this->app->auth->session($code);
- \Log::info($session);
- $decryptedData = $this->app->encryptor->decryptData($session['session_key'], $iv, $encryptData);
- return $this->api(compact('decryptedData'));
- }
- /**
- * @api {post} /api/home/getqueried 查询免费信息
- * @apiDescription 查询免费信息
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} [userid] 用户ID(必填)
- * @apiParam {string} [username] 考生姓名(必填)
- * @apiParam {string} [cnumber] 考号(必填)
- * @apiParam {string} [class] 科类(必填)
- * @apiParam {int} [grade] 高考成绩(必填)
- * @apiParam {int} [mobile] 手机号(必填)
- * @apiParam {string} [batch] 批次(必填)
- * @apiParam {string} [province] 省份(必填)
- * @apiParam {string} [code] 推荐码(选填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "rank": ""
- * "grade": ""
- * "college_count": ""
- *
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function getQueried(Request $request)
- {
- $validator = Validator::make($request->all(),
- [
- 'cnumber' => 'required',
- 'username' => 'required',
- 'grade' => 'required|integer',
- 'mobile' => 'required'
- ],
- [
- 'cnumber.required' => '考号不能为空!',
- 'username.required' => '姓名不能为空!',
- 'mobile.required' => '手机号不能为空!',
- 'grade.required' => '成绩不能为空!',
- 'grade.integer' => '请输入正确格式的成绩!',
- ]
- );
- if ($validator->fails()) {
- return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
- }
- /*更新用户的信息*/
- $this->updateUserinfo();
- $year = date('Y');
- $grade = $request->get('grade');
- $batch = $request->get('batch');
- $class = $request->get('class');
- $province = $request->get('province');
- if(StudentCountModel::where('year', $year)->where('class',$class)->count() > 0){
- $maxgrade = StudentCountModel::where('year', $year)->where('class',$class)->orderBy('grade', 'desc')->first()->grade;
- if ($grade > $maxgrade) {
- $rank = "前10";
- } else {
- $rank = StudentCountModel::where('year', $year)->where('class',$class)->where('grade', $grade)->first(['total'])->total;
- }
- }else{
- $rank = '未知';
- }
- if ($province == "全部省份") {
- $college = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where("major_grade", "<=", "$grade")->groupBy('college')->get();
- } else {
- $college = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('province', 'like', '%' . $province . '%')->where("major_grade", "<=", "$grade")->groupBy('college')->get();
- }
- $college_count = count($college);
- /*创建查询记录*/
- $user = UserInfoModel::find(request('userid'));
- $this->createQueryInfo($user);
- return $this->api(compact('rank', 'grade', 'province', 'college_count', 'batch'));
- }
- /**
- * @api {post} /api/home/getpaidmajors 查询付费信息
- * @apiDescription 查询付费信息
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} [userid] 用户ID(必填)
- * @apiParam {string} [username] 考生姓名(必填)
- * @apiParam {string} [batch] 批次(必填)
- * @apiParam {string} [province] 省份(必填)
- * @apiParam {string} [class] 科类(必填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK 已付费查看过,显示匹配信息
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "count": ""
- * "major":[
- * ]
- *
- * }
- * }
- *
- *HTTP/1.1 200 OK 未曾付费,跳转到支付页面
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "msg": "need to pay",
- * "price": ""
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function getPaidMajors(Request $request)
- {
- $validator = Validator::make($request->all(),
- [
- 'userid' => 'required',
- 'batch' => 'required',
- 'province' => 'required',
- 'class' => 'required'
- ],
- [
- 'userid.required' => 'userid不能为空!',
- 'batch.required' => 'batch不能为空!',
- 'province.required' => 'province不能为空!',
- 'class.required' => 'class不能为空!',
- ]
- );
- if ($validator->fails()) {
- return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
- }
- if ($this->checkPaid()) {
- $year = date('Y');
- $batch = $request->get('batch');
- $province = $request->get('province');
- $class = request('class');
- $userid = request('userid');
- $_user = UserInfoModel::find($userid);
- $grade = $_user->grade;
- if ($province == "全部省份") {
- $major = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('major_grade', "<=", "$grade")->paginate(10);
- } else {
- $major = MajorInfoModel::where('year', $year)->where('batch', $batch)->where('class', $class)->where('province', 'like', '%' . $province . '%')->where('major_grade', "<=", "$grade")->paginate(10);
- }
- $count = count($major);
- $this->createQueryInfo($_user, 1);
- return $this->api(compact('count', 'major'));
- } else {
- $price = PaidSettingModel::first();
- $msg = 'need to pay';
- $price = $price->price;
- return $this->api(compact('msg', 'price'));
- }
- }
- /**
- * @api {post} /api/home/pay 获取微信支付签名信息
- * @apiDescription 获取微信支付签名信息
- * @apiGroup 高考助手
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} [userid] 用户ID(必填)
- * @apiParam {string} [price] 付费金额(必填)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "appId":"wx1c2357232cd25f65",
- * "timeStamp":"1524907589",
- * "nonceStr":"5ae43e45eb499",
- * "package":"prepay_id=wx28172629917401724160128f0238805782",
- * "signType":"MD5",
- * "paySign":"8E9CF26B2B83C22471D023CBBDC36EDF"
- * }
- * }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- * 可能出现的错误代码:
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- */
- public function pay(Request $request)
- {
- $validator = Validator::make($request->all(),
- [
- 'userid' => 'required',
- 'price' => 'required',
- ],
- [
- 'userid.required' => 'userid不能为空!',
- 'price.required' => 'price不能为空!',
- ]
- );
- if ($validator->fails()) {
- return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
- }
- $money = $request->input('price');
- $user = UserInfoModel::find(request('userid'));
- $trade_no = date("YmdHis");
- \Log::info($this->options());
- $app = Factory::payment($this->options());
- $result = $app->order->unify([
- 'body' => '高考志愿助手 - 付费查询',
- 'out_trade_no' => $trade_no,
- 'total_fee' => $money * 100,
- 'trade_type' => 'JSAPI',
- 'notify_url' => url('/api/home/notify'),
- 'openid' => $user->openid
- ]);
- \Log::info($result);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $payment = Factory::payment($this->options());
- $jssdk = $payment->jssdk;
- $json = $jssdk->bridgeConfig($result['prepay_id']);
- \Log::info($json);
- return $this->api(compact('json'));
- }else{
- $msg = "签名失败,请稍后再试!";
- return $this->api(compact('msg'));
- }
- }
- //下面是回调函数
- public function notify()
- {
- $app = Factory::payment($this->options());
- \Log::info("wechat notify start!");
- return $app->handlePaidNotify(function ($notify, $successful) {
- \Log::info($notify);
- if ($notify['result_code'] == 'SUCCESS') {
- $user = UserInfoModel::where('openid',$notify['openid'])->first();
- $data['order_no'] = $notify['out_trade_no'];
- $data['price'] = $notify['total_fee'] / 100;
- $data['user_id'] = $user->id;
- $data['code'] = $user->code;
- \Log::info($data);
- $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
- $this->createQueryInfo($user,1);
- OrderInfoModel::create($data);
- $user->save();
- } else {
- return $successful('通信失败,请稍后再通知我');
- }
- return true;
- });
- }
- public function checkPaid()
- {
- $userid = request('userid');
- $_user = UserInfoModel::find($userid);
- $hasPaid = QueryInfoModel::where('user_id', $_user->id)->where('grade', $_user->grade)->where('is_paid', 1)->count();
- $not_expire = Carbon::now() < $_user->paid_end_time;
- $price = PaidSettingModel::first();
- if($price->price <=0 ) return 1;
- if($hasPaid && $not_expire){
- return 1;
- }else{
- return 0;
- }
- }
- public function updateUserinfo()
- {
- $userid = request('userid');
- $data['username'] = request('username');
- $data['cnumber'] = request('cnumber');
- $data['mobile'] = request('mobile');
- $data['grade'] = request('grade');
- $data['code'] = request('code');
- $data['class'] = request('class');
- $res = UserInfoModel::where('id', $userid)->update($data);
- }
- public function createQueryInfo($userinfo, $ispaid = 0)
- {
- $data['is_paid'] = $ispaid;
- $data['user_id'] = $userinfo->id;
- $data['cnumber'] = $userinfo->cnumber;
- $data['grade'] = $userinfo->grade;
- $data['code'] = $userinfo->code;
- $data['mobile'] = $userinfo->mobile;
- $data['class'] = $userinfo->class;
- \Log::info($data);
- QueryInfoModel::create($data);
- }
- public function options()
- {
- return [
- 'app_id' => "wxea7a26e5da5b46f2",
- 'mch_id' => "1398823402",
- 'key' => "c1891122765718911227657189112276",
- 'notify_url' => url('/api/home/pay'),
- 'sandbox' => false,
- ];
- }
- }
|