微信第三方平台上传小程序

引入了微信消息解码的包,因为不兼容php7.2,所以做了部分修改 链接: https://pan.baidu.com/s/1-TGZ6C3nZ3JI7_4QfxYbRg 提取码: 7itq

require_once 'lib/wxBizMsgCrypt.php';

class WxThird
{
public $encodingAesKey;
public $token;
public $appId;
public $appSecret;

protected $api_component_token = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
protected $api_create_preauthcode = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode';
protected $api_query_auth = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth';
protected $api_authorizer_token = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token';
protected $commit = 'https://api.weixin.qq.com/wxa/commit';
protected $get_page = 'https://api.weixin.qq.com/wxa/get_page';
protected $get_qrcode = 'https://api.weixin.qq.com/wxa/get_qrcode';
protected $submit_audit = 'https://api.weixin.qq.com/wxa/submit_audit';
protected $get_auditstatus = 'https://api.weixin.qq.com/wxa/get_auditstatus';
protected $get_latest_auditstatus = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus';
protected $undocodeaudit = 'https://api.weixin.qq.com/wxa/undocodeaudit';
protected $release = 'https://api.weixin.qq.com/wxa/release';
protected $modify_domain = 'https://api.weixin.qq.com/wxa/modify_domain';

public function __construct($appId,$appSecret,$token,$encodingAesKey)
{
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->token = $token;
$this->encodingAesKey = $encodingAesKey;
}

public function decryptMsg($msg_signature,$timestamp,$nonce,$from_xml)
{
$msg = '';
$wxBizMsgCrypt = new \WXBizMsgCrypt($this->token,$this->encodingAesKey,$this->appId);
$errCode = $wxBizMsgCrypt->decryptMsg($msg_signature,$timestamp,$nonce,$from_xml,$msg);
return ['errCode'=>$errCode,'msg'=>$msg];
}

public function componentToken($ticket)
{
$data = [
'component_appid' => $this->appId,
'component_appsecret' => $this->appSecret,
'component_verify_ticket' => $ticket
];
return $this->curl_post($this->api_component_token,json_encode($data));
}

public function createPreauthcode($token)
{
$this->api_create_preauthcode = $this->api_create_preauthcode.'?component_access_token='.$token;
$data = [
'component_appid' => $this->appId,
];
return $this->curl_post($this->api_create_preauthcode,json_encode($data));
}

public function queryAuth($token,$code)
{
$data = [
'component_appid' => $this->appId,
'authorization_code' => $code,
];
$this->api_query_auth = $this->api_query_auth.'?component_access_token='.$token;
return $this->curl_post($this->api_query_auth,json_encode($data));
}

public function refreshAuthCode($refresh_code,$token,$app_id)
{
$this->api_authorizer_token = $this->api_authorizer_token.'?component_access_token='.$token;
$data = [
'component_appid' => $this->appId,
'authorizer_appid' => $app_id,
'authorizer_refresh_token' => $refresh_code,
];
return $this->curl_post($this->api_authorizer_token,json_encode($data));
}

public function commit($auth_token,$template_id,$ext_json,$user_version,$user_desc)
{
$this->commit = $this->commit.'?access_token='.$auth_token;
$data = [
'template_id' => $template_id,
'ext_json' => $ext_json,
'user_version' => $user_version,
'user_desc' => $user_desc
];
return $this->curl_post($this->commit,json_encode($data,JSON_UNESCAPED_UNICODE));
}

public function getPage($auth_code)
{
$this->get_page = $this->get_page.'?access_token='.$auth_code;
return $this->curl_get($this->get_page);
}

public function getQrcode($auth_code)
{
$this->get_qrcode = $this->get_qrcode.'?access_token='.$auth_code;
return $this->curl_get($this->get_qrcode);
}


public function submitAudit($auth_code)
{
$this->submit_audit = $this->submit_audit.'?access_token='.$auth_code;
$data = '{}';
return $this->curl_post($this->submit_audit,$data);
}

public function getAuditStatus($auth_code,$auditid)
{
$this->get_auditstatus = $this->get_auditstatus.'?access_token='.$auth_code;
$data = [
'auditid' => $auditid,
];
return $this->curl_post($this->get_auditstatus,json_encode($data,JSON_UNESCAPED_UNICODE));
}

public function getLatestAuditStatus($auth_code)
{
$this->get_latest_auditstatus = $this->get_latest_auditstatus.'?access_token='.$auth_code;
return $this->curl_get($this->get_latest_auditstatus);
}

public function undoCodeAudit($auth_code)
{
$this->undocodeaudit = $this->undocodeaudit.'?access_token='.$auth_code;
return $this->curl_get($this->undocodeaudit);
}

public function release($auth_code)
{
$this->release = $this->release.'?access_token='.$auth_code;
$data = '{}';
return $this->curl_post($this->release,$data);
}

public function modifyDomain($auth_code,$action,$host='')
{
$this->modify_domain = $this->modify_domain.'?access_token='.$auth_code;
if ($action =='get') {
$data = [
'action' => $action,
];
} else {
$data = [
'action' => $action,
'requestdomain' => ['https://'.$host],
'wsrequestdomain' => ['wss://'.$host],
'uploaddomain' => ['https://'.$host],
'downloaddomain' => ['https://'.$host],
];
}
return $this->curl_post($this->modify_domain,json_encode($data));
}

protected function curl_post($url,$data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}

protected function curl_get($url)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_HEADER,true);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($curl);
curl_close($curl);
return $result;
}
}


