edit.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="container main">
  3. <view class="one">
  4. <view class="list" v-for="(item, index) in investigate" :key="index">
  5. <div class="problem">
  6. <text v-if="item.is_must=='0'" class="t-icon t-icon-xinghao"></text>
  7. <span v-if="item.is_must=='0'">{{item.problem}}</span>
  8. <span style="margin: 0 0 0 2vw;" v-else>{{item.problem}}</span>
  9. </div>
  10. <div class="type" v-if="item.type =='0'">
  11. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  12. <uni-data-checkbox v-model="item.reply" :localdata="item.answer"
  13. :map="{text:'text',value:'text'}" />
  14. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  15. </div>
  16. <div class="type" v-if="item.type =='1'">
  17. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  18. <uni-data-checkbox v-model="item.reply" multiple :localdata="item.answer"
  19. :map="{text:'text',value:'text'}" />
  20. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  21. </div>
  22. <div class="type" v-if="item.type =='2'">
  23. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  24. <uni-data-select v-model="item.reply" :localdata="item.answer"></uni-data-select>
  25. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  26. </div>
  27. <div class="type" v-if="item.type =='3'">
  28. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  29. <uni-easyinput v-model="item.reply" type="number"
  30. :placeholder="getField(item.problem)"></uni-easyinput>
  31. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  32. </div>
  33. <div class="type" v-if="item.type =='4'">
  34. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  35. <uni-easyinput v-model="item.reply" :placeholder="getField(item.problem)"></uni-easyinput>
  36. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  37. </div>
  38. <div class="type" v-if="item.type =='5'">
  39. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  40. <uni-easyinput type="textarea" v-model="item.reply"
  41. :placeholder="getField(item.problem)"></uni-easyinput>
  42. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  43. </div>
  44. <div class="type" v-if="item.type =='6'">
  45. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  46. <upload class='upload' :list="item.reply" name="reply" :count="1" @uplSuc="uplSuc($event,item)"
  47. @uplDel="uplDel($event,item)">
  48. </upload>
  49. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  50. </div>
  51. <div class="type" v-if="item.type =='7'">
  52. <div class="list">
  53. <div v-for="(aa, ina) in item.answer" :key="ina" class="name">{{aa.text}}</div>
  54. </div>
  55. <div class="list" v-for="(gg, inx) in item.reply" :key="inx">
  56. <div v-for="(aa, ina) in item.answer" :key="ina" class="input">
  57. <uni-easyinput v-model="gg[aa.text]" :clearable="false"
  58. :placeholder="getField(aa.text)"></uni-easyinput>
  59. </div>
  60. <uni-icons @tap="toDel(item,gg.sid)" class="icon" type="closeempty" size="20"></uni-icons>
  61. </div>
  62. <div class="add" @tap="toAdd(item)">+新增一行</div>
  63. <div v-if="errors[item.problem]" class="error-message">{{ errors[item.problem] }}</div>
  64. </div>
  65. <div class="type" v-if="item.type =='8'">
  66. <image class="image" v-if="item.answer&&item.answer.length>0" v-for="(as, img) in item.answer"
  67. :key="img" :src="getUrl(as.url)"></image>
  68. <div class="remark" v-if="item.remark">{{item.remark}}</div>
  69. </div>
  70. </view>
  71. <view class="button">
  72. <button type="warn" @tap="toSave">保存</button>
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. import upload from '../../components/upload/index.vue';
  79. import moment from 'moment';
  80. export default {
  81. components: {
  82. upload
  83. },
  84. data() {
  85. return {
  86. id: '',
  87. user: {},
  88. info: {},
  89. investigate: [],
  90. errors: {}
  91. }
  92. },
  93. onLoad: async function(e) {
  94. const that = this;
  95. that.$set(that, `id`, e && e.id || '');
  96. await that.searchToken();
  97. await that.search();
  98. },
  99. methods: {
  100. // 用户信息
  101. searchToken() {
  102. const that = this;
  103. try {
  104. const res = uni.getStorageSync('token');
  105. if (res) {
  106. const user = that.$jwt(res);
  107. that.$set(that, `user`, user);
  108. }
  109. } catch (e) {}
  110. },
  111. // 查询
  112. async search() {
  113. const that = this;
  114. if (that.id) {
  115. let res;
  116. res = await that.$api(`/matchReg/${that.id}`, 'GET', {})
  117. if (res.errcode == '0') {
  118. that.$set(that, `info`, res.data)
  119. if (res.data.info && res.data.info.length > 0) {
  120. for (let val of res.data.info) {
  121. if (val.type == '2') {
  122. for (let s of val.answer) {
  123. s.value = s.text
  124. }
  125. }
  126. }
  127. that.$set(that, `investigate`, res.data.info)
  128. }
  129. } else {
  130. uni.showToast({
  131. title: res.errmsg,
  132. icon: 'none'
  133. });
  134. }
  135. }
  136. },
  137. getField(data) {
  138. let res = "请输入内容"
  139. if (data) res = `请输入${data}`
  140. return res
  141. },
  142. // 图片处理
  143. getUrl(e) {
  144. const that = this;
  145. if (e) return that.$config.serverFile + e
  146. },
  147. // 图片上传
  148. uplSuc(e, item) {
  149. const that = this;
  150. for (let val of that.investigate) {
  151. if (val.sid == item.sid) val.reply = [e.data]
  152. }
  153. },
  154. // 图片删除
  155. uplDel(e, item) {
  156. const that = this;
  157. for (let val of that.investigate) {
  158. if (val.sid == item.sid) val.reply = []
  159. }
  160. },
  161. // 新增一行
  162. toAdd(item) {
  163. const that = this;
  164. for (let val of that.investigate) {
  165. if (val.sid == item.sid) {
  166. let answer = []
  167. let obj = {
  168. sid: moment().valueOf(),
  169. }
  170. for (let s of val.answer) {
  171. obj[s.text] = '';
  172. }
  173. answer.push(obj)
  174. if (val.reply) val.reply = [...val.reply, ...answer, ]
  175. else val.reply = answer
  176. }
  177. }
  178. },
  179. // 删除一行
  180. toDel(item, sid) {
  181. const that = this;
  182. if (item && item.reply && item.reply.length > 0) {
  183. let answer = item.reply.filter((i) => i.sid != sid)
  184. for (let val of that.investigate) {
  185. if (val.sid == item.sid) {
  186. val.reply = answer
  187. }
  188. }
  189. }
  190. },
  191. // 自定义的验证函数
  192. validateObject(arr) {
  193. const errors = {};
  194. for (const obj of arr) {
  195. if (obj.is_must == '0') {
  196. if (!obj.reply || obj.reply.length == 0) {
  197. if (obj.type == '0' || obj.type == '1' || obj.type == '2') errors[obj.problem] =
  198. `请选择${obj.problem}`
  199. else if (obj.type == '6') errors[obj.problem] = `请上传${obj.problem}`
  200. else errors[obj.problem] = `请填写${obj.problem}`
  201. }
  202. }
  203. }
  204. // 如果有错误,返回错误对象
  205. if (Object.keys(errors).length > 0) {
  206. return errors
  207. }
  208. // 如果没有错误,返回null或undefined
  209. return null
  210. },
  211. async toSave() {
  212. const that = this;
  213. const errorsInfo = await that.validateObject(that.investigate)
  214. // 检查是否有错误
  215. if (errorsInfo) {
  216. that.$set(that, `errors`, errorsInfo)
  217. // 遍历错误对象并显示错误信息
  218. for (const key in errorsInfo) {
  219. if (errorsInfo.hasOwnProperty(key)) {
  220. console.error(`${key} 错误: ${errorsInfo[key]}`);
  221. }
  222. }
  223. } else {
  224. that.$set(that, `errors`, {})
  225. let data = {
  226. id: that.id,
  227. status: '0',
  228. info: that.investigate
  229. }
  230. const res = await that.$api(`/matchReg/${that.id}`, 'POST', data)
  231. if (res.errcode == '0') {
  232. uni.showModal({
  233. content: "修改报名信息成功!",
  234. showCancel: false
  235. });
  236. uni.navigateBack({
  237. delta: 1
  238. })
  239. } else {
  240. uni.showToast({
  241. title: res.errmsg,
  242. icon: 'none'
  243. });
  244. }
  245. }
  246. },
  247. }
  248. }
  249. </script>
  250. <style lang="scss" scoped>
  251. .main {
  252. .one {
  253. padding: 2vw;
  254. .list {
  255. .problem {
  256. display: flex;
  257. align-items: center;
  258. .t-icon {
  259. width: 30px !important;
  260. height: 30px !important;
  261. }
  262. }
  263. .type {
  264. padding: 2vw 0 2vw 2vw;
  265. .list {
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. margin: 2vw 0;
  270. .name {
  271. width: 50%;
  272. text-align: center;
  273. }
  274. .input {
  275. margin: 0 1vw 0 0;
  276. .name {
  277. width: 50%;
  278. text-align: center;
  279. margin: 0 0 2vw 0;
  280. }
  281. }
  282. .icon {
  283. display: none;
  284. margin: 1vw 0 0 0;
  285. }
  286. }
  287. .list:hover {
  288. .icon {
  289. display: block;
  290. }
  291. }
  292. .add {
  293. border: 1px solid #e5e5e5;
  294. border-radius: 5px;
  295. text-align: center;
  296. padding: 2vw;
  297. }
  298. .image {
  299. width: 96%;
  300. }
  301. .error-message {
  302. text-align: left;
  303. margin: 5px 0 0 8px;
  304. color: var(--fF0Color);
  305. font-size: var(--font12Size);
  306. }
  307. .remark {
  308. margin: 2vw;
  309. font-size: 12px;
  310. color: red;
  311. }
  312. }
  313. }
  314. .button {
  315. margin: 2vw 0 0 0;
  316. padding: 2vw;
  317. text-align: center;
  318. button {
  319. background-color: var(--f3CColor);
  320. font-size: var(--font14Size);
  321. border-radius: 2vw;
  322. }
  323. }
  324. }
  325. }
  326. </style>