package cn.exrick.xboot.your.controller.wx; import cn.exrick.xboot.core.common.utils.ResultUtil; import cn.exrick.xboot.your.entity.*; import cn.exrick.xboot.your.service.*; import cn.exrick.xboot.your.util.HttpUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.*; /** * @author whj */ @Slf4j @RestController @Api(tags = "订单接口") @RequestMapping("/xboot/wx") @Transactional public class IndexController { @Autowired private IOrderTaskService iOrderTaskService; @Autowired private ICustomerService iCustomerService; @Autowired private IOrderDetailService iOrderDetailService; //4.获取今日配送订单 @RequestMapping("/getTodayOrder") public Object getTodayOrder(String customerId){ QueryWrapper wrapper2 = new QueryWrapper(); String format = DateUtil.format(new Date(), "yyyy-MM-dd"); wrapper2.eq("customer_id",customerId); wrapper2.eq("send_date",format); OrderTask orderTask = iOrderTaskService.getOne(wrapper2); if(orderTask==null){ return ResultUtil.data(new ArrayList<>()); }else{ QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("order_id",orderTask.getId()); List list = iOrderDetailService.list(wrapper); return ResultUtil.data(list); } } //3.微信绑定零售许可证 //licence:零售许可证号 //openId:微信id @RequestMapping("/bindWx") public Object bindWx(String licence,String openId){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("licence",licence); Customer one = iCustomerService.getOne(wrapper); if(one==null){ return ResultUtil.error("零售许可证无效");//首次登陆需绑定零售许可证 }else { one.setOpenId(openId); iCustomerService.saveOrUpdate(one); return ResultUtil.data(one,"登录成功"); } } //2.微信登陆 @RequestMapping("/login") public Object doLogin(String code){ JSONObject SessionKeyOpenId = getSessionKeyOrOpenId( code ); String openid = SessionKeyOpenId.getStr("openid"); if(StrUtil.isEmpty(openid)){ return ResultUtil.error(SessionKeyOpenId.toString()); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("open_id",openid); Customer one = iCustomerService.getOne(wrapper); if(one==null){ return ResultUtil.error(openid);//首次登陆需绑定零售许可证 }else { return ResultUtil.data(one,"登录成功"); } } public static JSONObject getSessionKeyOrOpenId(String code){ //微信端登录code String wxCode = code; String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wx0f10f6d253f3ee6b&secret=4d4cbc8da31a96559114ad693de70631&grant_type=authorization_code&js_code="+code; JSONObject jsonObject = JSONUtil.parseObj( HttpUtil.get(requestUrl)); return jsonObject; } }