<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Org\WxThird\WxThird;
use Illuminate\Support\Facades\Log;
use DB,Saas,Storage;

class WxThirdController extends Controller
{
protected $encodingAesKey='182c88b988*********************fdc99ae';
protected $token='SHj8epdN********************HW2HqtfI';
protected $appId='wxd*********d2';
protected $appSecret='22f08342***********79c3547';

protected $wxThird;

public function __construct()
{
$this->wxThird = new WxThird($this->appId,$this->appSecret,$this->token,$this->encodingAesKey);
}

/*接受消息*/
public function wxMessage(Request $request)
{
$data = $request->all();
$encryptMsg = file_get_contents('php://input');
$timestamp = $data['timestamp'];
$msg_signature = $data['msg_signature'];
$nonce = $data['nonce'];
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
$arr = $this->wxThird->decryptMsg($msg_signature,$timestamp,$nonce,$from_xml);
if ($arr['errCode'] == 0) {
$msg_tree = new \DOMDocument();
$msg_tree->loadXML($arr['msg']);
$array_appId = $msg_tree->getElementsByTagName('AppId');
$array_infoType = $msg_tree->getElementsByTagName('InfoType');
$info_type = $array_infoType->item(0)->nodeValue;
$appId = $array_appId->item(0)->nodeValue;
if($info_type == 'component_verify_ticket'){
$array_ticket = $msg_tree->getElementsByTagName('ComponentVerifyTicket');
$ticket_x = $array_ticket->item(0)->nodeValue;
$ticket = explode('@@@',$ticket_x)[1];
$info = DB::table('wx_third_message')->where('info_type',$info_type)->first();
if(empty($info)){
DB::table('wx_third_message')->insert([
'third_appid' => $appId,
'info_type' => $info_type,
'value' => $ticket,
]);
}else{
DB::table('wx_third_message')->where('id',$info->id)->update(['value' => $ticket]);
}
}elseif($info_type == 'unauthorized'){
$array_authAppId = $msg_tree->getElementsByTagName('AuthorizerAppid');
$authAppId = $array_authAppId->item(0)->nodeValue;
$info = DB::table('wx_third_message')->where('auth_appid',$authAppId)->where('info_type','unauthorized')->first();
if(empty($info)){
DB::table('wx_third_message')->insert([
'third_appid' => $appId,
'info_type' => 'unauthorized',
'auth_appid' => $authAppId,
]);
}
}
}
return 'success';
}

/*扫码授权回调*/
public function wxCode(Request $request,$app_id='')
{
$company_id = Saas::id();
$info = DB::table('wx_third_message')->where('auth_appid',$app_id)->where('info_type','code')->first();
$auth_code = $request->query('auth_code');
$expires_in = $request->query('expires_in');
$code = explode('@@@',$auth_code)[1];
if(empty($info)){
DB::table('wx_third_message')->insert([
'auth_appid' => $app_id,
'value' => $code,
'info_type' => 'code',
'expire_time' => time()+$expires_in,
'company_id' => $company_id,
'third_appid' => $this->appId
]);
}else{
DB::table('wx_third_message')->where('id',$info->id)->update(['value'=>$code,'expire_time'=>time()+$expires_in]);
}
if(empty($auth_code)) abort(404);
return redirect('/cadmin');
}

/*授权*/
public function authApp(Request $request)
{
$app_id = $request->get('app_id');
DB::table('wx_third_message')->where('info_type','unauthorized')->where('auth_appId',$app_id)->delete();
$company_id = Saas::id();
$perauthocode_info = DB::table('wx_third_message')->where('info_type','pre_auth_code')->where('expire_time','>',time())->where('company_id',$company_id)->orderBy('id','DESC')->first();
$componentToken_info = DB::table('wx_third_message')->where('info_type','component_access_token')->where('expire_time','>',time())->where('company_id',$company_id)->orderBy('id','DESC')->first();
if(!empty($componentToken_info) && !empty($perauthocode_info)){
$url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$this->appId.'&pre_auth_code='.$perauthocode_info->value.'&redirect_uri='.urlencode('http://'.$_SERVER['HTTP_HOST'].'/wxCode/'.$app_id).'&auth_type=2';
return response()->json(['errcode'=>'0','data'=>['url'=>$url],'errmsg'=>'ok']);
}
if(!empty($componentToken_info) && empty($perauthocode_info)){
$perauthocode = json_decode($this->wxThird->createPreauthcode($componentToken_info->value),true);
if(isset($perauthocode['errcode'])){
return response()->json(['errcode'=>'40011','errmsg'=>$perauthocode['errmsg']]);
}
$code = explode('@@@',$perauthocode['pre_auth_code'])[1];
$code_info = DB::table('wx_third_message')->where('info_type','pre_auth_code')->where('company_id',$company_id)->first();
if(empty($code_info)){
DB::table('wx_third_message')->insert([
'third_appid' => $this->appId,
'value' => $code,
'info_type' => 'pre_auth_code',
'expire_time' => time()+1800,
'company_id' => $company_id,
]);
}else{
DB::table('wx_third_message')->where('id',$code_info->id)->update(['expire_time' => time()+1800,'value' => $code,]);
}
$url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$this->appId.'&pre_auth_code='.$code.'&redirect_uri='.urlencode('http://'.$_SERVER['HTTP_HOST'].'/wxCode/'.$app_id).'&auth_type=2';
return response()->json(['errcode'=>'0','data'=>['url'=>$url],'errmsg'=>'ok']);
}
if(empty($componentToken_info)){
$token_info = $this->getComponentAccessToken($company_id);
if($token_info['errcode']){
return response()->json($token_info);
}
$perauthocode = json_decode($this->wxThird->createPreauthcode($token_info['value']),true);
if(isset($perauthocode['errcode'])){
return response()->json(['errcode'=>'40011','errmsg'=>$perauthocode['errmsg']]);
}
$code = explode('@@@',$perauthocode['pre_auth_code'])[1];
$code_info = DB::table('wx_third_message')->where('info_type','pre_auth_code')->where('company_id',$company_id)->first();
if(empty($code_info)){
DB::table('wx_third_message')->insert([
'third_appid' => $this->appId,
'value' => $code,
'info_type' => 'pre_auth_code',
'expire_time' => time()+1800,
'company_id' => $company_id,
]);
}else{
DB::table('wx_third_message')->where('id',$code_info->id)->update(['expire_time' => time()+1800,'value' => $code,]);
}
$url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$this->appId.'&pre_auth_code='.$code.'&redirect_uri='.urlencode('http://'.$_SERVER['HTTP_HOST'].'/wxCode/'.$app_id).'&auth_type=2';
return response()->json(['errcode'=>'0','data'=>['url'=>$url],'errmsg'=>'ok']);
}
return response()->json(['errcode'=>'40011','errmsg'=>'授权失败']);
}

/*轮询是否授权成功*/
public function getAuth(Request $request)
{
$app_id = $request->get('app_id');
if(empty($app_id)){
return response()->json(['errcode'=>'40016','errmsg'=>'缺少小程序appId']);
}
$info = DB::table('wx_third_message')->where('info_type','code')->where('auth_appid',$app_id)->where('expire_time','>',time())->orderBy('id','DESC')->first();
if(!empty($info)){
return response()->json(['errcode'=>'0','info'=>$info,'errmsg'=>'ok']);
}else{
$info = DB::table('wx_third_message')->where('info_type','unauthorized')->where('auth_appid',$app_id)->orderBy('id','DESC')->first();
if(!empty($info)){
return response()->json(['errcode'=>'10','info'=>$info,'errmsg'=>'取消授权']);
}
return response()->json(['errcode'=>'40011','errmsg'=>'没有授权信息']);
}
}

/*授权成功获取授权信息*/
public function queryAuth(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$token_info = DB::table('wx_third_message')->where('company_id',$company_id)->where('info_type','component_access_token')->where('expire_time','>',time())->first();
if(empty($token_info)){
return response()->json(['errcode'=>'40016','errmsg'=>'请重新授权']);
}
$code_info = DB::table('wx_third_message')->where('auth_appid',$app_id)->where('info_type','code')->where('expire_time','>',time())->first();
if(empty($code_info)){
return response()->json(['errcode'=>'40016','errmsg'=>'请重新授权']);
}
$token = $token_info->value;
$code = $code_info->value;
$info = json_decode($this->wxThird->queryAuth($token,$code),true);
if(isset($info['errcode'])){
return response()->json(['errcode'=>'40011','errmsg'=>'获取授权码失败']);
}else{
//Log::info($info);
$authInfo = $info['authorization_info'];
$authAppId = $authInfo['authorizer_appid'];
$data = [
'authorizer_access_token' => $authInfo['authorizer_access_token'],
'authorizer_refresh_token' => $authInfo['authorizer_refresh_token']
];
$expire_time = $authInfo['expires_in'] + time();
$authorization_info = DB::table('wx_third_message')->where('auth_appid',$authAppId)->where('company_id',$company_id)->where('info_type','authorization_info')->first();
if(empty($authorization_info)){
DB::table('wx_third_message')->insert([
'info_type' => 'authorization_info',
'value' => json_encode($data),
'auth_appid' => $authAppId,
'third_appid' => $this->appId,
'expire_time' => $expire_time,
'company_id' => $company_id,
]);
}else{
DB::table('wx_third_message')->where('id',$authorization_info->id)->update(['value'=>json_encode($data),'expire_time'=>$expire_time]);
}
return response()->json(['errcode'=>'0','errmsg'=>'ok']);
}
}

/*提交代码*/
public function commit(Request $request)
{
$setting = DB::table('admin_setting')->where('name','miniapp_shop_template_id')->first();
if(empty($setting)){
return response()->json(['errcode'=>'40016','errmsg'=>'菜东家后台没有设置小程序模板']);
}
$app_id = $request->get('app_id');
$user_version = $request->get('user_version');
$user_desc = $request->get('user_desc');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];

$host = $_SERVER['HTTP_HOST'];
$ext_json = json_encode([
'extAppid' => $app_id,
'ext' => [
'attr' => [
'host'=>'https://'.$host
],
],
]);

$result = json_decode($this->wxThird->commit($auth_code,$setting->value,$ext_json,$user_version,$user_desc),true);
if($result['errcode'] == 0){
return response()->json(['errcode'=>'0','errmsg'=>'ok']);
}else{
return response()->json(['errcode'=>'40011','errmsg'=>$result['errmsg']]);
}
}

