|
|
|
@ -126,39 +126,43 @@ class Server |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function getRefreshToken($appid) |
|
|
|
|
{ |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
return $model->where(['appid' => $appid])->value('refresh_token'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function storeId($appid) |
|
|
|
|
{ |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
return $model->where(['appid' => $appid])->value('store_id'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @notes:获取体验码 |
|
|
|
|
* @param $appid |
|
|
|
|
* @return array|mixed |
|
|
|
|
* @throws DataNotFoundException |
|
|
|
|
* @throws DbException |
|
|
|
|
* @throws ModelNotFoundException |
|
|
|
|
* @return string |
|
|
|
|
* @author: wanghousheng |
|
|
|
|
*/ |
|
|
|
|
public function getQrcode($appid) |
|
|
|
|
public function getQrcode($appid): string |
|
|
|
|
{ |
|
|
|
|
$savePath = root_path() . "public/wxserve/experience"; |
|
|
|
|
!is_dir($savePath) && mkdir($savePath, 0755, true); |
|
|
|
|
$savePath .= '/' . $appid . '.jpg'; |
|
|
|
|
if (file_exists($savePath)) { |
|
|
|
|
return '/wxserve/experience/' . $appid . '.jpg?time=' . time(); |
|
|
|
|
return '/wxserve/experience/' . $appid . '.jpg'; |
|
|
|
|
} |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
$info = $model->where(['appid' => $appid])->find(); |
|
|
|
|
if (!empty($info)) { |
|
|
|
|
$info = $info->toArray(); |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid, $info['refresh_token']); |
|
|
|
|
if ($access_token) { |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/get_qrcode?access_token=' . $access_token; |
|
|
|
|
$client = new Client(); |
|
|
|
|
$response = $client->get($url); |
|
|
|
|
$res = $response->body(); |
|
|
|
|
if ($res) { |
|
|
|
|
$newFile = fopen($savePath, "a"); |
|
|
|
|
fwrite($newFile, $res); |
|
|
|
|
fclose($newFile); |
|
|
|
|
return '/wxserve/experience/' . $appid . '.jpg?time=' . time(); |
|
|
|
|
} |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid); |
|
|
|
|
if ($access_token) { |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/get_qrcode?access_token=' . $access_token; |
|
|
|
|
$client = new Client(); |
|
|
|
|
$response = $client->get($url); |
|
|
|
|
$res = $response->body(); |
|
|
|
|
if ($res) { |
|
|
|
|
$newFile = fopen($savePath, "a"); |
|
|
|
|
fwrite($newFile, $res); |
|
|
|
|
fclose($newFile); |
|
|
|
|
return '/wxserve/experience/' . $appid . '.jpg'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ''; |
|
|
|
@ -169,75 +173,121 @@ class Server |
|
|
|
|
* @param $appid |
|
|
|
|
* @param $template_id |
|
|
|
|
* @return bool |
|
|
|
|
* @throws DataNotFoundException |
|
|
|
|
* @throws DbException |
|
|
|
|
* @throws ModelNotFoundException|BaseException |
|
|
|
|
* @throws BaseException |
|
|
|
|
* @author: wanghousheng |
|
|
|
|
*/ |
|
|
|
|
public function commit($appid, $template_id): bool |
|
|
|
|
{ |
|
|
|
|
$version = ''; |
|
|
|
|
$desc = ''; |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
$info = $model->where(['appid' => $appid])->find(); |
|
|
|
|
if (!empty($info)) { |
|
|
|
|
$info = $info->toArray(); |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid, $info['refresh_token']); |
|
|
|
|
//获取模板 |
|
|
|
|
$templatelist = $this->getTemplatelist(); |
|
|
|
|
if ($templatelist) { |
|
|
|
|
foreach ($templatelist as $value) { |
|
|
|
|
if (!empty($value['user_version']) && !empty($value['user_desc']) && $value['template_id'] == $template_id) { |
|
|
|
|
$version = $value['user_version']; |
|
|
|
|
$desc = $value['user_desc']; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid); |
|
|
|
|
//获取模板 |
|
|
|
|
$templatelist = $this->getTemplatelist(); |
|
|
|
|
if ($templatelist) { |
|
|
|
|
foreach ($templatelist as $value) { |
|
|
|
|
if (!empty($value['user_version']) && $value['template_id'] == $template_id) { |
|
|
|
|
$version = $value['user_version']; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ($version == '' || $desc == '') { |
|
|
|
|
throwError('模板不存在'); |
|
|
|
|
} |
|
|
|
|
if ($version == '') { |
|
|
|
|
throwError('模板不存在'); |
|
|
|
|
} |
|
|
|
|
$data['template_id'] = $template_id; |
|
|
|
|
$data['user_version'] = $version; |
|
|
|
|
$data['user_desc'] = 'updated'; |
|
|
|
|
$ext_json['extAppid'] = $appid; |
|
|
|
|
$ext_json['ext']['store_id'] = $this->storeId($appid); |
|
|
|
|
$ext_json['window'] = []; |
|
|
|
|
$data['ext_json'] = json_encode($ext_json); |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $access_token; |
|
|
|
|
$result = $this->curlPost($url, json_encode($data)); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['errmsg']) && $result['errmsg'] == 'ok') { |
|
|
|
|
//体验码 |
|
|
|
|
$qr_code = $this->getQrcode($appid); |
|
|
|
|
if ($qr_code) { |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
$model->update(['experience_code' => $qr_code], ['appid' => $appid]); |
|
|
|
|
} |
|
|
|
|
$data['template_id'] = $template_id; |
|
|
|
|
$data['user_version'] = '1.2.0'; |
|
|
|
|
$data['user_desc'] = 'wanghousheng'; |
|
|
|
|
$ext_json['extAppid'] = $appid; |
|
|
|
|
$ext_json['ext']['store_id'] = $info['store_id']; |
|
|
|
|
$ext_json['window'] = []; |
|
|
|
|
$data['ext_json'] = json_encode($ext_json); |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $access_token; |
|
|
|
|
$result = $this->curlPost($url, json_encode($data)); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['errmsg']) && $result['errmsg'] == 'ok') { |
|
|
|
|
$model->update(['version' => $version . $desc], ['id' => $info['id']]); |
|
|
|
|
return true; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @notes:获取版本信息 |
|
|
|
|
* @param $appid |
|
|
|
|
* @return bool |
|
|
|
|
* @author: wanghousheng |
|
|
|
|
*/ |
|
|
|
|
public function getVersioninfo($appid): bool |
|
|
|
|
{ |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid); |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/getversioninfo?access_token=' . $access_token; |
|
|
|
|
$result = $this->curlPost($url, json_encode([])); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['errmsg']) && $result['errmsg'] == 'ok') { |
|
|
|
|
$up = []; |
|
|
|
|
if (!empty($result['exp_info'])) { |
|
|
|
|
$up['exp_info'] = json_encode($result['exp_info']); |
|
|
|
|
} |
|
|
|
|
if (!empty($result['release_info'])) { |
|
|
|
|
$up['release_info'] = json_encode($result['release_info']); |
|
|
|
|
} |
|
|
|
|
if ($up) { |
|
|
|
|
$model = new WxserverAccount(); |
|
|
|
|
$model->update($up, ['appid' => $appid]); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @notes:获取隐私接口检测结果 |
|
|
|
|
* @param $appid |
|
|
|
|
* @return mixed|string |
|
|
|
|
* @author: wanghousheng |
|
|
|
|
*/ |
|
|
|
|
public function privacyInfo($appid) |
|
|
|
|
{ |
|
|
|
|
$errmsg = ''; |
|
|
|
|
$access_token = $this->authorizerAccessToken($appid); |
|
|
|
|
$url = 'https://api.weixin.qq.com/wxa/security/get_code_privacy_info?access_token=' . $access_token; |
|
|
|
|
$result = $this->curlGet($url); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['errmsg'])) { |
|
|
|
|
$errmsg = $result['errmsg']; |
|
|
|
|
} |
|
|
|
|
return $errmsg; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @notes:获取/刷新接口调用令牌 |
|
|
|
|
* @param $appid |
|
|
|
|
* @param $refresh_token |
|
|
|
|
* @return false|mixed |
|
|
|
|
* @author: wanghousheng |
|
|
|
|
*/ |
|
|
|
|
public function authorizerAccessToken($appid, $refresh_token) |
|
|
|
|
public function authorizerAccessToken($appid) |
|
|
|
|
{ |
|
|
|
|
if (Cache::has($appid . '_authorizer_access_token')) { |
|
|
|
|
return Cache::get($appid . '_authorizer_access_token'); |
|
|
|
|
} |
|
|
|
|
$token = $this->getComponentAccessToken(); |
|
|
|
|
if ($token) { |
|
|
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $token; |
|
|
|
|
$data['component_appid'] = self::APPID; |
|
|
|
|
$data['authorizer_appid'] = $appid; |
|
|
|
|
$data['authorizer_refresh_token'] = $refresh_token; |
|
|
|
|
$result = $this->curlPost($url, json_encode($data)); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['authorizer_access_token'])) { |
|
|
|
|
Cache::set($appid . '_authorizer_access_token', $result['authorizer_access_token'], 6200); |
|
|
|
|
return $result['authorizer_access_token']; |
|
|
|
|
$refresh_token = $this->getRefreshToken($appid); |
|
|
|
|
if ($refresh_token) { |
|
|
|
|
$token = $this->getComponentAccessToken(); |
|
|
|
|
if ($token) { |
|
|
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $token; |
|
|
|
|
$data['component_appid'] = self::APPID; |
|
|
|
|
$data['authorizer_appid'] = $appid; |
|
|
|
|
$data['authorizer_refresh_token'] = $refresh_token; |
|
|
|
|
$result = $this->curlPost($url, json_encode($data)); |
|
|
|
|
$result = json_decode($result, true); |
|
|
|
|
if ($result && !empty($result['authorizer_access_token'])) { |
|
|
|
|
Cache::set($appid . '_authorizer_access_token', $result['authorizer_access_token'], 6200); |
|
|
|
|
return $result['authorizer_access_token']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|