init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
137
application/admin/controller/shopro/wechat/Admin.php
Normal file
137
application/admin/controller/shopro/wechat/Admin.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\wechat;
|
||||
|
||||
use fast\Random;
|
||||
use addons\shopro\facade\Wechat;
|
||||
use app\admin\model\shopro\ThirdOauth;
|
||||
use app\admin\controller\shopro\Common;
|
||||
|
||||
class Admin extends Common
|
||||
{
|
||||
|
||||
protected $wechat;
|
||||
protected $noNeedRight = ['getQrcode', 'checkScan', 'unbind'];
|
||||
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->wechat = Wechat::officialAccountManage();
|
||||
}
|
||||
|
||||
// 获取公众号二维码
|
||||
public function getQrcode()
|
||||
{
|
||||
$event = $this->request->param('event');
|
||||
|
||||
if (!in_array($event, ['bind'])) {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
|
||||
$adminId = $this->auth->id;
|
||||
$thirdOauth = ThirdOauth::where([
|
||||
'provider' => 'wechat',
|
||||
'platform' => 'admin',
|
||||
'admin_id' => $adminId
|
||||
])->find();
|
||||
|
||||
if ($thirdOauth) {
|
||||
error_stop('已绑定微信账号', -2, $thirdOauth);
|
||||
}
|
||||
|
||||
// 二维码和缓存过期时间
|
||||
$expireTime = 1 * 60;
|
||||
|
||||
// 事件唯一标识
|
||||
$eventId = Random::uuid();
|
||||
|
||||
$cacheKey = "wechatAdmin.{$event}.{$eventId}";
|
||||
|
||||
cache($cacheKey, ['id' => 0], $expireTime);
|
||||
|
||||
try {
|
||||
$result = $this->wechat->qrcode->temporary($cacheKey, $expireTime);
|
||||
$qrcode = $this->wechat->qrcode->url($result['ticket']);
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('', null, [
|
||||
'url' => $qrcode,
|
||||
'eventId' => $eventId
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查扫码结果
|
||||
public function checkScan()
|
||||
{
|
||||
$event = $this->request->param('event');
|
||||
$eventId = $this->request->param('eventId');
|
||||
|
||||
if (!in_array($event, ['bind'])) {
|
||||
error_stop('参数错误');
|
||||
}
|
||||
|
||||
$cacheKey = "wechatAdmin.{$event}.{$eventId}";
|
||||
|
||||
$cacheValue = cache($cacheKey);
|
||||
|
||||
if (empty($cacheValue)) {
|
||||
error_stop('二维码已过期, 请重新扫码');
|
||||
}
|
||||
|
||||
if ($cacheValue['id'] === 0) {
|
||||
error_stop('等待扫码', -1);
|
||||
}
|
||||
|
||||
if ($cacheValue['id'] !== 0) {
|
||||
switch ($event) {
|
||||
case 'bind':
|
||||
$adminId = $this->auth->id;
|
||||
|
||||
$thirdOauth = ThirdOauth::where([
|
||||
'provider' => 'wechat',
|
||||
'platform' => 'admin',
|
||||
'openid' => $cacheValue['id'],
|
||||
])->find();
|
||||
|
||||
if ($thirdOauth && $thirdOauth->admin_id !== 0) {
|
||||
error_stop('该微信账号已被绑定');
|
||||
}
|
||||
|
||||
if (!$thirdOauth) {
|
||||
$thirdOauth = ThirdOauth::create([
|
||||
'provider' => 'wechat',
|
||||
'platform' => 'admin',
|
||||
'openid' => $cacheValue['id'],
|
||||
'admin_id' => $adminId
|
||||
]);
|
||||
} else {
|
||||
$thirdOauth->admin_id = $adminId;
|
||||
$thirdOauth->save();
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
|
||||
// 解绑
|
||||
public function unbind()
|
||||
{
|
||||
$adminId = $this->auth->id;
|
||||
|
||||
$thirdOauth = ThirdOauth::where([
|
||||
'provider' => 'wechat',
|
||||
'platform' => 'admin',
|
||||
'admin_id' => $adminId
|
||||
])->find();
|
||||
|
||||
if ($thirdOauth) {
|
||||
$thirdOauth->admin_id = 0;
|
||||
$thirdOauth->save();
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
30
application/admin/controller/shopro/wechat/Config.php
Normal file
30
application/admin/controller/shopro/wechat/Config.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\wechat;
|
||||
|
||||
use app\admin\model\shopro\Config as ShoproConfig;
|
||||
use app\admin\controller\shopro\Common;
|
||||
|
||||
class Config extends Common
|
||||
{
|
||||
/**
|
||||
* 公众号配置
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
if ('GET' === $this->request->method()) {
|
||||
|
||||
$configs = ShoproConfig::getConfigs('wechat.officialAccount', false);
|
||||
$configs['server_url'] = request()->domain() . '/addons/shopro/wechat.serve';
|
||||
} elseif ('POST' === $this->request->method()) {
|
||||
|
||||
$configs = ShoproConfig::setConfigs('wechat.officialAccount', $this->request->param());
|
||||
}
|
||||
|
||||
$this->success('操作成功', null, $configs);
|
||||
}
|
||||
}
|
||||
178
application/admin/controller/shopro/wechat/Material.php
Normal file
178
application/admin/controller/shopro/wechat/Material.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\wechat;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use addons\shopro\facade\Wechat;
|
||||
use think\Db;
|
||||
use addons\shopro\exception\ShoproException;
|
||||
|
||||
class Material extends Common
|
||||
{
|
||||
|
||||
protected $wechat = null;
|
||||
protected $model = null;
|
||||
protected $noNeedRight = ['select'];
|
||||
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->wechat = Wechat::officialAccountManage();
|
||||
$this->model = new \app\admin\model\shopro\wechat\Material;
|
||||
}
|
||||
|
||||
/**
|
||||
* 素材列表
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$list_rows = intval($this->request->param('list_rows', 10));
|
||||
$page = intval($this->request->param('page', 1));
|
||||
$type = $this->request->param('type', 'news');
|
||||
$offset = intval(($page - 1) * $list_rows);
|
||||
|
||||
if (in_array($type, ['text', 'link'])) {
|
||||
$data = $this->model->sheepFilter()->where('type', $type)->paginate(request()->param('list_rows', 10));
|
||||
} else {
|
||||
// 使用微信远程素材列表
|
||||
try {
|
||||
$res = $this->wechat->material->list($type, $offset, $list_rows);
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$data = [
|
||||
'current_page' => $page,
|
||||
'data' => $res['item'],
|
||||
'last_page' => intval(ceil($res['total_count'] / $list_rows)),
|
||||
'per_page' => $list_rows,
|
||||
'total' => intval($res['total_count']),
|
||||
];
|
||||
}
|
||||
|
||||
$this->success('', null, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$detail = $this->model->get($id);
|
||||
if (!$detail) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$this->success('获取成功', null, $detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only(['type', 'content']);
|
||||
|
||||
if ($params['type'] === 'text') {
|
||||
$params['content'] = urldecode($params['content']);
|
||||
}
|
||||
|
||||
Db::transaction(function () use ($params) {
|
||||
$this->model->save($params);
|
||||
});
|
||||
$this->success('保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$material = $this->model->get($id);
|
||||
$params = $this->request->only(['type', 'content']);
|
||||
if ($params['type'] === 'text') {
|
||||
$params['content'] = urldecode($params['content']);
|
||||
}
|
||||
|
||||
Db::transaction(function () use ($params, $material) {
|
||||
$material->save($params);
|
||||
});
|
||||
|
||||
$this->success('更新成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$is_real = $this->request->param('is_real', 0);
|
||||
Db::transaction(function () use ($id, $is_real) {
|
||||
$menu = $this->model->get($id);
|
||||
if ($is_real) {
|
||||
$menu->force()->delete();
|
||||
} else {
|
||||
$menu->delete();
|
||||
}
|
||||
});
|
||||
|
||||
$this->success('删除成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜单列表
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
$list_rows = intval($this->request->param('list_rows', 10));
|
||||
$page = intval($this->request->param('page', 1));
|
||||
$type = $this->request->param('type', 'news');
|
||||
$offset = intval(($page - 1) * $list_rows);
|
||||
|
||||
if (in_array($type, ['text', 'link'])) {
|
||||
$data = $this->model->where('type', $type)->order('id desc')->paginate(request()->param('list_rows', 10));
|
||||
} else {
|
||||
// 使用微信远程素材列表
|
||||
try {
|
||||
$res = $this->wechat->material->list($type, $offset, $list_rows);
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$data = [
|
||||
'current_page' => $page,
|
||||
'data' => $res['item'],
|
||||
'last_page' => intval(ceil($res['total_count'] / $list_rows)),
|
||||
'per_page' => $list_rows,
|
||||
'total' => intval($res['total_count']),
|
||||
];
|
||||
}
|
||||
$this->success('获取成功', null, $data);
|
||||
}
|
||||
}
|
||||
223
application/admin/controller/shopro/wechat/Menu.php
Normal file
223
application/admin/controller/shopro/wechat/Menu.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\wechat;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use addons\shopro\facade\Wechat;
|
||||
use think\Db;
|
||||
use addons\shopro\exception\ShoproException;
|
||||
|
||||
class Menu extends Common
|
||||
{
|
||||
|
||||
protected $wechat = null;
|
||||
protected $model = null;
|
||||
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->wechat = Wechat::officialAccountManage();
|
||||
$this->model = new \app\admin\model\shopro\wechat\Menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号配置
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$current = $this->getCurrentMenu();
|
||||
$data = $this->model->sheepFilter()->paginate(request()->param('list_rows', 10));
|
||||
|
||||
$this->success('操作成功', null, ['current' => $current, 'list' => $data]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$detail = $this->model->get($id);
|
||||
if (!$detail) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$this->success('获取成功', null, $detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加菜单
|
||||
*
|
||||
* @param int $publish 发布状态:0=不发布,1=直接发布
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$publish = $this->request->param('publish', 0);
|
||||
$params = $this->request->only(['name', 'rules']);
|
||||
// 参数校验
|
||||
$this->svalidate($params, '.add');
|
||||
|
||||
Db::transaction(function () use ($params, $publish) {
|
||||
$menu = $this->model->save($params);
|
||||
if ($menu && $publish) {
|
||||
$this->publishMenu($this->model->id);
|
||||
}
|
||||
return $menu;
|
||||
});
|
||||
|
||||
$this->success('保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$menu = $this->model->get($id);
|
||||
|
||||
$publish = $this->request->param('publish', 0);
|
||||
$params = $this->request->only(['name', 'rules']);
|
||||
|
||||
// 参数校验
|
||||
$this->svalidate($params);
|
||||
|
||||
$menu = Db::transaction(function () use ($params, $menu, $publish) {
|
||||
$menu->save($params);
|
||||
if ($publish) {
|
||||
$this->publishMenu($menu->id);
|
||||
}
|
||||
return $menu;
|
||||
});
|
||||
|
||||
$this->success('更新成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
Db::transaction(function () use ($id) {
|
||||
$menu = $this->model->get($id);
|
||||
$menu->delete();
|
||||
});
|
||||
|
||||
$this->success('删除成功');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发布菜单
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function publish($id)
|
||||
{
|
||||
Db::transaction(function () use ($id) {
|
||||
return $this->publishMenu($id);
|
||||
});
|
||||
|
||||
$this->success('发布成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制菜单
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function copy($id = 0)
|
||||
{
|
||||
if ($id == 0) {
|
||||
$data = [
|
||||
'name' => '复制 当前菜单',
|
||||
'rules' => $this->getCurrentMenu(),
|
||||
];
|
||||
} else {
|
||||
$menu = $this->model->get($id);
|
||||
$data = [
|
||||
'name' => '复制 ' . $menu->name,
|
||||
'rules' => $menu->rules
|
||||
];
|
||||
}
|
||||
|
||||
$menu = $this->model->save($data);
|
||||
$this->success('复制成功');
|
||||
}
|
||||
|
||||
// 发布菜单
|
||||
private function publishMenu($id)
|
||||
{
|
||||
$menu = $this->model->get($id);
|
||||
|
||||
if ($this->setCurrentMenu($menu->rules)) {
|
||||
$this->model->where('id', '<>', $menu->id)->update(['status' => 0]);
|
||||
|
||||
return $menu->save([
|
||||
'status' => 1,
|
||||
'publishtime' => time()
|
||||
]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 获取当前菜单
|
||||
private function getCurrentMenu()
|
||||
{
|
||||
try {
|
||||
$currentMenu = $this->wechat->menu->current();
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if (isset($currentMenu['selfmenu_info']['button'])) {
|
||||
$buttons = $currentMenu['selfmenu_info']['button'];
|
||||
foreach ($buttons as &$button) {
|
||||
if (isset($button['sub_button'])) {
|
||||
$button['sub_button'] = $button['sub_button']['list'];
|
||||
}
|
||||
}
|
||||
return $buttons;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// 设置菜单
|
||||
private function setCurrentMenu($rules)
|
||||
{
|
||||
try {
|
||||
$result = $this->wechat->menu->create($rules);
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
if (isset($result['errcode']) && $result['errcode'] === 0) {
|
||||
return true;
|
||||
} else {
|
||||
$this->error($result['errmsg'] ?? '发布失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
143
application/admin/controller/shopro/wechat/Reply.php
Normal file
143
application/admin/controller/shopro/wechat/Reply.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\wechat;
|
||||
|
||||
use app\admin\controller\shopro\Common;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 常见问题
|
||||
*/
|
||||
class Reply extends Common
|
||||
{
|
||||
|
||||
/**
|
||||
* Faq模型对象
|
||||
* @var \app\admin\model\shopro\wechat\Reply
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\wechat\Reply;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*
|
||||
* @return string|Json
|
||||
* @throws \think\Exception
|
||||
* @throws DbException
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$group = $this->request->param('group', 'keywords');
|
||||
$data = $this->model->sheepFilter()->where('group', $group)->select();
|
||||
$this->success('操作成功', null, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->only(['group', 'keywords', 'type', 'status', 'content']);
|
||||
|
||||
Db::transaction(function () use ($params) {
|
||||
$result = $this->model->save($params);
|
||||
if($result) {
|
||||
$reply = $this->model;
|
||||
if($reply->group !== 'keywords' && $reply->status === 'enable') {
|
||||
$this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
});
|
||||
|
||||
$this->success('保存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$detail = $this->model->get($id);
|
||||
if (!$detail) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$this->success('获取成功', null, $detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑(支持批量)
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
$reply = $this->model->get($id);
|
||||
$params = $this->request->only(['keywords', 'type', 'status', 'content']);
|
||||
|
||||
// 参数校验
|
||||
// $this->svalidate($params);
|
||||
|
||||
$result = Db::transaction(function () use ($params, $reply) {
|
||||
$result = $reply->save($params);
|
||||
if($result) {
|
||||
if($reply->group !== 'keywords' && $reply->status === 'enable') {
|
||||
$this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('更新成功', null, $result);
|
||||
} else {
|
||||
$this->error('更新失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除(支持批量)
|
||||
*
|
||||
* @param $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (empty($id)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'id'));
|
||||
}
|
||||
|
||||
$list = $this->model->where('id', 'in', $id)->select();
|
||||
$result = Db::transaction(function () use ($list) {
|
||||
$count = 0;
|
||||
foreach ($list as $item) {
|
||||
$count += $item->delete();
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('删除成功', null, $result);
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user