GroupDetail.vue 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">保存
        </el-button>
      </sticky>
      <div class="createPost-main-container">
        <el-row :gutter="20">
          <el-card class="box-card">

            <div slot="header" class="clearfix">
              <span>小组相关</span>
            </div>

            <el-row>
              <el-col :span="24">
                <el-form-item style="margin-bottom: 20px;" prop="name">
                  <MDinput v-model="postForm.name" :maxlength="100" name="name" required>
                    小组名称
                  </MDinput>
                </el-form-item>

                <div class="postInfo-container">
                  <el-row>
                    <el-col :span="8">
                      <el-form-item label-width="75px" label="明星称号:" class="postInfo-container-item">
                        <el-select
                          v-model="star"
                          :remote-method="getRemoteStarList"
                          filterable
                          remote
                          multiple
                          clearable
                          reserve-keyword
                          value-key="id"
                          placeholder="搜索明星"
                          style="width: 100%">
                          <el-option v-for="(item,index) in starListOptions" :key="item+index" :label="item"
                                     :value="item"/>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label-width="75px" label="小组组长:" class="postInfo-container-item" prop="user">
                        <el-select v-model="user" :remote-method="getRemoteUserList" filterable remote clearable
                                   value-key="id"
                                   placeholder="搜索用户" style="width: 100%">
                          <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item"
                                     :value="item"/>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="组员数量:" prop="user_nums">
                        <el-input v-model="postForm.user_nums" type="number" placeholder="请输入内容" style="width: 230px;"
                                  disabled/>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="下线小组:">
                        <el-radio-group v-model="postForm.is_online">
                          <el-radio :label="0"></el-radio>
                          <el-radio :label="1"></el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item style="margin-bottom: 20px;" label-width="75px" label="推荐小组:">
                        <el-radio-group v-model="postForm.is_recommend">
                          <el-radio :label="1"></el-radio>
                          <el-radio :label="0"></el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">

                    </el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
            <el-form-item style="margin-bottom: 20px;" label-width="75px" label="小组简介:">
              <el-input :rows="1" v-model="postForm.description" type="textarea" class="article-textarea" autosize
                        placeholder="请输入内容"/>
              <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}</span>
            </el-form-item>


            <div style="margin-bottom: 20px;">
          <el-form-item style="margin-bottom: 40px;" label-width="75px" label="小组头像:" prop="icon" >
            <span v-model="uploadType"></span>
          <Upload v-model="postForm.icon" :uploadType="uploadType" />
            </el-form-item>
        </div>

          </el-card>
        </el-row>

        <el-row :gutter="20" style="margin-top:50px;" >
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>组员相关</span>
            </div>
            <div style="margin-bottom:50px;">
              <div class="filter-container">
                <el-select v-model="temp_user_ids" :remote-method="getRemoteUserList" filterable remote
                         value-key="id"
                         placeholder="用户" style="width: 220px">
                <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item"/>
              </el-select>

                <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="appendUser">添加
                </el-button>
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-delete" @click="delUser">移除
                </el-button>
              </div>
              <el-table :data="data" border fit highlight-current-row style="width: 100%"
                        ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="76" align="center"></el-table-column>
                <el-table-column align="center" label="用户ID ">
                  <template slot-scope="scope">
                    <router-link :to="'/user/edit/'+scope.row.user_id" class="link-type">
                      <span>{{ scope.row.user_id }}</span>
                    </router-link>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="用户名">
                  <template slot-scope="scope">
                    <span>{{ scope.row.nick_name }}</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="联系电话">
                  <template slot-scope="scope">
                    <span>{{ scope.row.phone }}</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="发布帖子数">
                  <template slot-scope="scope">
                    <span>{{ scope.row.topic_nums }}</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="用户身份">
                  <template slot-scope="scope">
                    <span>{{ scope.row.user_identify }}</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="组内身份" v-if="isEdit">
                  <template slot-scope="scope">
                    <span>{{ scope.row.group_identify }}</span>
                  </template>
                </el-table-column>

              </el-table>

              <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                          style="margin-left: 250px;" @pagination="getList"/>
            </div>
          </el-card>
        </el-row>


      </div>
    </el-form>

  </div>
</template>

