123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <template>
- <!-- 表单 -->
- <view style="margin: 0rpx 20rpx 0rpx 30rpx;">
- <u-form :model="model" :rules="rules" labelPosition="left" ref="uForm" labelWidth="70">
- <view v-for="(item,index) in list" :key="index">
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-if="item.type == 'input' && customerName(item)">
- <view class="card_form_item">
- <u-input v-model="model[`${item.field}`]" :disabled="item.disabled ? true : false"
- :placeholder="item.placeholder"></u-input>
- <view class="card_sweep" v-if="item.field == 'tamperProofLabel' || item.field == 'code'"
- @click="sweep(item.field)">
- <u-icon name="scan" size="18px"></u-icon>
- <span>扫一扫</span>
- </view>
- </view>
- </u-form-item>
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-else-if="item.type == 'select'">
- <view class="card_form_item">
- <u-input :placeholder="item.placeholder" suffixIcon="arrow-down"
- v-model="model[`${item.field}`]" @focus="change(item)"></u-input>
- </view>
- </u-form-item>
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-else-if="item.type == 'selectiveSearch'">
- <view class="card_form_item">
- <view class="card_search_gray frame" @click="changeil(item)">
- <span v-if="model[`${item.field}`]">{{model[`${item.field}`]}}</span>
- <span class="title_gray" v-else>{{item.placeholder}}</span>
- <u-icon name="arrow-down" color="#c8c9cc" size="18"></u-icon>
- </view>
- </view>
- </u-form-item>
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-else-if="item.type == 'selectil'">
- <view class="card_form_item">
- <u-input :placeholder="item.placeholder" suffixIcon="arrow-down"
- v-model="model[`${item.field}`]" @focus="changeil(item)"></u-input>
- </view>
- </u-form-item>
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-else-if="item.type == 'textarea'">
- <view class="card_form_item">
- <u-textarea v-model="model[`${item.field}`]" autoHeight
- :placeholder="item.placeholder"></u-textarea>
- </view>
- </u-form-item>
- <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
- v-else-if="item.type == 'upload' && !item.visible">
- <view class="card_form_item">
- <u-upload :fileList="fileList1" name="1" multiple :maxCount="10" @afterRead="afterRead"
- @delete="deletePic"></u-upload>
- </view>
- </u-form-item>
- </view>
- </u-form>
- <u-picker :defaultIndex="findIndex(model[fieldType], columns[0])" :show="show" :columns="columns"
- keyName="label" @confirm="confirm($event, fieldType)" @cancel="cancel"></u-picker>
- </view>
- </template>
- <script>
- const ENV = require('@/.env.js')
- export default {
- name: 'x-form',
- props: {
- list: {
- type: Array,
- default () {
- return []
- }
- },
- model: {
- type: Object,
- default () {
- return {}
- }
- },
- rules: {
- type: Object,
- default () {
- return {}
- }
- },
- },
- data() {
- return {
- show: false,
- columns: [],
- fieldType: '',
- fileList1: [],
- }
- },
- // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
- onReady() {
- this.$refs.uForm.setRules(this.rules);
- },
- methods: {
- customerName(value) {
- var userInfo = this.$cache.getCache('userInfo')
- if (userInfo.userType == 'customer') {
- if (value.field != 'customerName') {
- if (value.type == 'input') {
- return true
- }
- }
- } else {
- if (value.type == 'input') {
- return true
- }
- }
- },
- change(e) {
- this.columns = []
- this.fieldType = e.field
- this.show = true
- this.$nextTick(() => {
- this.columns.push(e.options)
- })
- },
- // 选择弹窗滚动
- changeil(value) {
- if (value.field == "status") {
- this.columns = []
- this.fieldType = value.field
- this.show = true
- this.$nextTick(() => {
- this.columns.push(value.options)
- })
- } else {
- this.$emit('selector', value)
- }
- },
- // 选择确定
- confirm(value, type) {
- this.show = false
- this.model[type] = value.value[0].label
- },
- // 点击确定
- cancel() {
- this.show = false
- },
- findIndex(code, list) {
- if (!code || !list) {
- return [0]
- }
- return [list.findIndex((item) => (item.label === code || item.label === code))]
- },
- // 新增图片
- async afterRead(event) {
- // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
- let lists = [].concat(event.file)
- let fileListLen = this[`fileList${event.name}`].length
- lists.map((item) => {
- this[`fileList${event.name}`].push({
- ...item,
- status: 'uploading',
- message: '上传中'
- })
- })
- uni.showLoading({
- title: '上传中',
- mask: true,
- })
- for (let i = 0; i < lists.length; i++) {
- const result = await this.uploadFilePromise(lists[i].url)
- let item = this[`fileList${event.name}`][fileListLen]
- this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
- status: 'success',
- message: '',
- url: result
- }))
- fileListLen++
- }
- var arr = []
- this.fileList1.forEach(item1 => {
- arr.push(item1.url)
- })
- uni.hideLoading();
- this.model.tamperProofLabelImg = arr.join()
- this.$refs.uForm.validateField('tamperProofLabelImg')
- },
- // 删除图片
- deletePic(event) {
- this[`fileList${event.name}`].splice(event.index, 1)
- var arr = []
- this.fileList1.forEach(item1 => {
- arr.push(item1.url)
- })
- uni.hideLoading();
- this.model.tamperProofLabelImg = arr.join()
- },
- uploadFilePromise(url) {
- return new Promise((resolve, reject) => {
- let a = uni.uploadFile({
- url: ENV.APP_DEV_URL + '/api/upload', // 仅为示例,非真实的接口地址
- filePath: url,
- name: 'file',
- // formData: {
- // user: 'test'
- // },
- header: {
- 'Authorization': 'Bearer ' + uni.getStorageSync('access_token'),
- },
- success: (res) => {
- let state = JSON.parse(res.data)
- setTimeout(() => {
- if (state.code == 200) {
- resolve(state.data)
- }
- }, 100)
- }
- });
- })
- },
- // 扫一扫
- sweep(field) {
- // 允许从相机和相册扫码
- uni.scanCode({
- scanType: ['barCode', 'qrCode'],
- autoZoom: false,
- success: (res) => {
- if (res.result) {
- let url = res.result;
- this.model[field] = url
- } else {
- console.log('请重新扫描');
- return false;
- }
- },
- fail: (res) => {
- console.log('未识别到二维码1');
- }
- })
- },
- //子组件校验,传递到父组件
- async validateForm() {
- let flag = null
- await this.$refs.uForm.validate(valid => {}).then(res => {
- flag = true
- }).catch(err => {
- flag = false
- })
- return flag
- }
- },
- }
- </script>
- <style lang="scss">
- .card_form_item {
- display: flex;
- width: 100%;
- }
- .card_sweep {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- height: 70rpx;
- border: 1rpx solid #e5e5e5;
- border-radius: 8rpx;
- margin-left: 10rpx;
- span {
- margin: 0rpx 10rpx;
- display: flex;
- font-size: 22rpx;
- }
- }
- .card_search_gray {
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: space-between;
- flex: 1;
- padding: 12rpx 18rpx;
- border-radius: 8rpx;
- line-height: 48rpx;
- }
- .title_gray {
- font-size: 30rpx;
- color: #c8c9cc;
- }
- // 伪元素1rpx边框
- .frame {
- position: relative; //重要
- }
- .frame::after {
- position: absolute;
- content: '';
- border: 2rpx solid #e7e6e4;
- border-radius: 16rpx;
- width: 200%;
- height: 200%;
- top: 0;
- left: 0;
- transform: scale(0.5);
- transform-origin: 0 0;
- pointer-events: none;
- /* 使伪元素不会阻止鼠标事件 */
- }
- </style>
|