index.vue 11 KB

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