'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, ]; } }