WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2024-05-11 ed9696be2f6c8584e592cd72f5db2dbf14a9c2b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * CUserSignature - 用户签名
 * @author Tevin
 */
 
<template>
    <view class="c-user-signature">
        <AtInput
            ref="input"
            :name="itemRes.name"
            :title="itemRes.label"
            :required="itemRes.required"
            :error="itemRes.error"
        />
        <view
            ref="drawing"
            class="c-user-signature-drawing"
        >
            <view
                class="c-user-signature-button"
                @tap="evt => handleStartEdit()"
            >
                <image
                    class="c-user-signature-preview"
                    v-if="itemRes.formData[itemRes.name]"
                    mode="aspectFit"
                    :src="itemRes.formData[itemRes.name]"
                />
                <AtIcon
                    v-if="!itemRes.formData[itemRes.name]"
                    value="edit"
                />
            </view>
        </view>
        <CSignatureLayer
            v-if="drawSelf"
            ref="drawLayer"
        />
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { AtInput, AtIcon } from 'taro-ui-vue';
import { $ } from '@tarojs/extend';
import { Fetcher } from '@components/bases/Fetcher';
import { $fetchCommon } from '@fetchers/FCommon';
import { $bridge } from '@components/common/Bridge';
import { $hostBoot } from '@components/bases/HostBoot';
import CSignatureLayer from '@components/forms/userSignature/CSignatureLayer';
import { uploadImage } from '@components/forms/imagePicker';
import project from '@project';
import './cUserSignature.scss';
 
export default {
    name: 'CUserSignature',
    components: {
        AtInput,
        AtIcon,
        CSignatureLayer,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
    },
    data() {
        return {
            drawSelf: process.env.TARO_ENV === 'weapp',
        };
    },
    computed: {},
    methods: {
        handleStartEdit() {
            // 混合App中
            if (project.appHybrid) {
                $bridge.invoking('_get_user_sign', sign => {
                    if (!sign) {
                        return;
                    }
                    const url = this._transBase64ToBlob(sign.result);
                    this.itemRes.onChange(url);
                });
            }
            // 小程序中
            else if (process.env.TARO_ENV === 'weapp') {
                this.$refs.drawLayer.$onDraw(sign => {
                    if (sign.indexOf('http') >= 0) {
                        this.itemRes.onChange(sign);
                    } else {
                        this.itemRes.onChange(sign);
                    }
                });
            }
            // TODO: 普通h5,使用 canvas 签名
        },
        _transBase64ToBlob(base64) {
            const arr = base64.split(',');
            const mime = arr[0].match(/:(.*?);/)[1];
            const bstr = atob(arr[1]);
            let n = bstr.length;
            const u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            const blob = new Blob([u8arr], { type: mime });
            return URL.createObjectURL(blob);
        },
        $startEdit() {
            this.handleStartEdit();
        },
        $uploadImage(callback) {
            const file = {
                url: this.itemRes.formData[this.itemRes.name],
                fileName: 'file-' + Date.now() + '.png',
            };
            // 没有上传内容,视为无需上传
            if (!file.url) {
                callback('success');
                return;
            }
            uploadImage([file], (state, res) => {
                if (state === 'success') {
                    this.itemRes.onChange(res[0]);
                    setTimeout(() => {
                        callback(state);
                    }, 10);
                } else if (state === 'error') {
                    callback(state, res);
                }
            });
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.input.$el)
                .find('.at-input__input')
                .prepend(this.$refs.drawing.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .append(this.$refs.drawing);
        }
    },
};
</script>