Menus.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\admin\controller\AuthController;
  4. use app\models\system\Cache;
  5. use crmeb\services\WechatService;
  6. /**
  7. * 微信菜单 控制器
  8. * Class Menus
  9. * @package app\admin\controller\wechat
  10. */
  11. class Menus extends AuthController
  12. {
  13. public function index()
  14. {
  15. $menus = Cache::where('key','wechat_menus')->value('result');
  16. $menus = $menus ? : '[]';
  17. $this->assign('menus',$menus);
  18. return $this->fetch();
  19. }
  20. public function save()
  21. {
  22. $buttons = request()->post('button/a',[]);
  23. if(!count($buttons)) return $this->failed('请添加至少一个按钮');
  24. try{
  25. WechatService::menuService()->add($buttons);
  26. $count = Cache::where('key', 'wechat_menus')->count();
  27. if($count){
  28. $count = Cache::where('key', 'wechat_menus')->where('result', json_encode($buttons))->count();
  29. if(!$count)
  30. Cache::where('key', 'wechat_menus')->update(['result'=>json_encode($buttons),'add_time'=>time()]);
  31. }else
  32. Cache::insert(['key'=>'wechat_menus','result'=>json_encode($buttons),'add_time'=>time()],true);
  33. return $this->successful('修改成功!');
  34. }catch (\Exception $e){
  35. return $this->failed($e->getMessage());
  36. }
  37. }
  38. }