avatar data
This commit is contained in:
		
							
								
								
									
										30
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								README.md
									
									
									
									
									
								
							@@ -179,22 +179,20 @@ fetch('/user/ApSXNLoUy', {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#### 上传头像
 | 
					#### 上传头像
 | 
				
			||||||
 | 
					
 | 
				
			||||||
实际分为两步,
 | 
					 | 
				
			||||||
第一步先上传附件到自己的账户
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```html
 | 
					```html
 | 
				
			||||||
<!DOCTYPE html>
 | 
					<!DOCTYPE html>
 | 
				
			||||||
<input type="file" name="photos", multiple, onchange="upload()"/>
 | 
					<input type="file" name="photos", accept="image/*", multiple, onchange="upload($event)"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
function upload() {
 | 
					function upload(event) {
 | 
				
			||||||
    let myForm = new FormData();
 | 
					    let data = new FormData();
 | 
				
			||||||
    let files = document.querySelector("[type=file]").files;
 | 
					    //let files = document.querySelector("[type=file]").files;
 | 
				
			||||||
    files.map(item => myForm.append("photos", item))
 | 
					    //files.map(item => data.append("photos", item))
 | 
				
			||||||
 | 
					    data.append("img", event.target.files[0]);
 | 
				
			||||||
    fetch('/user/ApSXNLoUy', {
 | 
					    fetch('/user/ApSXNLoUy', {
 | 
				
			||||||
        method: 'POST',
 | 
					        method: 'POST',
 | 
				
			||||||
        headers: { 'Content-Type': 'multipart/form-data'},
 | 
					        headers: { 'Content-Type': 'multipart/form-data'},
 | 
				
			||||||
        body: myForm
 | 
					        body: data
 | 
				
			||||||
    }).then(Response => Response.json()).then(data => {
 | 
					    }).then(Response => Response.json()).then(data => {
 | 
				
			||||||
        console.log(data)
 | 
					        console.log(data)
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
@@ -202,20 +200,6 @@ function upload() {
 | 
				
			|||||||
</script>
 | 
					</script>
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
第二步修改自己的头像路径为返回的图像路径, (!注意此处未作安全检查)
 | 
					 | 
				
			||||||
```javascript
 | 
					 | 
				
			||||||
fetch('/user/ApSXNLoUy', {
 | 
					 | 
				
			||||||
    method: 'PATCH',
 | 
					 | 
				
			||||||
    headers: { 'Content-Type': 'application/json' },
 | 
					 | 
				
			||||||
    body: JSON.stringify({
 | 
					 | 
				
			||||||
        avatar: '/xxxx.jpg'
 | 
					 | 
				
			||||||
    }),
 | 
					 | 
				
			||||||
}).then(Response => Response.json()).then(data => {
 | 
					 | 
				
			||||||
  console.log(data)
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### 删除用户
 | 
					#### 删除用户
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								index.js
									
									
									
									
									
								
							@@ -396,15 +396,16 @@ const uploadavatar = function (req, res) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (!list[0]) return res.status(400).send('未获得图像')
 | 
					    if (!list[0]) return res.status(400).send('未获得图像')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let avatar = '/data/file/' + list[0].newFilename
 | 
				
			||||||
    let query = { _id: req.session.account.uid }
 | 
					    let query = { _id: req.session.account.uid }
 | 
				
			||||||
    let data = {
 | 
					    let data = {
 | 
				
			||||||
      $addToSet: { file: { $each: list } }, // 保存记录
 | 
					      $addToSet: { file: { $each: list } }, // 保存记录
 | 
				
			||||||
      $set: { avatar: '/data/file/' + list[0].newFilename }, // 替换头像
 | 
					      $set: { avatar },                     // 替换头像
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    db('user').update(query, data, (err, count) => {
 | 
					    db('user').update(query, data, (err, count) => {
 | 
				
			||||||
      if (!count) return res.status(500).send('附件挂载对象失败')
 | 
					      if (!count) return res.status(500).send('附件挂载对象失败')
 | 
				
			||||||
      res.json(list[0]) // 返回唯一图像
 | 
					      res.json({ ...list[0], avatar }) // 返回唯一图像
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user