/*获取上传页面*/
public function getPage(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->getPage($auth_code),true);
return $result;
}

/*获取体验二维码*/
public function getQrcode(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$steam = $this->wxThird->getQrcode($auth_code);
$result = json_decode($steam,true);
if(isset($result['errcode'])){
return response()->json(['errcode'=>'40016','errmsg'=>$result['errmsg']]);
}else{
$headArr = explode("\r\n", $steam);
$path = public_path().'/storage/wxQrcode/wx_'.time().'.jpg';
file_exists(public_path().'/storage/wxQrcode/')|| (mkdir(public_path().'/storage/wxQrcode/',0755,true) && chmod(public_path().'/storage/wxQrcode/',0755));
file_put_contents($path,$headArr[6]);
return response()->json(['errcode'=>'0','path'=>'/storage/wxQrcode/wx_'.time().'.jpg','errmsg'=>'ok']);
}
}

/*提交审核*/
public function submitAudit(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$setting = DB::table('admin_setting')->where('name','miniapp_shop_template_id')->first();
if(empty($setting)){
return response()->json(['errcode'=>'40016','errmsg'=>'菜东家后台没有设置小程序模板']);
}
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->submitAudit($auth_code),true);
if($result['errcode']){
return response()->json(['errcode'=>'40011','errmsg'=>$result['errmsg']]);
}else{
$auditid = $result['auditid'];
$audit = DB::table('wx_third_message')->where('company_id',$company_id)->where('auth_appid',$app_id)->where('info_type','auditid')->first();
if(empty($audit)){
DB::table('wx_third_message')->insert([
'company_id' => $company_id,
'info_type' => 'auditid',
'template_id' => $setting->value,
'auth_appid' => $app_id,
'value' => $auditid,
'third_appid' => $this->appId
]);
}else{
DB::table('wx_third_message')->where('id',$audit->id)->update(['template_id'=>$setting->value,'value'=>$auditid]);
}
return response()->json(['errcode'=>'0','errmsg'=>'ok']);
}
}

