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.
 
 
 

109 lines
3.3 KiB

<?php
namespace backend\modules\wechat_public_account\logic;
use backend\modules\shop\models\ars\Config;
use Yii;
use yii\base\Exception;
use yii\web\ServerErrorHttpException;
use EasyWeChat\Factory;
class PublicAccountManager
{
public $response_type = 'array';
//自定义菜单类型
const TYPE_VIEW = 'view';//跳转网页
const TYPE_MINI_PROGRAM = 'miniprogram';//跳转小程序
const TYPE_CUSTOM_PAGE = 'customPage';//自定义页面
private static function _getEasyApp()
{
$configModel = Config::find()->one();
if (!$configModel || !$configModel->)
$config = [
'app_id' =>
];
}
/**
* @param $data
* @param Config|$configModel
* @throws ServerErrorHttpException
*/
public static function dealCustomMenuData($data, $configModel)
{
$tra = Yii::$app->db->beginTransaction();
try {
$configModel->menu_setting = json_encode($data);
if (!$configModel->save()) {
throw new Exception(self::dealSaveErrorsString($configModel->errors));
}
$button = self
} catch (Exception $e) {
throw new ServerErrorHttpException($e->getMessage());
}
}
public static function dealSaveErrorsString($arr): string
{
$data = [];
foreach ($arr as $k => $v) {
$data[] = implode(' ', $v);
}
return implode(' ', $data);
}
/**
* @param $data
* @param $ret
* @return mixed
* 处理前端数据,递归处理
*/
private static function dealData($data, $ret)
{
foreach ($data as $k => $button) {
$name = $button['title'];
$type = $button['content']['type'];
$value = $button['content']['value'];
$children = $button['children'] ?? [];
$ret[$k] = self::spliceData($type, $value, $name);
if ($children) {
$ret[$k]['sub_button'] = [];
$ret[$k]['sub_button'] = self::dealData($children, $ret[$k]['sub_button']);
}
}
return $ret;
}
/**
* @param $type
* @param $value
* @param $name
* @return mixed
* 拼接数据为微信菜单格式
*/
private static function spliceData($type, $value, $name)
{
$ret['name'] = $name;
if ($type) {
$ret['type'] = $type;
if ($type == self::TYPE_VIEW) {
$ret['url'] = $value;
} elseif ($type == self::TYPE_MINI_PROGRAM) {
$ret['appid'] = $value['appId'];
$ret['pagepath'] = $value['url'];
$ret['url'] = $value['spareWebUrl'];
} elseif ($type == self::TYPE_CUSTOM_PAGE) {
// $ret['type'] = self::TYPE_MINI_PROGRAM;
// $page = PageLayout::findOne($value);
// $ret['appid'] = Yii::$app->config->getConfig()->mini_program_appId;
// if ($page->is_home) {
// $ret['pagepath'] = Yii::$app->params['pageIndex'];
// } else {
// $ret['pagepath'] = Yii::$app->params['pageOther'] . $value;
// }
// $ret['url'] = Yii::$app->config->getConfig()->wx_domain_name . '#/' . $ret['pagepath'];
}
}
return $ret;
}
}