index.vue 15 KB

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