mattersAdd.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. *@description: 新增资讯
  3. *@author: yh Fu
  4. *@date: 2024-03-04 14:45:49
  5. *@version: V1.0.5
  6. */
  7. <template>
  8. <view>
  9. <uni-forms :rules="rules" :value="formData" ref="form" validate-trigger="bind" err-show-type="undertext" class="uniForm">
  10. <uni-group title="基本信息" top="0">
  11. <uni-forms-item name="name" required label="事项标题">
  12. <uni-easyinput type="text" :inputBorder="true" v-model="formData.titleName" placeholder="请输入事项标题"></uni-easyinput>
  13. </uni-forms-item>
  14. <!-- 使用原生input,需要绑定binddata -->
  15. <uni-forms-item name="textDetails" required label="正文">
  16. <uni-easyinput type="textarea" v-model="formData.textDetails" :maxlength="50" @blur="binddata('textDetails', $event.detail.value)" placeholder="请输入正文"></uni-easyinput>
  17. </uni-forms-item>
  18. <uni-forms-item label="电话" required name="phone">
  19. <uni-easyinput v-model="formData.phone" placeholder="请输入电话" />
  20. </uni-forms-item>
  21. <uni-forms-item name="address" required label="地址">
  22. <uni-easyinput type="text" :inputBorder="true" v-model="formData.address" placeholder="请输入地址"></uni-easyinput>
  23. </uni-forms-item>
  24. <uni-forms-item name="checked" label="主题类型" label-width="30%">
  25. <uni-data-select
  26. style="width: 30%;margin: auto;"
  27. v-model="formData.type"
  28. placeholder="请选择主题类型"
  29. field="deptName as value, deptName as text"
  30. :localdata="topicTypeList"
  31. ></uni-data-select>
  32. </uni-forms-item>
  33. <uni-forms-item name="checked" label="所属部门" label-width="30%">
  34. <uni-data-select
  35. style="width: 30%;margin: auto;"
  36. v-model="formData.department"
  37. placeholder="请选择所属部门"
  38. field="name as value, name as text"
  39. :localdata="departmentList"
  40. ></uni-data-select>
  41. </uni-forms-item>
  42. <uni-forms-item name="checked" label="是否常办" label-width="30%">
  43. <switch :checked="formData.isTop" @change="change(' isTop', $event.detail.value)" style="margin-top: 2%;"/>
  44. </uni-forms-item>
  45. <uni-forms-item name="urlList" required label="图片">
  46. <upload :imgArr="imageList" :fileSize="1" :limit="3" @updateImg="updateImg"></upload>
  47. </uni-forms-item>
  48. </uni-group>
  49. <view>
  50. <!-- <button @click="submitForm('form')">校验表单</button>
  51. <button size="mini" @click="validateField('form')">只校验用户名和邮箱项</button>
  52. <button size="mini" @click="clearValidate('form', 'name')">移除用户名的校验结果</button>
  53. <button size="mini" @click="clearValidate('form')">移除全部表单校验结果</button>
  54. <button @click="resetForm">重置表单</button> -->
  55. <button @click="submitForm('form')">新增</button>
  56. </view>
  57. </uni-forms>
  58. </view>
  59. </template>
  60. <script>
  61. import {
  62. getListTopicType,
  63. getListDepts ,
  64. } from '@/api/me/matters/matters.js'
  65. import upload from '@/components/upload/index.vue'
  66. export default {
  67. components: {
  68. upload
  69. },
  70. created(){
  71. getListTopicType().then(e => {
  72. e.rows.forEach(v => {
  73. this.topicTypeList.push({
  74. text:v.deptName,
  75. value:v.id
  76. })
  77. })
  78. })
  79. getListDepts().then(e => {
  80. e.rows.forEach(v => {
  81. this.departmentList.push({
  82. text:v.name,
  83. value:v.name
  84. })
  85. })
  86. })
  87. },
  88. data() {
  89. return {
  90. formData: {
  91. titleName: '',
  92. textDetails: '',
  93. address:'',
  94. phone:'',
  95. email: '',
  96. sex: '0',
  97. hobby: [0, 2],
  98. remarks: '热爱学习,热爱生活',
  99. isGovernment: false,
  100. isTop: false,
  101. country: 2,
  102. weight: 120,
  103. birth: '',
  104. type:'',
  105. department:''
  106. },
  107. imageList:[],
  108. topicTypeList:[],
  109. departmentList:[],
  110. sex: [{
  111. text: '男',
  112. value: '0'
  113. },
  114. {
  115. text: '女',
  116. value: '1'
  117. },
  118. {
  119. text: '未知',
  120. value: '2'
  121. }
  122. ],
  123. hobby: [{
  124. text: '足球',
  125. value: 0
  126. },
  127. {
  128. text: '篮球',
  129. value: 1
  130. },
  131. {
  132. text: '游泳',
  133. value: 2
  134. }
  135. ],
  136. range: ['中国', '美国', '澳大利亚'],
  137. show: false,
  138. rules: {
  139. titleName: {
  140. rules: [{
  141. required: true,
  142. errorMessage: '请输入标题'
  143. },
  144. {
  145. minLength: 1,
  146. errorMessage: '标题不得为空'
  147. }
  148. ]
  149. },
  150. textDetails: {
  151. rules: [{
  152. required: true,
  153. errorMessage: '请输入正文'
  154. },
  155. {
  156. minLength: 10,
  157. errorMessage: '正文不得少于 {minLength} 字'
  158. }
  159. ]
  160. },
  161. weight: {
  162. rules: [{
  163. format: 'number',
  164. errorMessage: '体重必须是数字'
  165. },
  166. {
  167. minimum: 100,
  168. maximum: 200,
  169. errorMessage: '体重应该大于 {minimum} 斤,小于 {maximum} 斤'
  170. }
  171. ]
  172. },
  173. birth: {
  174. rules: [
  175. {
  176. required: true,
  177. errorMessage: '请选择时间'
  178. },
  179. {
  180. format: 'timestamp',
  181. errorMessage: '必须是时间戳'
  182. }
  183. ]
  184. },
  185. email: {
  186. rules: [{
  187. format: 'email',
  188. errorMessage: '请输入正确的邮箱地址'
  189. }]
  190. },
  191. isGovernment: {
  192. rules: [{
  193. format: 'bool'
  194. }]
  195. },
  196. isTop: {
  197. rules: [{
  198. format: 'bool'
  199. }]
  200. },
  201. sex: {
  202. rules: [{
  203. format: 'string'
  204. }]
  205. },
  206. hobby: {
  207. rules: [{
  208. format: 'array'
  209. },
  210. {
  211. validateFunction: function(rule, value, data, callback) {
  212. if (value.length < 2) {
  213. callback('请至少勾选两个兴趣爱好')
  214. }
  215. return true
  216. }
  217. }
  218. ]
  219. }
  220. }
  221. }
  222. },
  223. methods: {
  224. change(name, value) {
  225. this.formData.checked = value
  226. this.$refs.form.setValue(name, value)
  227. },
  228. submitForm(form) {
  229. this.$refs[form]
  230. .submit()
  231. .then(res => {
  232. console.log('表单的值:', res)
  233. uni.showToast({
  234. title: '验证成功'
  235. })
  236. })
  237. .catch(errors => {
  238. console.error('验证失败:', errors)
  239. })
  240. },
  241. //重置表单 。原生的组件input组件不能重置表单
  242. resetForm() {
  243. this.$refs.form.resetFields()
  244. },
  245. validateField(form) {
  246. this.$refs[form]
  247. .validateField(['name', 'email'])
  248. .then(res => {
  249. uni.showToast({
  250. title: '验证成功'
  251. })
  252. console.log('表单的值:', res)
  253. })
  254. .catch(errors => {
  255. console.error('验证失败:', errors)
  256. })
  257. },
  258. updateImg(imgList){
  259. this.imageList = imgList;
  260. },
  261. clearValidate(form, name) {
  262. if (!name) name = []
  263. this.$refs[form].clearValidate(name)
  264. }
  265. }
  266. }
  267. </script>
  268. <style lang="scss">
  269. .uniForm{
  270. .uni-forms-item{
  271. margin: auto;
  272. .uni-forms-item__content{
  273. margin: auto;
  274. }
  275. }
  276. }
  277. .radioView{
  278. margin: auto;
  279. .uni-forms-item__content{
  280. margin: auto;
  281. }
  282. }
  283. .uni-input-border {
  284. padding: 0 10px;
  285. height: 35px;
  286. width: 100%;
  287. font-size: 14px;
  288. color: #666;
  289. border: 1px #e5e5e5 solid;
  290. border-radius: 5px;
  291. box-sizing: border-box;
  292. }
  293. </style>