Browse Source

微信开发

chenyongxing 8 năm trước cách đây
mục cha
commit
a9c3b430cb

+ 24 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/base/BaseContants.java

@ -143,4 +143,28 @@ public class BaseContants {
        public static final String message_fail_code_no_exist="code no exist";
    }
    public static class Wechat {
        public static final String api_common="wechat";
        public static final String api_create="create";
        public static final String api_get="get";
        public static final String api_update="update";
        public static final String api_delete="delete";
        public static final String api_getByCode="getByCode";
        public static final String message_success_create="wechat create success";
        public static final String message_success_update="wechat update success";
        public static final String message_success_find="wechat find success";
        public static final String message_success_find_functions="wechat find success";
        public static final String message_success_delete="wechat delete success";
        public static final String message_fail_name_exist="wechat name exist";
        public static final String message_fail_code_is_null="code is null";
        public static final String message_fail_name_is_null="wechat is null";
        public static final String message_fail_id_is_null="id is null";
        public static final String message_fail_code_no_exist="code no exist";
        public static final String message_fail_appId_is_null="appId is null";
        public static final String message_fail_appSecret_is_null="appSecret is null";
        public static final String message_fail_appId_exist="wechat appId exist";
    }
}

+ 67 - 3
svr/svr-base/src/main/java/com/yihu/jw/wx/controller/WechatController.java

@ -1,8 +1,17 @@
package com.yihu.jw.wx.controller;
import com.yihu.jw.restmodel.base.BaseContants;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.common.EnvelopRestController;
import com.yihu.jw.restmodel.exception.ApiException;
import com.yihu.jw.wx.model.WxWechat;
import com.yihu.jw.wx.service.WechatService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
 * Created by chenweida on 2017/5/11.
@ -10,5 +19,60 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/wechat")
@Api(description = "微信相关操作")
public class WechatController {
public class WechatController  extends EnvelopRestController {
    @Autowired
    private WechatService wechatService;
    @PostMapping(value = BaseContants.Wechat.api_create, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建微信", notes = "创建微信")
    public Envelop createWechat(
            @ApiParam(name = "json_data", value = "", defaultValue = "")
            @RequestBody String jsonData) {
        try {
            WxWechat wechat = toEntity(jsonData, WxWechat.class);
            return Envelop.getSuccess(BaseContants.Wechat.message_success_create, wechatService.createWechat(wechat));
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PutMapping(value = BaseContants.Wechat.api_update, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "修改微信", notes = "修改微信")
    public Envelop updateWechat(
            @ApiParam(name = "json_data", value = "", defaultValue = "")
            @RequestBody String jsonData) {
        try {
            WxWechat Wechat = toEntity(jsonData, WxWechat.class);
            return Envelop.getSuccess(BaseContants.Wechat.message_success_update, wechatService.updateWxchat(Wechat));
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @DeleteMapping(value = BaseContants.Wechat.api_delete, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除微信", notes = "删除微信")
    public Envelop deleteWechat(
            @ApiParam(name = "code", value = "code")
            @RequestParam(value = "code", required = true) String code) {
        try {
            wechatService.deleteWechat(code);
            return Envelop.getSuccess(BaseContants.Wechat.message_success_delete );
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @GetMapping(value = BaseContants.Wechat.api_getByCode, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "根据code查找微信", notes = "根据code查找微信")
    public Envelop findByCode(
            @ApiParam(name = "code", value = "code")
            @RequestParam(value = "code", required = true) String code
    ) {
        try {
            return Envelop.getSuccess(BaseContants.Wechat.message_success_find, wechatService.findByCode(code));
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
}

+ 22 - 0
svr/svr-base/src/main/java/com/yihu/jw/wx/dao/WechatDao.java

@ -0,0 +1,22 @@
package com.yihu.jw.wx.dao;
import com.yihu.jw.wx.model.WxWechat;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by Administrator on 2017/5/20 0020.
 */
public interface WechatDao extends PagingAndSortingRepository<WxWechat, Long>, JpaSpecificationExecutor<WxWechat> {
    @Query("from WxWechat w where w.appId = ?1 and w.status!=-1")
    WxWechat findByAppId(String appId);
    @Query("from WxWechat w where w.appId = ?1 and w.code!= ?2 and w.status!=-1")
    WxWechat findByAppIdExcludeCode(String appId, String code);
    @Query("from WxWechat w where w.code =?1 and w.status!=-1")
    WxWechat findByCode(String code);
}

+ 73 - 0
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WechatService.java

@ -0,0 +1,73 @@
package com.yihu.jw.wx.service;
import com.yihu.jw.restmodel.base.BaseContants;
import com.yihu.jw.restmodel.common.CommonContants;
import com.yihu.jw.restmodel.exception.ApiException;
import com.yihu.jw.wx.dao.WechatDao;
import com.yihu.jw.wx.model.WxWechat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
/**
 * Created by Administrator on 2017/5/20 0020.
 */
public class WechatService {
    @Autowired
    private WechatDao wechatDao;
    public WxWechat createWechat(WxWechat wechat) {//Wechat     //WxWechat
        if (StringUtils.isEmpty(wechat.getCode())) {
            throw new ApiException(BaseContants.Wechat.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getAppId())) {
            throw new ApiException(BaseContants.Wechat.message_fail_appId_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getAppSecret())) {
            throw new ApiException(BaseContants.Wechat.message_fail_appSecret_is_null, CommonContants.common_error_params_code);
        }
        WxWechat wechatTem = wechatDao.findByAppId(wechat.getAppId());
        if (wechatTem != null) {
            throw new ApiException(BaseContants.Wechat.message_fail_appId_exist, CommonContants.common_error_params_code);
        }
        return wechatDao.save(wechat);
    }
    public WxWechat updateWxchat(WxWechat wechat) {
        if (StringUtils.isEmpty(wechat.getCode())) {
            throw new ApiException(BaseContants.Wechat.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getAppId())) {
            throw new ApiException(BaseContants.Wechat.message_fail_appId_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getAppSecret())) {
            throw new ApiException(BaseContants.Wechat.message_fail_appSecret_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getCode())) {
            throw new ApiException(BaseContants.Wechat.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        WxWechat wechatTem = wechatDao.findByAppIdExcludeCode(wechat.getAppId(),wechat.getCode());
        if(wechatTem!=null){
            throw new ApiException(BaseContants.Wechat.message_fail_appId_exist, CommonContants.common_error_params_code);
        }
        return wechatDao.save(wechat);
    }
    public WxWechat findByCode(String code) {
        WxWechat wxWechat = wechatDao.findByCode(code);
        if (wxWechat == null) {
            throw new ApiException(BaseContants.Wechat.message_fail_code_no_exist, CommonContants.common_error_params_code);
        }
        return wxWechat;
    }
    public void deleteWechat(String code) {
        WxWechat wxWechat = wechatDao.findByCode(code);
        if (wxWechat == null) {
            throw new ApiException(BaseContants.Wechat.message_fail_code_no_exist, CommonContants.common_error_params_code);
        }
        wxWechat.setStatus("-1");
        wechatDao.save(wxWechat);
    }
}