Skip to content

Commit

Permalink
editor编辑器图片上传功能,后台接口完成
Browse files Browse the repository at this point in the history
  • Loading branch information
shen100 committed May 14, 2017
1 parent 08382e3 commit 0c359c7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 59 deletions.
67 changes: 34 additions & 33 deletions configuration.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
{
"poweredByWeb": "wemall",
"poweredByAPI": "wemall api",
"webPoweredBy": "wemall", /*前端node.js加的X-Powered-By*/
"apiPoweredBy": "wemall api", /*后台go加的X-Powered-By*/
"database": {
"Dialect" : "mysql",
"Database" : "wemall",
"User" : "root",
"Password" : "test1234",
"Charset" : "utf8",
"SQLLog" : true,
"URL" : ""
"SQLLog" : true, /*是否输出SQL*/
"URL" : "" /*数据库连接地址*/
},
"nodejs": {
"page": {
"title" : "wemall-软件微商城",
"sitePath" : "",
"jsPath" : "/javascripts",
"imagePath" : "/images",
"cssPath" : "/styles"
"title" : "wemall-微商城", /*网站标题*/
"sitePath" : "", /*网站前缀,适用于子应用的场景*/
"jsPath" : "/javascripts", /*前端JS请求地址前缀*/
"imagePath" : "/images", /*前端图片请求地址前缀*/
"cssPath" : "/styles", /*前端css请求地址前缀*/
"ueditorURL" : "/ueditor/" /*前端ueditor请求地址前缀*/
},
"env" : "development",
"useProxy" : false,
"proxyUri" : "http://127.0.0.1:8881",
"port" : 8010,
"staticPort" : 8011
"env" : "development", /*模式(开发,测试,产品)*/
"useProxy" : false, /*node.js发请求是否使用代理*/
"proxyUri" : "http://127.0.0.1:8881", /*代理地址及端口*/
"port" : 8010, /*前端node.js监听的端口*/
"staticPort" : 8011 /*前端静态文件服务器监听的端口(本地开发时使用)*/
},
"go": {
"Debug" : true,
"UploadImgDir" : "/Users/liushen/dev/uploads/img",
"Port" : 8012,
"MaxOrder" : 10000,
"MinOrder" : 0,
"PageSize" : 20,
"MaxPageSize" : 100,
"MinPageSize" : 20,
"MaxNameLen" : 100,
"MaxRemarkLen" : 500,
"MaxContentLen" : 10000,
"MaxProductCateCount" : 6
"Debug" : true, /*是否是debug模式*/
"UploadImgDir" : "/Users/liushen/dev/uploads/img", /*图片上传的目录*/
"ImgPath" : "/img", /*上传后的图片请求地址前缀*/
"Port" : 8012, /*go监听的端口*/
"MaxOrder" : 10000, /*最大的排序号*/
"MinOrder" : 0, /*最小的排序号*/
"PageSize" : 20, /*默认每页的条数*/
"MaxPageSize" : 100, /*每页最大的条数*/
"MinPageSize" : 20, /*每页最小的条数*/
"MaxNameLen" : 100, /*最大的名称长度*/
"MaxRemarkLen" : 500, /*最大的备注长度*/
"MaxContentLen" : 10000, /*最大的内容长度*/
"MaxProductCateCount" : 6 /*产品最多的分类个数*/
},
"software": {
"name" : "wemall微商城",
"version" : "1.0.0",
"officialURL" : "https://www.shen100.com"
"name" : "wemall微商城", /*软件名称*/
"version" : "1.0.0", /*软件版本*/
"officialURL" : "https://www.shen100.com" /*官网地址*/
},
"api": {
"Prefix" : "/api",
"URL" : "/api",
"NodeURL" : "http://127.0.0.1:8012/api"
"Prefix" : "/api", /*api服务请求前缀*/
"URL" : "http://127.0.0.1:8012/api" /*api服务请求地址(给node.js调用)*/
},
"docs": {
"github": "https://xxx.com/shen100"
"github": "https://github.com/shen100/wemall" /*wemall项目github地址*/
}
}
10 changes: 9 additions & 1 deletion go/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"regexp"
"wemall/go/utils"
)

