index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="cust-form">
  3. <up-form labelPosition="left" :model="formData" ref="form_ref">
  4. <view v-for="item in column" :key="item.key + 'cust-form'" class="cust-form-item">
  5. <up-form-item v-if="item.type === 'input'" :label="item.label" :prop="item.key" borderBottom
  6. labelWidth="94" :required="item.required">
  7. <up-input v-model="formData[item.key]" border="none" :placeholder="'请输入' + item.label"></up-input>
  8. </up-form-item>
  9. <up-form-item v-if="item.type === 'select'" :label="item.label" :prop="item.key" borderBottom
  10. labelWidth="94" :required="item.required">
  11. <view @click.self="() => openActionSheet(item)">
  12. <up-input v-model="formData[item.key]" disabled disabledColor="#ffffff"
  13. :placeholder="'请选择' + item.label" border="none"></up-input>
  14. </view>
  15. <template #right>
  16. <up-icon name="arrow-right"></up-icon>
  17. </template>
  18. </up-form-item>
  19. <view v-if="item.type === 'phone-code'">
  20. <up-form-item label="手机号" prop="phonenumber" borderBottom labelWidth="94" :rules="[
  21. {
  22. type: 'string',
  23. required: true,
  24. len: 11,
  25. message: '请填写11位手机号',
  26. trigger: ['blur']
  27. }
  28. ]" :required="true">
  29. <up-input v-model="formData.phonenumber" border="none" placeholder="请输入手机号"></up-input>
  30. </up-form-item>
  31. <up-form-item label="验证码" prop="code" labelWidth="94" borderBottom :rules="[
  32. {
  33. type: 'string',
  34. required: true,
  35. len: 4,
  36. message: '请填写4位验证码',
  37. trigger: ['blur']
  38. }
  39. ]" :required="true" v-if="isCode">
  40. <up-input v-model="formData.code" border="none" placeholder="请填写验证码"></up-input>
  41. <template #right>
  42. <up-button @tap="getCode" :text="code_data.tips" type="success" size="mini"
  43. style="flex: 0.5;" :disabled="code_data.disabled"></up-button>
  44. </template>
  45. </up-form-item>
  46. </view>
  47. <up-form-item v-if="item.type === 'textarea'" :label="item.label" :prop="item.key" labelWidth="94"
  48. :required="item.required">
  49. <up-textarea :placeholder="'请输入' + item.label" v-model="formData[item.key]" count></up-textarea>
  50. </up-form-item>
  51. <up-form-item v-if="item.type === 'face'" :label="item.label" :prop="item.key" borderBottom
  52. labelWidth="94" :required="item.required">
  53. <up-button size="small" type="primary" text="前往人脸识别" @click="startFace"></up-button>
  54. </up-form-item>
  55. <up-form-item v-if="item.type === 'city'" :label="item.label" :prop="item.key" borderBottom
  56. labelWidth="94" :required="item.required">
  57. <view @click.self="() => openCity(item)">
  58. <up-input v-model="formData[item.key]" disabled disabledColor="#ffffff"
  59. :placeholder="'请选择' + item.label" border="none"></up-input>
  60. </view>
  61. <template #right>
  62. <up-icon name="arrow-right"></up-icon>
  63. </template>
  64. </up-form-item>
  65. <up-form-item v-if="item.type === 'cascader'" :label="item.label" :prop="item.key" borderBottom
  66. labelWidth="94" :required="item.required">
  67. <view @click.self="() => clickPick(item)">
  68. <up-input v-model="formData[item.key]" disabled disabledColor="#ffffff"
  69. :placeholder="'请选择' + item.label" border="none"></up-input>
  70. </view>
  71. <template #right>
  72. <up-icon name="arrow-right"></up-icon>
  73. </template>
  74. </up-form-item>
  75. </view>
  76. </up-form>
  77. <!-- <up-button type="primary" text="提交" customStyle="margin-top: 50px" @click="onSubmit"></up-button> -->
  78. <up-action-sheet :show="showSex" :actions="actions.option" :title="'请选择' + actions.label"
  79. @close="showSex = false" @select="ActionSheetSelect">
  80. </up-action-sheet>
  81. <up-code ref="uCode" @change="codeChange" seconds="20" @start="code_data.disabled = true"
  82. @end="code_data.disabled = false"></up-code>
  83. <!-- 省市区选择 province city area初始省市区设置 show:是否显示 @changeClick:更改省市区事件 @sureSelectArea:确认事件 @hideShow:隐藏事件-->
  84. <CcSelectDity :province="cityData.province" :city="cityData.city" :area="cityData.area" :show="cityShow"
  85. @sureSelectArea="onsetCity" @hideShow="onCityShow"></CcSelectDity>
  86. <Picker :columnData="pickerData" ref="pickerRef" @submit="ActionSheetSelect" />
  87. </view>
  88. </template>
  89. <script setup>
  90. import { onMounted, reactive, ref } from "vue";
  91. import CcSelectDity from '@/components/cc-selectDity/cc-selectDity.vue';
  92. import Picker from '@/components/picker/index.vue'
  93. const props = defineProps({
  94. column: {
  95. type: Array,
  96. default: [],
  97. },
  98. isCode: {
  99. type: Boolean,
  100. default: true
  101. }
  102. });
  103. const formData = reactive({
  104. sex: null,
  105. city: null,
  106. // address: "永川区",
  107. // age: "1",
  108. // city: "重庆",
  109. // code: "1111",
  110. // idCard: "5002302000000000001",
  111. // name: "陈陈",
  112. // phonenumber: "18696601933",
  113. // sex: '男',
  114. // skillDescribe: "测试备注",
  115. });
  116. const form_ref = ref(null);
  117. const showSex = ref(false);
  118. const actions = ref({
  119. option: []
  120. });
  121. const pickerData = ref([
  122. {
  123. businessName: '家庭教育',
  124. id: '1',
  125. children: [
  126. {
  127. businessName: '语文',
  128. id: '2',
  129. children: [
  130. {
  131. businessName: '一年级',
  132. id: '4',
  133. },
  134. {
  135. businessName: '二年级',
  136. id: '5',
  137. }
  138. ]
  139. },
  140. {
  141. businessName: '数学',
  142. id: '3',
  143. }
  144. ]
  145. },
  146. {
  147. businessName: '家庭清洁',
  148. id: '6',
  149. children: [
  150. {
  151. businessName: '清洁1',
  152. id: '7',
  153. children: [
  154. {
  155. businessName: '清洁11',
  156. id: '8',
  157. },
  158. {
  159. businessName: '清洁22',
  160. id: '9',
  161. }
  162. ]
  163. },
  164. {
  165. businessName: '清洁3',
  166. id: '10',
  167. }
  168. ]
  169. }
  170. ])
  171. const pickerRef = ref(null)
  172. const cityData = reactive({
  173. province: "广东省",
  174. city: "广州市",
  175. area: "天河区",
  176. })
  177. const cityShow = ref(false)
  178. const code_data = reactive({
  179. disabled: false,
  180. tips: ''
  181. })
  182. const uCode = ref(null);
  183. function hideKeyboard() {
  184. uni.hideKeyboard()
  185. }
  186. function openActionSheet(row) {
  187. console.log(row);
  188. hideKeyboard();
  189. showSex.value = true;
  190. actions.value = row
  191. }
  192. function onCityShow() {
  193. console.log('onCityShow');
  194. cityShow.value = false;
  195. }
  196. function openCity(row) {
  197. console.log(row);
  198. cityShow.value = true
  199. actions.value = row
  200. }
  201. function onsetCity(e) {
  202. console.log('onsetCity', e);
  203. cityShow.value = false
  204. Object.assign(formData, {
  205. [actions.value.key]: e.address
  206. })
  207. console.log('formData', formData);
  208. }
  209. function ActionSheetSelect(e) {
  210. console.log('e', e);
  211. Object.assign(formData, {
  212. [actions.value.key]: e.key || e.name
  213. })
  214. if(actions.value.type === 'cascader'){
  215. Object.assign(formData, {
  216. [actions.value.key + 'key' ]: e.value
  217. })
  218. }
  219. console.log('formData.value', formData);
  220. // form_ref.value.validateField(actions.key)
  221. }
  222. function getCode() {
  223. if (uCode.value.canGetCode) {
  224. // 模拟向后端请求验证码
  225. uni.showLoading({
  226. title: '正在获取验证码'
  227. })
  228. setTimeout(() => {
  229. uni.hideLoading();
  230. // 这里此提示会被this.start()方法中的提示覆盖
  231. uni.$u.toast('验证码已发送');
  232. // 通知验证码组件内部开始倒计时
  233. uCode.value.start();
  234. }, 2000);
  235. } else {
  236. uni.$u.toast('倒计时结束后再发送');
  237. }
  238. }
  239. function codeChange(text) {
  240. code_data.tips = text;
  241. }
  242. function startFace() {
  243. console.log('startFace', formData);
  244. }
  245. function setData(data) {
  246. console.log('setData=>data', data);
  247. for (let i = 0; i < props.column.length; i++) {
  248. const element = props.column[i];
  249. if(element.type === 'cascader') {
  250. const key = "5"
  251. const lable = pickerRef.value.piceInit(key);
  252. console.log('element.type',lable,key);
  253. Object.assign(formData, {
  254. [element.key]: lable,
  255. [element.key+'key']: key
  256. })
  257. }
  258. }
  259. Object.assign(formData, {
  260. ...data,
  261. sex: data.sex === 0 ? '男' : '女',
  262. })
  263. }
  264. function onSubmit() {
  265. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  266. return new Promise(async (resolve, reject) => {
  267. const res = await form_ref.value.validate();
  268. if (!res) {
  269. reject()
  270. return
  271. }
  272. resolve(formData);
  273. })
  274. }
  275. function onResetField() {
  276. form_ref.value.resetFields()
  277. form_ref.value.clearValidate()
  278. }
  279. function clickPick(row) {
  280. console.log('e', row);
  281. actions.value = row
  282. console.log('e', actions.value);
  283. pickerRef.value.show();
  284. }
  285. onMounted(() => {
  286. const rules = {};
  287. for (let i = 0; i < props.column.length; i++) {
  288. rules[props.column[i].key] = props.column[i].rules
  289. }
  290. form_ref.value && form_ref.value.setRules(rules)
  291. console.log('uni=====>', uni);
  292. })
  293. defineExpose({
  294. setData,//修改表单数据值
  295. onSubmit,//提交表单,先校验再提交
  296. onResetField,//表单重置
  297. })
  298. </script>
  299. <style lang="scss" scoped>
  300. // .cust-form-item{
  301. // position: relative;
  302. // }
  303. // .required-icon {
  304. // color: red;
  305. // position: absolute;
  306. // }</style>