submit-controller.js 5.31 KB
/**
 * 校验控制器
 */
define(
    "check/submit-controller",
    [
        'mk7/controller',
        'mk7/url',
        'mk7/utils',
        'mk7/modals',
        'mk7/jweixin'
    ],
    function (ctrl, url, utils, modals, jweixin) {
        var ctrl = new ctrl();
        var $$ = Dom7;
        var t7 = Template7;
        var uuid = "";

        ctrl.run = function () {
            var me = this;
            uuid = me.params.uuid;
            me.setPageTitle("验证标签");
            jweixin.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: window.$site.appid, // 必填,公众号的唯一标识
                timestamp: window.$site.timestamp, // 必填,生成签名的时间戳
                nonceStr: window.$site.noncestr, // 必填,生成签名的随机串
                signature: window.$site.signature,// 必填,签名,见附录1
                jsApiList: ['chooseImage','previewImage','uploadImage', 'getLocalImgData']
            });
            me.render();
        };

        ctrl.bindEvents = function () {
            var me = this;
            $$(".scan-image").click(function () {
                me.uploadImg($$(this));
            });
            $$(".scan-button").click(function () {
                var uploadFile = $$(".scan-image").attr("data");
                if (utils.isEmpty(uploadFile)) {
                    utils.toast({content:"请拍照文件上传后再试!"});
                    return false;
                }
                $$.ajax({
                    method : "POST",
                    url: url.to('check/default/check-actived'),
                    data : {upload_file: uploadFile, uuid: uuid},
                    dataType : "json",
                    beforeSend : function(){
                        me.showIndicator();
                    },
                    success : function(res){
                        try {
                            if(res.success) {
                                window.location.href = url.to('check#success/' + uuid);
                            } else {
                                window.location.href = url.to('check#error/' + uuid);
                            }
                        } catch(ex) {
                            utils.toast({content:'出错', closeDelay:3000});
                        }
                    },
                    error : function(res){
                        utils.toast({content:"提交出错,请联系系统管理员"});
                    },
                    complete : function(res){
                        me.hideIndicator();
                    },
                });
            });
        };

        ctrl.uploadImg=function(uploadParent) {
            var me = this;
            jweixin.ready(function () {
                //每次只传一张图片
                jweixin.chooseImage({
                    count: 1,
                    sizeType: ['compressed'],//'original',
                    sourceType: ['album', 'camera'], // 'album', 'camera'
                    success: function (res) {
                        if (res.localIds.length > 0) {
                            me.upload(res.localIds, uploadParent);
                        }
                    }
                });
            });
        };

        ctrl.upload = function (localIds, uploadParent) {
            var me = this;
            if (localIds[0] == undefined) {
                return '';
            }
            jweixin.uploadImage({
                localId: localIds[0],
                isShowProgressTips: 1, // 默认为1,显示进度提示
                success: function (res) {
                    $$.ajax({
                        method : "POST",
                        url: url.to('check/default/update-serviceid'),
                        data : {service_id:res.serverId, uuid: uuid},
                        dataType : "json",
                        beforeSend : function(){
                            me.showIndicator();
                        },
                        success : function(res){
                            try {
                                if(res.success) {
                                    var imgUrl = res.img_path;
                                    uploadParent.attr("data", imgUrl);
                                    uploadParent.attr("src", res.show_path);
                                } else {
                                    utils.toast({content:res.message, closeDelay:3000});
                                }
                            } catch(ex) {
                                utils.toast({content:'出错', closeDelay:3000});
                            }
                        },
                        error : function(res){
                            utils.toast({content:"提交出错,请联系系统管理员"});
                        },
                        complete : function(res){
                            me.hideIndicator();
                        },
                    });
                },
                fail: function (res) {
                    me.app.alert(JSON.stringify(res));
                }
            });
        };

        return ctrl;
    }
);