/*获取审核结果*/
public function getAuditStatus(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$setting = DB::table('admin_setting')->where('name','miniapp_shop_template_id')->first();
if(empty($setting)){
return response()->json(['errcode'=>'40016','errmsg'=>'菜东家后台没有设置小程序模板']);
}
$audit = DB::table('wx_third_message')->where('template_id',$setting->value)->where('company_id',$company_id)->where('info_type','auditid')->where('auth_appid',$app_id)->first();
if(empty($audit)){
return response()->json(['errcode'=>'40016','errmsg'=>'没有已上传版本']);
}
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->getAuditStatus($auth_code,$audit->value),true);
$status = [
'审核成功','审核被拒绝','审核中','已撤回','审核延后'
];
if($result['errcode']){
return response()->json(['errcode'=>'40011','status'=>$status[$result['status']],'reason'=>($result['status'] == 1 || $result['status'] == 4) ? $result['reason'] : '','errmsg'=>$result['errmsg']]);
}else{
return response()->json(['errcode'=>'0','status'=>$status[$result['status']],'reason'=>($result['status'] == 1 || $result['status'] == 4) ? $result['reason'] : '','errmsg'=>'ok']);
}
}

/*获取最近一次审核结果*/
public function getLatestAuditStatus(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->getLatestAuditStatus($auth_code),true);
if($result['errcode']){
return response()->json(['errcode'=>'40011','result'=>$result,'errmsg'=>'查询失败']);
}else{
return response()->json(['errcode'=>'0','result'=>$result,'errmsg'=>'ok']);
}
}

