- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明 Made-with: Cursor
74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ScottPan Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\services\wechat;
|
|
|
|
|
|
use app\dao\wechat\WechatMenuDao;
|
|
use app\services\BaseServices;
|
|
use crmeb\exceptions\AdminException;
|
|
use crmeb\services\wechat\ErrorMessage;
|
|
use crmeb\services\wechat\OfficialAccount;
|
|
use think\annotation\Inject;
|
|
|
|
/**
|
|
* 微信菜单
|
|
* Class WechatMenuServices
|
|
* @package app\services\wechat
|
|
* @mixin WechatMenuDao
|
|
*/
|
|
class WechatMenuServices extends BaseServices
|
|
{
|
|
/**
|
|
* @var WechatMenuDao
|
|
*/
|
|
#[Inject]
|
|
protected WechatMenuDao $dao;
|
|
|
|
/**
|
|
* 获取微信菜单
|
|
* @return array|mixed
|
|
*/
|
|
public function getWechatMenu()
|
|
{
|
|
$menus = $this->dao->value(['key' => 'wechat_menus'], 'result');
|
|
return $menus ? json_decode($menus, true) : [];
|
|
}
|
|
|
|
/**
|
|
* 保存微信菜单
|
|
* @param array $buttons
|
|
* @return bool
|
|
*/
|
|
public function saveMenu(array $buttons)
|
|
{
|
|
try {
|
|
$res = OfficialAccount::instance()->user()->menuCreate($buttons);
|
|
if ($res['errcode'] !== 0) {
|
|
throw new AdminException($res['errmsg']);
|
|
}
|
|
if ($this->dao->count(['key' => 'wechat_menus'])) {
|
|
$this->dao->update('wechat_menus', ['result' => json_encode($buttons), 'add_time' => time()], 'key');
|
|
} else {
|
|
$this->dao->save(['key' => 'wechat_menus', 'result' => json_encode($buttons), 'add_time' => time()]);
|
|
}
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
if (strstr($e->getMessage(), 'Request AccessToken fail. response')) {
|
|
$msgData = str_replace('Request AccessToken fail. response: ', '', $e->getMessage());
|
|
$msgData = json_decode($msgData, true);
|
|
$errcode = $msgData['errcode'] ?? 0;
|
|
if ($errcode == 40164) {
|
|
throw new AdminException('您得ip不再白名单中,请前往腾讯微信公众平台添加ip白名单');
|
|
}
|
|
}
|
|
if (strstr($e->getMessage(), 'invalid weapp appid')) {
|
|
throw new AdminException('您填写得appid无效,请检查');
|
|
}
|
|
throw new AdminException(ErrorMessage::getMessage($e->getMessage()));
|
|
}
|
|
}
|
|
}
|