WechatMenu.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. return app('json')->success($repository->getResult('wechat_menus') ?? []);
  34. }
  35. /**
  36. * @param CacheRepository $repository
  37. * @return mixed
  38. * @throws DbException
  39. * @author xaboy
  40. * @day 2020-04-24
  41. */
  42. public function save(CacheRepository $repository)
  43. {
  44. $buttons = (array)$this->request->param('button', []);
  45. if (!count($buttons)) return app('json')->fail('请添加至少一个按钮');
  46. try {
  47. WechatService::create()->getApplication()->menu->add($buttons);
  48. } catch (Exception $e) {
  49. return app('json')->fail('设置失败:' . $e->getMessage());
  50. }
  51. $repository->save('wechat_menus', $buttons);
  52. return app('json')->success('设置成功');
  53. }
  54. }