WechatMenu.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-24
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\wechat;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\system\CacheRepository;
  13. use crmeb\services\WechatService;
  14. use Exception;
  15. use think\db\exception\DbException;
  16. use think\facade\Db;
  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. $repository->save('wechat_menus', $buttons);
  47. $isnertall=[];
  48. $key=1;
  49. foreach ($buttons as $k=>&$v){
  50. // unset($v['key']);
  51. foreach ($v['sub_button'] as &$item){
  52. if($item['type']=='click'){
  53. $Wkey='WeChatMenuKey'.$key;
  54. $isnertall[]=[
  55. 'key'=>$Wkey,
  56. 'type'=>'text',
  57. 'data'=>json_encode(['content'=>$item['key']]),
  58. 'status'=>1,
  59. 'hidden'=>1,
  60. 'is_menu'=>1
  61. ];
  62. $item['key']=$Wkey;
  63. $key++;
  64. }
  65. }
  66. }
  67. try {
  68. WechatService::create()->getApplication()->menu->add($buttons);
  69. } catch (Exception $e) {
  70. return app('json')->fail('设置失败:' . $e->getMessage());
  71. }
  72. Db::name('wechat_reply')->where('is_menu',1)->delete();
  73. Db::name('wechat_reply')->insertAll($isnertall);
  74. return app('json')->success('设置成功');
  75. }
  76. }