/** * 保存文件到 aliyun oss * @param \AliYun\OSS $oss * @param string $bucket * @param string $fileName * @param string $file * @param array $options * @return bool */ private function saveFile($oss, $bucket, $fileName, $file, $options = null) { $resp = $oss->upload_file_by_file($bucket, $fileName, $file, $options); if ($resp->status == 200) { $this->di->get('logger')->info('oss ' . $bucket . ', 上传文件: ' . $fileName . ', response:' . json_encode($resp)); } else { $this->di->get('logger')->error('oss ' . $bucket . ', 上传文件: ' . $fileName . ', response:' . json_encode($resp)); } return $resp->status == 200; }
private function register($type, $attrType, $openId, $nickname, $cover, $otherData) { // 添加基础信息 $base = new UserBase(); $base->user_account = ''; $base->user_password = ''; $base->user_nickname = $nickname; /* if($cover!=''){ $base->user_cover = $this->getConfig()->defaultCover; //'default.png'; } */ //$config=$this->Config()->aliyun->oss; $img = file_get_contents($cover); file_put_contents('img/1.jpg', $img); $oss = new OSS('dUBfr1wNHjgMnJsd', '1U6ZSbOKhGBffxsopuMIKN6xCpSzUe', 'oss-cn-beijing.aliyuncs.com'); $options = array(); $object_name = time() . rand(10000, 99999); $file_path = "user-cover/" . date("Y/m/d") . "/" . $object_name . ".jpg"; $response = $oss->upload_file_by_file('meelier', $file_path, 'img/1.jpg'); unlink('img/1.jpg'); $url = $response->header['_info']['url']; $url = str_replace("http://oss-cn-beijing.aliyuncs.com/meelier/", "", $url); if ($attrType == 101) { $base->user_cover = json_decode($otherData)->headimgurl ?: $url; } else { $base->user_cover = $url; } // $base->user_cover=$this->getConfig()->defaultCover; if (!$base->save()) { $this->databaseErrorLog($base); return false; } $userId = $base->user_id; // 保存第三方登录信息 $attr = new UserAttribute(); $attr->user_id = $userId; $attr->attr_type = 100; $attr->attr_key = $type; $attr->attr_value_json = $openId; if (!$attr->save()) { $this->databaseErrorLog($attr); return false; } $attr2 = new UserAttribute(); $attr2->user_id = $userId; $attr2->attr_type = $attrType; $attr2->attr_key = $openId; $attr2->attr_value_json = $otherData; if (!$attr2->save()) { $this->databaseErrorLog($attr2); return false; } return $userId; }