wang hou sheng 9 months ago
parent 806e4154ce
commit a5e28a5bb6
  1. 16
      app/admin/controller/Wxserve.php
  2. 21
      app/api/controller/Wxserver.php
  3. 186
      app/common/library/wxserver/Server.php

@ -6,9 +6,7 @@ namespace app\admin\controller;
use app\common\library\wxserver\Server;
use cores\exception\BaseException;
use PHPQRCode\QRcode;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\response\Json;
@ -63,6 +61,15 @@ class Wxserve extends Controller
foreach ($list as &$item) {
$store_ids[] = $item['store_id'];
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
if ($item['exp_info']) {
$item['exp_info'] = json_decode($item['exp_info'], true);
}
if ($item['release_info']) {
$item['release_info'] = json_decode($item['release_info'], true);
}
if (!empty($item['experience_code'])) {
$item['experience_code'] = $this->request->domain(true) . $item['experience_code'] . '?time=' . time();
}
}
$store_list = Db::table('yoshop_store')
->whereIn('store_id', $store_ids)
@ -138,12 +145,9 @@ class Wxserve extends Controller
}
/**
* @notes:提交版本审核
* @notes:提交体验
* @return Json
* @throws DbException
* @throws BaseException
* @throws DataNotFoundException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function commit(): Json

@ -4,20 +4,12 @@ declare (strict_types=1);
namespace app\api\controller;
use cores\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class Wxserver extends BaseController
{
public function index()
{
$obj = new \app\common\library\wxserver\Server();
$img = $obj->getQrcode('wxe3ed157849bd07b5');
$img_url = '';
if ($img) {
$img_url = $this->request->domain(true) . $img;
}
return $this->renderSuccess(['url' => $img_url]);
}
public function verifyTicket()
{
$xmlData = file_get_contents("php://input");
@ -26,6 +18,13 @@ class Wxserver extends BaseController
echo 'success';
}
/**
* @notes:授权回调
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function redirect()
{
$authorization_code = $this->request->get('auth_code');

@ -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;

Loading…
Cancel
Save