HactcmCommand.php 16 KB

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