123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- namespace App\Console\Commands;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Enums\TaskMakeStatusEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Area;
- use App\Repositories\Models\Base\CategorySetting;
- use App\Repositories\Models\Base\Permission;
- use App\Repositories\Models\Base\Resource;
- use App\Repositories\Models\Base\Setting;
- use App\Repositories\Models\Base\Task;
- use App\Repositories\Models\Exam\BankCategory;
- use App\Repositories\Models\Wechat\Account;
- use App\Repositories\Models\Wechat\Rescource;
- use App\Repositories\Models\Wechat\Tag;
- use App\Support\Traits\MakeWord;
- use App\Support\Traits\WeChat;
- use Carbon\Carbon;
- use EasyWeChat\Factory;
- use Illuminate\Console\Command;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use PhpOffice\PhpWord\TemplateProcessor;
- /**
- * Class PresenterCommand
- * @package Prettus\Repository\Generators\Commands
- * @author Anderson Andrade <contato@andersonandra.de>
- */
- 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'));
- }
- }
|