| | |
| | | package com.ruoyi.web.controller.system; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.constant.HttpStatus; |
| | | import com.ruoyi.common.constant.WxConstant; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.domain.BasePage; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.utils.NumberUtil; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.*; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.model.TbQuestion; |
| | | import com.ruoyi.system.model.TbSystemConfig; |
| | |
| | | import com.ruoyi.system.service.TbQuestionService; |
| | | import com.ruoyi.system.service.TbSystemConfigService; |
| | | import com.ruoyi.system.service.TbUserService; |
| | | import com.ruoyi.system.utils.wx.WxProperties; |
| | | import com.ruoyi.system.vo.InviteUserListVo; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.entity.ContentType; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.mock.web.MockMultipartFile; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.security.SecureRandom; |
| | | import java.text.MessageFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import static com.ruoyi.system.utils.wx.tools.WxAppletTools.ACCESS_TOKEN_URL; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | |
| | | @Autowired |
| | | private TbSystemConfigService systemConfigService; |
| | | |
| | | @Autowired |
| | | private WxProperties wxConfig; |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | @ApiOperation(value = "修改用户信息",tags = {"用户模块"}) |
| | | @PostMapping("/updateUserInfo") |
| | | public R<?> updateUserInfo(String avatar,String username) { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return R.fail(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | |
| | | TbUser user = userService.getById(userId); |
| | |
| | | @GetMapping("/getUserInfo") |
| | | public R<?> getUserInfo() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return R.fail(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | TbUser user = userService.getById(userId); |
| | | return R.ok(user); |
| | |
| | | @PostMapping("/accountCancellation") |
| | | public R<?> accountCancellation() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return R.fail(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | TbUser user = userService.getById(userId); |
| | | user.setStatus(3); |
| | |
| | | @GetMapping("/getInviteUserList") |
| | | public R<Page<InviteUserListVo>> getInviteUserList(BasePage page) { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return R.fail(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | Page<TbUser> page1 = userService.page(new Page<>(page.getPageNum(), page.getPageSize()), new LambdaQueryWrapper<TbUser>().eq(TbUser::getInviteId, userId).orderByDesc(TbUser::getCreateTime)); |
| | | Page<InviteUserListVo> inviteUserListVoPage = new Page<>(); |
| | |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | @Autowired |
| | | private OSSUtil ossUtil; |
| | | @ApiOperation(value = "个人的小程序码(分享码)生成") |
| | | @GetMapping("/person-code") |
| | | public R<String> createQRcode() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return R.fail(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | // 获取调用凭证accessToken |
| | | String accessToken = getAccessToken(); |
| | | RestTemplate rest = new RestTemplate(); |
| | | InputStream inputStream = null; |
| | | OutputStream outputStream = null; |
| | | String imgUrl = ""; |
| | | try { |
| | | String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken; |
| | | log.info("url: "+url); |
| | | Map<String,Object> param = new HashMap<>(); |
| | | param.put("path","pages/shoppingMall/shoppingMall?userId="+userId); |
| | | param.put("width", 430); //二维码尺寸 |
| | | log.info("调用生成微信URL接口传参:" + param); |
| | | MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); |
| | | HttpEntity requestEntity = new HttpEntity(param, headers); |
| | | ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); |
| | | log.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody()); |
| | | byte[] result = entity.getBody(); |
| | | inputStream = new ByteArrayInputStream(result); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
| | | // 最后上传生成的文件名 |
| | | String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + ".jpg"; |
| | | // oss中的文件夹名 |
| | | String objectName = sdf.format(new Date()) + "/" + finalFileName; |
| | | // 上传oss |
| | | ossUtil.uploadFile2OSS(inputStream, objectName); |
| | | //获取文件的URl地址 |
| | | imgUrl = ossUtil.getImgUrl(objectName); |
| | | log.info("imgUrl: "+imgUrl); |
| | | return R.ok(imgUrl); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("调用小程序生成微信永久小程序码URL接口异常",e); |
| | | } finally { |
| | | if(inputStream != null){ |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if(outputStream != null){ |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return R.fail(); |
| | | } |
| | | @ApiOperation(value = "获取微信小程序二维码",tags = {"获取微信小程序二维码"}) |
| | | @PostMapping("/getQRCode") |
| | | public AjaxResult getQRCode() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | if(loginUser==null){ |
| | | return AjaxResult.error(HttpStatus.UNAUTHORIZED,"请重新登录"); |
| | | } |
| | | Long userId = loginUser.getUserId(); |
| | | String accessToken = getAccessToken(); |
| | | try { |
| | | String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken; |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("path", "pages/shoppingMall/shoppingMall?userId="+userId); |
| | | param.put("width", 240); //二维码尺寸 |
| | | HttpRequest post = HttpUtil.createPost(url); |
| | | post.body(JSON.toJSONString(param)); |
| | | HttpResponse execute = post.execute(); |
| | | String body = execute.body(); |
| | | System.out.println(body); |
| | | byte[] bytes = body.getBytes(); |
| | | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); |
| | | return AjaxResult.success(body); |
| | | } catch (Exception e) { |
| | | System.err.println("调用小程序生成微信永久小程序码URL接口异常" + e); |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | public String getAccessToken() { |
| | | String requestUrl = MessageFormat.format(ACCESS_TOKEN_URL, wxConfig.getMemberAppId(), wxConfig.getMemberAppSecret()); |
| | | String respBody = restTemplate.getForEntity(requestUrl, String.class).getBody(); |
| | | JSONObject jsonObject = JSONObject.parseObject(respBody); |
| | | return jsonObject.getString("access_token"); |
| | | } |
| | | } |