Ver código fonte

写到了存入数据库

shish 6 anos atrás
pai
commit
c0fec3eaa0

+ 7 - 3
app/client/controllers/TestController.php

@@ -2,16 +2,20 @@
 
 namespace client\controllers;
 
+use biz\message\services\InformAdminService;
+use common\components\util;
 use Yii;
 
 class TestController extends BaseController
 {
-
+	
 	public function actionRabbit()
 	{
+		InformAdminService::addInform(12358, 'goodsSold', ['price' => 20]);
+		util::end();
 		$producer = Yii::$app->rabbitmq->getProducer('producerCommon');
-		$msg = serialize(['user_id' => rand(111,999)]);
+		$msg = serialize(['user_id' => rand(111, 999)]);
 		$producer->publish($msg, 'exCommon', 'routeCommon');
 	}
-
+	
 }

+ 16 - 14
biz/admin/classes/AdminClass.php

@@ -31,13 +31,15 @@ class AdminClass extends BaseClass
 		$prefix = Yii::$app->params['imgUrl'];
 		foreach ($list as $key => $val) {
 			$roleId = $val['roleId'];
-			$list[$key]['roleName'] = isset($roleInfo[$roleId]['roleName']) ? $roleInfo[$roleId]['roleName'] : '';
 			$userId = $val['userId'];
+			$list[$key]['roleName'] = isset($roleInfo[$roleId]['roleName']) ? $roleInfo[$roleId]['roleName'] : '';
 			$list[$key]['nickName'] = isset($userInfo[$userId]['userName']) ? $userInfo[$userId]['userName'] : '';
 			$list[$key]['avatarUrl'] = isset($userInfo[$userId]['avatar']) ? $prefix . $userInfo[$userId]['avatar'] : $prefix . '/none.jpg';
 			$list[$key]['mobile'] = isset($userInfo[$userId]['mobile']) ? $userInfo[$userId]['mobile'] : '';
 			$list[$key]['addDate'] = date("Y-m-d H:i", $val['addTime']);
 			$list[$key]['lastLoginTime'] = date("Y-m-d H:i", $val['loginTime']);
+			$list[$key]['openId'] = isset($userInfo[$userId]['openId']) ? $userInfo[$userId]['openId'] : '';
+			$list[$key]['subscribe'] = isset($userInfo[$userId]['subscribe']) ? $userInfo[$userId]['subscribe'] : 0;
 		}
 		return $list;
 	}
@@ -53,6 +55,17 @@ class AdminClass extends BaseClass
 		return current($list);
 	}
 	
+	//获取员工详情 shish 2019.12.9
+	public static function getAdminById($id)
+	{
+		$detail = AdminClass::getById($id);
+		if (empty($detail)) {
+			util::fail('没有找到员工信息');
+		}
+		$list = self::groupAdminBaseInfo([$detail]);
+		return current($list);
+	}
+	
 	//添加员工 shish 2019.12.9
 	public static function addAdmin($data)
 	{
@@ -168,23 +181,12 @@ class AdminClass extends BaseClass
 	//取商家需要收款通知的全部员工 shish 2019.12.24
 	public static function getAllNoticeAdmin($merchantId)
 	{
-		$list = self::getAllByCondition(['merchantId' => $merchantId, 'delStatus' => 0, 'status' => 1]);
+		$list = self::getAllByCondition(['merchantId' => $merchantId, 'delStatus' => 0, 'status' => 1], null, '*');
 		if (empty($list)) {
 			util::fail('此商家没有员工,请检查原因');
 		}
 		//需要收款通知的需要拿到是否关注公众号
-		$ids = array_column($list, 'userId');
-		$userInfo = UserClass::getByIds($ids);
-		foreach ($list as $key => $val) {
-			if ($val['remind'] == 0) {
-				unset($list[$key]);
-				continue;
-			}
-			$userId = $val['userId'];
-			$list[$key]['openId'] = isset($userInfo[$userId]) ? $userInfo[$userId]['openId'] : '';
-			$list[$key]['subscribe'] = isset($userInfo[$userId]) ? $userInfo[$userId]['subscribe'] : 0;
-			$list[$key]['mobile'] = isset($userInfo[$userId]) ? $userInfo[$userId]['mobile'] : '';
-		}
+		$list = self::groupAdminBaseInfo($list);
 		return $list;
 	}
 	

+ 6 - 0
biz/admin/services/AdminService.php

@@ -119,6 +119,12 @@ class AdminService extends BaseService
 		return AdminClass::getDetail($id);
 	}
 	
