WechatMenu.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\wechat;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\CacheRepository;
  14. use crmeb\services\WechatService;
  15. use Exception;
  16. use think\db\exception\DbException;
  17. /**
  18. * Class WechatMenu
  19. * @package app\controller\admin\wechat
  20. * @author xaboy
  21. * @day 2020-04-24
  22. */
  23. class WechatMenu extends BaseController
  24. {
  25. /**
  26. * @param CacheRepository $repository
  27. * @return mixed
  28. * @author xaboy
  29. * @day 2020-04-24
  30. */
  31. public function info(CacheRepository $repository)
  32. {
  33. $data['wechat_menus'] = $repository->getResultByKey('wechat_menus') ?? [];
  34. return app('json')->success($data);
  35. }
  36. /**
  37. * @param CacheRepository $repository
  38. * @return mixed
  39. * @throws DbException
  40. * @author xaboy
  41. * @day 2020-04-24
  42. */
  43. public function save(CacheRepository $repository)
  44. {
  45. $buttons = (array)$this->request->param('button', []);
  46. if (!count($buttons)) return app('json')->fail('请添加至少一个按钮');
  47. try {
  48. WechatService::create()->getApplication()->menu->add($buttons);
  49. } catch (Exception $e) {
  50. return app('json')->fail('设置失败:' . $e->getMessage());
  51. }
  52. $repository->save('wechat_menus', $buttons);
  53. return app('json')->success('设置成功');
  54. }
  55. }