开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (6)
标签 (13)
master
dependabot/npm_and_yarn/js-yaml-3.13.1
dependabot/npm_and_yarn/lodash-4.17.15
dependabot/npm_and_yarn/mixin-deep-1.3.2
dependabot/npm_and_yarn/axios-0.18.1
dev
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
master
分支 (6)
标签 (13)
master
dependabot/npm_and_yarn/js-yaml-3.13.1
dependabot/npm_and_yarn/lodash-4.17.15
dependabot/npm_and_yarn/mixin-deep-1.3.2
dependabot/npm_and_yarn/axios-0.18.1
dev
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (6)
标签 (13)
master
dependabot/npm_and_yarn/js-yaml-3.13.1
dependabot/npm_and_yarn/lodash-4.17.15
dependabot/npm_and_yarn/mixin-deep-1.3.2
dependabot/npm_and_yarn/axios-0.18.1
dev
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
wechat-menu
/
src
/
Data.php
wechat-menu
/
src
/
Data.php
Data.php 7.79 KB
一键复制 编辑 原始数据 按行查看 历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
<?php
namespace Largezhou\WechatMenu;
use Largezhou\WechatMenu\Exceptions\WechatMenuException;
class Data
{
/**
* 允许的请求类型.
*/
const REQUEST_TYPES = [
// 公众号菜单
'menus',
// 菜单事件配置
'menu_events',
// 其他事件配置
'other_events',
// 素材列表
'materials',
// 素材详情
'media',
];
/**
* 永久素材类型.
*/
const MATERIAL_TYPES = [
'news',
'image',
'video',
'voice',
];
/**
* 素材每页数.
*/
const MEDIA_PER_PAGE = 20;
/**
* 返回成功.
*
* @param string $msg 消息
* @param string $data 附带数据
*
* @return string
*/
public static function success($msg = '', $data = null): string
{
return json_encode(
[
'status' => true,
'msg' => $msg,
'data' => $data,
]
);
}
/**
* 返回错误.
*
* @param string $msg 消息
* @param string $type 错误类型: default 默认,wechat 微信接口错误
*
* @return string
*/
public static function error($msg = '', $type = 'default'): string
{
return json_encode(
[
'status' => false,
'msg' => $msg,
'type' => $type,
]
);
}
/**
* 获取存储的数据.
*
* @param string|null $type
*
* @return array|mixed|null
*/
public static function getData(string $type = null)
{
$data = safe_json_decode(@file_get_contents(Manager::getInstance()->getConfig('data_path')), []);
if (!$type) {
return $data;
} else {
return $data[$type] ?? [];
}
}
/**
* 存储数据.
*
* @param mixed $allData 需要保存的数据
*/
public static function saveData($allData)
{
file_put_contents(Manager::getInstance()->getConfig('data_path'), $allData);
}
/**
* 返回从微信服务器获取的公众号菜单.
*
* @return string
*
* @throws Exceptions\WechatMenuException
*/
public static function getMenus(): string
{
$res = Manager::getInstance()->getWechat()->menu->list();
if ($err = static::getWechatError($res)) {
return $err;
}
return static::success('', $res);
}
/**
* 创建公众号菜单.
*
* @param array $menus
*
* @return string
*
* @throws Exceptions\WechatMenuException
*/
public static function postMenus(array $menus): string
{
if (empty($menus)) {
return static::error('至少要有一个菜单才能保存');
}
$res = Manager::getInstance()->getWechat()->menu->create($menus);
if ($err = static::getWechatError($res)) {
return $err;
}
return static::success('菜单保存成功');
}
/**
* 获取指定类型的数据.
*
* @param array $data 请求中的数据
*
* @return string json_encode 后的数据
*
* @throws WechatMenuException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public static function getResources(array $data)
{
$type = static::checkAndGetType($data);
switch ($type) {
case 'menus':
return static::getMenus();
case 'materials':
return static::getMaterials($data);
case 'media':
return static::getMedia($data);
default:
return static::getSettings($type);
}
}
/**
* 存储指定类型的数据.
*
* @param array $data
*
* @return string
*
* @throws WechatMenuException
*/
public static function postResources(array $data): string
{
$type = static::checkAndGetType($data);
if ($type == 'menus') {
return static::postMenus($data['data'] ?? null);
} else {
return static::postSettings($type, $data['data'] ?? null);
}
}
/**
* 获取指定键的数据.
*
* @param string $type
*
* @return string
*/
public static function getSettings($type): string
{
return static::success('', static::getData($type));
}
/**
* 保存来自请求的数据.
*
* @param string $type
* @param mixed $data
*
* @return string
*/
public static function postSettings(string $type, $data): string
{
$allData = static::getData();
$allData[$type] = $data;
$allData = json_encode($allData, JSON_UNESCAPED_UNICODE + JSON_PRETTY_PRINT);
static::saveData($allData);
return static::success('保存设置成功');
}
/**
* 检测请求中的 type 值,并返回.
*
* @param array $data 请求中的数据
*
* @return string
*
* @throws WechatMenuException
*/
protected static function checkAndGetType(array $data): string
{
$type = trim($data['type'] ?? '');
if (!$type) {
throw new WechatMenuException('请求中 type 不能为空');
}
if (!in_array($type, static::REQUEST_TYPES)) {
throw new WechatMenuException('请求中 type 参数不正确');
}
return $type;
}
/**
* 获取素材列表.
*
* @param array $data
*
* @return string
*
* @throws WechatMenuException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
protected static function getMaterials(array $data): string
{
$materialType = $data['material_type'] ?? '';
if (!in_array($materialType, static::MATERIAL_TYPES)) {
throw new WechatMenuException('请求中素材类型 material_type 参数不正确');
}
$res = Manager::getInstance()
->getWechat()
->material
->list(
$materialType,
($data['page'] - 1) * static::MEDIA_PER_PAGE,
static::MEDIA_PER_PAGE
);
if ($err = static::getWechatError($res)) {
return $err;
}
$res['per_page'] = static::MEDIA_PER_PAGE;
$res['type'] = $materialType;
return static::success('', $res);
}
/**
* 获取素材详情
*
* @param array $data
*
* @return \EasyWeChat\Kernel\Http\StreamResponse|string
*
* @throws WechatMenuException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
protected static function getMedia(array $data)
{
$mediaId = $data['media_id'] ?? '';
if (!$mediaId) {
return static::error('必须有媒体 ID 参数:media_id');
}
$res = Manager::getInstance()->getWechat()->material->get($mediaId);
if ($err = static::getWechatError($res)) {
return $err;
}
// 其他则直接返回文件内容,前端打开可直接下载
if ($res instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
return $res;
}
return static::success('', $res);
}
/**
* 检查微信接口返回的消息是否有错误,有则返回响应内容,没有则返回 false
*
* @param mixed $res 微信接口返回的内容
*
* @return bool|string
*/
protected static function getWechatError($res)
{
if (!is_array($res)) {
return false;
}
$errCode = $res['errcode'] ?? null;
if ($errCode) {
return static::error("[{$res['errcode']}] {$res['errmsg']}", 'wechat');
}
return false;
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Vue可视化的微信公众号菜单管理组件
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/sync-github/wechat-menu.git
git@gitee.com:sync-github/wechat-menu.git
sync-github
wechat-menu
wechat-menu
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /