前端uniapp+后端springboot 具体教程《实现微信小步伐授权登录》(附完整前 ...

李优秀  高级会员 | 2024-6-18 19:25:18 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 227|帖子 227|积分 681

微信小步伐官方登录流程图:

参考微信小步伐登录官网文档
1、前端技能栈

1.1、uniapp

使用uniapp构建一套代码多端使用的前端框架项目
1.2、前端封装工具



  • dateUtil.js:
    功能:
    1. 时间日期格式化
    2. 传入日期是否和当前日期的比力
    完整代码:
    1. // 判断传入日期是否和当前日期比较
    2. const judgeDate=(toDate)=>{
    3.      
    4.         return new Date().getTime()-new Date(toDate).getTime();
    5. }
    6. var timeFormat = function (msTime) {
    7.      
    8.     let time = new Date(msTime);
    9.     let yy = time.getFullYear();
    10.     let MM = time.getMonth() + 1;
    11.     let dd = time.getDate();
    12.     let hh = time.getHours() < 10 ? "0" + time.getHours() : time.getHours();
    13.     let min =
    14.         time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
    15.     let sec =
    16.         time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
    17.     return yy + "-" + MM + "-" + dd + " " + hh + ":" + min + ":" + sec;
    18. }
    19. export {
    20.      timeFormat,judgeDate}
    复制代码

  • requestUtil.js:
    功能:
    1. 定义公共的url
    2. 后端哀求工具封装
    完整代码:
    1. // 同时发送异步代码的次数
    2. let ajaxTimes = 0;
    3. // 定义公共的url
    4. const baseUrl = "http://localhost:8866";
    5. /**
    6. * 返回baseUrl
    7. */
    8. export const getBaseUrl = () => {
    9.      
    10.   return baseUrl;
    11. }
    12. /**
    13. * 后端请求工具类
    14. * @param {*} params 请求参数
    15. */
    16. export const requestUtil = (params) => {
    17.      
    18.   let header = {
    19.      
    20.     ...params.header
    21.   };
    22.   // 拼接header 带上token
    23.   header["token"] = uni.getStorageSync("token");
    24.   ajaxTimes++;
    25.   // 显示加载中 效果
    26.   wx.showLoading({
    27.      
    28.     title: "加载中",
    29.     mask: true
    30.   });
    31.   var start = new Date().getTime();
    32.   // 模拟网络延迟加载
    33.   while (true)
    34.     if (new Date().getTime() - start > 1000 * 1) break;
    35.   return new Promise((resolve, reject) => {
    36.      
    37.     wx.request({
    38.      
    39.       ...params,
    40.       header: header,
    41.       url: baseUrl + params.url,
    42.       success: (result) => {
    43.      
    44.         resolve(result.data);
    45.       },
    46.       fail: (err) => {
    47.      
    48.         uni.showToast({
    49.      
    50.           icon: 'error',
    51.           title: '连接服务器失败',
    52.           duration: 3000
    53.         })
    54.         reject(err);
    55.       },
    56.       complete: () => {
    57.      
    58.         ajaxTimes--;
    59.         if (ajaxTimes === 0) {
    60.      
    61.           //  关闭正在等待的图标
    62.           wx.hideLoading();
    63.         }
    64.       }
    65.     });
    66.   })
    67. }
    复制代码

  • stringUtil.js:
    功能:
    1. 判断字符串是否为空
    完整代码:
    1. //判断字符串是否为空
    2. export const isEmpty = (str) => {
    3.      
    4.   if (str === '' || str.trim().length === 0) {
    5.      
    6.     return true
    7.   } else {
    8.      
    9.     return false;
    10.   }
    11. }
    复制代码
1.3、Hbuilderx构建uniapp项目


项目结构:

   app.vue中,写两个方法:
  

  • 在onLaunch生命周期函数中调用wx.login()获取code(条件是在微信开辟者工具中登录微信账号,而且在uniapp中设置微信小步伐AppId),code的作用是后端担当到code,通过code参数向微信背景发送哀求,它是实现微信临时登录的url中的一个非常紧张的参数。
  • 三个紧张参数


  • appid:应用ID
  • secret:应用密钥
  • js_code:前台传给我们的code

  • wxlogin方法
    携带code参数发送哀求给后端来获取token和openid
  1. <script>
  2.   import {
  3.    
  4.     requestUtil
  5.   } from "./utils/requestUtil.js"
  6.   export default {
  7.    
  8.     onLaunch: function() {
  9.    
  10.       console.log('App Launch')
  11.       wx.login({
  12.    
  13.         timeout: 5000,
  14.         success: (res) => {
  15.    
  16.           console.log(res)
  17.           this.wxlogin(res.code);
  18.         }
  19.       });
  20.     },
  21.     onShow: function() {
  22.    
  23.       console.log('App Show')
  24.     },
  25.     onHide: function() {
  26.    
  27.       console.log('App Hide')
  28.     },
  29.     methods: {
  30.    
  31.       /**
  32.        * 请求后端获取用户token
  33.        * @param {} code
  34.        */
  35.       async wxlogin(code) {
  36.    
  37.         console.log("code=" + code)
  38.         // 发送请求 获取用户的token
  39.         const result = await requestUtil({
  40.    
  41.           url: "/user/wxlogin",
  42.           data: {
  43.    
  44.             code: code
  45.           },
  46.           method: "post"
  47.         });
  48.         console.log("token=" + result.token);
  49.         console.log("openid=" + result.openid);
  50.         if (result.code == 0) {
  51.    
  52.           console.log("登录成功")
  53.           uni.setStorageSync("token", result.token);
  54.           uni.setStorageSync("openid", result.openid);
  55.         } else {
  56.    
  57.           console.log("登录失败,报错信息:" + result.msg);
  58.           uni.showToast({
  59.    
  60.             icon: 'error',
  61.             title: result.msg,
  62.             duration: 3000
  63.           })
  64.         }
  65.       }
  66.     }
  67.   }
  68. </script>
  69. <style>
  70.   /*每个页面公共css */
  71. </style>
复制代码
2、后端技能栈



  • springboot后端技能框架
  • mybatis-plus数据持久层框架
2.1、创建springboot后端项目

利用idea工具,使用spring initializr初始化创建一个空的springboot项目
   springboot版本选择2.3.2.RELEASE。
  

  • 修改pom.xml
  1. <dependencies>
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-starter-web</artifactId>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>org.springframework.boot</groupId>
  8.             <artifactId>spring-boot-starter-test</artifactId>
  9.             <scope>test</scope>
  10.         </dependency>
  11.         <dependency>
  12.             <groupId>org.springframework.boot</groupId>
  13.             <artifactId>spring-boot-starter-validation</artifactId>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>org.springframework.boot</groupId>
  17.             <artifactId>spring-boot-devtools</artifactId>
  18.             <scope>runtime</scope>
  19.             <optional>true</optional>
  20.         </dependency>
  21.         <dependency>
  22.             <groupId>mysql</groupId>
  23.             <artifactId>mysql-connector-java</artifactId>
  24.             <scope>runtime</scope>
  25.         </dependency>
  26.         <!-- 连接池 -->
  27.         <dependency>
  28.             <groupId>com.alibaba</groupId>
  29.             <artifactId>druid</artifactId>
  30.             <version>1.1.10</version>
  31.         </dependency>
  32.         <!-- mybatis-plus -->
  33.         <dependency>
  34.             <groupId>com.baomidou</groupId>
  35.             <artifactId>mybatis-plus-boot-starter</artifactId>
  36.             <version>3.3.2</version>
  37.         </dependency>
  38.         <dependency>
  39.             <groupId>com.alibaba</groupId>
  40.             <artifactId>fastjson</artifactId>
  41.             <version>1.2.40</version>
  42.         </dependency>
  43.         <!-- JWT -->
  44.         <dependency>
  45.             <groupId>com.auth0</groupId>
  46.             <artifactId>java-jwt</artifactId>
  47.             <version>3.2.0</version>
  48.         </dependency>
  49.         <dependency>
  50.             <groupId>io.jsonwebtoken</groupId>
  51.             <artifactId>jjwt</artifactId>
  52.             <version>0.9.1</version>
  53.         </dependency>
  54.         <dependency>
  55.             <groupId>commons-io</groupId>
  56.             <artifactId>commons-io</artifactId>
  57.             <version>2.5</version>
  58.         </dependency>
  59.         <dependency>
  60.             <groupId>org.projectlombok</groupId>
  61.             <artifactId>lombok</artifactId>
  62.             <optional>true</optional>
  63.         </dependency>
  64.         <!-- spring boot redis 缓存引入 -->
  65.         <dependency>
  66.             <groupId>org.springframework.boot</groupId>
  67.             <artifactId>spring-boot-starter-data-redis</artifactId>
  68.         </dependency>
  69.         <!-- lettuce pool 缓存连接池 -->
  70.         <dependency>
  71.             <groupId>org.apache.commons</groupId>
  72.             <artifactId>commons-pool2</artifactId>
  73.         </dependency>
  74.         <!-- hutool工具类-->
  75.         <dependency>
  76.             <groupId>cn.hutool</groupId>
  77.             <artifactId>hutool-all</artifactId>
  78.             <version>5.3.3</version>
  79.         </dependency>
  80.         <!-- 验证码依赖-->
  81.         
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

李优秀

高级会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表