github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/plugins_tmpl.go (about)

     1  package controller
     2  
     3  import (
     4  	"bytes"
     5  	"html/template"
     6  
     7  	"github.com/kotovmak/go-admin/modules/remote_server"
     8  
     9  	"github.com/kotovmak/go-admin/modules/language"
    10  	"github.com/kotovmak/go-admin/modules/logger"
    11  )
    12  
    13  func GetPluginsPageJS(data PluginsPageJSData) template.JS {
    14  	t := template.New("plugins_page_js").Funcs(map[string]interface{}{
    15  		"lang":     language.Get,
    16  		"plugWord": plugWord,
    17  	})
    18  	t, err := t.Parse(pluginsPageJS)
    19  	if err != nil {
    20  		logger.Error(err)
    21  		return ""
    22  	}
    23  	buf := new(bytes.Buffer)
    24  	err = t.Execute(buf, data)
    25  	if err != nil {
    26  		logger.Error(err)
    27  		return ""
    28  	}
    29  	return template.JS(buf.String())
    30  
    31  }
    32  
    33  type PluginsPageJSData struct {
    34  	Prefix string
    35  }
    36  
    37  var pluginsPageJS = `
    38  
    39  function pluginInstall(name){
    40  	location.href="{{.Prefix}}/info/plugin_"+name+"/new"
    41  }
    42  
    43  var downloadLock = false;
    44  const apiTokenKey = "GOADMIN_SERVER_API_TOKEN";
    45  const apiTokenExpireKey = "GOADMIN_SERVER_API_TOKEN_EXPIRE";
    46  const serverHost = "` + remote_server.ServerHost + `"
    47  
    48  function login() {
    49  	$.ajax({
    50  		type: 'POST',
    51  		url:  '{{.Prefix}}/server/login',
    52  		dataType: "json",
    53  		contentType: "application/json",
    54  		data: JSON.stringify({
    55  			"account": $("#login-popup-modal form input.name").val(),
    56  			"password": $("#login-popup-modal form input.password").val()
    57  		}),
    58  		success: function (data) {
    59  			if (data.code == 0) {
    60  				localStorage.setItem(apiTokenKey, data.data.token);
    61  				localStorage.setItem(apiTokenExpireKey, (new Date()).getTime() + data.data.expire);
    62  				$('#login-popup-modal').modal('hide');
    63  				if (typeof loginSuccessCallback != "undefined") {
    64  					loginSuccessCallback(data);
    65  				}
    66  				$.pjax.reload('#pjax-container');
    67  			} else {
    68  				swal({
    69  					type: "error",
    70  					title: data.msg,
    71  					showCancelButton: false,
    72  					html: true,
    73  					confirmButtonColor: "#3c8dbc",
    74  					confirmButtonText: '{{lang "got it"}}',
    75          		})
    76  			}
    77  		},
    78  		error: function (data) {
    79  			swal({
    80  				type: "error",
    81  				title: data.responseJSON.msg ? data.responseJSON.msg : "error",
    82  				showCancelButton: false,
    83  				html: true,
    84  				confirmButtonColor: "#3c8dbc",
    85  				confirmButtonText: '{{lang "got it"}}',
    86  			})	
    87  		}
    88  	});	
    89  }
    90  
    91  function pluginBuy(name, uuid) {
    92  	window.open(serverHost + "/plugins/detail/" + uuid); 
    93  }
    94  
    95  function pluginDetail(name, uuid) {
    96  	$.ajax({
    97  		dataType: 'json',
    98  		type: 'POST',
    99  		url: '{{.Prefix}}/plugin/detail?name=' + name,
   100  		async: 'true',
   101  		success: function (data) {
   102  			if (data.code == 0) {
   103  				let head_ele = "#detail-popup-modal .plugin-detail .plugin-detail-head "
   104  				$(head_ele + ".plugin-detail-head-logo img").attr('src', data.data.mini_cover);
   105  				$(head_ele + ".plugin-detail-head-title .plugin-detail-title").html(data.data.title);
   106  				$(head_ele + ".plugin-detail-head-title .plugin-detail-provider").html(data.data.author);
   107  				let item_ele = "#detail-popup-modal .plugin-detail .plugin-detail-info .plugin-detail-info-item";
   108  				$(item_ele).eq(0).find(".plugin-detail-info-item-content").html(data.data.introduction);
   109  				$(item_ele).eq(1).find(".plugin-detail-info-item-content").html(data.data.website);
   110  				$(item_ele).eq(2).find(".plugin-detail-info-item-content").html(data.data.version);
   111  				$(item_ele).eq(3).find(".plugin-detail-info-item-content").html(data.data.created_at);
   112  				$(item_ele).eq(4).find(".plugin-detail-info-item-content").html(data.data.updated_at);
   113  				$(item_ele).eq(5).find("a").attr("href", "https://www.go-admin.cn/plugins/detail/" + data.data.uuid);
   114  
   115  				let footer_ele = "#detail-popup-modal .modal-footer .btn.btn-primary";
   116  
   117  				if (data.data.install) {
   118  					if (data.data.upgrade) {
   119  						$(footer_ele).html('{{plugWord "upgrade"}}').attr("onclick", "pluginDownload('"+ name +"', this)");
   120  						$(footer_ele).show();
   121  					} else {
   122  						$(footer_ele).hide();
   123  					}
   124  				} else {
   125  					if (data.data.downloaded) {
   126  						if (data.data.download_reboot) {
   127  							if (data.data.skip) {
   128  								$(footer_ele).html('{{plugWord "install"}}')
   129  							} else {
   130  								$(footer_ele).html('{{plugWord "install"}}').attr("onclick", "pluginInstall('"+ name +"')")
   131  							}
   132  						} else {
   133  							$(footer_ele).html('{{plugWord "install"}}').attr("onclick", 'pluginRebootInstall()')
   134  						}
   135  					} else {
   136  						if (data.data.free) {
   137  							$(footer_ele).html('{{plugWord "download"}}').attr("onclick", "pluginInstall('"+ name +"', this)")
   138  						} else {
   139  							$(footer_ele).html('{{plugWord "buy"}}').attr("onclick", "pluginBuy('"+ name +"', '"+ uuid +"')")
   140  						}
   141  					}
   142  					$(footer_ele).show();
   143  				}
   144  
   145  				$("#detail-popup-modal").modal('show');
   146  			} else {
   147  				swal({
   148  					type: "error",
   149  					title: data.msg,
   150  					showCancelButton: false,
   151  					html: true,
   152  					confirmButtonColor: "#3c8dbc",
   153  					confirmButtonText: '{{lang "got it"}}',
   154          		})
   155  			}
   156  		},
   157  		error: function (data) {
   158  			swal({
   159  				type: "error",
   160  				title: data.responseJSON.msg ? data.responseJSON.msg : "error",
   161  				showCancelButton: false,
   162  				html: true,
   163  				confirmButtonColor: "#3c8dbc",
   164  				confirmButtonText: '{{lang "got it"}}',
   165  			})	
   166  		}
   167  	});
   168  }
   169  
   170  function pluginDownload(name, ele) {
   171  	if (downloadLock) {
   172  		return
   173  	}
   174  	NProgress.start();
   175  	downloadLock = true;
   176  	$(ele).html('<i class="fa fa-refresh fa-spin text-primary" style="color: white;"></i> {{plugWord "downloading"}}')
   177  	$.ajax({
   178  		dataType: 'json',
   179  		type: 'POST',
   180  		url: '{{.Prefix}}/plugin/download',
   181  		async: 'true',
   182  		data: {
   183  			'name': name
   184  		},
   185  		success: function (data) {
   186  			NProgress.done();
   187  			downloadLock = false;
   188  			if (data.code == 0) {
   189  				$(ele).attr('onclick', 'pluginRebootInstall()')
   190  				$(ele).html('{{plugWord "install"}}')
   191  				swal({
   192  					type: "success",
   193  					title: data.msg,
   194  					showCancelButton: false,
   195  					confirmButtonColor: "#3c8dbc",
   196  					confirmButtonText: '{{lang "got it"}}',
   197          		})
   198  			} else {
   199  				$(ele).html('{{plugWord "download"}}')
   200  				swal({
   201  					type: "error",
   202  					title: data.msg,
   203  					showCancelButton: false,
   204  					text: "<a target='_blank' href='//baidu.com'>点这里手动下载</a>",
   205  					html: true,
   206  					confirmButtonColor: "#3c8dbc",
   207  					confirmButtonText: '{{lang "got it"}}',
   208          		})
   209  			}
   210  		},
   211  		error: function (data) {
   212  			downloadLock = false;
   213  			$(ele).html('{{plugWord "download"}}')
   214  			alert('download fail');	
   215  		}
   216  	});
   217  }
   218  
   219  function pluginRebootInstall() {
   220  	swal({
   221  		type: "success",
   222  		title: "{{plugWord "restart to install"}}",
   223  		showCancelButton: false,
   224  		confirmButtonColor: "#3c8dbc",
   225  		confirmButtonText: '{{lang "got it"}}',
   226  	})
   227  }
   228  `
   229  
   230  var pluginsPageCSS = template.CSS(`
   231  	.plugin-item-img {
   232  		text-align: center;
   233  	}
   234  	.plugin-item-img img {
   235  		width: 100%;
   236  		height: auto;
   237  		border: 1px solid #f5f4f4;
   238  	}
   239  	.plugin-item-title {
   240  		margin-top: 10px;
   241  		color: #717171;
   242  		overflow: hidden;
   243          text-overflow: ellipsis;
   244          word-break: break-all;
   245          display: -webkit-box;
   246          -webkit-line-clamp: 1;
   247          -webkit-box-orient: vertical;
   248  	}
   249  	@media screen and (min-width: 1200px) {
   250  		.pjax-container-content .row .col-lg-2 {
   251  			width: 14%;
   252  			max-width: 200px;
   253  		}
   254  	}
   255  	.plugin-item-label {
   256  		color: white;
   257  		background-color: red;
   258  		padding: 4px;
   259  		border-radius: 3px;
   260  		font-size: 12px;
   261  		position: absolute;
   262  		top: 5px;
   263  		right: 5px;
   264  	}
   265  `)
   266  
   267  var pluginsStorePageCSS = template.CSS(`
   268  	.plugin-item-content {
   269  		margin-left: 15px;
   270  	}
   271  	.plugin-store-item-img img, .plugin-detail-head-logo img {
   272  		border: 1px solid #f5f4f4;
   273  	}
   274  	.plugin-item-content-title {
   275  		font-size: 15px;
   276  		margin-bottom: 6px;
   277  		font-weight: bold;
   278  		padding-top: 2px;
   279  	}
   280  	.plugin-item-content {
   281  		position: absolute;
   282  		margin-left: 121px;
   283  		padding-right: 10px;
   284  		top: 7px;
   285      	width: 100%;
   286      	padding-right: 139px;
   287  	}
   288  	.plugin-item-content-description {
   289  		overflow: hidden;
   290  		text-overflow: ellipsis;
   291  		word-break: break-all;
   292  		display: -webkit-box;
   293  		font-size: 15px;
   294  		-webkit-line-clamp: 2;
   295  		-webkit-box-orient: vertical;
   296  		height: 42px;
   297  	}
   298  	.installation {
   299  		float: right;
   300  		margin-top: 10px;
   301  	}
   302  	.plugin-info {
   303  		float: right;
   304  		margin-top: 10px;
   305  		margin-left: 10px;
   306  	}
   307  	.plugin-detail {
   308  		padding: 10px;
   309  	}
   310  	.plugin-detail-head {
   311  		clear: both;
   312  		height: 112px;
   313      	margin-bottom: 33px;
   314  	}
   315  	.plugin-detail-title {
   316  		font-size: 30px;
   317  	}
   318  	.plugin-detail-provider {
   319      	font-size: 15px;
   320      	margin-top: 4px;
   321  	}
   322  	.plugin-detail-head-logo {
   323  		width: 120px;
   324  		float: left;
   325  	}
   326  	.plugin-detail-head-title {
   327  		float: left;
   328  		margin-left: 10px;
   329  	}
   330  	.plugin-detail-info-item {
   331  		clear: both;
   332  		height: 15px;
   333  		margin-bottom: 17px;
   334  	}
   335  	.plugin-detail-info-item-head {
   336  		width: 120px;
   337  		float: left;
   338  		font-weight: bold;
   339  	}
   340  	.plugin-detail-info-item-content {
   341  		float: left;
   342  		margin-left: 10px;
   343  	}
   344  `)
   345  
   346  var pluginStore404 = func() template.HTML {
   347  	return template.HTML(`
   348  <div class="plugin-store-404-content"><p>` + plugWord("can not connect to the goadmin remote server") + `</p></div>
   349  `)
   350  }
   351  
   352  var pluginsPageDetailPopupBody = func() template.HTML {
   353  	return template.HTML(`
   354  <div class="plugin-detail">
   355  	<div class="plugin-detail-head">
   356  		<div class="plugin-detail-head-logo">
   357  			<img src="" width="110px" height="110px">
   358  		</div>
   359  		<div class="plugin-detail-head-title">
   360  			<div class="plugin-detail-title"></div>
   361  			<div class="plugin-detail-provider"></div>
   362  		</div>
   363  	</div>
   364  <div class="plugin-detail-info">
   365  	<div class="plugin-detail-info-item">
   366  		<div class="plugin-detail-info-item-head">` + plugWord("introduction") + `</div>
   367  		<div class="plugin-detail-info-item-content"></div>
   368  	</div>
   369  	<div class="plugin-detail-info-item">
   370  		<div class="plugin-detail-info-item-head">` + plugWord("website") + `</div>
   371  		<div class="plugin-detail-info-item-content"></div>
   372  	</div>
   373  	<div class="plugin-detail-info-item">
   374  		<div class="plugin-detail-info-item-head">` + plugWord("version") + `</div>
   375  		<div class="plugin-detail-info-item-content"></div>
   376  	</div>
   377  	<div class="plugin-detail-info-item">
   378  		<div class="plugin-detail-info-item-head">` + plugWord("created at") + `</div>
   379  		<div class="plugin-detail-info-item-content"></div>
   380  	</div>
   381  	<div class="plugin-detail-info-item">
   382  		<div class="plugin-detail-info-item-head">` + plugWord("updated at") + `</div>
   383  		<div class="plugin-detail-info-item-content"></div>
   384  	</div>
   385  	<div class="plugin-detail-info-item">
   386  		<a target="_blank" href=""><div class="plugin-detail-info-item-head">` + plugWord("learn more") + `</div></a>
   387  	</div>
   388  </div>
   389  </div>`)
   390  }