Expand All @@ -15,6 +16,13 @@ func initJSON() {
if err != nil {
fmt.Println("ReadFile: ", err.Error())
}

configStr := string(bytes[:])
reg := regexp.MustCompile(`/\*.*\*/`)

configStr = reg.ReplaceAllString(configStr, "")
bytes = []byte(configStr)

if err := json.Unmarshal(bytes, &jsonData); err != nil {
fmt.Println("invalid config: ", err.Error())
}
Expand Down Expand Up @@ -45,6 +53,7 @@ func initDB() {

type serverConfig struct {
Debug bool
ImgPath string
UploadImgDir string
Port int
StaticPort int
Expand All @@ -69,7 +78,6 @@ func initServer() {
type apiConfig struct {
Prefix string
URL string
NodeURL string
}

// APIConfig api相关配置
Expand Down
25 changes: 12 additions & 13 deletions go/controller/ueditor/ueditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func upload(ctx *iris.Context) {
var index = strings.LastIndex(filename, ".")

if index < 0 {
errResData["state"] = "无效的文件名"
ctx.JSON(iris.StatusInternalServerError, errResData)
return
}
Expand All @@ -67,6 +68,7 @@ func upload(ctx *iris.Context) {
var mimeType = mime.TypeByExtension(ext)

if mimeType == "" {
errResData["state"] = "无效的图片类型"
ctx.JSON(iris.StatusInternalServerError, errResData)
return
}
Expand Down Expand Up @@ -107,32 +109,29 @@ func upload(ctx *iris.Context) {
}

uploadFilePath := uploadDir + sep + title

fmt.Println(uploadFilePath);

out, err := os.OpenFile(uploadFilePath, os.O_WRONLY|os.O_CREATE, 0666)

if err != nil {
ctx.JSON(iris.StatusInternalServerError, iris.Map{
"state" : "FAIL", //上传状态,上传成功时必须返回"SUCCESS"
"url" : "", //返回的地址
"title" : "", //新文件名
"original" : "", //原始文件名
"type" : "", //文件类型
"size" : "", //文件大小
})
ctx.JSON(iris.StatusInternalServerError, errResData)
return
}

defer out.Close()

io.Copy(out, file)

imgURL := config.ServerConfig.ImgPath + sep + timeDir + sep + title

ctx.JSON(iris.StatusOK, iris.Map{
"state" : "SUCCESS", //上传状态,上传成功时必须返回"SUCCESS"
"url" : "error.", //返回的地址
"title" : title, //新文件名
"state" : "SUCCESS", //上传状态,上传成功时必须返回"SUCCESS"
"url" : imgURL, //返回的地址
"title" : title, //新文件名
"original" : info.Filename, //原始文件名
"type" : "", //文件类型
"size" : "", //文件大小
"type" : mimeType, //文件类型
"size" : "", //文件大小
})
return
}
Expand Down
33 changes: 24 additions & 9 deletions nodejs/server/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
var configData = require('../../../configuration.json');
var fs = require('fs');
var path = require('path');

var configData

//只在程序启动时,读一次配置文件,此时可以使用同步的io操作
try {
var configStr = fs.readFileSync(path.join(__dirname, '../../../configuration.json'), 'utf8');
configStr = configStr.replace(/\/\*.*\*\//g, '');
configData = JSON.parse(configStr);
} catch (err) {
console.log(err);
process.exit(0);
}

/*
* nodejs和go都用同一个配置文件(即configuration.json)进行配置
Expand All @@ -11,14 +24,16 @@ var config = {
proxyUri : configData.nodejs.proxyUri,
port : configData.nodejs.port,
staticPort : configData.nodejs.staticPort,
uploadImgDir : configData.go.UploadImgDir, /*图片上传的目录*/
page: {
title : configData.nodejs.page.title,
sitePath : configData.nodejs.page.sitePath,
jsPath : configData.nodejs.page.jsPath,
imagePath : configData.nodejs.page.imagePath,
cssPath : configData.nodejs.page.cssPath,
apiPath : configData.api.URL,
UEDITOR_HOME_URL: '/ueditor/',
title : configData.nodejs.page.title,
sitePath : configData.nodejs.page.sitePath,
jsPath : configData.nodejs.page.jsPath,
imagePath : configData.nodejs.page.imagePath,
cssPath : configData.nodejs.page.cssPath,
ueditorURL : configData.nodejs.page.ueditorURL,
imgPath : configData.go.ImgPath, /*上传后的图片请求地址前缀*/
apiPath : configData.api.Prefix
},
software: {
name : configData.software.name,
Expand All @@ -37,7 +52,7 @@ var config = {
};

(function() {
var url = configData.api.NodeURL;
var url = configData.api.URL;
for (var key in config.api) {
if (config.api.hasOwnProperty(key)) {
config.api[key] = url + config.api[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ class EditProduct extends Component {
};
}
componentWillMount() {
console.log(this.state.productId);

}
componentDidMount() {
analyze.pv();
const { dispatch } = this.props;
console.log(this.state.productId);
if (this.state.productId) {
dispatch(requestProduct(this.state.productId));
}
Expand All @@ -46,7 +45,7 @@ class EditProduct extends Component {

let self = this;

window.UEDITOR_HOME_URL = pageConfig.UEDITOR_HOME_URL;
window.UEDITOR_HOME_URL = pageConfig.ueditorURL;

function loadCallback() {
if (configLoaded && editorLoaded) {
Expand Down
1 change: 1 addition & 0 deletions nodejs/staticServ.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ app.use(webpackDevMiddleware(compiler, {

app.use(webpackHotMiddleware(compiler));
app.use(express.static(path.join(__dirname, 'static')));
app.use(config.page.imgPath, express.static(config.uploadImgDir));

var server = http.Server(app);

Expand Down

0 comments on commit 0c359c7

Please sign in to comment.