You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form-builder-vue3.vue 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <!-- 表单 -->
  3. <form style="padding:10px 0" @submit="formSubmit">
  4. <view v-for="(item,index) in formData" :key="index">
  5. <view v-show="!item.show">
  6. <!-- 是否显示-->
  7. <view style="min-height: 90rpx;" :style="item.vertical == 2 ? 'margin-top: 20rpx' : ''"
  8. :class="item.vertical == 2 ? 'as-layout-vertical' : 'as-layout-horizontal'">
  9. <!-- 标题 -->
  10. <view v-if="!item.titleShow" class="as-gravity-center-start"
  11. :style="'min-width:'+ config.titleWidth + 'rpx'"
  12. style="margin:0 20rpx;flex-direction: row;display: flex;flex-direction: row;">
  13. <image class="img-size" :src="item.required ? '/static/image/must.png' : ''"></image>
  14. <view :class="item.vertical == 2 ? 'text-left' : 'text-justify'" class="text-title" style="width:100%;">
  15. {{item.title}}
  16. </view>
  17. </view>
  18. <!-- 内容 -->
  19. <view class="as-weight as-gravity-center-start">
  20. <!-- 文本内容 -->
  21. <view v-if="item.type == 1" class="text" :style="item.style">{{item[item.value]}}</view>
  22. <!-- 输入框 -->
  23. <view v-if="item.type == 2" style="width: 100%;">
  24. <input :disabled="item.disabled" v-model="item[item.value]" :type="item.inputType"
  25. :placeholder="item.hint ? item.hint : '请输入' + item.title" placeholder-class="text-hint"
  26. class="text as-gravity-center-start" :maxlength="item.maxlength" :style="item.style"
  27. style="min-height: 90rpx;word-break:break-all" />
  28. </view>
  29. <!-- 多项选择器 -->
  30. <view v-if="item.type == 3">
  31. <checkbox-group @change="checkboxChange($event,item)" :disabled="item.disabled">
  32. <view class="as-layout-horizontal" style="flex-wrap: wrap;margin: 10px 0;">
  33. <label style="display: flex;flex-direction: row;margin-right: 30rpx;"
  34. v-for="(itemData,index) in item.itemData " :key="index">
  35. <checkbox style="transform:scale(0.7)" :value="itemData.value"
  36. :checked="itemData.checked" />
  37. <view>{{itemData.value}}</view>
  38. </label>
  39. </view>
  40. </checkbox-group>
  41. </view>
  42. <!-- 普通选择器 当 range 是一个 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容
  43. :range-key="item[item.value]"-->
  44. <view v-if="item.type == 4" style="width: 100%;">
  45. <!-- range[范围] value[初始选择]-->
  46. <picker color="uni-picker-container" :disabled="item.disabled" :range-key="item.itemKey"
  47. :mode="item.mode ? item.mode : 'selector'" @change="bindPickerChange($event,item)"
  48. :range="item.itemData">
  49. <view class="text">
  50. {{item[item.value] ? (item.itemKey ? item[item.value][item.itemKey] : item[item.value]) :
  51. item.hint ? item.hint : '请选择' + item.title}}
  52. </view>
  53. </picker>
  54. </view>
  55. <!-- 富文本框 -->
  56. <view v-if="item.type == 5" style="width: 100%;">
  57. <view style="margin: 20rpx 30rpx 0 30rpx;border: 1px solid #999999;height: 200rpx;border-radius: 10px;">
  58. <textarea :disabled="item.disabled" class="text" v-model="item[item.value]"
  59. @input="textareaInput($event,item)" placeholder-class="text-hint"
  60. style="width: 100%;margin: 20rpx" :maxlength="item.maxlength" auto-height
  61. :placeholder="item.hint ? item.hint : '请输入' + item.title">
  62. </textarea>
  63. <view v-if="item.maxlength" class="as-gravity-center-end"
  64. style="display: flex;flex-direction: row;height: 20%;">
  65. <view class="text">{{!item.num ? '0' : item.num}}</view>
  66. <view class="text">/{{item.maxlength ? item.maxlength : 100}}</view>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 图片选择器 :imgWidth="imgWidth ? imgWidth : 0"-->
  71. <view v-if="item.type == 6" style="width: 100%;">
  72. <form-image @backImg="backImg($event,item)" style="width: 100%;" :retract="40"></form-image>
  73. </view>
  74. <!-- 单向选择器 -->
  75. <view v-if="item.type == 7" style="width: 100%;">
  76. <radio-group @change="radioChange($event,item)">
  77. <view class="as-layout-horizontal">
  78. <label style="display: flex;flex-direction: row;margin-right: 30rpx;"
  79. v-for="(itemData, index) in item.itemData" :key="index">
  80. <radio color="#1AAC1B" style="transform:scale(0.7)" :value="itemData.value"
  81. :checked="itemData.checked" />
  82. <view>{{itemData.name}}</view>
  83. </label>
  84. </view>
  85. </radio-group>
  86. </view>
  87. <!-- 证件照上传 -->
  88. <view v-if="item.type == 8" style="width: 100%;">
  89. <view style="margin: 0 40rpx;">
  90. <view class="text-error">
  91. {{item.hint && item.hint.split(',')[0] ? item.hint.split(',')[0] : ''}}</view>
  92. <view class="as-layout-horizontal as-gravity-center"
  93. style="height: 260rpx;margin-top: 20rpx;">
  94. <view class="as-layout-vertical as-gravity-center" style="width: 90%;height: 100%;">
  95. <image
  96. :src="item.placeholderImg ? item.placeholderImg.split(',')[0] : '../../static/image/license2.png'"
  97. style="width: 100%;height: 80%;" :style="item.style"
  98. @tap="ImageSelection(item,1)"></image>
  99. <view class="as-gravity-center text-hint" style="margin-top: 5rpx;">
  100. {{item.hint && item.hint.split(',')[1] ? item.hint.split(',')[1] : ''}}
  101. </view>
  102. </view>
  103. <view style="width: 40rpx;"></view>
  104. <view class="as-layout-vertical" style="width: 90%;height: 100%;"
  105. v-if="item.inputType != '999'">
  106. <image
  107. :src="item.placeholderImg && item.placeholderImg.split(',')[1] ? item.placeholderImg.split(',')[1] : '../../static/image/license2.png'"
  108. style="width: 100%;height: 80%;" @tap="ImageSelection(item,2)"></image>
  109. <view class="as-gravity-center text-hint" style="margin-top: 5rpx;">
  110. {{item.hint && item.hint.split(',')[2] ? item.hint.split(',')[2] : ''}}
  111. </view>
  112. </view>
  113. </view>
  114. </view>
  115. </view>
  116. <!-- 车牌号输入 -->
  117. <view v-if="item.type == 9" style="width: 100%">
  118. <view style="margin: 20rpx 30rpx 0 30rpx;">
  119. <car-num-ber-input @numberInputResult="numberInputResult($event,item)">
  120. </car-num-ber-input>
  121. </view>
  122. </view>
  123. <!-- 省市区选择器 -->
  124. <view v-if="item.type == 10" style="width: 100%">
  125. <pick-regions :defaultRegion="defaultRegionCode" @getRegion="handleGetRegion($event,item)">
  126. <view class="text">
  127. {{item[item.value] ? (item.itemKey ? item[item.value][item.itemKey] : item[item.value].value) :
  128. item.hint ? item.hint : '请选择' + item.title}}
  129. </view>
  130. </pick-regions>
  131. </view>
  132. <!-- 短信验证码 -->
  133. <view v-if="item.type == 11" style="width: 100%;">
  134. <view class="as-layout-horizontal as-gravity-center">
  135. <input v-model="item[item.value]" type="number"
  136. :placeholder="item.hint ? item.hint : '请输入' + item.title"
  137. placeholder-class="text-hint" class="text as-gravity-center-start"
  138. :maxlength="item.maxlength" :style="item.style"
  139. style="min-height: 90rpx;word-break:break-all" />
  140. <button style="width: 160rpx;font-size: 24rpx;height: 60rpx" :disabled="item.disabled"
  141. @click="SMSsending($event,item)">{{item.hint ? item.hint : '获取验证码'}}</button>
  142. </view>
  143. </view>
  144. <!-- 隐私协议 -->
  145. <view v-if="item.type == 12" style="width: 100%;">
  146. <view class="as-layout-horizontal as-gravity-center">
  147. <checkbox value="cb" />请先阅读并同意
  148. <a style="color:#007AFF;text-decoration: underline;">《开户服务协议》</a>
  149. </view>
  150. </view>
  151. <!-- 车牌颜色 -->
  152. <view v-if="item.type == 13" style="width: 100%;">
  153. <licensePlateColor></licensePlateColor>
  154. </view>
  155. </view>
  156. </view>
  157. <!-- 最后一位不展示 且 设置为展示 -->
  158. <view v-show="item.underline" class="as-line-h" style="margin: 0rpx 20rpx 0rpx 20rpx;"></view>
  159. </view>
  160. </view>
  161. <view style="display: flex;flex-direction: row;">
  162. <button v-if="config.btnBack" class="back-bg as-gravity-center btn as-weight"
  163. @click="back">{{config.submitOneName ? config.submitOneName : '上一步'}}
  164. </button>
  165. <button class="btn btn-text as-gravity-center nav-bg as-weight"
  166. form-type="submit">{{config.submitName ? config.submitName : '提交'}}
  167. </button>
  168. </view>
  169. </form>
  170. </template>
  171. <script setup lang='ts'>
  172. import {
  173. TypeData
  174. } from './tools'
  175. import licensePlateColor from '@/components/LicensePlateColor.vue';
  176. import carNumBerInput from '@/components/car-number-input/car-number-input'
  177. import {
  178. pathToBase64
  179. } from '../../static/js/util/imageTool.js'
  180. import pickRegions from '@/components/pick-regions/pick-regions.vue'
  181. const defaultRegionCode = '520115'
  182. const props = defineProps({
  183. formData: {
  184. type: Array as() => Array < TypeData > ,
  185. default: () => []
  186. },
  187. config: {
  188. type: Object,
  189. default: function() {
  190. return {
  191. submitName: '提交', //提交按钮名称
  192. titleWidth: 240, //标题宽度
  193. }
  194. }
  195. }
  196. })
  197. console.log('====123',props.formData)
  198. const emit = defineEmits < {
  199. (e: 'submit', content: any): void,
  200. (e: 'bindPickerChange', event: any, item: TypeData): void,
  201. (e: 'handleGetRegion', event: any, item: TypeData): void,
  202. (e: 'uploadImg', content: any, item: TypeData, index: Number): void,
  203. (e: 'radioChange', event: any, item: TypeData): void,
  204. (e: 'sendText', event: any, item: TypeData): void
  205. } > ()
  206. //defineExpose 可宏来显式指定在 <script setup> 组件中要暴露出去的属性。
  207. //普通选择器
  208. function bindPickerChange(e: any, item: TypeData) {
  209. let select = e.target.value
  210. // #ifdef H5
  211. select = e.detail.value
  212. // #endif
  213. if (item.mode == 'date' || item.mode == 'time') {
  214. /* 日期选择器*/
  215. item[item.value] = select
  216. } else {
  217. /* 普通选择器*/
  218. item[item.value] = item.itemData[select]
  219. }
  220. console.log('普通选择器', item, select)
  221. emit('bindPickerChange', e, item)
  222. }
  223. // 获取选择的地区
  224. function handleGetRegion(e: any, item: TypeData) {
  225. console.log('输出内容', e)
  226. item[item.value] = e[0].name + "-" + e[1].name + "-" + e[2].name
  227. emit('handleGetRegion', e, item);
  228. }
  229. //多项选择器
  230. function checkboxChange(e: any, item: TypeData) {
  231. item[item.value] = e.detail.value /* 赋值*/
  232. }
  233. //单项选择器
  234. function radioChange(e: any, item: TypeData) {
  235. item[item.value] = e.detail.value /* 赋值*/
  236. emit('radioChange', e, item)
  237. }
  238. //富文本框输入内容
  239. function textareaInput(e: Event, item: TypeData) {
  240. item.num = item[item.value].length
  241. }
  242. //点击图片按钮
  243. function backImg(e: Event, item: TypeData) {
  244. item[item.value] = e
  245. }
  246. function back(e: Event) {
  247. }
  248. //车牌输入
  249. function numberInputResult(e: any, item: TypeData) {
  250. console.log('输出内容', e)
  251. var str = e.replace(/\s*/g, "");
  252. item[item.value] = str
  253. }
  254. //短信验证码发送
  255. function SMSsending(e: any, item: TypeData) {
  256. let time = 60;
  257. emit('sendText', e, item)
  258. const fn = setInterval(function() {
  259. time--
  260. item.hint = time + 's'
  261. item.disabled = true
  262. if (time == 0) {
  263. clearInterval(fn)
  264. item.disabled = false
  265. item.hint = '获取验证码'
  266. }
  267. }, 1000)
  268. }
  269. //选择图片按钮
  270. function ImageSelection(item: TypeData, index: number) {
  271. uni.chooseImage({
  272. count: 1, //最多可以选择的文件个数
  273. sourceType: ['camera'], //album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']
  274. success(res: any) {
  275. if (res.tempFiles[0].size > 2000000) {
  276. uni.showToast({
  277. title: '图片大于2M,请重新上传',
  278. icon: 'none',
  279. duration: 1500
  280. })
  281. return;
  282. }
  283. // #ifdef H5
  284. pathToBase64(res.tempFilePaths[0]).then(data => {
  285. emit('uploadImg', data, item, index)
  286. })
  287. // #endif
  288. },
  289. fail(res: any) {
  290. if (!res.authSetting['scope.album']) {
  291. uni.showModal({
  292. title: '授权失败',
  293. content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  294. success: (res) => {
  295. if (res.confirm) {
  296. uni.openSetting()
  297. }
  298. }
  299. })
  300. }
  301. }
  302. })
  303. }
  304. function showToast(hint: string) {
  305. uni.showToast({
  306. icon: 'none',
  307. title: hint
  308. })
  309. }
  310. //内容提交
  311. function formSubmit() {
  312. let content = {}
  313. console.log('data值', props.formData)
  314. /* 整理数据对象返回内容 */
  315. for (var i = 0; i < props.formData.length; i++) {
  316. let data = props.formData[i]
  317. console.log('data', data)
  318. /* 时间另外判断 */
  319. if (data.required && !data.show) {
  320. let reg = new RegExp(':', 'g') //g代表全部
  321. let newMsg = data.title.replace(reg, '');
  322. if (data.value.indexOf(',') != -1 && data.type == 8) {
  323. if (!data[data.value.split(',')[0]]) {
  324. showToast(data.hint + "不能为空")
  325. return
  326. } else if (!data[data.value.split(',')[1]]) {
  327. showToast(data.hint + "不能为空")
  328. return
  329. }
  330. } else if (!data[data.value]) {
  331. showToast(newMsg + "不能为空")
  332. return
  333. }
  334. }
  335. if (data.value.indexOf(',') != -1 && data.type == 8) {
  336. if (data[data.value.split(',')[0]]) {
  337. content[data.value.split(',')[0]] = data[data.value.split(',')[0]]
  338. }
  339. if (data[data.value.split(',')[1]]) {
  340. content[data.value.split(',')[1]] = data[data.value.split(',')[1]]
  341. }
  342. } else if (data.type == 10) {
  343. for (var s = 0; s < data.value.split(',').length; s++) {
  344. if (data[data.value]) {
  345. content[data.value.split(',')[s]] = data[data.value].split('-')[s]
  346. }
  347. }
  348. } else {
  349. if (data.type == 4 && data.name && data[data.value]) {
  350. content[data.value] = data[data.value][data.name]
  351. } else {
  352. content[data.value] = data[data.value]
  353. }
  354. }
  355. }
  356. emit('submit', content)
  357. }
  358. </script>
  359. <style lang='scss' scoped>
  360. .img-size {
  361. width: 30rpx;
  362. height: 30rpx;
  363. }
  364. .btn {
  365. height: 80rpx;
  366. opacity: 1;
  367. border-radius: 100rpx;
  368. margin: 20rpx 20rpx 20rpx 20rpx;
  369. }
  370. .btn-text {
  371. color: #FFFFFF;
  372. font-size: 28rpx;
  373. }
  374. .nav-bg {
  375. /* background: linear-gradient(to right, #13E7C1, #43A1E0); */
  376. border: 1px solid #FFFFFF;
  377. background: #1AAC1B;
  378. }
  379. .back-bg {
  380. border: 1px solid #1AAC1B;
  381. color: #1AAC1B;
  382. background: #FFFFFF;
  383. }
  384. ::v-deep .uni-picker-container .uni-picker-action.uni-picker-action-confirm {
  385. background-color: blue;
  386. color: #1AAC1B;
  387. /* 添加其他样式属性 */
  388. }
  389. </style>