__http_post_request($url, $post_json, true); print_r($result); die; } /** * 获取code(Get code) * http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx * http://weapp.wemediacn.net/d/tiffany/api/oauth2/code?qaz=wsx */ function codeAction(){ //如果未获取到请求数据 if(empty(file_get_contents("php://input"))){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果有获取到请求数据 else{ //准备判断请求数据整体是否为空 $request_arr = json_decode(file_get_contents("php://input"), true); //如果请求数据整体为空 if(empty($request_arr)){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果请求数据整体不为空 else{ //准备获取所传appid $rs['errmsg'] = ""; //如果未获取到appid if(empty($request_arr['appid'])){ //告知用户"appid is empty;" $rs['errmsg'].= "appid is empty;"; } //如果以上判断未通过 if(!empty($rs['errmsg'])){ //告知用户"参数错误 error detail:".$rs['errmsg'] $rs['errmsg'] = "参数错误 error detail:".$rs['errmsg']; $rs['errcode'] = 61451; $rs['data'] = null; } //如果以上判断都通过 else{ //准备判断所传appid是否有效 $appid = $request_arr['appid']; //查询接口信息 $AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0"); //如果未查询到接口信息 if(empty($AppInfo)){ //告知用户"找不到app配置信息,appid无效,err detail:找不到app配置信息" $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; $rs['errcode'] = 40036; $rs['data'] = null; } //如果有查询到接口信息 else{ //获取上次请求日期 $last_req_time = $AppInfo->LastReqTime; $last_req_date = date("Y-m-d", strtotime($last_req_time)); //获取当前时间及当天日期 $current_time = time(); $current_datetime = date("Y-m-d H:i:s", $current_time); $current_date = date("Y-m-d", $current_time); //获取上次请求日期所在日的请求次数 $today_req_times = $AppInfo->TodayReqTimes; //获取接口请求次数限制 $day_max_req_times = $AppInfo->DayMaxReqTimes; //如果上次请求日期是当天日期 if($last_req_date==$current_date){ //如果当天请求次数已达到接口请求次数限制 if($today_req_times>=$day_max_req_times){ //告知用户"request today reached the request limit,err detail:超过请求上限限制" $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errcode'] = 80102; $rs['data'] = null; } //如果当天请求次数尚未达到接口请求次数限制 else{ //当天请求次数加一 $today_req_times = $today_req_times+1; } } //如果上次请求日期不是当天日期 else{ //如果接口请求次数限制不大于0 if($day_max_req_times<=0){ //告知用户"request today reached the request limit,err detail:超过请求上限限制" $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errcode'] = 80102; $rs['data'] = null; } //如果接口请求次数限制大于0 else{ //初始当天请求次数 $today_req_times = 1; } } //如果以上判断都通过 if(empty($rs['errcode'])){ //准备更新当天请求次数 $AppInfo->LastModifiedTime = $current_datetime; //其他字段 $AppInfo->TodayReqTimes = $today_req_times; $AppInfo->LastReqTime = $current_date; //如果更新失败 if(!($AppInfo->save())){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果更新成功 else{ //查询当前接口过去是否有生成过code $TempCode = TempCode::findFirst("AppId = '$appid'"); //如果当前接口过去未生成过code if(empty($TempCode)){ //准备新增临时code $TempCode = new TempCode(); $TempCode->AppId = $appid; $TempCode->CreateTime = $current_datetime; $TempCode->LastModifiedTime = $current_datetime; } //如果当前接口过去有生成过code else{ //准备更新临时code $TempCode->LastModifiedTime = $current_datetime; } //其他字段 $TempCode->Code = $code = $this->_get_act_code(32); $TempCode->IsUsed = 0; $TempCode->ExpireTime = date("Y-m-d H:i:s", $current_time+300); //如果保存失败 if(!($TempCode->save())){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果保存成功 else{ //告知用户"ok" $rs['errmsg'] = "ok"; $rs['errcode'] = 0; $rs['data']['code'] = $code; } } } } } } } //输出结果 echo json_encode($rs, JSON_UNESCAPED_UNICODE); } /** * 根据code获取access_token(Get access token by code) * http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx * http://weapp.wemediacn.net/d/tiffany/api/oauth2/accesstoken?qaz=wsx */ function accesstokenAction(){ //如果未获取到请求数据 if(empty(file_get_contents("php://input"))){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果有获取到请求数据 else{ //准备判断请求数据整体是否为空 $request_arr = json_decode(file_get_contents("php://input"), true); //如果请求数据整体为空 if(empty($request_arr)){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果请求数据整体不为空 else{ //准备获取所传appid、appsecrect和code //注意: //接口中的appsecrect字段本身就是用了错别词,比secret多了一个字母 //但接口信息表AppInfo里的AppSecret字段则没有多出一个字母,要当心 $rs['errmsg'] = ""; //如果未获取到所传appid if(empty($request_arr['appid'])){ //告知用户"appid is empty;" $rs['errmsg'].= "appid is empty;"; } //如果未获取到所传appsecrect if(empty($request_arr['appsecrect'])){ //告知用户"appsecrect is empty;" $rs['errmsg'].= "appsecrect is empty;"; } //如果未获取到所传code if(empty($request_arr['code'])){ //告知用户"code is empty;" $rs['errmsg'].= "code is empty;"; } //如果以上判断未通过 if(!empty($rs['errmsg'])){ //告知用户"参数错误 error detail:".$rs['errmsg'] $rs['errmsg'] = "参数错误 error detail:".$rs['errmsg']; $rs['errcode'] = 61451; $rs['data'] = null; } //如果以上判断都通过 else{ //准备判断所传appid是否有效 $appid = $request_arr['appid']; //查询接口信息 $AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0"); //如果未查询到接口信息 if(empty($AppInfo)){ //告知用户"找不到app配置信息,appid无效,err detail:找不到app配置信息" $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; $rs['errcode'] = 40036; $rs['data'] = null; } //如果有查询到接口信息 else{ //获取上次请求日期 $last_req_time = $AppInfo->LastReqTime; $last_req_date = date("Y-m-d", strtotime($last_req_time)); //获取当前时间及当天日期 $current_time = time(); $current_datetime = date("Y-m-d H:i:s", $current_time); $current_date = date("Y-m-d", $current_time); //获取上次请求日期所在日的请求次数 $today_req_times = $AppInfo->TodayReqTimes; //获取接口请求次数限制 $day_max_req_times = $AppInfo->DayMaxReqTimes; //如果上次请求日期是当天日期 if($last_req_date==$current_date){ //如果当天请求次数已达到接口请求次数限制 if($today_req_times>=$day_max_req_times){ //告知用户"request today reached the request limit,err detail:超过请求上限限制" $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errcode'] = 80102; $rs['data'] = null; } //如果当天请求次数尚未达到接口请求次数限制 else{ //当天请求次数加一 $today_req_times = $today_req_times+1; } } //如果上次请求日期不是当天日期 else{ //如果接口请求次数限制不大于0 if($day_max_req_times<=0){ //告知用户"request today reached the request limit,err detail:超过请求上限限制" $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errcode'] = 80102; $rs['data'] = null; } //如果接口请求次数限制大于0 else{ //初始当天请求次数 $today_req_times = 1; } } //如果以上判断都通过 if(empty($rs['errcode'])){ //准备更新当天请求次数 $AppInfo->LastModifiedTime = $current_datetime; //其他字段 $AppInfo->TodayReqTimes = $today_req_times; $AppInfo->LastReqTime = $current_date; //如果更新失败 if(!($AppInfo->save())){ //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果更新成功 else{ //准备判断appsecrect是否匹配 $appsecrect = $request_arr['appsecrect']; //如果appsecrect不匹配 if($appsecrect!=$AppInfo->AppSecret){ //告知用户"密钥不匹配,err detail:密钥不匹配" $rs['errmsg'] = "密钥不匹配,err detail:密钥不匹配"; $rs['errcode'] = 40037; $rs['data'] = null; } //如果appsecrect匹配 else{ //准备判断所传code是否匹配 $code = $request_arr['code']; //查询临时code $TempCode = TempCode::findFirst("AppId = '$appid' AND Code = '$code'"); //如果未查询到临时code if(empty($TempCode)){ //告知用户"code 不正确,err detail:code 不匹配" $rs['errmsg'] = "code 不正确,err detail:code 不匹配"; $rs['errcode'] = 40038; $rs['data'] = null; } //如果有查询到临时code else{ //准备判断所传code是否已失效 $expire_time = $TempCode->ExpireTime; //如果所传code已失效 if($current_datetime>=$expire_time){ //告知用户"authorize code is expired,err detail:code 已过期" $rs['errmsg'] = "authorize code is expired,err detail:code 已过期"; $rs['errcode'] = 80103; $rs['data'] = null; } //如果所传code未失效 else{ //准备判断所传code是否已使用过 $is_used = $TempCode->IsUsed; //如果所传code已使用过 if(!empty($is_used)){ //告知用户"authorize code is used,err detail:code已使用" $rs['errmsg'] = "authorize code is used,err detail:code已使用"; $rs['errcode'] = 80104; $rs['data'] = null; } //如果所传code未使用过 else{ //开启事务 $this->db->begin(); //准备更新临时code $TempCode->LastModifiedTime = $current_datetime; //其他字段 $TempCode->IsUsed = 1; //如果保存失败 if(!($TempCode->save())){ //回滚事务 $this->db->rollback(); //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果保存成功 else{ //查询当前接口过去是否有生成过access_token $AccessToken = AccessToken::findFirst("AppId = '$appid'"); //如果当前接口过去未生成过access_token if(empty($AccessToken)){ //准备新增AccessToken $AccessToken = new AccessToken(); $AccessToken->Id = $this->_get_act_code(32); $AccessToken->AppId = $appid; $AccessToken->PrevAccessToken = null; $AccessToken->CreateTime = $current_datetime; $AccessToken->LastModifiedTime = $current_datetime; } //如果当前接口过去有生成过access_token else{ //准备更新AccessToken $AccessToken->LastModifiedTime = $current_datetime; //如果前access_token已过期 if($current_datetime>$AccessToken->ExpireTime){ $AccessToken->PrevAccessToken = null; } //如果前access_token未过期 else{ $AccessToken->PrevAccessToken = $AccessToken->AccessToken; } } //其他字段 $AccessToken->AccessToken = $access_token = $this->_get_act_code(32); $AccessToken->ExpireTime = date("Y-m-d H:i:s", $current_time+14400); //如果保存失败 if(!($AccessToken->save())){ //回滚事务 $this->db->rollback(); //告知用户"系统繁忙,请稍候再试" $rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errcode'] = -1; $rs['data'] = null; } //如果保存成功 else{ //执行事务 $this->db->commit(); //告知用户"ok" $rs['errmsg'] = "ok"; $rs['errcode'] = 0; $rs['data']['access_token'] = $access_token; $rs['data']['expire_in'] = 14400; $rs['data']['refresh_token'] = null; } } } } } } } } } } } } //输出结果 echo json_encode($rs, JSON_UNESCAPED_UNICODE); } } ?>