/** * @return bool */ public function save() { if (!$this->validate()) { return false; } if ($this->material == Media::MATERIAL_TEMPORARY) { $method = 'uploadMedia'; } elseif ($this->material == Media::MATERIAL_PERMANENT) { $method = 'uploadMedia'; } else { $this->addError('material', '错误的素材类别'); return false; } $sdk = $this->wechat->getSdk(); $path = '@runtime/temp/' . md5($_SERVER['REQUEST_TIME_FLOAT']) . '/' . $this->file->name; $filePath = Yii::getAlias($path); FileHelper::createDirectory(dirname($filePath)); $this->file->saveAs($filePath); $data = []; if (!($result = call_user_func_array([$sdk, $method], [$filePath, $this->type, $data]))) { $this->addError('file', json_encode($sdk->lastError)); return false; } $media = Yii::createObject(Media::className()); $media->setAttributes(['mediaId' => $result['media_id'], 'filename' => $this->file->name, 'type' => $this->type, 'material' => $this->material, 'result' => $result]); return $media->save(); }
/** * 根据ID查找公众号 * @param $id * @return Object * @throws NotFoundHttpException */ protected function findWechat($id) { if (($model = Wechat::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function rules() { if ($this->isNewRecord) { // 新创建只需两个填写两个字段, return [[['name', 'description'], 'required'], [['status'], 'defaultStatus']]; } return parent::rules(); }
public function safeUp() { $wechat = new Wechat(); $wechat->setAttributes(['name' => '示例公众号', 'token' => 'lL2hcT4c41H3wL21Hlftlz4A4w22j12L', 'account' => 'example', 'original' => 'gh_example12345', 'type' => Wechat::TYPE_SUBSCRIBE, 'key' => 'wxd8cce40cc50ce6c7', 'secret' => '8e850dfed1befa106ea0bbe7cb67ad25', 'encoding_aes_key' => '8J4In8J3R3ppqWgQkCXhOvU1ZxaPFa5eSDZ8u3XNy45', 'avatar' => 'example_avatar.png', 'qrcode' => 'example_qr_code.png', 'address' => '测试地址', 'description' => '该条记录并不是真实的公众号记录.只作为本地测试示例使用', 'username' => '*****@*****.**', 'password' => 'example', 'status' => Wechat::STATUS_ACTIVE]); echo "\nexample测试示例公众号数据创建" . ($wechat->save() ? '成功' : '失败') . "\n"; $rule = new ReplyRule(); $rule->setAttributes(['wid' => $wechat->id, 'name' => '示例规则', 'mid' => 'example', 'status' => ReplyRule::STATUS_ACTIVE]); echo "\nexample示例规则数据创建" . ($rule->save() ? '成功' : '失败') . "\n"; // // $ruleKeyword = new ReplyRuleKeyword; // $ruleKeyword->setAttributes([ // 'rid' => $rule->id, // 'keyword' => 'example', // 'type' => ReplyRuleKeyword::TYPE_MATCH // ]); // echo "\nexample示例规则关键字数据创建" . ($ruleKeyword->save() ? '成功' : '失败') ."\n"; }
/** * 获取公众号 * 该方法会通过设定的公众wid函数来获取公众号数据 * @return null * @throws NotFoundHttpException */ public function getWechat() { if ($this->_wechat === null) { $wid = Yii::$app->request->getQueryParam(Yii::$app->getModule('wechat')->wechatUrlParam); if (!$wid || ($wechat = Wechat::find()->active()->one()) === null) { throw new NotFoundHttpException('The requested page does not exist.'); } $this->setWechat($wechat); } return $this->_wechat; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Wechat::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'type' => $this->type, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'token', $this->token])->andFilterWhere(['like', 'access_token', $this->access_token])->andFilterWhere(['like', 'account', $this->account])->andFilterWhere(['like', 'original', $this->original])->andFilterWhere(['like', 'key', $this->key])->andFilterWhere(['like', 'secret', $this->secret])->andFilterWhere(['like', 'encoding_aes_key', $this->encoding_aes_key])->andFilterWhere(['like', 'avatar', $this->avatar])->andFilterWhere(['like', 'qrcode', $this->qrcode])->andFilterWhere(['like', 'address', $this->address])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'password', $this->password]); return $dataProvider; }
/** * @return mixed */ protected function sendText() { return $this->wechat->getSdk()->sendMessage(['touser' => $this->toUser, 'msgtype' => $this->msgType, $this->msgType => ['content' => $this->content]]); }
/** * 公众号表 */ public function initWechatTable() { $tableName = Wechat::tableName(); $this->createTable($tableName, ['id' => Schema::TYPE_PK, 'name' => Schema::TYPE_STRING . "(40) NOT NULL DEFAULT '' COMMENT '公众号名称'", 'token' => Schema::TYPE_STRING . "(32) NOT NULL DEFAULT '' COMMENT '微信服务访问验证token'", 'access_token' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '访问微信服务验证token'", 'account' => Schema::TYPE_STRING . "(30) NOT NULL DEFAULT '' COMMENT '微信号'", 'original' => Schema::TYPE_STRING . "(40) NOT NULL DEFAULT '' COMMENT '原始ID'", 'type' => Schema::TYPE_BOOLEAN . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '公众号类型'", 'key' => Schema::TYPE_STRING . "(50) NOT NULL DEFAULT '' COMMENT '公众号的AppID'", 'secret' => Schema::TYPE_STRING . "(50) NOT NULL DEFAULT '' COMMENT '公众号的AppSecret'", 'encoding_aes_key' => Schema::TYPE_STRING . "(43) NOT NULL DEFAULT '' COMMENT '消息加密秘钥EncodingAesKey'", 'avatar' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '头像地址'", 'qrcode' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '二维码地址'", 'address' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '所在地址'", 'description' => Schema::TYPE_STRING . " NOT NULL DEFAULT '' COMMENT '公众号简介'", 'username' => Schema::TYPE_STRING . "(40) NOT NULL DEFAULT '' COMMENT '微信官网登录名'", 'status' => Schema::TYPE_BOOLEAN . " NOT NULL DEFAULT '0' COMMENT '状态'", 'password' => Schema::TYPE_STRING . "(32) NOT NULL DEFAULT '' 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('key', $tableName, 'key'); }
/** * 关联公众号 * @return \yii\db\ActiveQuery */ public function getWechat() { return $this->hasOne(Wechat::className(), ['id' => 'wid']); }
<?php use yii\helpers\Html; use callmez\wechat\models\Wechat; use callmez\wechat\assets\WechatAsset; use callmez\wechat\assets\AngularAsset; use callmez\wechat\helpers\ModuleHelper; AngularAsset::register($this); $wechatAsset = WechatAsset::register($this); $this->title = '微信模拟器'; $wechats = []; foreach (Wechat::find()->all() as $k => $wechat) { $wechats[$wechat->id] = ['name' => $wechat->name, 'original' => $wechat->original, 'api' => ModuleHelper::getApiUrl($wechat, ['id' => $wechat->id, 'token' => $wechat->token])]; } ?> <div ng-app="simulatorApp"> <div class="row" ng-controller="SimulatorController"> <div class="col-sm-8"> <div class="page-header"><h4><?php echo Html::encode($this->title); ?> </h4></div> <?php echo Html::beginForm('', 'post', ['id' => 'wechatForm', 'class' => 'form-horizontal']); ?> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-block btn-primary" ng-click="submitMessage()" ng-disabled="submit.disabled">{{submit.text || '发送'}}</button> </div> </div>