/** * 回复规则表 */ public function initReplyRuleTable() { $tableName = ReplyRule::tablename(); $this->createTable($tableName, ['id' => Schema::TYPE_PK, 'wid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '所属微信公众号ID'", 'name' => Schema::TYPE_STRING . "(40) NOT NULL DEFAULT '' COMMENT '规则名称'", 'mid' => Schema::TYPE_STRING . "(20) NOT NULL DEFAULT '' COMMENT '处理的插件模块'", 'processor' => Schema::TYPE_STRING . "(40) NOT NULL DEFAULT '' COMMENT '处理类'", 'status' => Schema::TYPE_BOOLEAN . " NOT NULL DEFAULT '0' COMMENT '状态'", 'priority' => Schema::TYPE_BOOLEAN . "(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '优先级'", 'created_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间'", 'updated_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '修改时间'"]); $this->createIndex('wid', $tableName, 'wid'); $this->createIndex('mid', $tableName, 'mid'); // 回复规则关键字表 $tableName = ReplyRuleKeyword::tablename(); $this->createTable($tableName, ['id' => Schema::TYPE_PK, 'rid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '所属规则ID'", 'keyword' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '规则关键字'", 'type' => Schema::TYPE_STRING . "(20) NOT NULL DEFAULT '' COMMENT '关键字类型'", 'priority' => Schema::TYPE_BOOLEAN . "(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '优先级'", 'start_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '开始时间'", 'end_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '结束时间'", 'created_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间'", 'updated_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '修改时间'"]); $this->createIndex('rid', $tableName, 'rid'); $this->createIndex('keyword', $tableName, 'keyword'); $this->createIndex('type', $tableName, 'type'); $this->createIndex('start_at', $tableName, 'start_at'); $this->createIndex('end_at', $tableName, 'end_at'); }
/** * 取消关注事件 * @return array */ protected function matchEventUnsubscribe() { $match = ReplyRuleKeyword::find()->andFilterWhere(['type' => [ReplyRuleKeyword::TYPE_UNSUBSCRIBE]])->wechatRule($this->getWechat()->id)->limitTime(TIMESTAMP)->all(); return array_merge([['route' => '/wechat/process/fans/unsubscribe']], $match); }
/** * 回复的关键字 * @return static */ public function getKeywords() { return $this->hasMany(ReplyRuleKeyword::className(), ['rid' => 'id'])->inverseOf('rule'); }
/** * 保存内容 * @param $rule * @param $keyword * @param array $keywords * @return bool */ protected function save($rule, $keyword, $keywords = []) { if (!$rule->save()) { return false; } $_keywords = ArrayHelper::index($keywords, 'id'); $keywords = []; $valid = true; foreach (Yii::$app->request->post($keyword->formName(), []) as $k => $data) { if (!empty($data['id']) && $_keywords[$data['id']]) { $_keyword = $_keywords[$data['id']]; unset($_keywords[$data['id']]); } else { $_keyword = clone $keyword; } unset($data['id']); $keywords[] = $_keyword; $_keyword->setAttributes(array_merge($data, ['rid' => $rule->id])); $valid = $valid && $_keyword->save(); } !empty($_keywords) && ReplyRuleKeyword::deleteAll(['id' => array_keys($_keywords)]); // 无更新的则删除 return $valid; }