/*撤销提交审核*/
public function undoCodeAudit(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->undoCodeAudit($auth_code),true);
if($result['errcode']){
return response()->json(['errcode'=>'40011','errmsg'=>$result['errmsg']]);
}else{
return response()->json(['errcode'=>'0','result'=>$result,'errmsg'=>'ok']);
}
}

/*小程序发布*/
public function release(Request $request)
{
$setting = DB::table('admin_setting')->where('name','miniapp_shop_template_id')->first();
if(empty($setting)){
return response()->json(['errcode'=>'40016','errmsg'=>'菜东家后台没有设置小程序模板']);
}
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->release($auth_code),true);
if($result['errcode']){
return response()->json(['errcode'=>'40011','errmsg'=>$result['errmsg']]);
}else{
DB::table('wx_third_message')->insert([
'info_type' => 'release',
'value' => '',
'template_id' => $setting->value,
'third_appid' => $this->appId,
'auth_appid' => $app_id,
'company_id' => $company_id
]);
return response()->json(['errcode'=>'0','result'=>$result,'errmsg'=>'ok']);
}
}

/*查询是否发布小程序*/
public function getRelease(Request $request)
{
$setting = DB::table('admin_setting')->where('name','miniapp_shop_template_id')->first();
if(empty($setting)){
return response()->json(['errcode'=>'40016','errmsg'=>'菜东家后台没有设置小程序模板']);
}
$app_id = $request->get('app_id');
$company_id = Saas::id();
$info = DB::table('wx_third_message')->where('info_type','release')->where('template_id',$setting->value)->where('auth_appid',$app_id)->first();
if(empty($info)){
return response()->json(['errcode'=>'40011','errmsg'=>'小程序未发布']);
}else{
return response()->json(['errcode'=>'0','errmsg'=>'小程序已发布']);
}
}

