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.

108 lines
3.3 KiB

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