shish 5 лет назад
Родитель
Сommit
ad544e82e5
3 измененных файлов с 211 добавлено и 298 удалено
  1. 4 3
      common/components/wxUtil.php
  2. 145 207
      common/services/xhWxMenuService.php
  3. 62 88
      console/controllers/InitController.php

+ 4 - 3
common/components/wxUtil.php

@@ -154,10 +154,11 @@ class wxUtil
     /**
      * 提交自定义菜单到微信平台
      */
-    public function userMenuToWeiXin($menu)
+    public static function userMenuToWeiXin($menu, $merchant, $isOpen)
     {
+        $accessToken = self::getAuthorizerAccessToken($merchant, $isOpen);
         $jsonData = urldecode(json_encode($menu, JSON_UNESCAPED_UNICODE));
-        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->access_token;
+        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $accessToken;
         $curl = new curl\Curl();
         $response = $curl->setOption(CURLOPT_POSTFIELDS, $jsonData)->post($url);
         return $response;
@@ -647,7 +648,7 @@ class wxUtil
                     xhMerchantService::updateById($sjId, $uData);
                 }
             } else {
-                noticeUtil::push("access_token已经失效,并且通过refresh_token没有获取到新的token isOpen:{$isOpen}", '15280215347');
+                noticeUtil::push("access_token已经失效,并且通过refresh_token没有获取到新的token isOpen:{$isOpen}。返回:" . json_encode($result), '15280215347');
             }
         }
         Yii::info('获取componentAccessToken 成功:' . $componentAccessToken);

+ 145 - 207
common/services/xhWxMenuService.php

@@ -2,8 +2,8 @@
 
 namespace common\services;
 
-use bizHd\message\services\TMessageService;
-use bizHd\wx\services\WxOpenService;
+use bizHd\wx\classes\WxOpenClass;
+use common\components\util;
 use Yii;
 use common\components\wxUtil;
 use common\components\dict;
