*/ class TestCommand extends Command { use WeChat; /** * The name of command. * * @var string */ protected $name = 'mead:test'; /** * The description of command. * * @var string */ protected $description = 'test'; /** * The type of class being generated. * * @var string */ protected $type = 'permission'; /** * Execute the command. * * @return void * @see fire() */ public function handle() { // $this->initAdmin(); // $d = BankCategory::byPidGetParentIds(1); // dd($d); // dd($this->wechatUploadImg('data/1.jpg')); // $this->wechatUploadImg('data/20240914/gongzhonghao.png'); // $this->wechatUploadImg('data/20240914/shipinhao.png'); // $this->wechatUploadImg('data/20240914/douyin.png'); // $this->wechatUploadImg('data/20240914/weibo.png'); $name = '希梦耐康科技'; $room_path = 'xmnk'; $sub_tag_name = "关注群组"; // $this->initWechat($name, [ // 'app_id' => 'wx22dc4d8cbc0a192d', // 'secret' => '05f73303557ad75bcfe195b52183ca57', // 'token' => 'fsdkDLlffeTg3RozsnreoTiEe', // 'aes_key' => 'aiaPI44r3GnYt1jDaaGthfgrFU56tintbJB7aOpux6T', // 明文模式请勿填写 EncodingAESKey // ]); $isOver = true; while ($isOver) { $cmd = $this->choice("【{$name}】请选择操作菜单?", ['退出', '获取菜单列表', '删除全部菜单', '设置未关注时菜单', '设置关注时的标签', '给关注用户设置标签', '同步标签', '上传资源', '创建标签', '删除标签', '标签列表', '清空缓存'], 0); switch ($cmd) { case '退出': $isOver = false; break; case "获取菜单列表": $dd = self::menus($name); $this->line(php2jszh($dd)); break; case "删除全部菜单": $dd = self::delAllMenus($name); $this->line(php2jszh($dd)); break; case "设置未关注时菜单": $button = [ [ "type" => "click", "name" => "请先关注", "key" => "qingxianguanzhu", ], [ "type" => "click", "name" => "获取服务", "key" => "qingxianguanzhu", ], ]; $dd = self::setMenus($name, $button); break; case "设置关注时的标签": $account = self::getAccount($name); $tag_id = Tag::byNameGetId($account['id'], $sub_tag_name); $button = [ [ "name" => "信息服务", "sub_button" => [ [ "type" => "view", "name" => "入校预约", "url" => "https://appli.hactcm.edu.cn/common-service/clyy-mobile/query/apply", ], [ "type" => "view", "name" => "博物馆讲解预约", "url" => "http://bowuguan.site.xmnk.cn/", // "url" => "http://v.o3v.cn/XPSG0O", ], [ "type" => "view", "name" => "网上办事大厅", "url" => "https://i.hactcm.edu.cn/new/portal/browerTips.html", ], ] ], [ "name" => "融媒矩阵", "sub_button" => [ [ "type" => "view", "name" => "官方网站", "url" => "https://www.hactcm.edu.cn/", ], [ "type" => "click", "name" => "视频号", "key" => "shipinhao", ], [ "type" => "click", "name" => "抖音", "key" => "douyin", ], [ "type" => "view", "name" => "微博", "url" => "https://m.weibo.cn/u/3970249166?topnav=1&wvr=6&jumpfrom=weibocom", // "url" => "http://v.o3v.cn/CCa1OC", ], [ "type" => "view", "name" => "校报", "url" => "https://www.hactcm.edu.cn/wmbd/xb.htm", ], ] ], [ "name" => "魅力河中医", "sub_button" => [ [ "type" => "view", "name" => "媒体河中医", "url" => "https://www.hactcm.edu.cn/mthzy.htm", ], [ "type" => "view", "name" => "视听图说", "url" => "https://www.hactcm.edu.cn/spts.htm", ], [ "type" => "view", "name" => "学术预告", "url" => "https://www.hactcm.edu.cn/xsyg.htm", ], [ "type" => "view", "name" => "专题专栏", "url" => "https://www.hactcm.edu.cn/ztzl.htm", ], [ "type" => "click", "name" => "联系我们", "key" => "lianxiwomen", ] ] ] ]; $matchRule = [ "tag_id" => $tag_id, ]; $dd = self::setMenus($name, $button); break; case "同步标签": $this->syncTags($name); break; case "给关注用户设置标签": $this->updateUserTags($name, $sub_tag_name); break; case "上传资源": $this->uploadImage($name, $room_path); break; case "创建标签": $account = self::getAccount($name); Tag::query()->updateOrCreate([ 'account_id' => $account['id'], 'name' => $sub_tag_name, ]); break; case '删除标签': $account = self::getAccount($name); $tag = Tag::query()->where('account_id', $account['id'])->where('name', $sub_tag_name)->first(); if (!$tag) { $this->line('找不到该标签'); break; } self::delTag($name, $tag->wechat_id); $tag->delete(); break; case '标签列表': $lists = self::tags($name); $this->line(php2jszh($lists)); break; case "清空缓存": Cache::flush(); break; } $this->line("{$cmd}==>ok"); } // $this->syncTags($name); // $this->syncResources($name); // $this->updateUserTags($name, '希梦耐康'); $this->line('ok'); } /** * 上传文件 * @param $name * @return void */ public function uploadImage($name, $room_path) { $account = self::getAccount($name); $files = Storage::disk('wechat')->allFiles($room_path); // 输出文件列表 foreach ($files as $file) { $rescource = Rescource::query()->where('path', $file)->where('account_id', $account['id'])->exists(); if ($rescource) continue; $path = Storage::disk('wechat')->path($file); $result = self::uploadRes($name, 'image', $path); Rescource::query()->create([ 'name' => str_replace($room_path . '/', '', $file), 'media_id' => $result['media_id'], 'account_id' => $account['id'], 'path' => $file, 'type' => 'image', 'data' => $result, ]); } } /** * 更新用户 * @param $name * @param $tag_name * @return void * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateUserTags($name, $tag_name) { $account = self::getAccount($name); $tag_id = Tag::byNameGetId($account['id'], $tag_name); if (!$tag_id) { $this->line('找不到 tag' . $tag_name); return; } $next = null; $isOver = true; while ($isOver) { $users = self::users($name, $next); $next = $users['next_openid']; if (!$users['count']) { $isOver = false; } else { self::usersAddTag($name, $users['data']['openid'], $tag_id); } } } /** * 添加标签 * @param $account_name * @param $tag_name * @return void * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function addTag($account_name, $tag_name) { $account = Account::byNameGetModel($account_name); $app = Factory::officialAccount($account['settings']); $tag = $app->user_tag->create($tag_name); Tag::query()->updateOrCreate([ 'wechat_id' => $tag['id'], 'account_id' => $account['id'], ], [ 'name' => $tag['name'], 'data' => $tag ]); } /** * 同步标签 * @param $name * @return void * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException */ public function syncTags($name) { $account = Account::byNameGetModel($name); $app = Factory::officialAccount($account['settings']); $tags = $app->user_tag->list(); foreach ($tags['tags'] as $tag) { Tag::withoutEvents(function () use ($tag, $account) { Tag::query()->updateOrCreate([ 'wechat_id' => $tag['id'], 'account_id' => $account['id'], ], [ 'name' => $tag['name'], 'data' => $tag ]); }); } } /** * 同步图片资源 * @param $name * @return void * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException */ public function syncResources($name, $type = 'image') { $account = Account::byNameGetModel($name); $app = Factory::officialAccount($account['settings']); $isOver = true; $page = 0; $page_nums = 2; while ($isOver) { $this->line($page); $lists = $app->material->list($type, ($page * $page_nums), $page_nums); foreach ($lists['item'] as $item) { Rescource::withoutEvents(function () use ($item, $account, $type) { Rescource::query()->updateOrCreate([ 'media_id' => $item['media_id'], 'account_id' => $account['id'], 'type' => $type, ], [ 'name' => $item['name'], 'data' => $item ]); }); } $total_nums = $lists['total_count']; if (($page + 1) >= ceil($total_nums / $page_nums)) { $isOver = false; } $page++; } } /** * 初始化微信 * @param $name * @param $config * @return void */ public function initWechat($name, $config) { if (Account::query()->where('name', $name)->where('status', ModelStatusEnum::OK)->exists()) { Account::query()->updateOrCreate(['name' => $name], ['settings' => $config]); } else { Account::query()->create(['name' => $name, 'settings' => $config, 'key' => Str::random(8)]); } } /** * 上传图片 * @param $path * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function wechatUploadImg($path) { $config = [ 'app_id' => 'wx22dc4d8cbc0a192d', 'secret' => '05f73303557ad75bcfe195b52183ca57', 'token' => 'fsdkDLlffeTg3RozsnreoTiEe', 'aes_key' => 'aiaPI44r3GnYt1jDaaGthfgrFU56tintbJB7aOpux6T', // 明文模式请勿填写 EncodingAESKey ]; $app = Factory::officialAccount($config); $result = $app->material->uploadImage(base_path($path)); $this->line("{$path}==>" . php2js($result)); return $result; } public function initAdmin() { $admin = Admin::query()->where('id', 1)->first(); $admin->password = Hash::make('123456'); $admin->save(); $this->line(base64_encode('123456')); } }