/** * @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(); }
/** * @return mixed */ protected function sendText() { return $this->wechat->getSdk()->sendMessage(['touser' => $this->toUser, 'msgtype' => $this->msgType, $this->msgType => ['content' => $this->content]]); }