index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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">
  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" :required="item.required">
  48. <up-textarea :placeholder="'请输入' + item.label" v-model="formData[item.key]" count></up-textarea>
  49. </up-form-item>
  50. <up-form-item v-if="item.type === 'face'" :label="item.label" :prop="item.key" borderBottom
  51. labelWidth="94" :required="item.required">
  52. <up-button size="small" type="primary" text="前往人脸识别" @click="startFace"></up-button>
  53. </up-form-item>
  54. </view>
  55. </up-form>
  56. <!-- <up-button type="primary" text="提交" customStyle="margin-top: 50px" @click="onSubmit"></up-button> -->
  57. <up-action-sheet :show="showSex" :actions="actions.option" :title="'请选择' + actions.label"
  58. @close="showSex = false" @select="ActionSheetSelect">
  59. </up-action-sheet>
  60. <up-code ref="uCode" @change="codeChange" seconds="20" @start="code_data.disabled = true"
  61. @end="code_data.disabled = false"></up-code>
  62. </view>
  63. </template>
  64. <script setup>
  65. import { onMounted, reactive, ref } from "vue";
  66. const props = defineProps({
  67. column: {
  68. type: Array,
  69. default: [],
  70. },
  71. });
  72. const formData = reactive({
  73. sex: null,
  74. city: null,
  75. // address: "永川区",
  76. // age: "1",
  77. // city: "重庆",
  78. // code: "1111",
  79. // idCard: "5002302000000000001",
  80. // name: "陈陈",
  81. // phonenumber: "18696601933",
  82. // sex: '男',
  83. // skillDescribe: "测试备注",
  84. });
  85. const form_ref = ref(null);
  86. const showSex = ref(false);
  87. const actions = ref({
  88. option: []
  89. });
  90. const code_data = reactive({
  91. disabled: false,
  92. tips: ''
  93. })
  94. const uCode = ref(null);
  95. function hideKeyboard() {
  96. uni.hideKeyboard()
  97. }
  98. function openActionSheet(row) {
  99. console.log(row);
  100. hideKeyboard();
  101. showSex.value = true;
  102. actions.value = row
  103. }
  104. function ActionSheetSelect(e) {
  105. console.log('e', e);
  106. Object.assign(formData, {
  107. [actions.value.key]: e.key || e.name
  108. })
  109. console.log('formData.value', formData);
  110. // form_ref.value.validateField(actions.key)
  111. }
  112. function getCode() {
  113. if (uCode.value.canGetCode) {
  114. // 模拟向后端请求验证码
  115. uni.showLoading({
  116. title: '正在获取验证码'
  117. })
  118. setTimeout(() => {
  119. uni.hideLoading();
  120. // 这里此提示会被this.start()方法中的提示覆盖
  121. uni.$u.toast('验证码已发送');
  122. // 通知验证码组件内部开始倒计时
  123. uCode.value.start();
  124. }, 2000);
  125. } else {
  126. uni.$u.toast('倒计时结束后再发送');
  127. }
  128. }
  129. function codeChange(text) {
  130. code_data.tips = text;
  131. }
  132. function startFace(){
  133. console.log('startFace',formData);
  134. }
  135. function setData(data) {
  136. Object.assign(formData, data)
  137. }
  138. function onSubmit() {
  139. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  140. return new Promise(async(resolve, reject) => {
  141. const res = await form_ref.value.validate();
  142. if(!res){
  143. reject()
  144. return
  145. }
  146. resolve(formData);
  147. })
  148. }
  149. function onResetField() {
  150. form_ref.value.resetFields()
  151. form_ref.value.clearValidate()
  152. }
  153. onMounted(()=>{
  154. const rules = {};
  155. for (let i = 0; i < props.column.length; i++) {
  156. rules[props.column[i].key] = props.column[i].rules
  157. }
  158. form_ref.value && form_ref.value.setRules(rules)
  159. console.log('uni=====>',uni);
  160. })
  161. defineExpose({
  162. setData,//修改表单数据值
  163. onSubmit,//提交表单,先校验再提交
  164. onResetField,//表单重置
  165. })
  166. </script>
  167. <style lang="scss" scoped>
  168. // .cust-form-item{
  169. // position: relative;
  170. // }
  171. // .required-icon {
  172. // color: red;
  173. // position: absolute;
  174. // }
  175. </style>