wang hou sheng 11 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 app\common\library\wxserver\Server;
use cores\exception\BaseException; use cores\exception\BaseException;
use PHPQRCode\QRcode; use PHPQRCode\QRcode;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db; use think\facade\Db;
use think\response\Json; use think\response\Json;
@ -63,6 +61,15 @@ class Wxserve extends Controller
foreach ($list as &$item) { foreach ($list as &$item) {
$store_ids[] = $item['store_id']; $store_ids[] = $item['store_id'];
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); $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') $store_list = Db::table('yoshop_store')
->whereIn('store_id', $store_ids) ->whereIn('store_id', $store_ids)
@ -138,12 +145,9 @@ class Wxserve extends Controller
} }
/** /**
* @notes:提交版本审核 * @notes:提交体验
* @return Json * @return Json
* @throws DbException
* @throws BaseException * @throws BaseException
* @throws DataNotFoundException
* @throws ModelNotFoundException
* @author: wanghousheng * @author: wanghousheng
*/ */
public function commit(): Json public function commit(): Json

@ -4,20 +4,12 @@ declare (strict_types=1);
namespace app\api\controller; namespace app\api\controller;
use cores\BaseController; use cores\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class Wxserver extends BaseController 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() public function verifyTicket()
{ {
$xmlData = file_get_contents("php://input"); $xmlData = file_get_contents("php://input");
@ -26,6 +18,13 @@ class Wxserver extends BaseController
echo 'success'; echo 'success';
} }
/**
* @notes:授权回调
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function redirect() public function redirect()
{ {
$authorization_code = $this->request->get('auth_code'); $authorization_code = $this->request->get('auth_code');

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

Loading…
Cancel
Save