LoginAction.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package com.minpay.common.action;
  2. import com.alipay.api.AlipayApiException;
  3. import com.alipay.api.AlipayClient;
  4. import com.alipay.api.DefaultAlipayClient;
  5. import com.alipay.api.request.AlipaySystemOauthTokenRequest;
  6. import com.alipay.api.request.AlipayUserInfoShareRequest;
  7. import com.alipay.api.response.AlipaySystemOauthTokenResponse;
  8. import com.alipay.api.response.AlipayUserInfoShareResponse;
  9. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  10. import com.minpay.common.bean.User;
  11. import com.minpay.common.constant.ServConstant;
  12. import com.minpay.common.pay.bean.PrePay;
  13. import com.minpay.common.service.IAccountService;
  14. import com.minpay.common.service.IPayOsService;
  15. import com.minpay.common.service.IPublicService;
  16. import com.minpay.common.service.impl.PayOsServiceImpl;
  17. import com.minpay.common.service.impl.PublicServiceImpl;
  18. import com.minpay.common.util.*;
  19. import com.minpay.db.table.mapper.PubSendCodeMapper;
  20. import com.minpay.db.table.mapper.VmAccountMapper;
  21. import com.minpay.db.table.mapper.VmEquipmentInfMapper;
  22. import com.minpay.db.table.mapper.VmPersonInfMapper;
  23. import com.minpay.db.table.model.*;
  24. import com.minpay.db.table.own.mapper.SequenceMapper;
  25. import com.startup.minpay.frame.business.IMINAction;
  26. import com.startup.minpay.frame.business.MINHttpServletRequestContext;
  27. import com.startup.minpay.frame.business.res.MINActionResult;
  28. import com.startup.minpay.frame.exception.MINBusinessException;
  29. import com.startup.minpay.frame.service.MINServiceLocator;
  30. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  31. import com.startup.minpay.frame.service.base.IMINSessionService;
  32. import com.startup.minpay.frame.service.base.Service;
  33. import com.startup.minpay.frame.session.MINSession;
  34. import com.startup.minpay.frame.target.MINAction;
  35. import com.startup.minpay.frame.target.MINComponent;
  36. import com.startup.minpay.frame.target.MINParam;
  37. import com.startup.minpay.frame.target.MINValidator;
  38. import net.sf.json.JSONObject;
  39. import org.apache.commons.lang.StringUtils;
  40. import javax.servlet.http.HttpServletResponse;
  41. import javax.servlet.http.HttpSession;
  42. import java.util.List;
  43. /**
  44. * 登录处理类
  45. * @author xu
  46. */
  47. @MINComponent
  48. public class LoginAction implements IMINAction {
  49. // 登录
  50. public final static String LOGIN = "login";
  51. // 发送信息
  52. public final static String SENSMS = "sendSmsMsg";
  53. /**
  54. * 发送短信验证码
  55. * @param mobileNo 手机号码
  56. * @param request
  57. * @return
  58. * @throws Exception
  59. */
  60. @MINAction(value = SENSMS)
  61. public MINActionResult sendSmsMsg(
  62. @MINParam(key = "mobileNo", regex = RegexUtil.MOBILE, error = "请输入正确的手机号码") String mobileNo,
  63. @MINParam(key = "channel") String channel,
  64. MINHttpServletRequestContext request
  65. ) throws Exception {
  66. MINActionResult res = new MINActionResult();
  67. // 验证是否已经发送验证码
  68. HttpSession httpSession = request.getSession();
  69. String lastMobileTime = (String)httpSession.getAttribute(ServConstant.LAST_MOBILE_TIME);
  70. // 如果已经发送过短信验证码,并且在60S以内,稍后再发送
  71. if(StringUtils.isNotBlank(lastMobileTime)
  72. && DateUtil.getDifferentTimeByMinute(-1).compareTo(lastMobileTime) < 0) {
  73. throw new MINBusinessException("JA000035", "请勿频繁发送验证码!");
  74. }
  75. // 是否一分钟内已经有短信验证码
  76. PubSendCodeExample deExp = new PubSendCodeExample();
  77. deExp.createCriteria().andUseridEqualTo(mobileNo).andLasttimeGreaterThan(DateUtil.getDifferentTimeByMinute(-1));
  78. List<PubSendCode> deList = Service.lookup(IMINDataBaseService.class).getMybatisMapper(PubSendCodeMapper.class).selectByExample(deExp);
  79. if(deList.size() > 0) {
  80. throw new MINBusinessException("JA000036", "发送验证码过于频繁,请稍后重试!");
  81. }
  82. // 获取随机数
  83. String random = PublicServiceImpl.generateNumber(6);
  84. // 获取验证码时间
  85. httpSession.setAttribute(ServConstant.LAST_MOBILE_TIME, DateUtil.getCurrentDateTimeString());
  86. // 验证码
  87. httpSession.setAttribute(ServConstant.MOBILE_RANDOM, random);
  88. // 手机号
  89. httpSession.setAttribute(ServConstant.MOBILE_NO, mobileNo);
  90. //短信渠道
  91. String messageChannel = Service.lookup(IPublicService.class).getSysParValue("SHORT_MESSAGE_"+channel);
  92. if("aliyun".equals(messageChannel)){
  93. //获取阿里云模板code
  94. String sms = Service.lookup(IPublicService.class).getSysParValue(channel+"_ALI_CODE");
  95. //获取签名
  96. String sign = Service.lookup(IPublicService.class).getSysParValue(channel+"_ALI_SIGN");
  97. // 暂时屏蔽阿里云的
  98. //SendSmsResponse response = SmsAliSendUtil.sendSms(mobileNo, ServConstant.BTXZS_NAME, "SMS_145590111", "{code:" + random + "}");
  99. SendSmsResponse response = SmsAliSendUtil.sendSms(mobileNo, sign, sms, "{code:" + random + "}",channel);
  100. if(!"OK".equals(response.getCode())) {
  101. throw new MINBusinessException("发送短信失败");
  102. }
  103. }else if("jisu".equals(messageChannel)){
  104. String regVal = Service.lookup(IPublicService.class).getSysParValue(channel + "_REG_MOBILE_MODE");
  105. // 发送短信
  106. String msg = regVal.replaceAll("@", random);
  107. // 非debug模式发送短信验证码
  108. if(!ServConstant.IS_DEBUG) {
  109. SmsSendUtil.sendSms(mobileNo, msg);
  110. }
  111. }
  112. String nowTime = DateUtil.getCurrentDateTimeString();
  113. PubSendCode sendDetail = new PubSendCode();
  114. sendDetail.setCode(random);
  115. sendDetail.setUserid(mobileNo);
  116. sendDetail.setLasttime(nowTime);
  117. sendDetail.setStatus("0");
  118. sendDetail.setFirsttime(nowTime);
  119. Service.lookup(IMINDataBaseService.class).getMybatisMapper(PubSendCodeMapper.class).insert(sendDetail);
  120. return res;
  121. }
  122. /**
  123. *小程序获取用户OPENID,然后登陆
  124. * @param code 用户code
  125. * @param type 登录类型:WX微信AL支付宝
  126. * @param channel 渠道:售货机=V01
  127. * @param equNo 售货机编号
  128. * @param version 版本
  129. * @param response
  130. * @param request
  131. * @return
  132. * @throws Exception
  133. */
  134. @MINAction(value = LOGIN, session = false)
  135. public MINActionResult login(
  136. @MINParam(key = "code") String code,
  137. @MINParam(key = "type") String type,
  138. @MINParam(key = "channel") String channel,
  139. @MINParam(key = "equNo") String equNo,
  140. @MINParam(key = "version") String version,
  141. HttpServletResponse response,
  142. MINHttpServletRequestContext request
  143. ) throws Exception {
  144. MINActionResult res = new MINActionResult();
  145. /*if(CommonUtil.isEmpty(equNo)){
  146. throw new BusinessCodeException("JINM1101");//售货机异常,请联系工作人员
  147. }
  148. //解密售货机编号
  149. String equNoNew = OffSetUtil.deScanCode(equNo);*/
  150. String nowTime = DateUtil.getCurrentDateTimeString();
  151. String versionCheck = Service.lookup(IPublicService.class).getSysParValue(channel + "_CHECK_VERSION");
  152. // 检查是否是检查
  153. if(!CommonUtil.isEmpty(version) && versionCheck.equals(version)) {
  154. res.set("isCheckIng", "1");
  155. } else {
  156. res.set("isCheckIng", "0");
  157. }
  158. VmEquipmentInf vmEquipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(equNo);
  159. if(vmEquipmentInf == null){
  160. throw new MINBusinessException("机器不存在");
  161. }
  162. MINActionResult result = null;
  163. if("WX".equals(type)){
  164. result = this.getWxPayOpenid(channel, code);
  165. }else if("AL".equals(type)){
  166. result = this.getALiPayOpenid(channel, code);
  167. }
  168. if(CommonUtil.isEmpty(result.get("openId"))) {
  169. throw new MINBusinessException("JA0001", "非法登录!");
  170. }
  171. // 获取是否有用户
  172. VmPersonInf person = new VmPersonInf();
  173. VmPersonInfExample personex = new VmPersonInfExample();
  174. personex.createCriteria().andChannelEqualTo(channel).andTypeEqualTo(type).andOpenidEqualTo(result.get("openId"));
  175. List<VmPersonInf> perLst = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).selectByExample(personex);
  176. if(perLst.size() == 0) {
  177. //获取用户主键
  178. String perId = Service.lookup(IMINDataBaseService.class).getMybatisMapper(SequenceMapper.class).getSequence("VM_PERSON_INF_NO");
  179. person.setId(perId);
  180. person.setName(result.get("nickName"));
  181. person.setHeadpath(result.get("headImgurl"));
  182. person.setOpenid(result.get("openId"));
  183. person.setStatus("00");//正常
  184. person.setChannel(channel);
  185. person.setIsUser("0");//是否商户0否1是
  186. person.setPhone("00000000000");
  187. person.setType(type);//类型:WX微信AL支付宝
  188. person.setCreateUser(perId);
  189. person.setCreateTime(nowTime);
  190. person.setModifyUser(perId);
  191. person.setModifyTime(nowTime);
  192. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).insertSelective(person);
  193. // 创建用户基本账户
  194. IAccountService service = Service.lookup(IAccountService.class);
  195. // 01:基本账户 02:提现账户 03:待结算账户
  196. service.addAccount(channel, perId, "01",equNo);
  197. service.addAccount(channel, perId, "02",equNo);
  198. service.addAccount(channel, perId, "03",equNo);
  199. }if(perLst.size() > 1) {
  200. throw new MINBusinessException("数据异常");
  201. }if(perLst.size() == 1) {
  202. person = perLst.get(0);
  203. if(!person.getStatus().equals("00")){
  204. throw new MINBusinessException("账号异常,请联系管理员");
  205. }
  206. res.set("isMerchants","0");
  207. if("1".equals(person.getIsUser())){
  208. if(vmEquipmentInf.getBranchid().equals(person.getUserId())){
  209. res.set("isMerchants",person.getIsUser()); //是本机器的商户(是否管理员)
  210. res.set("role",person.getRole());
  211. }
  212. }
  213. //查询是否有所属机器是否有账户
  214. VmAccountExample vmAccountExample = new VmAccountExample();
  215. vmAccountExample.createCriteria().andEquipmentIdEqualTo(equNo).andUsridEqualTo(person.getId()).andChannelEqualTo(person.getChannel());
  216. List<VmAccount> vmAccountList = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmAccountMapper.class).selectByExample(vmAccountExample);
  217. if(vmAccountList.size() == 0){
  218. // 创建用户基本账户
  219. IAccountService service = Service.lookup(IAccountService.class);
  220. // 01:基本账户 02:提现账户 03:待结算账户
  221. service.addAccount(channel, person.getId(), "01",equNo);
  222. service.addAccount(channel, person.getId(), "02",equNo);
  223. service.addAccount(channel, person.getId(), "03",equNo);
  224. }
  225. person.setName(result.get("nickName"));
  226. person.setHeadpath(result.get("headImgurl"));
  227. person.setModifyTime(nowTime);
  228. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPersonInfMapper.class).updateByExampleSelective(person, personex);
  229. }
  230. // 注册到session
  231. User sessionUser = new User(person);
  232. sessionUser.setEquipmentNo(equNo);
  233. sessionUser.setBranchId(vmEquipmentInf.getBranchid());
  234. res.setSessionId(MINServiceLocator.getInstance().lookup(IMINSessionService.class).registerSession(request, sessionUser));
  235. res.set("nickName", result.get("nickName"));
  236. res.set("headImgurl", result.get("headImgurl"));
  237. res.set("equipmentAddress", vmEquipmentInf.getAddress());
  238. res.set("headImeiaddr", vmEquipmentInf.getImeiAddr());
  239. res.set("versionType", vmEquipmentInf.getVersionType());
  240. res.set("equType", vmEquipmentInf.getEquType());
  241. res.set("payment", vmEquipmentInf.getPayment()); //支付金额
  242. return res;
  243. }
  244. @MINValidator(value = LOGIN)
  245. public MINActionResult loginValidator(
  246. @MINParam(key = "code" ) String code,
  247. @MINParam(key = "type") String type,
  248. @MINParam(key = "channel") String channel,
  249. @MINParam(key = "equNo") String equNo,
  250. @MINParam(key = "version") String version,
  251. MINSession session) throws MINBusinessException {
  252. MINActionResult res = new MINActionResult();
  253. if(CommonUtil.isEmpty(code) ||
  254. CommonUtil.isEmpty(type) ||
  255. CommonUtil.isEmpty(channel) ||
  256. CommonUtil.isEmpty(equNo) ||
  257. CommonUtil.isEmpty(version)){
  258. throw new MINBusinessException("参数为空或数据异常");
  259. }
  260. //校验机器编号是否存在
  261. VmEquipmentInf vmEquipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(equNo);
  262. if(vmEquipmentInf == null){
  263. throw new MINBusinessException("机器不存在或异常!");
  264. }
  265. return res;
  266. }
  267. /***
  268. * 微信用户授权,获取用户信息
  269. * @param code
  270. * @param channel
  271. * @return
  272. * @throws MINBusinessException
  273. */
  274. private MINActionResult getWxPayOpenid(String channel ,String code) throws MINBusinessException {
  275. MINActionResult res = new MINActionResult();
  276. //在微信的appId
  277. String appId = Service.lookup(IPublicService.class).getSysParValue(channel + "_APP_ID");
  278. //在微信的secret
  279. String appSecret = Service.lookup(IPublicService.class).getSysParValue(channel + "_APP_SECRET");
  280. //微信通用请求路径
  281. String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
  282. //配置参数
  283. StringBuffer paramStr = new StringBuffer();
  284. paramStr.append("appid=");
  285. paramStr.append(appId);
  286. paramStr.append("&secret=");
  287. paramStr.append(appSecret);
  288. paramStr.append("&code=");
  289. paramStr.append(code);
  290. paramStr.append("&grant_type=authorization_code");
  291. try {
  292. if("123".equals(code)){
  293. res.set("nickName", "Clearlove");
  294. res.set("headImgurl", "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIZ3h0UPPibR06bowe0Zpq5LeEib67VrRT44bibRDFkEQq89G6cMc9JxoOn1vPG89icEbXyZiaNKbhRLsw/132");
  295. res.set("openId", "oKW8s1T5vCXVAqfZ44tcFAbYdetI");
  296. }else{
  297. //发送请求
  298. String jsonObj = HttpPostUtil.sendPost(openUrl, paramStr.toString());
  299. JSONObject js = JSONObject.fromObject(jsonObj);
  300. //获取openId
  301. String openId = js.getString("openid");//用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
  302. //微信通用请求路径
  303. String openUrl2 = "https://api.weixin.qq.com/sns/userinfo";
  304. //配置参数
  305. StringBuffer paramStr2 = new StringBuffer();
  306. paramStr2.append("access_token=");
  307. paramStr2.append(js.getString("access_token"));
  308. paramStr2.append("&openid=");
  309. paramStr2.append(openId);
  310. paramStr2.append("&lang=zh_CN");
  311. //发送请求
  312. String jsonObj2 = HttpPostUtil.sendPost(openUrl2, paramStr2.toString());
  313. JSONObject js2 = JSONObject.fromObject(jsonObj2);
  314. String nickName = EmojiFilter.filterEmoji(js2.getString("nickname"));
  315. // String nickName = js2.getString("nickname");
  316. String headImgurl = js2.getString("headimgurl");
  317. res.set("openId", openId);
  318. res.set("headImgurl", headImgurl);
  319. res.set("nickName", nickName);
  320. }
  321. } catch (Exception e) {
  322. throw new MINBusinessException("PAY10006", "微信获取openId失败!");
  323. }
  324. return res;
  325. }
  326. /***
  327. * 支付宝用户授权,获取用户信息
  328. * @param code
  329. * @param channel
  330. * @return
  331. * @throws MINBusinessException
  332. */
  333. private MINActionResult getALiPayOpenid(String channel ,String code) throws MINBusinessException {
  334. MINActionResult res = new MINActionResult();
  335. if("123".equals(code)){
  336. res.set("nickName", "啦啦啦");
  337. res.set("headImgurl", "https://tfs.alipayobjects.com/images/partner/T103tfXjpXXXXXXXXX");
  338. res.set("openId", "2088412369546572");
  339. }else{
  340. //在支付宝的appId
  341. String appId = Service.lookup(IPublicService.class).getSysParValue(channel + "_ZHIFUBAO_APP_ID");
  342. //在支付宝的私钥
  343. String privateKeyLocation = ServConstant.PRIVATE_KEY_LOCATION;
  344. //支付宝的公钥s
  345. String publicKeyLocation = ServConstant.PUBLIC_KEY_LOCATION;
  346. //支付宝通用请求路径
  347. String openUrl = "https://openapi.alipay.com/gateway.do";
  348. //根据code获取accessToken
  349. AlipayClient alipayClient = new DefaultAlipayClient(openUrl,
  350. appId, privateKeyLocation, "json","UTF-8", publicKeyLocation,"RSA2");
  351. AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
  352. request.setCode(code);
  353. request.setGrantType("authorization_code");//固定值authorization_code
  354. AlipaySystemOauthTokenResponse response = null;
  355. try {
  356. response = alipayClient.execute(request);
  357. } catch (AlipayApiException e) {
  358. throw new MINBusinessException("PAY10007", "支付宝获取openId失败!");
  359. }
  360. if (response.isSuccess()) {
  361. System.out.println(response.getBody());
  362. System.out.println(response.getAlipayUserId());
  363. String openId = response.getAlipayUserId();
  364. AlipayUserInfoShareRequest request2 = new AlipayUserInfoShareRequest();
  365. AlipayUserInfoShareResponse response2 = null;
  366. try {
  367. response2 = alipayClient.execute(request2,response.getAccessToken());
  368. } catch (AlipayApiException e) {
  369. // TODO Auto-generated catch block
  370. e.printStackTrace();
  371. }
  372. if (response2.isSuccess()) {
  373. System.out.println(response2.getBody());
  374. System.out.println(response2.getUserId());
  375. String avatar = response2.getAvatar();
  376. String nickName = EmojiFilter.filterEmoji(response2.getNickName());
  377. // String nickName = response2.getNickName();
  378. String userId = response2.getUserId();
  379. res.set("openId", userId);
  380. res.set("headImgurl", avatar);
  381. res.set("nickName", nickName);
  382. }
  383. }
  384. }
  385. return res;
  386. }
  387. // public static void main(String args[]) {
  388. // String appId = "2019051764975953";
  389. // //在支付宝的私钥
  390. // String privateKeyLocation = ServConstant.PRIVATE_KEY_LOCATION;
  391. // //支付宝的公钥s
  392. // String publicKeyLocation = ServConstant.PUBLIC_KEY_LOCATION;
  393. // //支付宝通用请求路径
  394. // String openUrl = "https://openapi.alipay.com/gateway.do";
  395. // //根据code获取accessToken
  396. // AlipayClient alipayClient = new DefaultAlipayClient(openUrl,
  397. // appId, privateKeyLocation, "json","GBK", publicKeyLocation);
  398. // AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
  399. // request.setCode("21321321321");
  400. // request.setGrantType("authorization_code");//固定值authorization_code
  401. // AlipaySystemOauthTokenResponse response = null;
  402. // try {
  403. // response = alipayClient.execute(request);
  404. // } catch (AlipayApiException e) {
  405. // e.printStackTrace();
  406. // }
  407. // if (response.isSuccess()) {
  408. // System.out.println(response.getBody());
  409. // System.out.println(response.getAlipayUserId());
  410. // }
  411. // }
  412. @MINAction(value = "test" , session = false)
  413. public MINActionResult test(
  414. MINSession session ) throws MINBusinessException{
  415. MINActionResult res = new MINActionResult();
  416. PrePay prePay = new PrePay();
  417. IPayOsService IPayOsService = new PayOsServiceImpl();
  418. prePay.setBody("大头");
  419. try {
  420. IPayOsService.prePay(prePay);
  421. } catch (MINBusinessException e) {
  422. e.printStackTrace();
  423. }
  424. return res;
  425. }
  426. }