/*设置服务器域名*/
//protected function setRequestUrl($auth_code,$host)
public function setRequestUrl(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$host = $_SERVER['HTTP_HOST'];/*$_SERVER['HTTP_HOST']*/
$result = json_decode($this->wxThird->modifyDomain($auth_code,'set',$host),true);
if($result['errcode']){
return ['errcode'=>'40011','errmsg'=>$result['errmsg']];
}else{
return ['errcode'=>'0','result'=>$result,'errmsg'=>'ok'];
}
}

/*获取服务器域名*/
//protected function getRequestUrl($auth_code)
public function getRequestUrl(Request $request)
{
$app_id = $request->get('app_id');
$company_id = Saas::id();
$auth = $this->getAuthCode($app_id,$company_id);
if($auth['errcode']){
return response()->json($auth);
}
$auth_code = $auth['value'];
$result = json_decode($this->wxThird->modifyDomain($auth_code,'get'),true);
if($result['errcode']){
return ['errcode'=>'40011','errmsg'=>$result['errmsg']];
}else{
$host = $_SERVER['HTTP_HOST'];/*$_SERVER['HTTP_HOST']*/
if($result['requestdomain'][0] != 'https://'.$host){
return ['errcode'=>'40080','result'=>$result,'errmsg'=>'ok'];
}
return ['errcode'=>'0','result'=>$result,'errmsg'=>'ok'];
}
}

/*
* 获取授权信息
*/
protected function getAuthCode($app_id,$company_id)
{
$info = DB::table('wx_third_message')->where('auth_appid',$app_id)->where('company_id',$company_id)->where('info_type','authorization_info')->first();
if(empty($info)){
return ['errcode'=>'40016','errmsg'=>'请先授权'];
}
$data = json_decode($info->value,true);
$auth_code = $data['authorizer_access_token'];
if(time() > $info->expire_time){
$token_info = DB::table('wx_third_message')->where('company_id',$company_id)->where('info_type','component_access_token')->where('expire_time','>',time())->first();
if(empty($token_info)){
$token_info = $this->getComponentAccessToken($company_id);
if($token_info['errcode']){
return $token_info;
}
$token = $token_info['value'];
}else{
$token = $token_info->value;
}
$auth_code = $this->refreshAuthCode($data['authorizer_refresh_token'],$token,$app_id,$info->id);
if(!$auth_code){
return ['errcode'=>'40016','errmsg'=>'获取授权码失败,请重新授权'];
}
}
return ['errcode'=>0,'value'=>$auth_code];
}

/*
* 获取令牌
*/
protected function getComponentAccessToken($company_id)
{
$ticket_info = DB::table('wx_third_message')->where('info_type','component_verify_ticket')->orderBy('id','DESC')->first();
if(empty($ticket_info)){
return ['errcode'=>'40016','errmsg'=>'等待平台推送消息'];
}
$componentToken = json_decode($this->wxThird->componentToken($ticket_info->value),true);
if(isset($componentToken['errcode'])){
return ['errcode'=>'40011','errmsg'=>$componentToken['errmsg']];
}
$token_info = DB::table('wx_third_message')->where('info_type','component_access_token')->where('company_id',$company_id)->first();
if(empty($token_info)){
DB::table('wx_third_message')->insert([
'third_appid' => $this->appId,
'value' => $componentToken['component_access_token'],
'info_type' => 'component_access_token',
'expire_time' => time()+3600,
'company_id' => $company_id,
]);
}else{
DB::table('wx_third_message')->where('id',$token_info->id)->update(['expire_time' => time()+3600,'value'=>$componentToken['component_access_token'],]);
}
return ['errcode'=>'0','value'=>$componentToken['component_access_token']];
}

/*
* 刷新 authorizer_access_token
*/
protected function refreshAuthCode($refresh_code,$token,$app_id,$id)
{
$info = json_decode($this->wxThird->refreshAuthCode($refresh_code,$token,$app_id),true);
if(isset($info['errcode'])){
return false;
}
$data = [
'authorizer_access_token' => $info['authorizer_access_token'],
'authorizer_refresh_token' => $info['authorizer_refresh_token'],
];
$expire_time = time() + $info['expires_in'];
DB::table('wx_third_message')->where('id',$id)->update(['value'=>json_encode($data),'expire_time'=>$expire_time]);
return $info['authorizer_access_token'];
}

}
posted @ 2021-06-21 14:44  我是叮当啊  阅读(538)  评论(0编辑  收藏  举报