@@ -11,211 +11,149 @@ use common\models\xhWxMenu;
 
 class xhWxMenuService
 {
-	
-	/**
-	 * 初始化菜单
-	 */
-	public static function applyInit($merchant, $isOpen = 0)
-	{
-		$sjId = isset($merchant['id']) ? $merchant['id'] : 0;
-		//删除之前存在的菜单
-		self::delMenuList($sjId);
-		$menuOption = dict::getDict('wxMenuOption');
-		$mallDomain = Yii::$app->params['mallDomain'];
-		$hdDomain = Yii::$app->params['hdDomain'];
-		//强制转成https方式
-		if (strpos($mallDomain, 'https') === false) {
-			$mallDomain = str_replace('http', 'https', $mallDomain);
-			$hdDomain = str_replace('http', 'https', $hdDomain);
-		}
-		if ($isOpen == 0) {
-//		$button = [
-//			["type" => 'view', "name" => "鲜花速递", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $sjId,],
-//			["type" => 'view', "name" => "会员服务", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $sjId,],
-//			["type" => 'view', "name" => "我的订单", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $sjId,],
-//		];
-			$button = [
-				["type" => 'miniprogram', "name" => "鲜花速递", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/index", "url" => $mallDomain . '/#/?account=' . $sjId,],
-				["type" => 'miniprogram', "name" => "会员服务", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/user", "url" => $mallDomain . '/#/?account=' . $sjId,],
-				["type" => 'miniprogram', "name" => "我的订单", "appid" => $merchant['miniAppId'], "pagepath" => "pages/order/list", "url" => $mallDomain . '/#/?account=' . $sjId,],
-			];
-			
-			if ($sjId == 12358) {
-				$menuOption = dict::getDict('wxMenuOption');
-				
-				$button = [
-					["type" => 'miniprogram', "name" => "鲜花速递", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/index", "url" => $mallDomain . '/#/?account=' . $sjId,],
-					["type" => "view", "name" => "国兰花尚", "keyName" => "glhsSchool", "menuOption" => $menuOption['redirectToCommonUseUrl'], "sub_button" => [
-						["type" => "view", "name" => "联系我们", "keyName" => "callUs", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'https://mp.weixin.qq.com/s/-jFCr2TcmMc0SDflSqZKCA',],
-						["type" => "view", "name" => "品牌介绍", "keyName" => "aboutUs", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'https://mp.weixin.qq.com/s/C_Vv9Cq48i8vNRK-P8VegQ',],
-						["type" => 'miniprogram', "name" => "我的会员", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/user", "url" => $mallDomain . '/#/?account=' . $sjId,],
-					]],
-					["type" => "view", "name" => "花艺培训", "keyName" => "hyTrain", "menuOption" => $menuOption['redirectToCommonUseUrl'], "sub_button" => [
-						["type" => "view", "name" => "名师风采", "keyName" => "teacher", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'http://m.fjglhs.com/main/teacher',],
-						["type" => "view", "name" => "培训课程", "keyName" => "course", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'http://m.fjglhs.com/main/course',],
-						["type" => "view", "name" => "学员天地", "keyName" => "student", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'http://m.fjglhs.com/main/student',],
-						["type" => "view", "name" => "首页", "keyName" => "index", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => 'http://m.fjglhs.com',],
-					]],
-				];
 
-			}
-			
-		} else {
-//			$button = [
-//				["type" => "view", "name" => "官网", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $sjId,],
-//				["type" => "view", "name" => "后台", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $sjId,],
-//			];
-			$button = [
-				["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "admin/official/index", "url" => $mallDomain . '/#/?account=' . $sjId,],
-				["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $sjId,],
-			];
-		}
-		$wxUtil = new wxUtil($merchant);
-		$menuList = $button;
-		foreach ($button as $key => $val) {
-			unset($button[$key]['keyName']);
-		}
-		$menu['button'] = $button;
-		$info = $wxUtil->userMenuToWeiXin($menu);
-		$info = json_decode($info, true);
+    /**
+     * 初始化菜单
+     */
+    public static function menuInit()
+    {
+        $ptStyle = dict::getDict('ptStyle');
+        if (empty($ptStyle)) {
+            util::stop('没有找到平台');
+        }
+        foreach ($ptStyle as $style) {
+            if ($style == dict::getDict('ptStyle', 'hd')) {
+                $merchant = WxOpenClass::getWxInfo();
+                $url = Yii::$app->params['hdDomain'];
+                self::menuGenerate($style, $merchant, $url);
+            } elseif ($style == dict::getDict('ptStyle', 'ghs')) {
+                $merchant = WxOpenClass::getGhsWxInfo();
+                $url = Yii::$app->params['ghsDomain'];
+                self::menuGenerate($style, $merchant, $url);
+            } elseif ($style == dict::getDict('ptStyle', 'mall')) {
+                $merchant = WxOpenClass::getMallWxInfo();
+                $url = Yii::$app->params['mallDomain'];
+                self::menuGenerate($style, $merchant, $url);
+            }
+        }
+    }
+
+    public static function menuGenerate($style, $merchant, $url)
+    {
+        if ($style == dict::getDict('ptStyle', 'mall')) {
+            $button = [
+                ["type" => 'miniprogram', "name" => "最近访问", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/recent", "url" => $url,],
+            ];
+        } else {
+            $button = [
+                ["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "pagesClient/official/index", "url" => $url,],
+                ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/workbench", "url" => $url,],
+            ];
+        }
+
+        foreach ($button as $key => $val) {
+            unset($button[$key]['keyName']);
+        }
+        $menu['button'] = $button;
+        $info = wxUtil::userMenuToWeiXin($menu, $merchant, $style);
+        $info = json_decode($info, true);
+        print_r($info);
+    }
+
+    public static function getByMenuKey($menuKey)
+    {
+        return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
+    }
+
+    public static function getMenuList($sjId)
+    {
+        $list = self::getAllMenu($sjId);
+        return $list;
+    }
+
+    /**
+     * 取出商家所有菜单
+     */
+    public static function getAllMenu($sjId, $getKeyName = false)
+    {
+        $list = self::getMenuById(0, $sjId, $getKeyName);
+        return $list;
+    }
+
+    /**
+     * 根据菜单id取子菜单
+     * $getKeyName false 默认不带键名 true 返回带键名的
+     */
+    public static function getMenuById($parentId, $sjId, $getKeyName = false)
+    {
+        $condition = ['parentId' => $parentId, 'sjId' => $sjId];
+        $wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
+        $data = [];
+        if (empty($wxMenu)) {
+            return $data;
+        }
+        foreach ($wxMenu as $k => $v) {
+            $id = $v['id'];
+            $cate = [
+                'id' => $id,
+                'name' => $v['name'],
+                'menuKey' => $v['menuKey'],
+                'menuOption' => $v['menuOption'],
+                'parentId' => $v['parentId'],
+                'sjId' => $v['sjId'],
+                'replyType' => $v['replyType'],
+                'replyUrl' => $v['replyUrl'],
+                'replyText' => $v['replyText'],
+                'replyNews' => $v['replyNews'],
+                'replyMultiNews' => $v['replyMultiNews'],
+                'hasSonMenu' => $v['hasSonMenu'],
+                'menuOrder' => $v['menuOrder'],
+                'createTime' => $v['createTime'],
+                'updateTime' => $v['updateTime'],
+                'child' => self::getMenuById($id, $sjId, $getKeyName),
+            ];
+            if ($getKeyName) {
+                $data[$id] = $cate;
+            } else {
+                $data[] = $cate;
+            }
+        }
+        return $data;
+    }
+
+    public static function delMenuList($sjId)
+    {
+        xhWxMenu::deleteByCondition(['sjId' => $sjId]);
+    }
+
+    public static function add($data)
+    {
+        return xhWxMenu::add($data);
+    }
+
+    public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
+    {
+        $addData = [];
+        $wxMenuOptionList = dict::getDict('wxMenuOption');
+        $commonUseUrl = dict::getDict('commonUseUrl');
+        switch ($wxMenuOption) {
+            case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
+                foreach ($commonUseUrl as $comKey => $comVal) {
+                    if ($comKey == $wxMenuContent) {
+                        $addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
+                        $addData['targetId'] = $wxMenuContent;
+                    }
+                }
+                break;
+            case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
+                $addData['replyUrl'] = urldecode($wxMenuContent);
+                break;
+            case $wxMenuOptionList['showText']://回复文字
+                $addData['replyText'] = $wxMenuContent;
+                break;
+            default:
+        }
+        return $addData;
+    }
 
-		xhMerchantExtendService::updateBySjId($sjId, ['wxMenuInit' => 1]);
-		$now = time();
-		$date = date("Y-m-d H:i:s", $now);
-		$mapped = dict::getDict('commonUseUrlMapped');
-		$redirectUrl = dict::getDict('replyType', 'redirectUrl');//回复类型 跳转链接
-		if ($info['errcode'] != 0) {
-			return false;
-		}
-		foreach ($menuList as $key => $val) {
-			$addData = [];
-			$addData['parentId'] = 0;
-			$addData['sjId'] = $sjId;
-			$addData['menuOrder'] = $key;
-			$addData['menuOption'] = $val['menuOption'];
-			$addData['hasSonMenu'] = isset($val['sub_button']) && !empty($val['sub_button']) ? 1 : 0;
-			$addData['name'] = $val['name'];
-			$addData['menuKey'] = $sjId . '_' . $key;
-			$addData['createTime'] = $date;
-			$addData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
-			$addData['targetId'] = isset($val['keyName']) ? $mapped[$val['keyName']] : 0;//常用链接的链接选项id;活动id等
-			$addData['replyUrl'] = isset($val['url']) ? $val['url'] : '';
-			$parentMenu = self::add($addData);
-			if (isset($val['sub_button']) && !empty($val['sub_button'])) {
-				foreach ($val['sub_button'] as $sonKey => $sonVal) {
-					$sonAddData = [];
-					$sonAddData['parentId'] = $parentMenu['id'];
-					$sonAddData['sjId'] = $sjId;
-					$sonAddData['menuOrder'] = $sonKey;
-					$sonAddData['menuOption'] = $sonVal['menuOption'];//回复类型
-					$sonAddData['hasSonMenu'] = 0;
-					$sonAddData['name'] = $sonVal['name'];
-					$sonAddData['menuKey'] = $sjId . '_' . $key . '_' . $sonKey;
-					$sonAddData['createTime'] = $date;
-					$sonAddData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
-					$sonAddData['targetId'] = isset($sonVal['keyName']) ? $mapped[$sonVal['keyName']] : 0;//常用链接选项id、活动id
-					$sonAddData['replyUrl'] = isset($sonVal['url']) ? $sonVal['url'] : '';
-					self::add($sonAddData);
-				}
-			}
-		}
-		return true;
-	}
-	
-	public static function getByMenuKey($menuKey)
-	{
-		return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
-	}
-	
-	public static function getMenuList($sjId)
-	{
-		$list = self::getAllMenu($sjId);
-		return $list;
-	}
-	
-	/**
-	 * 取出商家所有菜单
-	 */
-	public static function getAllMenu($sjId, $getKeyName = false)
-	{
-		$list = self::getMenuById(0, $sjId, $getKeyName);
-		return $list;
-	}
-	
-	/**
-	 * 根据菜单id取子菜单
-	 * $getKeyName false 默认不带键名 true 返回带键名的
-	 */
-	public static function getMenuById($parentId, $sjId, $getKeyName = false)
-	{
-		$condition = ['parentId' => $parentId, 'sjId' => $sjId];
-		$wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
-		$data = [];
-		if (empty($wxMenu)) {
-			return $data;
-		}
-		foreach ($wxMenu as $k => $v) {
-			$id = $v['id'];
-			$cate = [
-				'id' => $id,
-				'name' => $v['name'],
-				'menuKey' => $v['menuKey'],
-				'menuOption' => $v['menuOption'],
-				'parentId' => $v['parentId'],
-				'sjId' => $v['sjId'],
-				'replyType' => $v['replyType'],
-				'replyUrl' => $v['replyUrl'],
-				'replyText' => $v['replyText'],
-				'replyNews' => $v['replyNews'],
-				'replyMultiNews' => $v['replyMultiNews'],
-				'hasSonMenu' => $v['hasSonMenu'],
-				'menuOrder' => $v['menuOrder'],
-				'createTime' => $v['createTime'],
-				'updateTime' => $v['updateTime'],
-				'child' => self::getMenuById($id, $sjId, $getKeyName),
-			];
-			if ($getKeyName) {
-				$data[$id] = $cate;
-			} else {
-				$data[] = $cate;
-			}
-		}
-		return $data;
-	}
-	
-	public static function delMenuList($sjId)
-	{
-		xhWxMenu::deleteByCondition(['sjId' => $sjId]);
-	}
-	
-	public static function add($data)
-	{
-		return xhWxMenu::add($data);
-	}
-	
-	public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
-	{
-		$addData = [];
-		$wxMenuOptionList = dict::getDict('wxMenuOption');
-		$commonUseUrl = dict::getDict('commonUseUrl');
-		switch ($wxMenuOption) {
-			case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
-				foreach ($commonUseUrl as $comKey => $comVal) {
-					if ($comKey == $wxMenuContent) {
-						$addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
-						$addData['targetId'] = $wxMenuContent;
-					}
-				}
-				break;
-			case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
-				$addData['replyUrl'] = urldecode($wxMenuContent);
-				break;
-			case $wxMenuOptionList['showText']://回复文字
-				$addData['replyText'] = $wxMenuContent;
-				break;
-			default:
-		}
-		return $addData;
-	}
-	
 }

+ 62 - 88
console/controllers/InitController.php

@@ -2,22 +2,14 @@
 
 namespace console\controllers;
 
-use bizHd\goods\services\GoodsService;
-use bizHd\goods\services\GoodsStyleService;
-use biz\sj\services\MerchantService;
 use bizHd\user\services\UserService;
 use bizHd\wx\services\WxOpenService;
-use common\services\xhAdminService;
 use common\services\xhMerchantAssetService;
-use common\services\xhMerchantExtendService;
 use common\services\xhWxMenuService;
 use Yii;
 use yii\console\Controller;
-use common\components\util;
 use common\services\xhTMessageService;
 use common\services\xhMerchantService;
-use common\models\xhMerchant;
-use common\services\xhInitService;
 
 /**
  * 脚本
@@ -25,94 +17,76 @@ use common\services\xhInitService;
 class InitController extends Controller
 {
 
-	//开店初始化 ssh 2020.2.8
-	public function actionRabbit()
-	{
-		$data = ['sjId' => 12362];
-		$producer = Yii::$app->rabbitmq->getProducer('openShopInitProducer');
-		$msg = serialize($data);
-		$producer->publish($msg, 'openShopInitExchange', 'openShopInitRoute');
-	}
-
-	//二次初始化 ssh 2020.1.19
-	public function actionSecondInit($id = 0)
-	{
-		$merchant = xhMerchantService::getById($id);
-		if (empty($merchant)) {
-			echo "商家无效\n";
-		}
-		//初始化微信菜单
-		xhWxMenuService::applyInit($merchant);
-	}
-
-	public function actionChangeDb()
-	{
-		echo 'ok';
-	}
+    //微信菜单初始化 shish 20210526
+    // yii init/menu-init
+    public function actionMenuInit()
+    {
+        xhWxMenuService::menuInit();
+    }
 
-	//接入一个商家需要的操作 ssh 2019.9.7
-	//  yii init/join-one 12358 更新某个商家公众号
-	//  yii init/join-one 更新平台公众号
-	public function actionJoinOne($id = 0)
-	{
-		//初始化微信菜单
-		if ($id == 0) {
-			$isOpen = 1;
-			$merchant = WxOpenService::getWxInfo();
-			$merchant['id'] = 0;
-		} else {
-			$isOpen = 0;
-			$merchant = xhMerchantService::getById($id);
-		}
-		//xhWxMenuService::applyInit($merchant);
-		//模板消息
-		$r = xhTMessageService::supplement($merchant, $isOpen);
-		print_r($r);
-	}
+    //接入一个商家需要的操作 ssh 2019.9.7
+    //  yii init/join-one 12358 更新某个商家公众号
+    //  yii init/join-one 更新平台公众号
+    public function actionJoinOne($id = 0)
+    {
+        //初始化微信菜单
+        if ($id == 0) {
+            $isOpen = 1;
+            $merchant = WxOpenService::getWxInfo();
+            $merchant['id'] = 0;
+        } else {
+            $isOpen = 0;
+            $merchant = xhMerchantService::getById($id);
+        }
+        //xhWxMenuService::applyInit($merchant);
+        //模板消息
+        $r = xhTMessageService::supplement($merchant, $isOpen);
+        print_r($r);
+    }
 
 
-	//yii init/sub
-	public function actionSub()
-	{
-		$name = 'redis_shi';
-		$redisPubSub = \Yii::$app->redisPubSub;
-		$redisPubSub->setOptReadTimeout(-1);
-		$redisPubSub->subscribe($name, function ($instance, $channelName, $message) {
-			$a = 123;
-		});
-	}
+    //yii init/sub
+    public function actionSub()
+    {
+        $name = 'redis_shi';
+        $redisPubSub = \Yii::$app->redisPubSub;
+        $redisPubSub->setOptReadTimeout(-1);
+        $redisPubSub->subscribe($name, function ($instance, $channelName, $message) {
+            $a = 123;
+        });
+    }
 
-	public function actionPub()
-	{
-		$name = 'redis_shi';
-		$time = 'nihao1123';
-		$redisPubSub = \Yii::$app->redisPubSub;
-		$redisPubSub->publish($name, $time);
-	}
+    public function actionPub()
+    {
+        $name = 'redis_shi';
+        $time = 'nihao1123';
+        $redisPubSub = \Yii::$app->redisPubSub;
+        $redisPubSub->publish($name, $time);
+    }
 
-	public function actionPullUser()
-	{
-		//拉取用户
-		$this->actionPullUser();
-	}
+    public function actionPullUser()
+    {
+        //拉取用户
+        $this->actionPullUser();
+    }
 
-	// 拉取微信用户 ssh 2019.9.9
-	public function actionPullUser2()
-	{
-		$sjId = 12359;
-		$return = UserService::syncWxAllUser($sjId);
-		$total = isset($return['data']['total']) ? $return['data']['total'] : 0;
-		xhMerchantAssetService::updateBySjId($sjId, ['totalFans' => $total]);
-	}
+    // 拉取微信用户 ssh 2019.9.9
+    public function actionPullUser2()
+    {
+        $sjId = 12359;
+        $return = UserService::syncWxAllUser($sjId);
+        $total = isset($return['data']['total']) ? $return['data']['total'] : 0;
+        xhMerchantAssetService::updateBySjId($sjId, ['totalFans' => $total]);
+    }
 
-	//更新用户名称
-	public function actionUpdateName()
-	{
-		$sjId = 12362;
-		UserService::syncWxAllUser($sjId);
-	}
+    //更新用户名称
+    public function actionUpdateName()
+    {
+        $sjId = 12362;
+        UserService::syncWxAllUser($sjId);
+    }
 
-	//消息模板初始化 linqh ./yii init/t-message
+    //消息模板初始化 linqh ./yii init/t-message
     public function actionTMessage()
     {
         xhTMessageService::initTMessage();