TestCommand.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Enums\ResponseCodeEnum;
  5. use App\Repositories\Enums\TaskMakeStatusEnum;
  6. use App\Repositories\Models\Base\Admin;
  7. use App\Repositories\Models\Base\Area;
  8. use App\Repositories\Models\Base\CategorySetting;
  9. use App\Repositories\Models\Base\Permission;
  10. use App\Repositories\Models\Base\Resource;
  11. use App\Repositories\Models\Base\Setting;
  12. use App\Repositories\Models\Base\Task;
  13. use App\Repositories\Models\Exam\BankCategory;
  14. use App\Repositories\Models\Wechat\Account;
  15. use App\Repositories\Models\Wechat\Rescource;
  16. use App\Repositories\Models\Wechat\Tag;
  17. use App\Support\Traits\MakeWord;
  18. use App\Support\Traits\WeChat;
  19. use Carbon\Carbon;
  20. use EasyWeChat\Factory;
  21. use Illuminate\Console\Command;
  22. use Illuminate\Support\Arr;
  23. use Illuminate\Support\Collection;
  24. use Illuminate\Support\Facades\Cache;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\File;
  27. use Illuminate\Support\Facades\Hash;
  28. use Illuminate\Support\Facades\Storage;
  29. use Illuminate\Support\Str;
  30. use PhpOffice\PhpWord\TemplateProcessor;
  31. /**
  32. * Class PresenterCommand
  33. * @package Prettus\Repository\Generators\Commands
  34. * @author Anderson Andrade <contato@andersonandra.de>
  35. */
  36. class TestCommand extends Command
  37. {
  38. use WeChat;
  39. /**
  40. * The name of command.
  41. *
  42. * @var string
  43. */
  44. protected $name = 'mead:test';
  45. /**
  46. * The description of command.
  47. *
  48. * @var string
  49. */
  50. protected $description = 'test';
  51. /**
  52. * The type of class being generated.
  53. *
  54. * @var string
  55. */
  56. protected $type = 'permission';
  57. /**
  58. * Execute the command.
  59. *
  60. * @return void
  61. * @see fire()
  62. */
  63. public function handle()
  64. {
  65. // $this->initAdmin();
  66. // $d = BankCategory::byPidGetParentIds(1);
  67. // dd($d);
  68. // dd($this->wechatUploadImg('data/1.jpg'));
  69. // $this->wechatUploadImg('data/20240914/gongzhonghao.png');
  70. // $this->wechatUploadImg('data/20240914/shipinhao.png');
  71. // $this->wechatUploadImg('data/20240914/douyin.png');
  72. // $this->wechatUploadImg('data/20240914/weibo.png');
  73. $name = '希梦耐康科技';
  74. $room_path = 'xmnk';
  75. $sub_tag_name = "关注群组";
  76. // $this->initWechat($name, [
  77. // 'app_id' => 'wx22dc4d8cbc0a192d',
  78. // 'secret' => '05f73303557ad75bcfe195b52183ca57',
  79. // 'token' => 'fsdkDLlffeTg3RozsnreoTiEe',
  80. // 'aes_key' => 'aiaPI44r3GnYt1jDaaGthfgrFU56tintbJB7aOpux6T', // 明文模式请勿填写 EncodingAESKey
  81. // ]);
  82. $isOver = true;
  83. while ($isOver) {
  84. $cmd = $this->choice("【{$name}】请选择操作菜单?", ['退出', '获取菜单列表', '删除全部菜单', '设置未关注时菜单', '设置关注时的标签', '给关注用户设置标签', '同步标签', '上传资源', '创建标签', '删除标签', '标签列表', '清空缓存'], 0);
  85. switch ($cmd) {
  86. case '退出':
  87. $isOver = false;
  88. break;
  89. case "获取菜单列表":
  90. $dd = self::menus($name);
  91. $this->line(php2jszh($dd));
  92. break;
  93. case "删除全部菜单":
  94. $dd = self::delAllMenus($name);
  95. $this->line(php2jszh($dd));
  96. break;
  97. case "设置未关注时菜单":
  98. $button = [
  99. [
  100. "type" => "click",
  101. "name" => "请先关注",
  102. "key" => "qingxianguanzhu",
  103. ],
  104. [
  105. "type" => "click",
  106. "name" => "获取服务",
  107. "key" => "qingxianguanzhu",
  108. ],
  109. ];
  110. $dd = self::setMenus($name, $button);
  111. break;
  112. case "设置关注时的标签":
  113. $account = self::getAccount($name);
  114. $tag_id = Tag::byNameGetId($account['id'], $sub_tag_name);
  115. $button = [
  116. [
  117. "name" => "信息服务",
  118. "sub_button" => [
  119. [
  120. "type" => "view",
  121. "name" => "入校预约",
  122. "url" => "https://appli.hactcm.edu.cn/common-service/clyy-mobile/query/apply",
  123. ],
  124. [
  125. "type" => "view",
  126. "name" => "博物馆讲解预约",
  127. "url" => "http://bowuguan.site.xmnk.cn/",
  128. // "url" => "http://v.o3v.cn/XPSG0O",
  129. ],
  130. [
  131. "type" => "view",
  132. "name" => "网上办事大厅",
  133. "url" => "https://i.hactcm.edu.cn/new/portal/browerTips.html",
  134. ],
  135. ]
  136. ],
  137. [
  138. "name" => "融媒矩阵",
  139. "sub_button" => [
  140. [
  141. "type" => "view",
  142. "name" => "官方网站",
  143. "url" => "https://www.hactcm.edu.cn/",
  144. ],
  145. [
  146. "type" => "click",
  147. "name" => "视频号",
  148. "key" => "shipinhao",
  149. ],
  150. [
  151. "type" => "click",
  152. "name" => "抖音",
  153. "key" => "douyin",
  154. ],
  155. [
  156. "type" => "view",
  157. "name" => "微博",
  158. "url" => "https://m.weibo.cn/u/3970249166?topnav=1&wvr=6&jumpfrom=weibocom",
  159. // "url" => "http://v.o3v.cn/CCa1OC",
  160. ],
  161. [
  162. "type" => "view",
  163. "name" => "校报",
  164. "url" => "https://www.hactcm.edu.cn/wmbd/xb.htm",
  165. ],
  166. ]
  167. ],
  168. [
  169. "name" => "魅力河中医",
  170. "sub_button" => [
  171. [
  172. "type" => "view",
  173. "name" => "媒体河中医",
  174. "url" => "https://www.hactcm.edu.cn/mthzy.htm",
  175. ],
  176. [
  177. "type" => "view",
  178. "name" => "视听图说",
  179. "url" => "https://www.hactcm.edu.cn/spts.htm",
  180. ],
  181. [
  182. "type" => "view",
  183. "name" => "学术预告",
  184. "url" => "https://www.hactcm.edu.cn/xsyg.htm",
  185. ],
  186. [
  187. "type" => "view",
  188. "name" => "专题专栏",
  189. "url" => "https://www.hactcm.edu.cn/ztzl.htm",
  190. ],
  191. [
  192. "type" => "click",
  193. "name" => "联系我们",
  194. "key" => "lianxiwomen",
  195. ]
  196. ]
  197. ]
  198. ];
  199. $matchRule = [
  200. "tag_id" => $tag_id,
  201. ];
  202. $dd = self::setMenus($name, $button);
  203. break;
  204. case "同步标签":
  205. $this->syncTags($name);
  206. break;
  207. case "给关注用户设置标签":
  208. $this->updateUserTags($name, $sub_tag_name);
  209. break;
  210. case "上传资源":
  211. $this->uploadImage($name, $room_path);
  212. break;
  213. case "创建标签":
  214. $account = self::getAccount($name);
  215. Tag::query()->updateOrCreate([
  216. 'account_id' => $account['id'],
  217. 'name' => $sub_tag_name,
  218. ]);
  219. break;
  220. case '删除标签':
  221. $account = self::getAccount($name);
  222. $tag = Tag::query()->where('account_id', $account['id'])->where('name', $sub_tag_name)->first();
  223. if (!$tag) {
  224. $this->line('找不到该标签');
  225. break;
  226. }
  227. self::delTag($name, $tag->wechat_id);
  228. $tag->delete();
  229. break;
  230. case '标签列表':
  231. $lists = self::tags($name);
  232. $this->line(php2jszh($lists));
  233. break;
  234. case "清空缓存":
  235. Cache::flush();
  236. break;
  237. }
  238. $this->line("{$cmd}==>ok");
  239. }
  240. // $this->syncTags($name);
  241. // $this->syncResources($name);
  242. // $this->updateUserTags($name, '希梦耐康');
  243. $this->line('ok');
  244. }
  245. /**
  246. * 上传文件
  247. * @param $name
  248. * @return void
  249. */
  250. public function uploadImage($name, $room_path)
  251. {
  252. $account = self::getAccount($name);
  253. $files = Storage::disk('wechat')->allFiles($room_path);
  254. // 输出文件列表
  255. foreach ($files as $file) {
  256. $rescource = Rescource::query()->where('path', $file)->where('account_id', $account['id'])->exists();
  257. if ($rescource) continue;
  258. $path = Storage::disk('wechat')->path($file);
  259. $result = self::uploadRes($name, 'image', $path);
  260. Rescource::query()->create([
  261. 'name' => str_replace($room_path . '/', '', $file),
  262. 'media_id' => $result['media_id'],
  263. 'account_id' => $account['id'],
  264. 'path' => $file,
  265. 'type' => 'image',
  266. 'data' => $result,
  267. ]);
  268. }
  269. }
  270. /**
  271. * 更新用户
  272. * @param $name
  273. * @param $tag_name
  274. * @return void
  275. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  276. * @throws \GuzzleHttp\Exception\GuzzleException
  277. */
  278. public function updateUserTags($name, $tag_name)
  279. {
  280. $account = self::getAccount($name);
  281. $tag_id = Tag::byNameGetId($account['id'], $tag_name);
  282. if (!$tag_id) {
  283. $this->line('找不到 tag' . $tag_name);
  284. return;
  285. }
  286. $next = null;
  287. $isOver = true;
  288. while ($isOver) {
  289. $users = self::users($name, $next);
  290. $next = $users['next_openid'];
  291. if (!$users['count']) {
  292. $isOver = false;
  293. } else {
  294. self::usersAddTag($name, $users['data']['openid'], $tag_id);
  295. }
  296. }
  297. }
  298. /**
  299. * 添加标签
  300. * @param $account_name
  301. * @param $tag_name
  302. * @return void
  303. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  304. * @throws \GuzzleHttp\Exception\GuzzleException
  305. */
  306. public function addTag($account_name, $tag_name)
  307. {
  308. $account = Account::byNameGetModel($account_name);
  309. $app = Factory::officialAccount($account['settings']);
  310. $tag = $app->user_tag->create($tag_name);
  311. Tag::query()->updateOrCreate([
  312. 'wechat_id' => $tag['id'],
  313. 'account_id' => $account['id'],
  314. ], [
  315. 'name' => $tag['name'],
  316. 'data' => $tag
  317. ]);
  318. }
  319. /**
  320. * 同步标签
  321. * @param $name
  322. * @return void
  323. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  324. */
  325. public function syncTags($name)
  326. {
  327. $account = Account::byNameGetModel($name);
  328. $app = Factory::officialAccount($account['settings']);
  329. $tags = $app->user_tag->list();
  330. foreach ($tags['tags'] as $tag) {
  331. Tag::withoutEvents(function () use ($tag, $account) {
  332. Tag::query()->updateOrCreate([
  333. 'wechat_id' => $tag['id'],
  334. 'account_id' => $account['id'],
  335. ], [
  336. 'name' => $tag['name'],
  337. 'data' => $tag
  338. ]);
  339. });
  340. }
  341. }
  342. /**
  343. * 同步图片资源
  344. * @param $name
  345. * @return void
  346. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  347. */
  348. public function syncResources($name, $type = 'image')
  349. {
  350. $account = Account::byNameGetModel($name);
  351. $app = Factory::officialAccount($account['settings']);
  352. $isOver = true;
  353. $page = 0;
  354. $page_nums = 2;
  355. while ($isOver) {
  356. $this->line($page);
  357. $lists = $app->material->list($type, ($page * $page_nums), $page_nums);
  358. foreach ($lists['item'] as $item) {
  359. Rescource::withoutEvents(function () use ($item, $account, $type) {
  360. Rescource::query()->updateOrCreate([
  361. 'media_id' => $item['media_id'],
  362. 'account_id' => $account['id'],
  363. 'type' => $type,
  364. ], [
  365. 'name' => $item['name'],
  366. 'data' => $item
  367. ]);
  368. });
  369. }
  370. $total_nums = $lists['total_count'];
  371. if (($page + 1) >= ceil($total_nums / $page_nums)) {
  372. $isOver = false;
  373. }
  374. $page++;
  375. }
  376. }
  377. /**
  378. * 初始化微信
  379. * @param $name
  380. * @param $config
  381. * @return void
  382. */
  383. public function initWechat($name, $config)
  384. {
  385. if (Account::query()->where('name', $name)->where('status', ModelStatusEnum::OK)->exists()) {
  386. Account::query()->updateOrCreate(['name' => $name], ['settings' => $config]);
  387. } else {
  388. Account::query()->create(['name' => $name, 'settings' => $config, 'key' => Str::random(8)]);
  389. }
  390. }
  391. /**
  392. * 上传图片
  393. * @param $path
  394. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  395. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  396. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  397. * @throws \GuzzleHttp\Exception\GuzzleException
  398. */
  399. public function wechatUploadImg($path)
  400. {
  401. $config = [
  402. 'app_id' => 'wx22dc4d8cbc0a192d',
  403. 'secret' => '05f73303557ad75bcfe195b52183ca57',
  404. 'token' => 'fsdkDLlffeTg3RozsnreoTiEe',
  405. 'aes_key' => 'aiaPI44r3GnYt1jDaaGthfgrFU56tintbJB7aOpux6T', // 明文模式请勿填写 EncodingAESKey
  406. ];
  407. $app = Factory::officialAccount($config);
  408. $result = $app->material->uploadImage(base_path($path));
  409. $this->line("{$path}==>" . php2js($result));
  410. return $result;
  411. }
  412. public function initAdmin()
  413. {
  414. $admin = Admin::query()->where('id', 1)->first();
  415. $admin->password = Hash::make('123456');
  416. $admin->save();
  417. $this->line(base64_encode('123456'));
  418. }
  419. }