package com.ematou.wxservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.ematou.wxservice.common.web.R;
import com.ematou.wxservice.entity.pojo.ScannVO;
import com.ematou.wxservice.entity.pojo.UserInfo;
import com.ematou.wxservice.entity.vo.BindIngVo;
import com.ematou.wxservice.entity.vo.BindingInfo;
import com.ematou.wxservice.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author lhm
 * @version 1.0
 * 2021-05-12 16:00
 */
@Controller
public class WeChatViewController {

    @Autowired
    UserInfoService userInfoService;


    @GetMapping("/view/")
    public String main() {
        return "hello";
    }

    @GetMapping("/view/binding")
    public String binding(HttpServletRequest request, HttpServletResponse response) {
        //判断是否注册
        UserInfo msg = userInfoService.queryUserInfoByOpenId(request.getParameter("openid"));
        if (msg != null) {
            return "return";
        } else {
            return "binding";
        }

    }


 /*   @PostMapping("/binding")
    public String binding(HttpServletRequest request, Model model) {
        String phone = request.getParameter("phone1");
        String openid = request.getParameter("btnSendCode2");
        String code1 = request.getParameter("code1");

        BindingInfo bindingInfo = new BindingInfo();
        bindingInfo.setOpenId(openid);
        bindingInfo.setTellPhoneNumber(phone);
        bindingInfo.setValidateCode(code1);

        boolean result = userInfoService.binding(bindingInfo);
        if (result) {
            return "return";
        }
        model.addAttribute("errmsg", "验证码错误！");
        return "binding";
    }*/

    @PostMapping("/binding")
    @ResponseBody
    public R<?>  binding(@RequestBody BindIngVo bindIngVo) {
        String phone = bindIngVo.getPhone();
        if(StringUtils.isEmpty(phone)){
            return new R<String>().error("手机号不能为空！");
        }
        String openid = bindIngVo.getOpenId();
        if(StringUtils.isEmpty(openid)){
            return new R<String>().error("网络异常，请退出重试");
        }
        String code1 = bindIngVo.getCode();
        if(StringUtils.isEmpty(phone)){
            return new R<String>().error("验证码不能为空！");
        }

        BindingInfo bindingInfo = new BindingInfo();
        bindingInfo.setOpenId(openid);
        bindingInfo.setTellPhoneNumber(phone);
        bindingInfo.setValidateCode(code1);

        boolean result = userInfoService.binding(bindingInfo);

        if (result) {
            return new R<String>().success();
        }
        return new R<String>().error("验证码错误或未匹配到手机号！");
    }



    /**
     * 扫一扫
     *
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/scanning")
    public String getScanningture(HttpServletRequest request, Model model) {
        ScannVO scannVO = userInfoService.getScanningture();
        return JSONObject.toJSONString(scannVO);
    }


//
//    /**
//     * 获取微信签名signature
//     * @param url jsapi_ticket
//     * add by zl on 2016-2-2
//     * @return signature
//     * @throws
//     */
//    public Map<String, Object> getSignature(String url)  {
//        Map<String, Object> wxMap = new HashMap<String, Object>();
//        String jsapi_ticket = "";
//        Long timeJt = (Long) map.get("timeJt");
//        jsapi_ticket = (String) map.get("jsapi_ticket");
//        Long nowDateJt = new Date().getTime();
//        if (jsapi_ticket != null && timeJt != null && ((nowDateJt - timeJt) < 7200 * 1000L)) {
//            System.out.println("jsapi_ticket 存在");
//            System.out.println("nowDateJt - timeJt=="+(nowDateJt - timeJt));
//        } else {
//            jsapi_ticket = getJsapiTicket();
//            System.out.println("jsapi_ticket 获取");
//            map.put("jsapi_ticket",jsapi_ticket);
//            map.put("timeJt",nowDateJt);
//        }
//
//        //随机字符串和时间戳
//        String new_novestr = RandomStringUtils.randomAlphanumeric(16); //随机字符串
//        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
//        String signature = "";
//        // 拼接字符串
//        String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + new_novestr + "&timestamp=" + timestamp + "&url=" + url;
//        signature = CheckUtil.SHA1(string1);
//        System.out.println("签名=="+signature);
//
//        wxMap.put("noncestr", new_novestr);
//        wxMap.put("timesTamp", timestamp);
//        wxMap.put("signature", signature);
//        return wxMap;
//    }


}
