微信小程序任何的语言都可以做后台,现在微信小程序推出云函数,做后台也可以。但是自己感觉想要完整的后台,做后台用java和php更好点,这样涉及数据库更好的可以数据。
1、wmxl
微信小程序的前段代码(提交数据主要以from表单实现的)
<view class="btn-submit">
<button formType="reset">请完善注册信息</button>
</view>
<form catchsubmit="formSubmit" catchreset="formReset">
<view class="input-list">
<view class="input-item">
<text class="input-item-label">姓名</text>
<view class="input-item-content">
<input type="text" name="name" auto-focus placeholder="请输入您的姓名" bindinput="inputName"></input>
</view>
</view>
<view class="input-item">
<text class="input-item-label">性别</text>
<picker class="input-item-content" bindchange="bindPickerChange" data-pickername="industry" value="{{industryindex}}" range="{{industryarr}}" mode="selector">{{industryarr[industryindex]}}
</picker>
</view>
<view class="input-item">
<text class="input-item-label">账号</text>
<view class="input-item-content">
<input type="idcard" name="tel" placeholder="请输入您的手机号码" maxlength="11" bindinput="inputPhone"></input>
</view>
</view>
<view class="input-item">
<text class="input-item-label">登录密码</text>
<view class="input-item-content">
<input type="password" name="password" auto-focus placeholder="请设置登录密码" bindinput="inputName"></input>
</view>
</view>
<view class="input-item">
<text class="input-item-label">邮箱</text>
<view class="input-item-content">
<input type="text" name="email" auto-focus placeholder="请输入您的邮箱" bindinput="inputName"></input>
</view>
</view>
<view class="input-item">
<text class="input-item-label">注册vip</text>
<picker class="input-item-content" bindchange="bindPickerChange" data-pickername="status" value="{{statusindex}}" range="{{statusarr}}" mode="selector">{{statusarr[statusindex]}}
</picker>
</view>
</view>
<view class="btn-submit">
<button type="primary" formType="submit">提交</button>
<button formType="reset">置空</button>
</view>
/form>
2、js
js是重点,他是一个中间桥梁,获取微信小程序前端的数值和传导ssm的后台。
wx.request这是微信的接口,也就是发起请求。
url: ‘http://localhost:8080/lg/wechat/add’,这就是你的项目的地址,也就是controller。
dada就是你要传到后台的数据。
wx.request({
url: 'http://localhost:8080/lg/wechat/add',
data: {
openid: openid,
userpassword: userpassword,
name: name,
sex: app.sex,
tel: tel,
email: email,
vip: app.vip,
},
3、ssm的后台的具体的实现
因为小程序的数据格式都是json格式,所以我们的ssm后台也必须是就json的格式,Java要实现json的格式,需要对应的jar包,打下自行下载。
package org.lg.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.lg.entity.user;
import org.lg.entity.wcuser;
import org.lg.service.roomlistService;
import org.lg.service.roomtypesService;
import org.lg.service.wcuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.util.JSONPObject;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("wechat")
public class wechatController {
@Autowired
public wcuserService wcservice;
@Autowired
public roomlistService roomlistservice;
@Autowired
public roomtypesService roomtypesservice;
//用户注册
@RequestMapping("add")
@ResponseBody
public JSONObject adduser(@RequestParam("openid") String openid,@RequestParam("name") String name,@RequestParam("sex") String sex,@RequestParam("tel") String tel,
@RequestParam("email") String email,@RequestParam("vip") String vip,HttpServletRequest request,
HttpServletResponse response,@RequestParam("userpassword") String userpassword) {
System.out.println(openid+name+sex+tel+email+vip);
Map<String, String> map = new HashMap<String, String>();
if(openid!=null) {
//判断openid在注册的列表中是否存在
wcuser queryopenid = wcservice.queryopenid(openid);
//String openid2 = queryopenid.getOpenid();
if(queryopenid!=null) {
map.put("msg","您已经注册过,请不要重复注册");
JSONObject json = JSONObject.fromObject(map);
return json;
}else{
wcservice.adduc(openid,name, sex, tel, email, vip,userpassword);
//map.put("status","succ");
map.put("msg","注册成功");
JSONObject json = JSONObject.fromObject(map);
return json;
}
}else {
wcuser wcuser1 = wcservice.queryopenid(openid);
String openid2 = wcuser1.getOpenid();
if(openid2!=null) {
map.put("msg","请不要重复注册");
JSONObject json = JSONObject.fromObject(map);
return json;
}else {
map.put("msg","完善信息");
JSONObject json = JSONObject.fromObject(map);
return json;
}
}
}
}
更多的案例可以关注公众号:小白XBIT