edit.vue 9.1 KB

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