图片新增更新与删除Vue+thinkphp

文章描述:

查询单条信息关联一对多图片信息   $info = $model->field(‘*’) -& […]

查询单条信息关联一对多图片信息

 

$info = $model->field('*')
->with(['photos'])
->where($where)->find();
if (empty($info)) {
      if ($exce) {
            exception('物品不存在:' . $id);
      }
      return [];
}

模型

public function photos(){
    return $this->hasMany(MaterialInfoModel::class,'material_id','id');
}

 

 

 

用户点击删除时弹出确定是否删除,当点击确定删除后改变值状态

// 删除附件
del(index){
      this.$confirm('是否确定删除该文件?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        // 删除成功!

        // this.model.photos.splice(index,1)
        this.model.photos[index].is_delete = 1
        this.$forceUpdate()
      }).catch(() => {
        // 已取消删除
      })

},

 

 

php

单独获取图片然后传过去处理

public function edit()
{
        $param = $this->params(MaterialService::$edit_field);

        validate(MaterialValidate::class)->scene('edit')->check($param);

        $photos = $this->param('photos');
        $data = MaterialService::edit($param['id'], $param, $photos);

        return success($data);
}

 

1、获取需要删除的数据id,进行删除

2、重新获取数据库当前数据相关的所有数据

3、进行处理判断已存在和不存在的

public static function edit($id, $param = [], $photos = [])
{
        // 删除
        $delete_ids = [];
        foreach($photos as $k=>$v)
        {
            if($v['is_delete']==1){
                array_push($delete_ids,$v['id']);
            }
        }
        (new MaterialInfoModel())->where('id','in',$delete_ids)->delete();

        $post_papers_ids = array_column($photos,'id');
        $db_papers_ids = (new MaterialInfoModel())->where('material_id',$param['id'])->column('id');
        $insertAll = [];
        $updateAll = [];

        foreach ($photos as $key=>$val)
        {

            if(!isset($val['id'])){

                // 不存在新增
                $array = array(

                    'material_id' =>isset($val['material_id'])?$val['material_id']:'',
                    'papersname' => isset($val['papersname']) ? $val['papersname']:'',
                    'start_date' => isset($val['start_date']) ? $val['start_date'] :'',
                    'end_date' => isset($val['end_date']) ? $val['end_date'] :'',
                    'img_url' => isset($val['img_url']) ? $val['img_url']:''
                );
                array_push($insertAll,$array);
            }else{

                // 存在处理
                if(in_array($val['id'],$db_papers_ids)){

                    $array = array(
                        'id'=>$val['id'],
                        'material_id' =>isset($val['material_id'])?$val['material_id']:'',
                        'papersname' => isset($val['papersname']) ? $val['papersname']:'',
                        'start_date' => isset($val['start_date']) ? $val['start_date'] :'',
                        'end_date' => isset($val['end_date']) ? $val['end_date'] :'',
                        'img_url' => isset($val['img_url']) ? $val['img_url']:''
                    );
                    array_push($updateAll,$array);
                }else{

                }
            }
        }

        (new MaterialInfoModel())->insertAll($insertAll);
        (new MaterialInfoModel())->saveAll($updateAll);
}

 

 

发布时间:2024/09/23

发表评论