WechatMenuServices.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\wechat;
  12. use app\dao\wechat\WechatMenuDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\app\WechatService;
  16. /**
  17. * 微信菜单
  18. * Class WechatMenuServices
  19. * @package app\services\wechat
  20. */
  21. class WechatMenuServices extends BaseServices
  22. {
  23. /**
  24. * 构造方法
  25. * WechatMenuServices constructor.
  26. * @param WechatMenuDao $dao
  27. */
  28. public function __construct(WechatMenuDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取微信菜单
  34. * @return array|mixed
  35. */
  36. public function getWechatMenu()
  37. {
  38. $menus = $this->dao->value(['key' => 'wechat_menus'], 'result');
  39. return $menus ? json_decode($menus, true) : [];
  40. }
  41. /**
  42. * 保存微信菜单
  43. * @param array $buttons
  44. * @return bool
  45. */
  46. public function saveMenu(array $buttons)
  47. {
  48. try {
  49. WechatService::menuService()->add($buttons);
  50. if ($this->dao->count(['key' => 'wechat_menus'])) {
  51. $this->dao->update('wechat_menus', ['result' => json_encode($buttons), 'add_time' => time()], 'key');
  52. } else {
  53. $this->dao->save(['key' => 'wechat_menus', 'result' => json_encode($buttons), 'add_time' => time()]);
  54. }
  55. return true;
  56. } catch (\Exception $e) {
  57. if (strstr($e->getMessage(), 'Request AccessToken fail. response')) {
  58. $msgData = str_replace('Request AccessToken fail. response: ', '', $e->getMessage());
  59. $msgData = json_decode($msgData, true);
  60. $errcode = $msgData['errcode'] ?? 0;
  61. if ($errcode == 40164) {
  62. throw new AdminException(400704);
  63. }
  64. }
  65. if (strstr($e->getMessage(), 'invalid weapp appid')) {
  66. throw new AdminException(400705);
  67. }
  68. throw new AdminException(WechatService::getMessage($e->getMessage()));
  69. }
  70. }
  71. }