You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.8 KiB

  1. <?php
  2. namespace backend\modules\wx_public_account\logic;
  3. use Yii;
  4. use yii\base\Exception;
  5. use yii\web\ServerErrorHttpException;
  6. use EasyWeChat\Factory;
  7. use backend\modules\wx_public_account\models\ars\WxPublicAccountConfig;
  8. class PublicAccountManager
  9. {
  10. //已授权对象
  11. public static $openPlatform;
  12. //自定义菜单类型
  13. const TYPE_VIEW = 'view';//跳转网页
  14. const TYPE_MINI_PROGRAM = 'miniprogram';//跳转小程序
  15. const TYPE_CUSTOM_PAGE = 'customPage';//自定义页面
  16. /**
  17. * 处理前端自定义菜单数据
  18. * @param $data
  19. * @param WxPublicAccountConfig|$configModel
  20. * @return bool
  21. * @throws ServerErrorHttpException
  22. */
  23. public static function dealCustomMenuData($data, $configModel)
  24. {
  25. $tra = Yii::$app->db->beginTransaction();
  26. try {
  27. $configModel->menu_setting = json_encode($data);
  28. if (!$configModel->save()) {
  29. throw new Exception(self::dealSaveErrorsString($configModel->errors));
  30. }
  31. $button = self::dealData($data, []);
  32. $res = self::$openPlatform->menu->delete();
  33. if ($res['errcode'] != 0) {
  34. throw new Exception($res['errmsg']);
  35. }
  36. if ($button) {
  37. $res = self::$openPlatform->menu->create($button);
  38. if ($res['errcode'] != 0) {
  39. throw new Exception($res['errmsg']);
  40. }
  41. }
  42. $tra->commit();
  43. return ['status' => true];
  44. } catch (Exception $e) {
  45. $tra->rollBack();
  46. return ['status' => false, 'info' => $e->getMessage()];
  47. }
  48. }
  49. /**
  50. * 处理数据模型保存的报错信息
  51. * @param $arr
  52. * @return string
  53. */
  54. public static function dealSaveErrorsString($arr): string
  55. {
  56. $data = [];
  57. foreach ($arr as $k => $v) {
  58. $data[] = implode(' ', $v);
  59. }
  60. return implode(' ', $data);
  61. }
  62. /**
  63. * 处理前端数据,递归处理
  64. * @param $data
  65. * @param $ret
  66. * @return mixed
  67. */
  68. private static function dealData($data, $ret)
  69. {
  70. foreach ($data as $k => $button) {
  71. $name = $button['title'];
  72. $type = $button['content']['type'];
  73. $value = $button['content']['value'];
  74. $children = $button['children'] ?? [];
  75. $ret[$k] = self::spliceData($type, $value, $name);
  76. if ($children) {
  77. $ret[$k]['sub_button'] = [];
  78. $ret[$k]['sub_button'] = self::dealData($children, $ret[$k]['sub_button']);
  79. }
  80. }
  81. return $ret;
  82. }
  83. /**
  84. * 拼接数据为微信菜单格式
  85. * @param $type
  86. * @param $value
  87. * @param $name
  88. * @return mixed
  89. */
  90. private static function spliceData($type, $value, $name)
  91. {
  92. $ret['name'] = $name;
  93. if ($type) {
  94. $ret['type'] = $type;
  95. if ($type == self::TYPE_VIEW) {
  96. $ret['url'] = $value;
  97. } elseif ($type == self::TYPE_MINI_PROGRAM) {
  98. $ret['appid'] = $value['appId'];
  99. $ret['pagepath'] = $value['url'];
  100. $ret['url'] = $value['spareWebUrl'];
  101. } elseif ($type == self::TYPE_CUSTOM_PAGE) {
  102. // $ret['type'] = self::TYPE_MINI_PROGRAM;
  103. // $page = PageLayout::findOne($value);
  104. // $ret['appid'] = Yii::$app->config->getConfig()->mini_program_appId;
  105. // if ($page->is_home) {
  106. // $ret['pagepath'] = Yii::$app->params['pageIndex'];
  107. // } else {
  108. // $ret['pagepath'] = Yii::$app->params['pageOther'] . $value;
  109. // }
  110. // $ret['url'] = Yii::$app->config->getConfig()->wx_domain_name . '#/' . $ret['pagepath'];
  111. }
  112. }
  113. return $ret;
  114. }
  115. }