wang hou sheng 10 months ago
parent c4065a2aeb
commit 20a6168c33
  1. 73
      app/admin/controller/Wxserve.php
  2. 7
      app/api/controller/Wxserver.php
  3. 274
      app/common/library/wxserver/Server.php

@ -0,0 +1,73 @@
<?php
declare (strict_types=1);
namespace app\admin\controller;
use app\common\library\wxserver\Server;
use PHPQRCode\QRcode;
use think\db\exception\DbException;
use think\facade\Db;
use think\response\Json;
class Wxserve extends Controller
{
/**
* @notes:生成授权二维码
* @return Json
* @author: wanghousheng
*/
public function qrcode(): Json
{
$obj = new Server();
$url = $this->request->domain(true) . '/api/wxserver/redirect';
$jumUrl = $obj->jumpH5Url($url);
$savePath = root_path() . "public/wxserve";
!is_dir($savePath) && mkdir($savePath, 0755, true);
$savePath .= '/auth_qrcode.png';
QRcode::png($jumUrl, $savePath, 'L', 15, 1);
return $this->renderSuccess(['img_url' => $this->request->domain(true) . '/wxserve/auth_qrcode.png?time=' . time()]);
}
/**
* @notes:授权列表
* @return Json
* @throws DbException
* @author: wanghousheng
*/
public function authList(): Json
{
$store_ids = [];
$where = [];
$store_id = intval($this->request->get('store_id'));
if ($store_id) {
$where[] = ['store_id', '=', $store_id];
}
$nick_name = $this->request->get('nick_name');
if ($nick_name) {
$where[] = ['nick_name', 'like', "%$nick_name%"];
}
$appid = $this->request->get('appid');
if ($appid) {
$where[] = ['nick_name', '=', "%$appid%"];
}
$result = Db::table('yoshop_wxserver_account')
->where($where)
->field(['appid', 'nick_name', 'head_image', 'user_name', 'qrcode_image', 'store_id', 'create_time', 'version'])
->paginate(15);
$list = $result->items();
$total = $result->total();
if ($total) {
foreach ($list as &$item) {
$store_ids[] = $item['store_id'];
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
}
$store_list = Db::table('yoshop_store')
->whereIn('store_id', $store_ids)
->column('store_name', 'store_id');
foreach ($list as &$item) {
$item['store_name'] = $store_list[$item['store_id']] ?? '';
}
}
return $this->renderSuccess(['list' => $list, 'total' => $total]);
}
}

