作者 郭文星

123

@@ -209,6 +209,7 @@ class Login extends Base @@ -209,6 +209,7 @@ class Login extends Base
209 public function getuserdata(){ 209 public function getuserdata(){
210 $username=$this->request->param("username"); 210 $username=$this->request->param("username");
211 $avatar=$this->request->param("avatar"); 211 $avatar=$this->request->param("avatar");
  212 + $avatar=$this->uploadBase64($avatar);
212 $user_id=$this->auth->id; 213 $user_id=$this->auth->id;
213 Db::name("user") 214 Db::name("user")
214 ->where("id", $user_id) 215 ->where("id", $user_id)
@@ -219,5 +220,27 @@ class Login extends Base @@ -219,5 +220,27 @@ class Login extends Base
219 ]); 220 ]);
220 $this->success("请求成功"); 221 $this->success("请求成功");
221 } 222 }
  223 + public function uploadBase64($base64Str)
  224 + {
  225 +
  226 + // 将Base64字符串转换为文件并保存
  227 + if ($base64Str) {
  228 + // 获取Base64字符串的类型
  229 + preg_match('/^data:image\/(.*);base64/', $base64Str, $match);
  230 + $ext = $match[1];
  231 +
  232 + // 生成文件名
  233 + $fileName = uniqid() . '.' . $ext;
  234 + // 获取项目根目录
  235 + //$rootPath = root_path();
  236 +
  237 + // 保存文件路径
  238 + $filePath = ROOT_PATH . 'public/uploads/' . $fileName;
222 239
  240 + // 保存文件
  241 + file_put_contents($filePath, base64_decode(str_replace($match[0], '', $base64Str)));
  242 + return "uploads/".$fileName;
  243 +
  244 + }
  245 + }
223 } 246 }