Newer
Older
KaiFengWechat / src / views / questionnaire.vue
@鲁yixuan 鲁yixuan on 23 Jul 6 KB updata
<template>
  <div class="questionnaire">
    <div class="top">
      <div>
        <img class="ImgCLass" src="../assets/images/wjtc.png" alt="" />
      </div>
      <div class="below">
        <p>
          您好!今年是开封市创建海绵城市建设示范城市的第二年,为进一步了解群众对孝感市海绵城市建设的看法,我们正在进行项关于海绵城市建设群众满意度的调查,希望能够占用您三分钟的时间,填写问卷,您的看法对我们非常重要,数据资料我们将严格保密,结果将作为海绵城市建设绩效评价的参考,希望能够得到您的支持和配合,非常感谢!
        </p>
      </div>
    </div>
    <div class="under">
      <van-form @submit="onSubmitData">
        <div class="ContentView" id="ContentViewRiver">
          <van-cell-group inset>
            <van-field
              v-model="AllData.formData.name"
              label="您的姓名"
              placeholder="请输入您的姓名"
              required
              :rules="[{ required: true, message: '请输入您的姓名' }]"
              style="display: block"
            />
            <van-field
              v-model="AllData.formData.telephone"
              label="您的手机号"
              placeholder="请输入您的手机号"
              required
              :rules="[{ required: true, message: '请输入您的手机号' }]"
              style="display: block"
            />

            <div v-for="(item, index) in AllData.problemConfigListA" :key="index">
              <!-- 单选 -->
              <van-field :label="index + 1 + ',' + item.problemName" v-if="item.problemType == 'radio'">
                <template #input>
                  <van-radio-group v-model="AllData.formData.problemResultList[index].answersResult">
                    <van-radio v-for="(item2, index2) in item.problemOptionList" :name="item2.id" :key="item2.id">
                      {{ item2.optionName }}
                    </van-radio>
                  </van-radio-group>
                </template>
              </van-field>

              <!--  multiple 多选 -->
              <!-- <van-field :label="index + 1 + ',' + item.problemName" v-if="item.problemType == 'checkbox'">
                <template #input>
                  <van-radio-group v-model="AllData.formData.problemResultList[index].answersResult">
                    <van-radio v-for="(item2, index2) in item.problemOptionList" :name="item2.id" :key="item2.id">
                      {{ item2.optionName }}
                    </van-radio>
                  </van-radio-group>
                </template>
              </van-field> -->
              <!-- 简答题 -->
              <van-field
                v-model="AllData.formData.problemResultList[index].answersResult"
                rows="2"
                autosize
                :label="AllData.problemConfigListA.length + ',' + item.problemName"
                type="textarea"
                maxlength="200"
                placeholder="请输入意见和建议"
                show-word-limit
                v-if="item.problemType == 'answers'"
              />
            </div>
          </van-cell-group>
        </div>
        <div class="BottomView">
          <van-button native-type="submit" class="BotBtn" type="primary"> 提交调查问卷 </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup name="questionnaire">
import { executingQuestionnaireList, publicServiceAdd } from '@/api/PublicAdvice.js';
const { proxy } = getCurrentInstance();
const AllData = reactive({
  problemConfigListA: [],
  formData: {
    name: '',
    telephone: '',
    problemResultList: [],
  },

  formOptions: {},
});

// 提交上报
const onSubmitData = async () => {
  let pattrens = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
  if (!pattrens.test(AllData.formData.telephone)) {
    proxy.showFailToast('手机号格式不正确');
  }
  // console.log(AllData.formData.problemResultList, 'AllData.formData');
  AllData.formData.problemResultList.forEach((item) => {
    // console.log(item, 'element.problemType');
    if (item.problemType == 'radio') {
      console.log('radio');
      item.problemOptionList = [];
      item.problemOptionList.push({
        optionId: item.answersResult,
      });
    }
  });
  let params = JSON.parse(JSON.stringify(AllData.formData));
  // console.log(params, 'params---');

  // 多选转换格式为string,方便后端接收

  console.log(params, 'params');
  // params.problemResultList.map((item) => {
  //   if (item.problemType == 'checkbox') {
  //     item.answersResult = item.answersResult.join(',');
  //   }
  // });
  publicServiceAdd(params).then((response) => {
    proxy.showSuccessToast('填报成功');
    AllData.formData.name = '';
    AllData.formData.telephone = '';
    AllData.formData.problemResultList = [];
    getData();
  });
};

// 获取数据
const getData = async () => {
  let res = await executingQuestionnaireList();
  AllData.problemConfigListA = res.data.problemConfigList;
  AllData.formData.questionnaireId = res.data.id;
  res.data.problemConfigList.map((item) => {
    AllData.formData.problemResultList.push({
      answersResult: item.problemType == 'checkbox' ? [] : '',
      problemType: item.problemType,
      problemId: item.id,
    });
  });
};
getData();
</script>

<style lang="less" scoped>
.questionnaire {
  height: 100%;
  overflow: auto;
  background: #f1f1f1;

  .top {
    width: 100%;
    // background: red;
    .ImgCLass {
      margin: auto;
      display: block;
      width: 667px;
      height: 120px;
      position: relative;
      top: 12px;
    }
    .below {
      margin-top: 15px;
      width: 667px;
      margin-left: auto;
      margin-right: auto;
      // background: yellow;
      p {
        text-indent: 2em;
        padding: 0px;
        margin: 0px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        font-size: 25px;
        color: #121212;
        line-height: 42px;
      }
    }
  }
  .under {
    width: 100%;
    margin-top: 10px;
    .BottomView {
      width: 100%;
      height: 120px;
      display: flex;
      justify-content: space-around;
      margin-bottom: 100px;
      // background: red;
      .BotBtn {
        margin-top: 20px;
        width: 45%;
      }
    }
  }
}
:deep(.van-field__label) {
  flex: none;
  box-sizing: border-box;
  width: var(--van-field-label-width);
  margin-right: var(--van-field-label-margin-right);
  color: var(--van-field-label-color);
  text-align: left;
  word-wrap: break-word;
  width: 100%;
  font-family: Source Han Sans CN;
  font-weight: 500;
  font-size: 30px;
  color: #121212;
  font-weight: bold;
  // background: red;
}
</style>