微信小程序+java springboot后台
2022-10-28 15:19:56
300
{{single.collect_count}}

1、准备工作:(个人用户的小程序服务功能并不多,企业小程序的服务功能开发多)

自己的域名、服务器,国内的话,最好是备案过的。

2、无小程序:自己去公众平台进行注册,地址:https://mp.weixin.qq.com/wxopen/waregister?action=step1

3、注册之后登录,绑定开发人员,一般设置自己就行。

开发设置:域名服务器配置自行配置,建议使用宝塔方便快捷。

4、说到这里,随便说下怎么安装使用宝塔吧(服务器运维工具面板)

官网:https://www.bt.cn/

进入官网:点 立即安装 

安装教程 

安装之前,需要去自己服务器后台放行8888端口,这是宝塔访问面板的默认端口。

放行之后,根据自己服务器的操作系统选择命令,登录ssh控制台,执行命令一键安装。

安装成功,成功页面会给出登录路径以及密码,然后登录后台,添加网站:

添加网站:

建站成功的nginx默认页面:

nginx配置:

一键配置https:配置完成后即可进行https的安全访问

另外,随便安装一个tomcat,来部署 java后台的war包:

至此,服务器以及域名已经搭建完成。

5、代码开发

一部分是前端代码,另一部分是后端代码,其实就跟开发 安卓 ios app一样 ,小程序的前端展示代码开发,会js就行,会一些前端代码更好。


前端代码开发:

点击 微信提供的开发工具 下载微信提供的工具

下载完毕后,进行安装,无需配置环境,双击运行即可:

新建项目

 

建项目后,会默认一套微信提供的框架,按照这个编写即可,也可以使用其他工具编写好后,再来这里提交上传代码(可以使用微信提供的git库管理代码)

前端代码就这个样子,然后是后端的java代码。

 

后端java代码:

这里使用一个封装的jar,pom地址

<!-- 小程序 公众号使用sdk --><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>3.3.0</version></dependency>

 然后就是编写自己的java代码(注意配置自己的小程序相关信息):

wx:app-id: appidapp-secret: app密钥mch-id: 商户id(用作微信支付相关)mch-key: 商户密钥notify-url: 商户证书# 商户证书文件路径# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3key-path: xxxxx

后台我使用的springboot,写一个简单controller demo:WxController.java

import java.sql.Timestamp;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.JSON;import cn.binarywang.wx.miniapp.api.WxMaService;import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;import io.swagger.annotations.ApiOperation;/** ** @类名称 WxController.java * @类描述 <pre>微信登录鉴权相关</pre> * @作者yw* @创建时间 2020年6月21日 下午6:38:42 * @版本 5.0.0 * * @修改记录 * <pre> * 版本 修改人 修改日期修改内容描述 * ---------------------------------------------- * 5.0.0 yw 2020年6月21日* ---------------------------------------------- * </pre> */@RestController@RequestMapping("/wx/auth")public class WxController {@Autowiredprivate WxMaService wxService;//上面引入的java里面的服务器@Autowiredprivate AdminUserService adminUserService;//自己的用户相关信息服务,需要自己去编码/** * 微信登录 * * @param wxLoginInfo 请求内容,{ code: xxx, userInfo: xxx } * @param request 请求对象 * @return 登录结果 */@PostMapping("wxlogin")@ApiOperation(value="app用户登录", notes="app用户登录")public ResponseMsg loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {String code = wxLoginInfo.getCode();//该code即是前端调用微信登录获取的AdminUser userInfo = wxLoginInfo.getUserInfo();if (code == null || userInfo == null) {return new ResponseMsg(CommonReturnCode.WX_PARAM_IS_NULL);}String sessionKey = null;String openId = null;try {WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);sessionKey = result.getSessionKey();openId = result.getOpenid();} catch (Exception e) {e.printStackTrace();}//拿到openId,再进行自己的业务操作if (sessionKey == null || openId == null) {return new ResponseMsg(CommonReturnCode.WX_PARAM_IS_NULL);}//比如查询库里面是否存在该用户,有则更新登录次数,无则注册账号,看自己的业务AdminUser user = adminUserService.getModelByLogin(openId);;if (user == null) {user = new AdminUser();user.setLoginName(openId);user.setPassword(openId);user.setHeadUrl(userInfo.getHeadUrl());user.setName(userInfo.getName());user.setCreateTime(new Timestamp(System.currentTimeMillis()));adminUserService.insertModelWithoutNull(user);} else {user.setLoginNum(user.getLoginNum() + 1);adminUserService.updateModelWithoutNull(user);}// token 登录成功后,可以使用jwt,返回给前端token 后续需要token进行接口鉴权验证Map<String, Object> result = new HashMap<>();String subject = JSON.toJSONString(user);//openId 作为id,用户对象作为 subject 这两个参数自己随意String token = TokenUtils.createJwtTokenApp(openId, subject);result.put("token", token);result.put("userInfo", user); //相关信息再返回给前端,token必须return new ResponseMsg(result);}}

到这里微信小程序就已经跟java后台联通上了,然后就是进一步的进行自己的业务开发。

简单分享,祝你顺利,有问题留言,不吝赐教!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}