<template>
    <view>
        <!-- camera.wxml -->
        <camera device-position="front" flash="off" @error="error" style="width: 100%; height: 100%" frame-size="small"></camera>
        <!-- <button type="primary" bindtap="takePhoto">拍照</button> -->
        <!-- <view>预览</view>
<image mode="widthFix" src="{{src}}"></image> -->
    </view>
</template>

<script>
import Cache from '../../../utils/cache';
// pages/tabBar/faceLogin/faceLogin.js
import { $http } from '../../../config/https.js';
import messageUtil from '../../../utils/message.js';
export default {
    data() {
        return {
            src: '',
            countTakePicture: 1
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        setTimeout(() => {
            this.takePhoto();
        }, 1000);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        takePhoto: function () {
            const ctx = uni.createCameraContext();
            ctx.takePhoto({
                quality: 'high',
                success: (res) => {
                    this.getImgBase(res.tempImagePath);
                }
            });
        },

        getImgBase: function (path) {
            var that = this;
            uni.getFileSystemManager().readFile({
                filePath: path,
                //选择图片返回的相对路径
                encoding: 'base64',
                //编码格式
                success: (res) => {
                    //成功的回调
                    let data = {
                        params: [
                            {
                                imageBase64: res.data,
                                // data.base64   imgBase
                                IP: '127.0.0.1',
                                deviceID: 'PHONE',
                                mac: '00-00-00-00-00-00'
                            }
                        ]
                    };
                    $http.post('urlDeault', this, {
                        code: '01040113',
                        data: data,
                        success: function (res) {
                            let errorCode = res.errorCode;
                            if (errorCode == '0') {
                                let locInfo = res.result[0];
                                uni.setStorage({
                                    key: 'userData',
                                    data: locInfo
                                });
                                let TIMInfo = Cache.get('TIMInfo') || {};
                                if (TIMInfo.loginStatus !== 'Y') {
                                    messageUtil.loginTim();
                                    setTimeout(() => {
                                        uni.switchTab(
                                            {
                                                url: '/pages/tabBar/home/home',
                                                success: function () {
                                                    that.saveLoginInfo();
                                                }
                                            },
                                            2000
                                        );
                                    });
                                }
                                that.setData({
                                    countTakePicture: 1
                                });
                            }
                        },
                        fail: (res) => {
                            if (res.errorCode == '88010025' || res.errorCode == '088080006' || res.errorCode == '088080012') {
                                if (that.countTakePicture < 5) {
                                    setTimeout(() => {
                                        that.setData({
                                            countTakePicture: that.countTakePicture + 1
                                        });
                                        that.takePhoto();
                                    }, 2000);
                                } else {
                                    setTimeout(() => {
                                        uni.navigateBack({});
                                    }, 1500);
                                }
                            } else {
                                setTimeout(() => {
                                    uni.navigateBack({});
                                }, 1500);
                            }
                        }
                    });
                }
            });
        },

        /**
         * 保存登录信息
         */
        saveLoginInfo: function () {
            var accountInfo = typeof uni.getAccountInfoSync === 'function' ? uni.getAccountInfoSync() : { miniProgram: { appId: '' } };
            Util.getOpenID(res.code, (openid) => {
                var appID = accountInfo.miniProgram.appId;
                // 登录接口
                let data = {
                    params: [
                        {
                            openID: openid,
                            appID: appID,
                            status: 'Y'
                        }
                    ]
                };
                $http.post('urlDeault', this, {
                    code: '03100013',
                    data: data,
                    success: function (res) {}
                });
            });
        },

        error() {
            console.log('占位：函数 error 未声明');
        }
    }
};
</script>
<style>
/* pages/tabBar/faceLogin/faceLogin.wxss */

page {
    height: 100%;
}
</style>
