index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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="options[actions.optionKey]" 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. import { sendCode } from '@/api/sm.js';
  94. const props = defineProps({
  95. column: {
  96. type: Array,
  97. default: [],
  98. },
  99. isCode: {
  100. type: Boolean,
  101. default: true
  102. }
  103. });
  104. const formData = reactive({
  105. sex: null,
  106. city: null,
  107. // address: "永川区",
  108. // age: "1",
  109. // city: "重庆",
  110. // code: "1111",
  111. // idCard: "5002302000000000001",
  112. // name: "陈陈",
  113. // phonenumber: "18696601933",
  114. // sex: '男',
  115. // skillDescribe: "测试备注",
  116. });
  117. const form_ref = ref(null);
  118. const showSex = ref(false);
  119. const actions = ref({
  120. option: [],
  121. optionKey: 'businessManagementOption'
  122. });
  123. const pickerRef = ref(null)
  124. const options = reactive({
  125. businessManagementOption: []
  126. })
  127. const cityData = reactive({
  128. province: "重庆",
  129. city: "重庆市",
  130. area: "永川区",
  131. })
  132. const cityShow = ref(false)
  133. const code_data = reactive({
  134. disabled: false,
  135. tips: ''
  136. })
  137. const uCode = ref(null);
  138. function hideKeyboard() {
  139. uni.hideKeyboard()
  140. }
  141. function openActionSheet(row) {
  142. console.log(row);
  143. hideKeyboard();
  144. showSex.value = true;
  145. actions.value = row
  146. }
  147. function onCityShow() {
  148. console.log('onCityShow');
  149. cityShow.value = false;
  150. }
  151. function openCity(row) {
  152. console.log(row);
  153. cityShow.value = true
  154. actions.value = row
  155. }
  156. function onsetCity(e) {
  157. console.log('onsetCity', e);
  158. cityShow.value = false
  159. Object.assign(formData, {
  160. [actions.value.key]: e.address
  161. })
  162. console.log('formData', formData);
  163. }
  164. function ActionSheetSelect(e) {
  165. console.log('e', e);
  166. Object.assign(formData, {
  167. [actions.value.key]: e.key || e.name
  168. })
  169. if (actions.value.type === 'cascader') {
  170. Object.assign(formData, {
  171. [actions.value.key + 'key']: e.value
  172. })
  173. }
  174. console.log('formData.value', formData);
  175. // form_ref.value.validateField(actions.key)
  176. }
  177. async function getCode() {
  178. if (uCode.value.canGetCode) {
  179. // 模拟向后端请求验证码
  180. if (formData.phonenumber) {
  181. uni.showLoading({
  182. title: '正在获取验证码'
  183. })
  184. const res = await sendCode(formData.phonenumber);
  185. console.log(res);
  186. if (res.code == 200) {
  187. uni.hideLoading();
  188. uCode.value.start();
  189. uni.$u.toast('验证码已发送');
  190. }
  191. }else{
  192. uni.$u.toast('请输入手机号');
  193. }
  194. } else {
  195. uni.$u.toast('倒计时结束后再发送');
  196. }
  197. }
  198. function codeChange(text) {
  199. code_data.tips = text;
  200. }
  201. function startFace() {
  202. console.log('startFace', formData);
  203. }
  204. function setData(data) {
  205. console.log('setData=>data', data);
  206. for (let i = 0; i < props.column.length; i++) {
  207. const element = props.column[i];
  208. if (element.type === 'cascader') {
  209. const key = data[element.key]
  210. // const key ='7'
  211. const lable = pickerRef.value.piceInit(key);
  212. console.log('element.type', lable, key);
  213. Object.assign(formData, {
  214. [element.key]: lable,
  215. [element.key + 'key']: key
  216. })
  217. }
  218. }
  219. Object.assign(formData, {
  220. ...data,
  221. sex: data.sex === 0 ? '男' : '女',
  222. })
  223. }
  224. function onSubmit() {
  225. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  226. return new Promise(async (resolve, reject) => {
  227. const res = await form_ref.value.validate();
  228. if (!res) {
  229. reject()
  230. return
  231. }
  232. resolve(formData);
  233. })
  234. }
  235. function onResetField() {
  236. form_ref.value.resetFields()
  237. form_ref.value.clearValidate()
  238. }
  239. function clickPick(row) {
  240. console.log('e', row);
  241. actions.value = row
  242. console.log('e', actions.value);
  243. pickerRef.value.show();
  244. }
  245. onMounted(async () => {
  246. const rules = {};
  247. for (let i = 0; i < props.column.length; i++) {
  248. rules[props.column[i].key] = props.column[i].rules;
  249. if (props.column[i].type === 'cascader') {
  250. const res = await props.column[i].apifun();
  251. Object.assign(options, {
  252. [props.column[i].optionKey]: res
  253. })
  254. console.log('onMounted9999', options);
  255. }
  256. }
  257. form_ref.value && form_ref.value.setRules(rules)
  258. console.log('uni=====>', uni);
  259. })
  260. defineExpose({
  261. setData,//修改表单数据值
  262. onSubmit,//提交表单,先校验再提交
  263. onResetField,//表单重置
  264. })
  265. </script>
  266. <style lang="scss" scoped>
  267. // .cust-form-item{
  268. // position: relative;
  269. // }
  270. // .required-icon {
  271. // color: red;
  272. // position: absolute;
  273. // }</style>