<script>
  import MDinput from '@/components/MDinput'
  import Upload from '@/components/Upload/singleImage3'
  import Sticky from '@/components/Sticky' // 粘性header组件
  import waves from '@/directive/waves'
  import Pagination from '@/components/Pagination'
  import {isInArray, removeByvale} from "@/utils";

  import {createGroup, GroupDetail, fetchGroupRelateduser, GroupUserDetail} from '@/api/group'
  import {starSearch, userSearch} from '@/api/remoteSearch'


  function Assembledata(target, source) {
    var region_data = []
    console.log(target, source)
    for (var i=0;i<target.length;i++){
      if (isInArray(source, target[i]['name'])){
        region_data.push(target[i]['id'])
        removeByvale(source, target[i]['name']);
      }
    }
    region_data.push(...source)
    return JSON.stringify(region_data)
  }

  const defaultForm = {
    status: 'draft',
    name: '', // 小组名称
    description: '', // 小组简介
    id: undefined,
    user_nums: 0,

    user: '',
    star: [],
    icon: '',
    is_online: 0,
    is_recommend: 0,
    group_users: [],
  }

  export default {
    name: 'GroupDetail',
    components: {MDinput, Sticky, Pagination, Upload},
    directives: {waves},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      const validateRequire = (rule, value, callback) => {
        if (value === '') {
          this.$message({
            message: rule.field + '为必传项',
            type: 'error'
          })
          callback(new Error(rule.field + '为必传项'))
        } else {
          callback()
        }
      }
      return {
        postForm: Object.assign({}, defaultForm),
        loading: false,
        rules: {
          name: [{validator: validateRequire, trigger: 'blur'}],
          icon: [{validator: validateRequire, trigger: 'blur'}],
        },

        user: '',
        star: [],
        temp_user_ids: '',
        group_users: [],
        judge_user_ids: [],

        tempRoute: {},
        uploadType: 98,
        starListOptions: [],
        userListOptions: [],

        // 小组相关
        list: null,
        tableData: [],
        del_list: [],
        total: 0,
        listLoading: true,
        multipleSelection: [],
        listQuery: {
          page: 0,
          limit: 10,
          filter: {
            value: '',
            key: '',
          },
        },
      }
    },
    computed: {
      contentShortLength() {
        return this.postForm.description.length
      },
      data() {
        return this.tableData.filter((d) => {
          let is_del = false;
          for (let i = 0; i < this.del_list.length; i++) {
            if (d.user_id == this.del_list[i].user_id) {
              is_del = true;
              break
            }
          }
          if (!is_del) {
            return d
          }
        })
      }
    },
    created() {
      if (this.isEdit) {
        const id = this.$route.params && this.$route.params.id
        this.fetchData(id)
        this.getList()
      } else {
        this.postForm = Object.assign({}, defaultForm)
      }

      this.tempRoute = Object.assign({}, this.$route)
    },
    methods: {
      fetchData(id) {
        GroupDetail(id).then(response => {
          let rep = response.data.data
          this.postForm = response.data.data

          this.user = rep.creator.id + ':' + rep.creator.name
          for (let i = 0;i < rep.star.length; i++){
            this.star.push(rep.star[i]['id'] + ':' + rep.star[i]['name'])
          }
        }).catch(err => {
          console.log(err)
        })
      },
      submitForm() {
        this.$refs.postForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.user === ''){
              this.$message.error('小组组长必须填')
              this.loading = false;
              return false;
            }
            this.postForm.user = this.user
            this.postForm.star = JSON.stringify(this.star)
            this.postForm.group_users = JSON.stringify(this.group_users);
            createGroup(this.postForm).then(response => {
              this.$notify({
                title: '成功',
                message: response.data.data.message,
                type: 'success',
                duration: 2000
              })
              setTimeout(() => {
                this.$router.push('/group/list')
              }, 1000)

            }).catch(err => {
              this.$notify({
                title: '失败',
                message: '操作失败',
                type: 'danger',
                duration: 2000
              })
            });


            this.postForm.status = 'published'
            this.loading = false
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 添加组员相关
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      appendUser() {
        if (this.temp_user_ids  == ""){
          this.$message.error('还没输入小组呢~~')
          this.temp_user_ids = ''
          return false
        }
        let append_user_id = parseInt(this.temp_user_ids.split(':')[0])
        for (let i = 0;i < this.tableData.length; i++){
          if (append_user_id == this.tableData[i].user_id){
            this.$message.error('存在同样数据, 请勿重复操作')
            this.temp_user_ids = ''
            return false;
          }
        }
        GroupUserDetail(this.temp_user_ids).then(response => {
          if (!response.data.data) return
          if (!response.data.data[0].user_id){
            this.$message.error('这条数据错误, 请换条数据')
            return
          }
          this.judge_user_ids.push(response.data.data[0].user_id)
          this.tableData.push(...response.data.data)
          this.total = this.tableData.length
          this.group_users.push(this.temp_user_ids)
          this.temp_user_ids = ''
        })

      },
      delUser() {
        let origin_user_ids = [];
        var left_user_ids = [];
        let select_user_ids = []

        this.del_list.push(...this.multipleSelection)

        for (let i = 0; i < this.tableData.length; i++){
          origin_user_ids.push(this.tableData[i].user_id)
        }
        for (let i = 0; i < this.multipleSelection.length; i++){
          select_user_ids.push(this.multipleSelection[i].user_id)
        }
        for (let i =0; i < origin_user_ids.length; i++){
          if (!isInArray(select_user_ids ,origin_user_ids[i])){
            left_user_ids.push(origin_user_ids[i])
          }
        }
        this.group_users = left_user_ids;
      },
      getList() {
        this.listLoading = true
         this.listQuery.id = this.$route.params && this.$route.params.id
        fetchGroupRelateduser(this.listQuery).then(response => {
          let rep = response.data.data.data
          this.tableData = response.data.data.data
          this.total = response.data.data.total
          for (let i=0;i<rep.length;i++){
            this.group_users.push(rep[i].user_id + ':' + rep[i].nick_name)
          }

          this.listLoading = false
        })
      },

      // 远程搜索
      getRemoteUserList(query) {
        userSearch(query).then(response => {
          if (!response.data.data.data) return
          this.userListOptions = response.data.data.data
        })
      },
      getRemoteStarList(query) {
        starSearch(query).then(response => {
          if (!response.data.data.data) return
          this.starListOptions = response.data.data.data
        })
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";

  .createPost-container {
    position: relative;
    .createPost-main-container {
      padding: 40px 45px 20px 50px;
      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;
        .postInfo-container-item {
          float: left;
        }
      }
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
          text-align: right;
          margin-right: 10px;
          .editor-upload-btn {
            display: inline-block;
          }
        }
      }
    }
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
  }
</style>