/** * @inheritdoc */ public function sendMessage(array $data) { $result = parent::sendMessage($data); if ($result && isset($data['touser'])) { // 记录发送日志 MessageHistory::add(['wid' => $this->model->id, 'from' => $this->model->original, 'to' => $data['touser'], 'message' => $data, 'type' => MessageHistory::TYPE_CUSTOM]); } return $result; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = MessageHistory::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'wid' => $this->wid, 'rid' => $this->rid, 'kid' => $this->kid, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'from', $this->from])->andFilterWhere(['like', 'to', $this->to])->andFilterWhere(['like', 'module', $this->module])->andFilterWhere(['like', 'message', $this->message])->andFilterWhere(['like', 'type', $this->type]); return $dataProvider; }
/** * 解析到控制器 * @return null */ public function resolveProcess() { $result = null; foreach ($this->match() as $model) { if ($model instanceof ReplyRuleKeyword) { $processor = $model->rule->processor; $route = $processor[0] == '/' ? $processor : '/wechat/' . $model->rule->mid . '/' . $model->rule->processor; } elseif (isset($model['route'])) { // 直接返回处理route $route = $model['route']; } else { continue; } // 转发路由请求 参考: Yii::$app->runAction() $parts = Yii::$app->createController($route); if (is_array($parts)) { list($controller, $actionID) = $parts; // 微信请求的处理器必须继承callmez\wechat\components\ProcessController if (!$controller instanceof ProcessController) { throw new InvalidCallException("Wechat process controller must instance of '" . ProcessController::className() . "'"); } // 传入当前公众号和微信请求内容 $controller->message = $this->message; $controller->setWechat($this->getWechat()); $oldController = Yii::$app->controller; $result = $controller->runAction($actionID); Yii::$app->controller = $oldController; } // 如果有数据则跳出循环直接输出. 否则只作为订阅类型继续循环处理 if ($result !== null) { break; } } $module = isset($controller) ? $controller->module->id : 'wechat'; // 处理的模块 if ($model instanceof ReplyRuleKeyword) { $kid = $model->id; $rid = $model->rid; } else { $kid = $rid = 0; } // 记录请求内容 MessageHistory::add(['wid' => $this->getWechat()->id, 'rid' => $rid, 'kid' => $kid, 'from' => $this->message['FromUserName'], 'to' => $this->message['ToUserName'], 'module' => $module, 'message' => $this->message, 'type' => MessageHistory::TYPE_REQUEST]); // 记录响应内容 if ($result !== null) { // 记录响应内容 MessageHistory::add(['wid' => $this->getWechat()->id, 'rid' => $rid, 'kid' => $kid, 'from' => $this->message['ToUserName'], 'to' => $this->message['FromUserName'], 'module' => $module, 'message' => $result, 'type' => MessageHistory::TYPE_RESPONSE]); } return $result; }
/** * 消息记录表 */ public function initMessageHistoryTable() { $tableName = MessageHistory::tableName(); $this->createTable($tableName, ['id' => Schema::TYPE_PK, 'wid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '所属微信公众号ID'", 'rid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '相应规则ID'", 'kid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '所属关键字ID'", 'from' => Schema::TYPE_STRING . "(50) NOT NULL DEFAULT '' COMMENT '请求用户ID'", 'to' => Schema::TYPE_STRING . "(50) NOT NULL DEFAULT '' COMMENT '相应用户ID'", 'module' => Schema::TYPE_STRING . "(20) NOT NULL DEFAULT '' COMMENT '处理模块'", 'message' => Schema::TYPE_TEXT . " NOT NULL COMMENT '消息体内容'", 'type' => Schema::TYPE_STRING . "(10) NOT NULL DEFAULT '' COMMENT '发送类型'", 'created_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间'"]); $this->createIndex('wid', $tableName, 'wid'); $this->createIndex('module', $tableName, 'module'); }