Java tutorial
package com.ironchain.intfc.modules.member; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.validation.Valid; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.ironchain.common.cache.CacheService; import com.ironchain.common.dao.DemandOfferDao; import com.ironchain.common.dao.MemberDao; import com.ironchain.common.dao.MemberLevelDao; import com.ironchain.common.dao.ShopExpressAddressDao; import com.ironchain.common.domain.Constants; import com.ironchain.common.domain.Constants.CacheConstants; import com.ironchain.common.domain.Constants.RegexConstants; import com.ironchain.common.domain.EquipmentClass; import com.ironchain.common.domain.Member; import com.ironchain.common.domain.R; import com.ironchain.common.domain.ShopExpressAddress; import com.ironchain.common.kits.IdcardKit; import com.ironchain.common.sms.SmsService; import com.ironchain.common.upload.UploadService; import com.ironchain.intfc.annotation.IgnoreApiSecurity; import com.ironchain.intfc.annotation.IgnoreAuth; import com.ironchain.intfc.web.ApiBaseController; @RestController @RequestMapping("/api/member") public class MemberController extends ApiBaseController { private static final Logger LOGGER = LoggerFactory.getLogger(MemberController.class); @Autowired private MemberDao memberDao; @Autowired private MemberService memberService; @Autowired private MemberLevelDao memberLevelDao; @Autowired private CacheService cacheService; @Autowired private SmsService smsService; @Autowired private UploadService uploadService; @Autowired private DemandOfferDao demandOfferDao; @Autowired private ShopExpressAddressDao expressAddressDao; /** * * @return */ @IgnoreAuth @RequestMapping("/register") public R register(@RequestParam int type, @RequestParam String email, @RequestParam String mobilephone, @RequestParam String password, @RequestParam String idcard, @RequestParam(required = false) Integer serviceType, @RequestParam(required = false) Long levelId, @RequestParam(required = false) String companyName, @RequestParam(required = false) String companyLegal, @RequestParam(required = false) String companyLegalPhone, @RequestParam(required = false) String companyIdcard, @RequestParam(required = false) String companyTel, @RequestParam(required = false) BigDecimal companyPrecision, @RequestParam(required = false) String companyLicenseImg, @RequestParam(required = false) String companyAddress, @RequestParam(required = false) Long[] companyEquipment, @RequestParam String verifyCode) { Assert.isTrue(verifyCode != null && verifyCode.length() == 6, "???"); // Assert.isTrue(cacheService.check(CacheConstants.VERIFYCODE, mobilephone, verifyCode), "???"); Assert.isTrue(mobilephone.matches(RegexConstants.MOBILE_REGEX), "????"); Assert.isTrue(!memberService.mobilephoneExists(mobilephone), "??"); Assert.isTrue(password.length() >= 6 && password.length() <= 20, "???6?20?"); Member member = memberService.create(type, email, mobilephone, password, idcard, serviceType, levelId, companyName, companyLegal, companyLegalPhone, companyIdcard, companyTel, companyPrecision, companyLicenseImg, companyAddress, companyEquipment); Long uid = member.getId(); String mobile = member.getMobilephone(); String token = memberService.getToken(uid, mobile); cacheService.set(CacheConstants.LOGIN_TOKEN, uid.toString(), token); cacheService.set(CacheConstants.LOGIN_NAME, uid.toString(), mobile); Map<String, Object> map = new HashMap<>(); map.put("userId", uid); map.put("name", member.getName()); map.put("token", token); return R.ok(map); } /** * * @param name * @param password * @return */ @IgnoreAuth @RequestMapping("/login") public R login(@RequestParam String mobilephone, @RequestParam String password) { LOGGER.debug("? mobilephone{} password{}", mobilephone, password); Assert.hasText(mobilephone, "???"); Assert.hasText(password, "??"); Member member = memberService.findByMobilephoneAndPassword(mobilephone, password); Long uid = member.getId(); String mobile = member.getMobilephone(); String token = memberService.getToken(uid, mobile); cacheService.set(CacheConstants.LOGIN_TOKEN, uid.toString(), token); cacheService.set(CacheConstants.LOGIN_NAME, uid.toString(), mobile); Map<String, Object> map = new HashMap<>(); map.put("userId", uid); map.put("name", member.getName()); map.put("token", token); return R.ok(map); } /** * ???? * @param mobilephone * @return */ @IgnoreAuth @RequestMapping("/send_verify_code") public R sendVerifyCode(@RequestParam String mobilephone, @RequestParam int type) { LOGGER.debug("????? mobilephone{} type{}", mobilephone, type); //?? Assert.isTrue(mobilephone.matches(RegexConstants.MOBILE_REGEX), "????"); String code = memberService.createRandomCode(); Map<String, Object> param = new HashMap<>(); param.put("code", code); switch (type) { case 1:// if (memberService.mobilephoneExists(mobilephone)) throw new IllegalArgumentException("??"); smsService.send("sms.register-verify-code", new Object[] { code }, mobilephone); break; case 2://? if (!memberService.mobilephoneExists(mobilephone)) throw new IllegalArgumentException("???"); smsService.send("sms.reset-verify-code", new Object[] { code }, mobilephone); break; default: throw new IllegalArgumentException("?type"); } //? cacheService.set(CacheConstants.VERIFYCODE, mobilephone, code); return R.ok(); } /** * ?? 1 * @return */ @IgnoreAuth @RequestMapping("/reset_password_one") public R resetPasswordStepOne() { return R.ok(); } /** * ?? 2 * @return */ @IgnoreAuth @RequestMapping("/reset_password_two") public R resetPasswordStepTwo(@RequestParam String newPassword) { LOGGER.debug("?? 2? newPassword{}", newPassword); return R.ok(); } /** * ? * @return */ @RequestMapping("/modify_password") public R modifyPassword(@RequestParam Long userId, @RequestParam String oldPassword, @RequestParam String newPassword) { LOGGER.debug("?? userId{} oldPassword{} newPassword{}", userId, oldPassword, newPassword); memberService.modifyPassword(userId, oldPassword, newPassword); return R.ok(); } /** * ?? */ @RequestMapping("/company_info") public R companyInfo(@RequestParam Long id, Long userId) { Member member = memberDao.findOne(id); Assert.isTrue(member != null && member.getType() == Member.TYPE_COMPANY, "?? id:" + id + ", userId:" + userId); Map<String, Object> result = new HashMap<>(); result.put("id", member.getId()); result.put("companyName", member.getCompanyName());//??? result.put("companyLegal", member.getCompanyLegal());// result.put("companyLegalPhone", member.getCompanyLegalPhone()); result.put("companyIdcard", member.getCompanyIdcard()); result.put("levelName", member.getLevel().getName());//?? result.put("bidCount", demandOfferDao.countByOfferId(id));//? List<Map<String, Object>> companyEqus = new ArrayList<>(); for (EquipmentClass equClass : member.getCompanyEquipment()) { companyEqus.add(equClass.toMap()); } result.put("companyEquipment", companyEqus); result.put("companyPrecision", member.getCompanyPrecision()); result.put("companyAdress", member.getCompanyAddress()); result.put("companyTel", member.getCompanyTel()); result.put("companyLicenseImg", member.getCompanyLicenseImg()); return R.ok(result); } /** * ? * @return */ @RequestMapping("/info") public R info(@RequestParam Long userId) { Member member = memberDao.findOne(userId); Map<String, Object> result = new HashMap<>(); result.put("name", member.getName()); result.put("headImg", member.getHeadImg()); result.put("status", member.getLevel() != null ? "" : ""); result.put("type", member.getTypeStr()); return R.ok(result); } private static int[][] SIZE_ARR = { { 360, 360 }, { 240, 240 }, { 120, 120 } }; /** * ? * @return */ @IgnoreApiSecurity(ignoreRequest = true, ignoreResponse = false) @RequestMapping("/modify_head_img") public R modifyHeadImg(@RequestParam Long userId, @RequestParam MultipartFile headImg) { String fileName = headImg.getOriginalFilename(); int idx = fileName.lastIndexOf("."); if (idx == -1 || !UploadService.imgExt.contains(fileName.substring(idx + 1).toLowerCase())) { return R.error("??????\n??" + StringUtils.join(UploadService.imgExt, ",") + "?"); } String uploadPath = uploadService.store(true, SIZE_ARR, headImg)[0]; Member member = memberDao.findOne(userId); member.setHeadImg(uploadPath); memberDao.save(member); return R.ok(); } /** * ?? * @param userId * @param name * @return */ @RequestMapping("/modify_name") public R modifyName(@RequestParam Long userId, @RequestParam String name) { Member member = memberDao.findOne(userId); member.setName(name); memberDao.save(member); return R.ok(); } /** * * @return */ @IgnoreAuth @RequestMapping("/level/list") public R levelList() { return R.ok(memberLevelDao.findByStatusOrderByPriceAsc(Constants.DISPLAY_SHOW)); } /** * ? * @return */ @RequestMapping("/levelup") public R levelup(@RequestParam Long userId, @RequestParam Long id, @RequestParam Integer serviceType, @RequestParam String companyName, @RequestParam String companyLegal, @RequestParam String companyLegalPhone, @RequestParam String companyIdcard, @RequestParam String companyTel, @RequestParam BigDecimal companyPrecision, @RequestParam String companyLicenseImg, @RequestParam String companyAddress, @RequestParam Long[] companyEquipment) { Assert.hasText(companyName, "????"); Assert.hasText(companyLegal, "???"); Assert.isTrue(companyTel != null && companyTel.matches(RegexConstants.TEL_REGEX), "????"); Assert.isTrue(companyIdcard != null && IdcardKit.validateCard(companyIdcard), "???"); Assert.hasText(companyAddress, "???"); Assert.hasText(companyLicenseImg, "???"); memberService.addLevelup(id, memberDao.findOne(userId), serviceType, companyName, companyLegal, companyLegalPhone, companyIdcard, companyTel, companyPrecision, companyLicenseImg, companyAddress, companyEquipment); return R.ok(); } /** * ? * @return */ @RequestMapping("/express/address/list") public R expressAddress(@RequestParam Long userId) { return R.ok(memberService.findExpressAddressById(userId)); } /** * ? * @return */ @RequestMapping("/express/address/save") public R saveExpressAddress(@Valid ShopExpressAddress expressAddress, @RequestParam Long userId) { expressAddress.setMemberId(userId); expressAddressDao.save(expressAddress); return R.ok(); } /** * ? * @param userId * @param id * @return */ @RequestMapping("/express/address/info") public R expressAddressInfo(@RequestParam Long userId, @RequestParam Long id) { return R.ok(memberService.findExpressAddressInfo(userId, id)); } }