| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\WechatConfig;
- use app\models\WechatMenuConfig;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use yii\base\Model;
- use GuzzleHttp\Exception\RequestException;
- class WechatMenuConfigForm extends Model
- {
- public $id;
- public $name;
- public $type;
- public $parent_id;
- public $sort;
- // type in [0, 1]
- public $value;
- // type = 2
- public $appid;
- public $pagePath;
- // type = 3
- public $title;
- public $desc;
- public $pic;
- public $link;
- public $app;
- public function rules()
- {
- return [
- [['name', 'value', 'appid', 'pagePath', 'title', 'desc', 'pic', 'link'],
- 'string'],
- [['id', 'type', 'parent_id', 'sort'], 'integer'],
- ['value', 'default', 'value'=> ''],
- ];
- }
- 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' => '设置失败'
- ];
- }
- }
|