@ -16,6 +16,13 @@ class Wxserver extends BaseController
return $this->renderSuccess(compact('jumUrl'));
}
public function indexCount()
{
$obj = new \app\common\library\wxserver\Server();
$list = $obj->getStoreId('wxd87b1a7332d974d0');
return $this->renderSuccess(compact('list'));
}
public function verifyTicket()
{
$xmlData = file_get_contents("php://input");

@ -4,6 +4,9 @@ declare (strict_types=1);
namespace app\common\library\wxserver;
use app\common\model\WxserverAccount;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;
use think\facade\Db;
@ -36,8 +39,7 @@ class Server
$data = $this->crypt->fromXml($encryptXml);
if (!empty($data['ComponentVerifyTicket'])) {
$ticket = $data['ComponentVerifyTicket'];
Db::table('yoshop_wx_server')->insertGetId(['content' => '存入ticket成功&' . $ticket, 'created_at' => date('Y-m-d H:i:s')]);
Cache::set('component_verify_ticket', $ticket, 3600);
Cache::set('component_verify_ticket', $ticket);
}
}
return $ticket;
@ -109,9 +111,226 @@ class Server
return false;
}
/**
* @notes:发布版本
* @param $appid
* @param $template_id
* @param $version
* @param $desc
* @return true|void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function commit($appid, $template_id, $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']);
$data['template_id'] = $template_id;
$data['user_version'] = $version;
$data['user_desc'] = $desc;
$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;
}
}
}
/**
* @notes:获取/刷新接口调用令牌
* @param $appid
* @param $refresh_token
* @return false|mixed
* @author: wanghousheng
*/
public function authorizerAccessToken($appid, $refresh_token)
{
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'];
}
}
return false;
}
private function getAuditStatusText($status): string
{
$status = intval($status);
switch ($status) {
case 0:
$text = '未提交审核';
break;
case 1:
$text = '审核中';
break;
case 2:
$text = '审核驳回';
break;
case 3:
$text = '审核通过';
break;
case 4:
$text = '提审中';
break;
case 5:
$text = '提审失败';
break;
default:
$text = '未知';
break;
}
return $text;
}
/**
* @notes:获取草稿箱列表
* @return array|mixed
* @author: wanghousheng
*/
public function getTemplatedraftlist()
{
if (Cache::has('templatedraftlist')) {
return Cache::get('templatedraftlist');
}
$token = $this->getComponentAccessToken();
$list = [];
if ($token) {
$url = 'https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=' . $token;
$result = $this->curlGet($url);
$result = json_decode($result, true);
if ($result && !empty($result['draft_list'])) {
$draft_list = $result['draft_list'];
foreach ($draft_list as $value) {
$list[] = [
'create_time' => date('Y-m-d H:i:s', strtotime($value['create_time'])),
'user_version' => $value['user_version'],
'user_desc' => $value['user_desc'],
'id' => $value['draft_id'],
];
}
Cache::set('templatelist', $list, 86400);
}
}
return $list;
}
/**
* @notes:添加草稿箱到模板中
* @param $draft_id
* @return bool
* @author: wanghousheng
*/
public function addTotemplate($draft_id): bool
{
$templateList = $this->getTemplatelist();
if ($templateList) {
$draft_ids = array_column($templateList, 'draft_id');
if (in_array($draft_id, $draft_ids)) {
return true;
}
}
$token = $this->getComponentAccessToken();
if ($token) {
$url = 'https://api.weixin.qq.com/wxa/addtotemplate?access_token=' . $token;
$data['draft_id'] = $draft_id;
$data['template_type'] = 1;
$res = $this->curlPost($url, json_encode($data));
$res = json_decode($res, true);
if ($res && !empty($res['errmsg']) && $res['errmsg'] == 'ok') {
Cache::delete('templatelist');
return true;
}
}
return false;
}
/**
* @notes:删除模板
* @param $template_id
* @return bool
* @author: wanghousheng
*/
public function deleTetemplate($template_id): bool
{
$token = $this->getComponentAccessToken();
if ($token) {
$url = 'https://api.weixin.qq.com/wxa/deletetemplate?access_token=' . $token;
$data['template_id'] = $template_id;
$res = $this->curlPost($url, json_encode($data));
$res = json_decode($res, true);
if ($res && !empty($res['errmsg']) && $res['errmsg'] == 'ok') {
Cache::delete('templatelist');
return true;
}
}
return false;
}
/**
* @notes:代码模板列表
* @return array
* @author: wanghousheng
*/
public function getTemplatelist(): array
{
if (Cache::has('templatelist')) {
return Cache::get('templatelist');
}
$token = $this->getComponentAccessToken();
$list = [];
if ($token) {
$url = 'https://api.weixin.qq.com/wxa/gettemplatelist?access_token=' . $token;
$data['template_type'] = 1;
$result = $this->curlGet($url, $data);
$result = json_decode($result, true);
if ($result && !empty($result['template_list'])) {
$template_list = $result['template_list'];
foreach ($template_list as $value) {
$list[] = [
'create_time' => date('Y-m-d H:i:s', strtotime($value['create_time'])),
'user_version' => $value['user_version'],
'user_desc' => $value['user_desc'],
'template_id' => $value['template_id'],
'audit_status' => $value['audit_status'],
'audit_status_text' => $this->getAuditStatusText($value['audit_status']),
'reason' => $value['reason'],
'draft_id' => $value['draft_id'],
];
}
Cache::set('templatelist', $list, 86400);
}
}
return $list;
}
/**
* @notes:获取授权信息
* @param $authorization_code
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function authorizationInfo($authorization_code)
@ -133,6 +352,11 @@ class Server
$authorizer_info = $res['authorizer_info'];
$authorization_info = $res['authorization_info'];
$insert_data['appid'] = $authorization_info['authorizer_appid'];
$model = new WxserverAccount();
$account_info = $model->where(['appid' => $insert_data['appid']])->find();
if (!empty($account_info)) {
return;
}
$insert_data['access_token'] = $result['authorization_info']['authorizer_access_token'];
$insert_data['refresh_token'] = $authorization_info['authorizer_refresh_token'];
$insert_data['nick_name'] = $authorizer_info['nick_name'];
@ -141,12 +365,40 @@ class Server
$insert_data['qrcode_image'] = $authorizer_info['qrcode_url'];
$insert_data['principal_name'] = $authorizer_info['principal_name'];
$insert_data['signature'] = $authorizer_info['signature'];
$model = new WxserverAccount();
$insert_data['store_id'] = $this->getStoreId($authorization_info['authorizer_appid']);
$insert_data['create_time'] = time();
$insert_data['update_time'] = time();
$model->insertGetId($insert_data);
}
}
}
/**
* @notes:获取店铺ID
* @param $appid
* @return int|mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function getStoreId($appid)
{
$list = Db::table('yoshop_wxapp_setting')->where(['key' => 'basic'])->select();
if (!$list->isEmpty()) {
$list = $list->toArray();
foreach ($list as $value) {
if ($value['values']) {
$item = json_decode($value['values'], true);
if (!empty($item['app_id']) && $item['app_id'] == $appid) {
return $value['store_id'];
}
}
}
}
return 0;
}
private function curlPost($url, $post_data, $timeout = 5)
{
$ch = curl_init();
@ -180,4 +432,20 @@ class Server
return false;
}
}
protected function curlGet(string $url, array $data = [])
{
if (!empty($data)) {
$url = $url . '?' . http_build_query($data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
Loading…
Cancel
Save