''], ]; } public function __construct($config = []) { parent::__construct($config); $store_id = get_store_id(); $app = null; $wechatConfig = WechatConfig::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => 2]); if (!empty($wechatConfig) && $wechatConfig->app_id && $wechatConfig->app_secret) { $config = [ 'app_id' => $wechatConfig->app_id, 'secret' => $wechatConfig->app_secret, // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 'response_type' => 'array', ]; $app = Factory::officialAccount($config); } $this->app = $app; } public function getMenuList () { try { $store_id = get_store_id(); $p_menus = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->asArray()->all(); $num = 0; $data = []; foreach ($p_menus as $key => $p_menu) { $p_menu['value'] = json_decode($p_menu['value'], true); $p_menu['type'] = (int)$p_menu['type']; $p_menu['created_at'] = date('Y-m-d H:i:s', $p_menu['created_at']); $p_menu['update_at'] = date('Y-m-d H:i:s', $p_menu['update_at']); $p_menu['parent_name'] = ''; $p_menu['parent_id'] = (int)$p_menu['parent_id']; $id = $p_menu['id']; $data = array_merge($data, [ $p_menu ]); ++$num; $s_menu = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => $id, 'is_delete' => 0])->asArray()->all(); foreach ($s_menu as $item) { $item['value'] = json_decode($item['value'], true); $item['type'] = (int)$item['type']; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['update_at'] = date('Y-m-d H:i:s', $item['update_at']); $item['parent_name'] = $p_menu['name']; $item['parent_id'] = (int)$item['parent_id']; $data = array_merge($data, [$item]); ++$num; } } return [ 'code' => 0, 'msg' => '成功', 'data' => [ 'list' => $data ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取微信公众号菜单配置 */ public function setMenuConfig() { $t = \Yii::$app->db->beginTransaction(); try { $store_id = get_store_id(); $type = (int)$this->type; $name = $this->name; $appid = $this->appid; $pagePath = $this->pagePath; $title = $this->title; $desc = $this->desc; $pic = $this->pic; $link = $this->link; $value = $this->value; $parent_id = (int)$this->parent_id; $sort = (int)$this->sort; $id = $this->id; if (is_null($type) || !in_array($type, [0, 1, 2, 3])) { throw new \Exception('类型错误'); } //修改菜单 if ($id) { $wechat_menu = WechatMenuConfig::findOne(['store_id' => $store_id, 'id' => $id]); }else{ $wechat_menu = new WechatMenuConfig(); } if (in_array($type, [0, 1])) { if (trim($value) === '') { throw new \Exception('值未填写'); } } //小程序 if ($type === 2) { if (trim($appid) == '') { return [ 'code' => 1, 'msg' => '小程序的appid不能为空', ]; } if (trim($pagePath) == '') { return [ 'code' => 1, 'msg' => '小程序的pagePath不能为空', ]; } $value = json_encode([ 'appid' => $appid, 'pagepath' => $pagePath, ]); } //图文 if ($type === 3) { if (trim($title) == '') { return [ 'code' => 1, 'msg' => '图文消息的标题不能为空', ]; } if (trim($desc) == '') { return [ 'code' => 1, 'msg' => '图文消息的描述不能为空', ]; } if (trim($pic) == '') { return [ 'code' => 1, 'msg' => '图文消息的图片不能为空', ]; } if (trim($link) == '') { return [ 'code' => 1, 'msg' => '图文消息的链接不能为空', ]; } $value = json_encode([ 'title' => $title, 'desc' => $desc, 'pic' => $pic, 'link' => $link, ]); } $wechat_menu->name = $name; $wechat_menu->parent_id = $parent_id; $wechat_menu->store_id = $store_id; $wechat_menu->value = $value; $wechat_menu->sort = $sort; $wechat_menu->type = $type; if (!$wechat_menu->save()) { throw new \Exception(json_encode($wechat_menu->errors)); } $result = $this->updateMenu(); if ($result['code'] === 0) { $t->commit(); return [ 'code' => 0, 'msg' => '设置成功' ]; } else { throw new \Exception($result['msg']); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function delMenu() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $store_id = get_store_id(); $menu_config = WechatMenuConfig::findOne(['store_id' => $store_id, 'id' => $id, 'is_delete' => 0]); if (!$menu_config) { throw new \Exception('微信公众号菜单不存在或已经删除'); } $sub = WechatMenuConfig::findOne(['store_id' => $store_id, 'parent_id' => $id]); if ($sub) { throw new \Exception('菜单名称“'.$menu_config->name.'”下还有子菜单,请先删除子菜单'); } $menu_config->is_delete = 1; if (!$menu_config->save()) { throw new \Exception(json_encode($menu_config->errors)); } else { $app = $this->app; $result = $app->menu->delete(); if(intval($result['errcode']) === 0){ $t->commit(); return [ 'code' => 0, 'msg' => '设置成功' ]; }else{ throw new \Exception($result['errmsg']); } } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function getMenuInfo() { $id = $this->id; $store_id = get_store_id(); $menu_config = WechatMenuConfig::find()->where(['store_id' => $store_id, 'id' => $id, 'is_delete' => 0])->asArray()->one(); if ($menu_config) { $menu_config['parent_id'] = (int)$menu_config['parent_id']; $menu_config['type'] = (int)$menu_config['type']; $value = json_decode($menu_config['value'], true); $menu_config['appid'] = $value['appid'] ?: ''; $menu_config['pagePath'] = $value['pagepath'] ?: ''; $menu_config['title'] = $value['title'] ?: ''; $menu_config['desc'] = $value['desc'] ?: ''; $menu_config['pic'] = $value['pic'] ?: ''; $menu_config['link'] = $value['link'] ?: ''; } $ret = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->orderBy('sort asc')->asArray()->all(); return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'info' => $menu_config ?: [ 'id' => 0, 'name' => '', 'sort' => 0, 'value' => '', 'parent_id' => 0, 'type' => 0, 'appid' => '', 'pagePath' => '', 'title' => '', 'desc' => '', 'pic' => '', 'link' => '' ], 'parent_list' => $ret ] ]; } public function updateMenu(){ $store_id = get_store_id(); $button = []; $ret = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->orderBy('sort asc')->asArray()->all(); if($ret){ foreach($ret as $k=>$v){ $button[$k]['name'] = $v['name']; $ret2 = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => $v['id'], 'is_delete' => 0]) ->orderBy('id asc')->asArray()->all(); if($ret2){ foreach($ret2 as $kk=>$vv){ $button[$k]['sub_button'][$kk]['name'] = $vv['name']; if(intval($vv['type']) === 0){ // $vv['value'] = str_replace('{id}', $id, $vv['value']); $button[$k]['sub_button'][$kk]['url'] = $vv['value']; $button[$k]['sub_button'][$kk]['type'] = "view"; }elseif(intval($vv['type']) === 1){ $button[$k]['sub_button'][$kk]['key'] = $vv['value']; $button[$k]['sub_button'][$kk]['type'] = "click"; } elseif (intval($vv['type']) === 2) { $miniprogram = json_decode($vv['value']); $button[$k]['sub_button'][$kk]['url'] = 'http://'; $button[$k]['sub_button'][$kk]['type'] = "miniprogram"; $button[$k]['sub_button'][$kk]['appid'] = $miniprogram->appid; $button[$k]['sub_button'][$kk]['pagepath'] = $miniprogram->pagepath; } elseif (intval($vv['type']) === 3) { $button[$k]['sub_button'][$kk]['key'] = 'news_' . $vv['id']; $button[$k]['sub_button'][$kk]['type'] = "click"; } } }else{ if(intval($v['type']) === 0){ // $v['value'] = str_replace('{id}', $id, $v['value']); $button[$k]['url'] = $v['value']; $button[$k]['type'] = "view"; }elseif(intval($v['type']) === 1){ $button[$k]['key'] = $v['value']; $button[$k]['type'] = "click"; } else if (intval($v['type']) === 2) { $miniprogram = \json_decode($v['value']); $button[$k]['url'] = 'http://'; $button[$k]['type'] = "miniprogram"; $button[$k]['appid'] = $miniprogram->appid; $button[$k]['pagepath'] = $miniprogram->pagepath; } else if (intval($v['type']) === 3) { $button[$k]['key'] = 'news_' . $v['id']; $button[$k]['type'] = "click"; } } } } if ($this->app) { $app = $this->app; if (!empty($button)) { $res = $app->menu->create($button); if(intval($res['errcode']) === 0){ return [ 'code' => 0, 'msg' => '设置成功' ]; }else{ return [ 'code' => 1, 'msg' => $res['errmsg'] ]; } } } return [ 'code' => 1, 'msg' => '设置失败' ]; } }