+	//获取员工信息 shish 2019.12.8
+	public static function getAdminById($id)
+	{
+		return AdminClass::getAdminById($id);
+	}
+	
 	//考虑权限情况下删除员工 shish 2019.12.9
 	public static function deleteAdmin($admin, $adminId)
 	{

+ 28 - 16
biz/message/classes/InformAdminClass.php

@@ -11,38 +11,51 @@ class InformAdminClass extends BaseClass
 	public static $baseFile = '\biz\message\models\InformAdmin';
 	
 	//宝贝售出通知 shish 2019.12.24
-	public static function goodsSoldInformAdmin($params, $informWay = 0, $TMList = [])
+	public static function goodsSoldInformAdmin($params, $admin, $TMList)
 	{
-		$params = [];
+		$informWay = isset($admin['openId']) && !empty($admin['openId']) && isset($admin['subscribe']) && $admin['subscribe'] == 1 ? 'wx' : 'sms';
+		$totalFee = 199;
+		$orderSn = '2051229823132466';
+		$orderName = '商品';
+		$userName = '石头';
+		$data = ['tMessage' => '', 'content' => '', 'briefContent' => ''];
+		$data['briefContent'] = "客户" . $userName . "付款" . $totalFee . '元';
 		if ($informWay == 'wx') {
 			//收入提醒
+			$openId = $admin['openId'];
 			$shortTempId = 'OPENTM207422813';
 			$tempId = isset($TMList[$shortTempId]) ? $TMList[$shortTempId] : '';
-			$tmData = ["touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderId, "data" => [
+			$tMessage = ["touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderSn, "data" => [
 				"first" => ["value" => '您有宝贝售出哦!', "color" => "#173177"],
-				"keyword1" => ["value" => (string)$orderId, "color" => "#173177"],
+				"keyword1" => ["value" => (string)$orderSn, "color" => "#173177"],
 				"keyword2" => ["value" => $orderName, "color" => "#173177"],
 				"keyword3" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
 				"keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
 				"keyword5" => ["value" => $userName, "color" => "#173177"],
 				"remark" => ["value" => '点此查看详细、编辑收花人信息。', "color" => "#173177"]]];
-			return $tmData;
+			$data['tMessage'] = json_encode($tMessage);
 		}
 		if ($informWay == 'sms') {
-			return "客户" . $userName . "付款" . $totalFee . '元';
+			$msg = "客户" . $userName . "付款" . $totalFee . '元';
+			$data['content'] = $msg;
 		}
+		return $data;
 	}
-
-	//宝贝售出通知 shish 2019.12.24
-	public static function inform($params, $type, $informWay = 0, $TMList = [])
+	
+	//通知员工 shish
+	public static function addInform($params, $admin, $type, $TMList)
 	{
 		switch ($type) {
-			case 'goodsSoldInformAdmin':
-				$respond = self::goodsSoldInformAdmin($params, $informWay, $TMList);
+			case 'goodsSold':
+				$respond = self::goodsSoldInformAdmin($params, $admin, $TMList);
 				break;
 			default:
 		}
-		return $respond;
+		$data = ['messageType' => 'inform', 'informWay' => 'wx', 'userType' => 'admin', 'id' => 1, 'mobile' => '15280215347'];
+		$data = array_merge($data, $respond);
+		$producer = Yii::$app->rabbitmq->getProducer('producerInform');
+		$msg = serialize($data);
+		$producer->publish($msg, 'exInform', 'routeInform');die;
 	}
 	
 	//门店收入提醒 shish 2019.12.24
@@ -60,7 +73,7 @@ class InformAdminClass extends BaseClass
 			$openId = $admin['openId'];
 			if (!empty($openId) && $admin['subscribe'] == 1) {
 				$data = ["touser" => $openId, "template_id" => $tempId,
-					"url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderId, "data" => [
+					"url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderSn, "data" => [
 						"first" => ["value" => $userName . $payWay . '付款', "color" => "#173177"],
 						"keyword1" => ["value" => $totalFee . '元', "color" => "#173177"],
 						"keyword2" => ["value" => '客户付款', "color" => "#173177"],
@@ -79,7 +92,6 @@ class InformAdminClass extends BaseClass
 	//宝贝售出通知 shish 2019.12.24
 	public static function goodsSoldInformAdmin2($merchantId)
 	{
-		
 		$TMList = xhTMessageService::getList($merchantId);
 		$merchant = MerchantService::getById($merchantId);
 		$shortTempId = 'OPENTM207422813';//收入提醒
@@ -87,9 +99,9 @@ class InformAdminClass extends BaseClass
 		foreach ($adminList as $adminKey => $admin) {
 			$openId = $admin['openId'];
 			if (!empty($openId) && $admin['subscribe'] == 1) {
-				$sendData = ["touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderId, "data" => [
+				$sendData = ["touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['adminUrl'] . '/order/detail?id=' . $orderSn, "data" => [
 					"first" => ["value" => '您有宝贝售出哦!', "color" => "#173177"],
-					"keyword1" => ["value" => (string)$orderId, "color" => "#173177"],
+					"keyword1" => ["value" => (string)$orderSn, "color" => "#173177"],
 					"keyword2" => ["value" => $orderName, "color" => "#173177"],
 					"keyword3" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
 					"keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],

+ 33 - 16
biz/message/services/InformAdminService.php

@@ -3,40 +3,57 @@
 namespace biz\message\services;
 
 use biz\admin\classes\AdminClass;
+use biz\admin\services\AdminService;
 use biz\base\services\BaseService;
 use biz\merchant\services\MerchantService;
 use biz\message\classes\InformAdminClass;
 use common\components\sms;
-use common\components\weixinUtil;
 use common\services\xhTMessageService;
 use Yii;
 
 class InformAdminService extends BaseService
 {
-
+	
 	public static $baseFile = '\biz\message\classes\InformAdminClass';
-
+	
 	//通知到员工 shish 2020.1.1
-	public static function inform($merchantId, $type, $params)
+	public static function addInform($merchantId, $type, $params)
 	{
 		$adminList = AdminClass::getAllNoticeAdmin($merchantId);
 		if (empty($adminList)) {
 			return false;
 		}
+		//本地环境不进行通知
+		if (getenv('YII_ENV') == 'local') {
+			//return false;
+		}
 		$TMList = xhTMessageService::getList($merchantId);
-		$merchant = MerchantService::getById($merchantId);
 		foreach ($adminList as $adminKey => $admin) {
-			$openId = $admin['openId'];
-			$informWay = !empty($openId) && $admin['subscribe'] == 1 ? 'wx' : 'sms';
-			$sendData = InformAdminClass::inform($params, $type, $informWay, $TMList);
-			if (!empty($openId) && $admin['subscribe'] == 1) {
-				$respond = weixinUtil::sendTaskInform($sendData, $merchant);
-				Yii::info('tm:' . json_encode($respond));
-			} else {
-				$mobile = $admin['mobile'];
-				sms::merchantSend($mobile, $sendData, $merchant);
-			}
+			InformAdminClass::addInform($params, $admin, $type, $TMList);
 		}
 	}
-	
+
+	//消费通知队列 shish 2020.1.1
+	public static function consumeInformQueue($data)
+	{
+		//['messageType'=>'inform','informWay'=>'wx','userType'=>'admin','id'=>1,'mobile'=>'15280215347','tMessage'=>'','content'=>'','briefContent'=>'']
+		$informWay = $data['$informWay'];
+		$id = $data['id'];
+		$admin = AdminService::getAdminById($id);
+		$merchantId = $admin['merchantId'];
+		$mobile = $data['mobile'];
+		$merchant = MerchantService::getMerchantById($merchantId);
+		if ($informWay == 'wx') {
+			$tMessage = json_decode($data['tMessage'], true);
+			weixinUtil::sendTaskInform($tMessage, $merchant);
+			Yii::info('tm:' . json_encode($respond));
+		}
+		if ($informWay == 'sms') {
+			$msg = $data['content'];
+			sms::merchantSend($mobile, $msg, $merchant);
+		}
+		//存入数据库
+		//InformAdminService::addData();
+	}
+
 }

+ 41 - 0
common/components/rabbitmq/consumer/CommonConsumer.php

@@ -0,0 +1,41 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: shish <shish@zhhinc.com>
+ * Date: 2019/9/9 0009
+ * Time: 14:54
+ */
+
+namespace common\components\rabbitmq\consumer;
+
+use mikemadisonweb\rabbitmq\components\ConsumerInterface;
+use PhpAmqpLib\Message\AMQPMessage;
+
+class CommonConsumer implements ConsumerInterface
+{
+	/**
+	 * @param AMQPMessage $msg
+	 * @return bool
+	 */
+	public function execute(AMQPMessage $msg)
+	{
+		$data = unserialize($msg->body);
+		print_r($data);
+		print_r('PrintMsgConsumer done! ');
+		
+		if ($data['status'] == 200) {
+			print_r('suceess ');
+			return ConsumerInterface::MSG_ACK;
+		} elseif ($data['status'] == 500) {
+			sleep(5);
+			print_r('faile ');
+			return ConsumerInterface::MSG_REQUEUE;
+		}
+		/**
+		 * ConsumerInterface::MSG_ACK - Acknowledge message (mark as processed) and drop it from the queue
+		 * ConsumerInterface::MSG_REJECT - Reject and drop message from the queue
+		 * ConsumerInterface::MSG_REQUEUE - Reject and requeue message in RabbitMQ
+		 */
+	}
+	
+}

+ 51 - 0
common/components/rabbitmq/consumer/InformConsumer.php

@@ -0,0 +1,51 @@
+<?php
+/**
+ * 通知的消费者
+ * User: shish <shish@zhhinc.com>
+ * Date: 2019/9/9 0009
+ * Time: 14:54
+ */
+
+namespace common\components\rabbitmq\consumer;
+
+use biz\message\services\InformAdminService;
+use biz\message\services\InformUserService;
+use mikemadisonweb\rabbitmq\components\ConsumerInterface;
+use PhpAmqpLib\Message\AMQPMessage;
+
+class InformConsumer implements ConsumerInterface
+{
+	/**
+	 * @param AMQPMessage $msg
+	 * @return bool
+	 */
+	public function execute(AMQPMessage $msg)
+	{
+		$data = unserialize($msg->body);
+		//inform
+		//$messageType = $data['messageType'];
+		$userType = $data['userType'];
+		
+		if ($userType == 'user') {
+			InformUserService::consumeInformQueue($data);
+		}
+		if ($userType == 'admin') {
+			InformAdminService::consumeInformQueue($data);
+		}
+		
+		if ($data['status'] == 200) {
+			print_r('suceess ');
+			return ConsumerInterface::MSG_ACK;
+		} elseif ($data['status'] == 500) {
+			sleep(5);
+			print_r('faile ');
+			return ConsumerInterface::MSG_REQUEUE;
+		}
+		/**
+		 * ConsumerInterface::MSG_ACK - Acknowledge message (mark as processed) and drop it from the queue
+		 * ConsumerInterface::MSG_REJECT - Reject and drop message from the queue
+		 * ConsumerInterface::MSG_REQUEUE - Reject and requeue message in RabbitMQ
+		 */
+	}
+	
+}

+ 1 - 1
common/config/rabbitMQ.php

@@ -164,7 +164,7 @@ $rabbitMQ = [
 			//通知模块消费者
 			'name' => 'consumerInform',
 			'callbacks' => [
-				'queueInform' => '\common\components\rabbitmq\consumer\ImportDataConsumer', //设定队列的数据给谁转回调而以,只是个配置
+				'queueInform' => '\common\components\rabbitmq\consumer\InformConsumer', //设定队列的数据给谁转回调而以,只是个配置
 			],
 		],
 		/*