用户查看到商家上架宠物后,觉得ok后。进行领养,领养是就要创建订单。
提交过去后业务逻辑
1)修改宠物状态
2)绑定用户
3)创建领养订单
将pay拷贝为adoptOrder,替换引入路径,修改title,
petDetail.html的立即购买A标签绑定方法携带参数跳转过去
methods:{
adopt(){
location.href="adoptOrder.html?petId="+this.pet.id;
}
},
adoptOrder页面引入vue和axios,包div,解析出url上的petId
mounted() {
let petId = parseUrlParams2Obj(location.href).petId;
if (petId) {
this.order.pet_id = petId;
this.$http.get("/pet/" + petId)
.then(result => {
this.pet = result.data;
})
.catch(result => {
alert("系统错误!");
})
}
}
结合用户地址(新增模块增删改查),物流方式(来自数据字典),支付方式(来自数据字典),暂时简单做都写死
调整领养结算页面…
点击提交订单按钮绑定事件带数据发请求
data: {
pet: {},
order: {
service_method: '0',//送货方式
address_id: '1',//收货地址:t_user_address的id
pay_method: '1',//1 支付宝 2 微信 3 银联 0 余额
pet_id: null,
}
},
methods: {
orderSubmit() {
this.$http.post("/adopt/submit", this.order)
.then(result => {
console.log(result.data);
$("#alipayForm").html(result.data.resultObj);
})
.catch(result => {
alert("系统错误!!");
})
}
},
@PostMapping("/submit")
public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
try{
String payData = adoptOrderService.submit(params, LoginContext.getLoginIn(request));
System.out.println(payData);
return AjaxResult.me().setResultObj(payData);
}catch (Exception e){
e.printStackTrace();
return AjaxResult.me().setSuccess(false).setMessage("下单失败!"+e.getMessage());
}
}
/**
* 领养订单结算
*/
@Override
@Transactional
public String submit(Map<String, Object> params, Logininfo loginIn) {
Long petId = Long.valueOf(params.get("pet_id").toString());
Long addressId = Long.valueOf(params.get("address_id").toString());//收货地址:t_user_address的id
Integer payMethod = Integer.valueOf(params.get("pay_method").toString());//1支付宝 2微信 3银联 0余额
Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());//送货方式
//1.修改状态 下架
Pet pet = petMapper.loadById(petId);
pet.setState(0);
pet.setOffsaletime(new Date());
//2.绑定用户
User user = userMapper.loadByloginInfoId(loginIn.getId());
pet.setUser(user);
pet.setUser_id(user.getId());
pet.setShop_id(pet.getShop().getId());
petMapper.update(pet);
//3.生成订单 一次性
AdoptOrder order = initAdoptOrder(pet, user);
adoptOrderMapper.save(order);
// 3.1 生成订单地址
UserAddress userAddress = userAddressMapper.loadById(addressId);
OrderAddress orderAddress = userAddress2OrderAddress(order, userAddress);
orderAddressMapper.save(orderAddress);
// 4.支付单
// 5.订单定时取消任务 @TODO
return null;
}
private AdoptOrder initAdoptOrder(Pet pet, User user) {
AdoptOrder order = new AdoptOrder();
order.setDigest("【摘要】" + pet.getName());
order.setPrice(pet.getSaleprice());
order.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
order.setLastConfirmTime(new Date(System.currentTimeMillis() + 15*60*1000));//最后确认时间
order.setPet_id(pet.getId());
order.setUser_id(user.getId());
order.setShop_id(pet.getShop().getId());
return order;
}
private OrderAddress userAddress2OrderAddress(AdoptOrder order, UserAddress userAddress) {
OrderAddress orderAddress = new OrderAddress();
BeanUtils.copyProperties(userAddress, orderAddress);
orderAddress.setId(null);
orderAddress.setOrder_id(order.getId());
orderAddress.setOrderSn(order.getOrderSn());
return orderAddress;
}
此项目服务统一由平台提供不由店铺提供,所以t_product表不设shopid字段,
采用方案:平台接单 - 分给店铺 - 店铺提供服务
步骤:
服务备份到订单服务详情
订单地址保存
销量加一
生成支付单
定时任务取消
拷贝adoptOrder页面为productOrder页面
替换大小写,title,
productDetail的立即购买按钮跳转过来
<li>
<div class="clearfix tb-btn tb-btn-buy theme-login">
<a id="LikBuy" title="点此按钮到下一步确认购买信息" href="javascript:;" @click="goOrder">立即购买a>
div>
li>
methods:{
goOrder(){//跳转到服务结算页面
location.href = "productOrder.html?productId="+this.product.id;
}
},
adoptOrder页面调整参数和方法,放开数量的注释,定义数量属性,替换pet为product,
<script type="text/javascript">
new Vue({
el: "#productOrderDiv",
data: {
product: {},
order: {
service_method: '0',//送货方式
address_id: '1',//收货地址:t_user_address的id
pay_method: '1',//1 支付宝 2 微信 3 银联 0 余额
product_id: null,
product_num:1
}
},
methods: {
orderSubmit() {
this.$http.post("/productOrder/submit", this.order)
.then(result => {
console.log(result.data);
location.href = "personCenter.html";
})
.catch(result => {
alert("系统错误!!");
})
}
},
mounted() {
let productId = parseUrlParams2Obj(location.href).productId;
if (productId) {
this.order.product_id = productId;
this.$http.get("/product/" + productId)
.then(result => {
this.product = result.data;
})
.catch(result => {
alert("系统错误!");
})
}
}
})
script>
可多次消费,需要备份订单详情
@PostMapping("/submit")
public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
try{
String payData = productOrderService.submit(params, LoginContext.getLoginIn(request));
System.out.println(payData);
return AjaxResult.me().setResultObj(payData);
}catch (Exception e){
e.printStackTrace();
return AjaxResult.me().setSuccess(false).setMessage("下单失败!"+e.getMessage());
}
}
@Override
public String submit(Map<String, Object> params, Logininfo loginInfo) {
//1.生成订单
Long productId = Long.valueOf(params.get("product_id").toString());
Long addressId = Long.valueOf(params.get("address_id").toString());//收货地址:t_user_address的id
Integer payMethod = Integer.valueOf(params.get("pay_method").toString());//1 支付宝 2 微信 3 银联 0 余额
Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());//送货方式
Integer productNum = Integer.valueOf(params.get("product_num").toString());//购买数量
Product product = productMapper.loadById(productId);
User user = userMapper.loadByloginInfoId(loginInfo.getId());
// 1.1 我们需要分配最近的门店
UserAddress userAddress = userAddressMapper.loadById(addressId);
ProductOrder order = initProductOrder(userAddress, productNum, product, user);
productOrderMapper.save(order);
//2.改销量
Long num = product.getSalecount() + productNum;
product.setSalecount(num);
productMapper.update(product);
//3.生成订单详情? 防止下单之后服务信息变更带来的问题 价格
ProductOrderDetail detail = getProductOrderDetail(product, order, num);
productOrderDetailMapper.save(detail);
//4.订单地址
OrderAddress orderAddress = userAddress2OrderAddress(order, userAddress);
orderAddressMapper.save(orderAddress);
//5.支付 @TODO
//6.订单超时自动取消@TODO
return null;
}
private OrderAddress userAddress2OrderAddress(ProductOrder order, UserAddress userAddress) {
OrderAddress orderAddress = new OrderAddress();
BeanUtils.copyProperties(userAddress, orderAddress);
orderAddress.setId(null);
orderAddress.setOrder_id(order.getId());
orderAddress.setOrderSn(order.getOrderSn());
return orderAddress;
}
private ProductOrderDetail getProductOrderDetail(Product product, ProductOrder order, Long num) {
ProductOrderDetail detail = new ProductOrderDetail();
BeanUtils.copyProperties(product, detail);
detail.setId(null);
detail.setSalecount(num);
detail.setOrder_id(order.getId());
return detail;
}
private ProductOrder initProductOrder(UserAddress userAddress, Integer productNum, Product product, User user) {
ProductOrder order = new ProductOrder();
order.setDigest("【摘要】" + product.getName());
order.setPrice(product.getSaleprice().multiply(new BigDecimal(productNum)));//总价=单价*数量
order.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
order.setLastConfirmTime(new Date(System.currentTimeMillis() + 15 * 60 * 1000));//最后确认时间
order.setProduct_id(product.getId());
order.setUser_id(user.getId());
Point point = DistanceUtil.getPoint(userAddress.getFullAddress());
if (point != null) {
Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
order.setShop_id(nearestShop.getId());
}
return order;
}
见文档
tips:尽量不要提供修改,覆盖或者分拆支付,只给取消重下或者加单,不然影响多,对账麻烦,
/**
* 领养订单后台查询接口,平台管理,店铺员工(店铺管理员+员工)
*/
@PostMapping("/admin")
public PageList<AdoptOrder> queryAdmin(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
try {
Logininfo loginInfo = LoginContext.getLoginIn(request);
return adoptOrderService.queryAdmin(query, loginInfo.getId());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 前端用户查询自己领养订单
*/
@PostMapping("/user")
public PageList<AdoptOrder> queryUser(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
try {
Logininfo loginInfo = LoginContext.getLoginIn(request);
return adoptOrderService.queryUser(query, loginInfo.getId());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public PageList<AdoptOrder> queryAdmin(AdoptOrderQuery query, Long loginInfoId) {
//1.通过loginInfoID查询出Employee
Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
//2.如果employee中的shopID不为null,就是店铺。否则就是平台员工
if (employee.getShop_id() != null) {
query.setShopId(employee.getShop_id());
}
return super.queryPage(query);
}
@Override
public PageList<AdoptOrder> queryUser(AdoptOrderQuery query, Long loginInfoId) {
User user = userMapper.loadByloginInfoId(loginInfoId);
query.setUserId(user.getId());
return super.queryPage(query);
}
<sql id="whereSql">
<where>
<if test="keyword!=null and keyword!=''">
AND t.name LIKE concat('%',#{keyword},'%')
if>
<if test="userId!= null">
AND t.user_id =#{userId}
if>
<if test="shopId!= null">
AND t.shop_id =#{shopId}
if>
where>
sql>
<select id="queryCount" parameterType="AdoptOrderQuery" resultType="integer">
select count(*) from t_order_adopt t
<include refid="whereSql"/>
select>
<select id="queryData" parameterType="AdoptOrderQuery" resultType="AdoptOrder">
select t.* from t_order_adopt t
<include refid="whereSql"/>
limit #{start},#{pageSize}
select>
拷贝department.vue为adoptOrder.vue,替换大小写,交给路由
调整页面内容
拷贝mySearchMaster.html为myAdoptOrder.html,从个人中心跳转进去
调整页面内容
订单查看列表页尽量不要关联查询,可以加详情,针对各条单独查详情
https://open.alipay.com/platform/home.htm
控制台 - 开发服务 - 研发服务 - 沙箱环境
pethome与支付宝各有一套公钥和私钥,要交换公钥
安装支付宝开放平台开发助手
rsa - Java适用 - 生成秘钥
复制自己的公钥贴到支付宝平台秘钥设置,可得到支付宝公钥
保存支付宝的公钥,自己的私钥和appid到数据库t_pay_alipay_info中
回到沙箱账号可以做模拟