wang-hao-jie
2021-12-27 fb53a9997de491c3d0a30ed333a6bd3b24877b82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;
 
 
    //获取今日配送订单
    @RequestMapping("/getTodayOrder")
    public Object getTodayOrder(String customerId){
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        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<OrderDetail> wrapper = new QueryWrapper<OrderDetail>();
            wrapper.eq("order_id",orderTask.getId());
            List<OrderDetail> list = iOrderDetailService.list(wrapper);
            return ResultUtil.data(list);
        }
    }
 
    //微信绑定零售许可证
    @RequestMapping("/bindWx")
    public Object bindWx(String licence,String openId){
        QueryWrapper<Customer> 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,"登录成功");
        }
    }
 
    //微信登陆
    @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<Customer> 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;
    }
}