addon.js 19.9 KB
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: Config.fastadmin.api_url + '/addon/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: ''
                }
            });

            var table = $("#table");

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                var parenttable = table.closest('.bootstrap-table');
                var d = $(".fixed-table-toolbar", parenttable).find(".search input");
                d.off("keyup drop blur");
                d.on("keyup", function (e) {
                    if (e.keyCode == 13) {
                        var that = this;
                        var options = table.bootstrapTable('getOptions');
                        var queryParams = options.queryParams;
                        options.pageNumber = 1;
                        options.queryParams = function (params) {
                            var params = queryParams(params);
                            params.search = $(that).val();
                            return params;
                        };
                        table.bootstrapTable('refresh', {});
                    }
                });
            });

            Template.helper("Moment", Moment);
            Template.helper("addons", Config['addons']);

            // 初始化表格
            table.bootstrapTable({
                url: location.protocol === "https:" ? "addon/downloaded" : $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'id', title: 'ID', operate: false},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'}
                    ]
                ],
                dataType: 'jsonp',
                templateView: true,
                search: true,
                showColumns: false,
                showToggle: false,
                showExport: false,
                commonSearch: false,
                searchFormVisible: false,
                pageSize: 12,
                queryParams: function (params) {
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    filter.faversion = Config.fastadmin.version;
                    op.faversion = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('click', '.btn-addoninfo', function (event) {
                var index = parseInt($(this).data("index"));
                var data = table.bootstrapTable("getData");
                var item = data[index];
                var addon = typeof Config.addons[item.name] != 'undefined' ? Config.addons[item.name] : null;
                Layer.alert(Template("addoninfotpl", {item: item, addon: addon}), {
                    btn: [__('OK'), __('Donate'), __('Feedback'), __('Document')],
                    title: __('Detail'),
                    area: ['450px', '490px'],
                    btn2: function () {
                        //打赏
                        Layer.open({
                            content: Template("paytpl", {payimg: item.donateimage}),
                            shade: 0.8,
                            area: ['800px', '600px'],
                            skin: 'layui-layer-msg layui-layer-pay',
                            title: false,
                            closeBtn: true,
                            btn: false,
                            resize: false,
                        });
                    },
                    btn3: function () {
                        return false;
                    },
                    btn4: function () {
                        return false;
                    },
                    success: function (layero, index) {
                        $(".layui-layer-btn2", layero).attr("href", "http://forum.fastadmin.net/t/bug?ref=addon&name=" + item.name).attr("target", "_blank");
                        $(".layui-layer-btn3", layero).attr("href", "http://www.fastadmin.net/store/" + item.name + ".html?ref=addon").attr("target", "_blank");
                    }
                });
            });

            // 如果是https则启用提示
            if (location.protocol === "https:") {
                $("#warmtips").removeClass("hide");
                $(".btn-switch,.btn-userinfo").addClass("disabled");
            }

            require(['upload'], function (Upload) {
                Upload.api.plupload("#plupload-addon", function (data, ret) {
                    Config['addons'][data.addon.name] = data.addon;
                    $('.btn-refresh').trigger('click');
                    Toastr.success(ret.msg);
                });
            });

            //查看插件首页
            $(document).on("click", ".btn-addonindex", function () {
                if ($(this).attr("href") == 'javascript:;') {
                    Layer.msg(__('Not installed tips'), {icon: 7});
                } else if ($(this).closest(".operate").find("a.btn-enable").size() > 0) {
                    Layer.msg(__('Not enabled tips'), {icon: 7});
                    return false;
                }
            });
            //切换URL
            $(document).on("click", ".btn-switch", function () {
                $(".btn-switch").removeClass("active");
                $(this).addClass("active");
                table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
            });
            // 会员信息
            $(document).on("click", ".btn-userinfo", function () {
                var userinfo = Controller.api.userinfo.get();
                if (!userinfo) {
                    Layer.open({
                        content: Template("logintpl", {}),
                        area: ['400px', '330px'],
                        title: __('Login FastAdmin'),
                        resize: false,
                        btn: [__('Login'), __('Register')],
                        yes: function (index, layero) {
                            Fast.api.ajax({
                                url: Config.fastadmin.api_url + '/user/login',
                                dataType: 'jsonp',
                                data: {account: $("#inputAccount", layero).val(), password: $("#inputPassword", layero).val(), _method: 'POST'}
                            }, function (data, ret) {
                                Controller.api.userinfo.set(data);
                                Layer.closeAll();
                                Layer.alert(ret.msg);
                            }, function (data, ret) {
                                Layer.alert(ret.msg);
                            });
                        },
                        btn2: function () {
                            return false;
                        },
                        success: function (layero, index) {
                            $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
                        }
                    });
                } else {
                    var userinfo = Controller.api.userinfo.get();
                    if (!userinfo) {
                        Layer.alert(__('You\'re not login'));
                        return false;
                    }
                    Layer.open({
                        content: Template("userinfotpl", userinfo),
                        area: ['400px', '330px'],
                        title: __('Userinfo'),
                        resize: false,
                        btn: [__('Logout'), __('Cancel')],
                        yes: function () {
                            Fast.api.ajax({
                                url: Config.fastadmin.api_url + '/user/logout',
                                dataType: 'jsonp',
                                data: {uid: userinfo.id, token: userinfo.token}
                            }, function (data, ret) {
                                Controller.api.userinfo.set(null);
                                Layer.closeAll();
                                Layer.alert(ret.msg);
                            }, function (data, ret) {
                                Controller.api.userinfo.set(null);
                                Layer.closeAll();
                                Layer.alert(ret.msg);
                            });
                        }
                    });
                }
            });

            // 点击安装
            $(document).on("click", ".btn-install", function () {
                var that = this;
                var name = $(this).closest(".operate").data("name");
                var version = $(this).data("version");
                var userinfo = Controller.api.userinfo.get();
                var uid = userinfo ? userinfo.id : 0;
                var token = userinfo ? userinfo.token : '';
                var install = function (name, force) {
                    Fast.api.ajax({
                        url: 'addon/install',
                        data: {name: name, force: force ? 1 : 0, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
                    }, function (data, ret) {
                        Layer.closeAll();
                        Config['addons'][data.addon.name] = ret.data.addon;
                        Layer.alert(__('Online installed tips'), {
                            btn: [__('OK'), __('Donate')],
                            title: __('Warning'),
                            icon: 1,
                            btn2: function () {
                                //打赏
                                Layer.open({
                                    content: Template("paytpl", {payimg: $(that).data("donateimage")}),
                                    shade: 0.8,
                                    area: ['800px', '600px'],
                                    skin: 'layui-layer-msg layui-layer-pay',
                                    title: false,
                                    closeBtn: true,
                                    btn: false,
                                    resize: false,
                                });
                            }
                        });
                        $('.btn-refresh').trigger('click');
                    }, function (data, ret) {
                        //如果是需要购买的插件则弹出二维码提示
                        if (ret && ret.code === -1) {
                            //扫码支付
                            Layer.open({
                                content: Template("paytpl", ret.data),
                                shade: 0.8,
                                area: ['800px', '600px'],
                                skin: 'layui-layer-msg layui-layer-pay',
                                title: false,
                                closeBtn: true,
                                btn: false,
                                resize: false,
                                end: function () {
                                    Layer.alert(__('Pay tips'));
                                }
                            });
                        } else if (ret && ret.code === -2) {
                            //跳转支付
                            Layer.alert(__('Pay click tips'), {
                                btn: [__('Pay now'), __('Cancel')],
                                icon: 0,
                                success: function (layero) {
                                    $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
                                }
                            }, function () {
                                Layer.alert(__('Pay new window tips'), {icon: 0});
                            });

                        } else if (ret && ret.code === -3) {
                            //插件目录发现影响全局的文件
                            Layer.open({
                                content: Template("conflicttpl", ret.data),
                                shade: 0.8,
                                area: ['800px', '600px'],
                                title: __('Warning'),
                                btn: [__('Continue install'), __('Cancel')],
                                end: function () {

                                },
                                yes: function () {
                                    install(name, true);
                                }
                            });

                        } else {
                            Layer.alert(ret.msg);
                        }
                        return false;
                    });
                };
                if ($(that).data("type") !== 'free') {
                    if (parseInt(uid) === 0) {
                        return Layer.alert(__('Not login tips'), {
                            title: __('Warning'),
                            btn: [__('Login now'), __('Continue install')],
                            yes: function (index, layero) {
                                $(".btn-userinfo").trigger("click");
                            },
                            btn2: function () {
                                install(name, false);
                            }
                        });
                    }
                }
                install(name, false);
            });

            //点击卸载
            $(document).on("click", ".btn-uninstall", function () {
                var name = $(this).closest(".operate").data("name");
                var uninstall = function (name, force) {
                    Fast.api.ajax({
                        url: 'addon/uninstall',
                        data: {name: name, force: force ? 1 : 0}
                    }, function (data, ret) {
                        delete Config['addons'][name];
                        Layer.closeAll();
                        $('.btn-refresh').trigger('click');
                    }, function (data, ret) {
                        if (ret && ret.code === -3) {
                            //插件目录发现影响全局的文件
                            Layer.open({
                                content: Template("conflicttpl", ret.data),
                                shade: 0.8,
                                area: ['800px', '600px'],
                                title: __('Warning'),
                                btn: [__('Continue uninstall'), __('Cancel')],
                                end: function () {

                                },
                                yes: function () {
                                    uninstall(name, true);
                                }
                            });

                        } else {
                            Layer.alert(ret.msg);
                        }
                        return false;
                    });
                };
                Layer.confirm(__('Uninstall tips'), function () {
                    uninstall(name, false);
                });
            });

            //点击配置
            $(document).on("click", ".btn-config", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("addon/config?name=" + name, __('Setting'));
            });

            //点击启用/禁用
            $(document).on("click", ".btn-enable,.btn-disable", function () {
                var name = $(this).closest(".operate").data("name");
                var action = $(this).data("action");
                var operate = function (name, action, force) {
                    Fast.api.ajax({
                        url: 'addon/state',
                        data: {name: name, action: action, force: force ? 1 : 0}
                    }, function (data, ret) {
                        var addon = Config['addons'][name];
                        addon.state = action === 'enable' ? 1 : 0;
                        Layer.closeAll();
                        $('.btn-refresh').trigger('click');
                    }, function (data, ret) {
                        if (ret && ret.code === -3) {
                            //插件目录发现影响全局的文件
                            Layer.open({
                                content: Template("conflicttpl", ret.data),
                                shade: 0.8,
                                area: ['800px', '600px'],
                                title: __('Warning'),
                                btn: [__('Continue operate'), __('Cancel')],
                                end: function () {

                                },
                                yes: function () {
                                    operate(name, action, true);
                                }
                            });

                        } else {
                            Layer.alert(ret.msg);
                        }
                        return false;
                    });
                };
                operate(name, action, false);
            });

            //点击升级
            $(document).on("click", ".btn-upgrade", function () {
                if ($(this).closest(".operate").find("a.btn-disable").size() > 0) {
                    Layer.alert(__('Please disable addon first'), {icon: 7});
                    return false;
                }
                var name = $(this).closest(".operate").data("name");
                var version = $(this).data("version");
                var userinfo = Controller.api.userinfo.get();
                var uid = userinfo ? userinfo.id : 0;
                var token = userinfo ? userinfo.token : '';
                var upgrade = function (name) {
                    Fast.api.ajax({
                        url: 'addon/upgrade',
                        data: {name: name, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
                    }, function (data, ret) {
                        Config['addons'][name].version = version;
                        Layer.closeAll();
                        $('.btn-refresh').trigger('click');
                    }, function (data, ret) {
                        Layer.alert(ret.msg);
                        return false;
                    });
                };
                Layer.confirm(__('Upgrade tips'), function () {
                    upgrade(name);
                });
            });

            $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
                $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        config: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            userinfo: {
                get: function () {
                    var userinfo = localStorage.getItem("fastadmin_userinfo");
                    return userinfo ? JSON.parse(userinfo) : null;
                },
                set: function (data) {
                    if (data) {
                        localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
                    } else {
                        localStorage.removeItem("fastadmin_userinfo");
                    }
                }
            }
        }
    };
    return Controller;
});