index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. <pickerAddress @change="(info) => addressChange(info, item)"
  59. :selectValue="[formData.provinceInd, formData.cityInd, formData.districtInd]">
  60. <view class="inp"
  61. :class="formData.provinceName && formData.cityName && formData.districtName ? '' : 'address-inp'">
  62. {{ formData.provinceName ? formData.provinceName : '省' }} /
  63. {{ formData.cityName ? formData.cityName : '市' }} /
  64. {{ formData.districtName ? formData.districtName : '区' }}
  65. </view>
  66. </pickerAddress>
  67. <template #right>
  68. <up-icon name="map" size="22" @click="onCityWx(item)"></up-icon>
  69. </template>
  70. </up-form-item>
  71. <up-form-item v-if="item.type === 'cascader'" :label="item.label" :prop="item.key" borderBottom
  72. labelWidth="94" :required="item.required">
  73. <view @click.self="() => clickPick(item)">
  74. <up-input v-model="formData[item.key]" disabled disabledColor="#ffffff"
  75. :placeholder="'请选择' + item.label" border="none"></up-input>
  76. </view>
  77. <template #right>
  78. <up-icon name="arrow-right"></up-icon>
  79. </template>
  80. </up-form-item>
  81. </view>
  82. </up-form>
  83. <up-action-sheet :show="showSex" :actions="actions.option" :title="'请选择' + actions.label"
  84. @close="showSex = false" @select="ActionSheetSelect">
  85. </up-action-sheet>
  86. <up-code ref="uCode" @change="codeChange" seconds="60" @start="code_data.disabled = true"
  87. @end="code_data.disabled = false"></up-code>
  88. <!-- 省市区选择 province city area初始省市区设置 show:是否显示 @changeClick:更改省市区事件 @sureSelectArea:确认事件 @hideShow:隐藏事件-->
  89. <CcSelectDity :province="cityData.province" :city="cityData.city" :area="cityData.area" :show="cityShow"
  90. @sureSelectArea="onsetCity" @hideShow="onCityShow"></CcSelectDity>
  91. <Picker :columnData="options[actions.optionKey]" ref="pickerRef" @submit="ActionSheetSelect" />
  92. </view>
  93. </template>
  94. <script setup>
  95. import { onMounted, reactive, ref } from "vue";
  96. import CcSelectDity from '@/pages_home/components/cc-selectDity/cc-selectDity.vue';
  97. import Picker from '@/pages_home/components/picker/index.vue'
  98. import { sendCode } from '@/api/sm.js';
  99. import {
  100. onShow,
  101. onUnload
  102. } from "@dcloudio/uni-app";
  103. import { splitAddress, getCityCode,getAdressCode } from '@/utils/adress'
  104. import { regionAddresstree } from '@/api/home.js'
  105. import pickerAddress from '../pickerAddress/pickerAddress.vue'
  106. const props = defineProps({
  107. column: {
  108. type: Array,
  109. default: [],
  110. },
  111. isCode: {
  112. type: Boolean,
  113. default: true
  114. }
  115. });
  116. const formData = reactive({
  117. sex: null,
  118. city: null,
  119. provinceName: '', // 省
  120. provinceCode: '',
  121. provinceInd: 21,
  122. cityName: '', // 市
  123. cityCode: '',
  124. cityInd: 0,
  125. districtName: '',
  126. districtCode: '',
  127. districtInd: 17,
  128. // address: "永川区",
  129. // age: "1",
  130. // city: "重庆",
  131. // code: "1111",
  132. // idCard: "5002302000000000001",
  133. // name: "陈陈",
  134. // phonenumber: "18696601933",
  135. // sex: '男',
  136. // skillDescribe: "测试备注",
  137. });
  138. const form_ref = ref(null);
  139. const showSex = ref(false);
  140. const actions = ref({
  141. option: [],
  142. optionKey: 'businessManagementOption'
  143. });
  144. const pickerRef = ref(null)
  145. const options = reactive({
  146. businessManagementOption: []
  147. })
  148. const cityData = reactive({
  149. province: "重庆",
  150. city: "重庆市",
  151. area: "永川区",
  152. })
  153. const cityShow = ref(false)
  154. const code_data = reactive({
  155. disabled: false,
  156. tips: ''
  157. })
  158. const uCode = ref(null);
  159. function hideKeyboard() {
  160. uni.hideKeyboard()
  161. }
  162. function openActionSheet(row) {
  163. console.log(row);
  164. hideKeyboard();
  165. showSex.value = true;
  166. actions.value = row
  167. }
  168. function onCityShow() {
  169. console.log('onCityShow');
  170. cityShow.value = false;
  171. }
  172. function openCity(row) {
  173. console.log(row);
  174. cityShow.value = true
  175. actions.value = row
  176. }
  177. function onsetCity(e) {
  178. console.log('onsetCity', e);
  179. cityShow.value = false
  180. Object.assign(formData, {
  181. [actions.value.key]: e.address
  182. })
  183. console.log('formData', formData);
  184. }
  185. function ActionSheetSelect(e) {
  186. console.log('e', e);
  187. Object.assign(formData, {
  188. [actions.value.key]: e.key || e.name
  189. })
  190. if (actions.value.type === 'cascader') {
  191. Object.assign(formData, {
  192. [actions.value.key + 'key']: e.value
  193. })
  194. }
  195. console.log('formData.value', formData);
  196. // form_ref.value.validateField(actions.key)
  197. }
  198. async function getCode() {
  199. if (uCode.value.canGetCode) {
  200. // 模拟向后端请求验证码
  201. if (formData.phonenumber) {
  202. uni.showLoading({
  203. title: '正在获取验证码'
  204. })
  205. const res = await sendCode(formData.phonenumber);
  206. console.log(res);
  207. if (res.code == 200) {
  208. uni.hideLoading();
  209. uCode.value.start();
  210. uni.$u.toast('验证码已发送');
  211. }
  212. } else {
  213. uni.$u.toast('请输入手机号');
  214. }
  215. } else {
  216. uni.$u.toast('倒计时结束后再发送');
  217. }
  218. }
  219. function codeChange(text) {
  220. code_data.tips = text;
  221. }
  222. function startFace() {
  223. console.log('startFace', formData);
  224. }
  225. function setData(data) {
  226. console.log('setData=>data', data);
  227. Object.assign(formData, {
  228. ...data,
  229. sex: data.sex === 0 ? '男' : '女',
  230. })
  231. for (let i = 0; i < props.column.length; i++) {
  232. const element = props.column[i];
  233. if (element.type === 'cascader') {
  234. const key = data[element.key]
  235. // const key ='7'
  236. const lable = pickerRef.value.piceInit(key);
  237. console.log('element.type', lable, key, element.key);
  238. Object.assign(formData, {
  239. [element.key]: lable,
  240. [element.key + 'key']: key
  241. })
  242. console.log('eformData', formData);
  243. } else {
  244. }
  245. }
  246. }
  247. function onSubmit() {
  248. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  249. return new Promise(async (resolve, reject) => {
  250. const res = await form_ref.value.validate();
  251. if (!res) {
  252. reject()
  253. return
  254. }
  255. resolve(formData);
  256. })
  257. }
  258. function onResetField() {
  259. form_ref.value.resetFields()
  260. form_ref.value.clearValidate()
  261. }
  262. function clickPick(row) {
  263. console.log('e', row);
  264. actions.value = row
  265. console.log('e', actions.value);
  266. pickerRef.value.show();
  267. }
  268. function addressChange(info, row) {
  269. actions.value = row
  270. console.log(info, '>>>>data');
  271. if (!info || !info.data) {
  272. console.error('Invalid address change info:', info);
  273. return;
  274. }
  275. if (!Array.isArray(info.data) || info.data.length < 3) {
  276. console.error('Invalid address data:', info.data);
  277. return;
  278. }
  279. const code = Array.isArray(info.code) ? info.code : ['', '', ''];
  280. let indexArray;
  281. if (Array.isArray(info.index)) {
  282. indexArray = info.index;
  283. } else if (info.index && typeof info.index[Symbol.iterator] === 'function') {
  284. indexArray = Array.from(info.index);
  285. } else {
  286. indexArray = [0, 0, 0];
  287. }
  288. Object.assign(formData, {
  289. provinceName: info.data[0],
  290. provinceCode: info.code[0],
  291. provinceInd: indexArray[0] || 0,
  292. cityName: info.data[1],
  293. cityCode: info.code[1],
  294. cityInd: indexArray[1] || 0,
  295. districtName: info.data[2],
  296. districtCode: info.code[2],
  297. districtInd: indexArray[2] || 0
  298. })
  299. console.log('formData===>', formData);
  300. }
  301. function onCityWx(row) {
  302. console.log('地区选点', row);
  303. wx.chooseLocation({
  304. success: async function (res) {
  305. console.log('res.address', res);
  306. const result = splitAddress(res.address);
  307. const res_dara = await regionAddresstree();
  308. const handlecityData = getCityCode([result.province, result.city, result.district].join(' '), res_dara.data)
  309. const api_res =await getAdressCode({ latitude: res.latitude, longitude: res.longitude },res_dara.data)
  310. console.log("=================>", handlecityData,api_res)
  311. addressChange(api_res.cityCode, row)
  312. Object.assign(formData, {
  313. // [row.key]: `${result.province}${result.city}${result.district}`,
  314. address: result.detail
  315. })
  316. },
  317. fail: function (err) {
  318. console.log(err);
  319. uni.showToast({
  320. title: '获取地址失败',
  321. icon: 'error',
  322. });
  323. },
  324. })
  325. }
  326. onShow(() => {
  327. })
  328. onUnload(() => {
  329. })
  330. onMounted(async () => {
  331. const rules = {};
  332. for (let i = 0; i < props.column.length; i++) {
  333. rules[props.column[i].key] = props.column[i].rules;
  334. if (props.column[i].type === 'cascader') {
  335. const res = await props.column[i].apifun();
  336. Object.assign(options, {
  337. [props.column[i].optionKey]: res
  338. })
  339. console.log('onMounted9999', options);
  340. }
  341. }
  342. form_ref.value && form_ref.value.setRules(rules)
  343. console.log('uni=====>', uni);
  344. })
  345. defineExpose({
  346. setData,//修改表单数据值
  347. onSubmit,//提交表单,先校验再提交
  348. onResetField,//表单重置
  349. })
  350. </script>
  351. <style lang="scss" scoped>
  352. // .cust-form-item{
  353. // position: relative;
  354. // }
  355. // .required-icon {
  356. // color: red;
  357. // position: absolute;
  358. // }</style>