最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uni-app?微信小程序授權(quán)登錄的實(shí)現(xiàn)步驟

 更新時間:2022年01月04日 08:36:57   作者:gblfy  
本文主要介紹了uni-app?微信小程序授權(quán)登錄的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

在這里插入圖片描述

一、appID相關(guān)申請和配置

1. appid獲取方式

登錄微信公眾平臺

官網(wǎng)鏈接:https://mp.weixin.qq.com/

第一次需要小伙伴們點(diǎn)擊注冊按鈕,進(jìn)行注冊,如果有賬號,直接掃描登錄即可

在這里插入圖片描述

官網(wǎng)小程序鏈接:

在這里插入圖片描述

2. appID配置

在manifest.json中輸入申請的微信小程序id

在這里插入圖片描述

二、獲取用戶基礎(chǔ)數(shù)據(jù)

這里給小伙伴們演示二種api

2.1. 獲取用戶信息

可以使用uni.getUserProfile請求用戶授權(quán)獲取用戶信息, 也可以使用uni.getUserInfo獲取

在這里插入圖片描述

授權(quán)成功后獲取到的用戶信息在userInfo中:

在這里插入圖片描述

頁面部分:

  <button class="login-btn" type="primary" @click="getUserInfo">
        微信用戶一鍵登錄
      </button>

js部分:

 methods: {
    getUserInfo() {
      uni.getUserInfo({
        provider: 'weixin',
        success: (res) => {
          console.log('getUserInfo', res);
        },
      });
    },
   }

獲取的用戶基礎(chǔ)數(shù)據(jù)(無openid=》微信用戶唯一標(biāo)識)

在這里插入圖片描述

2.2. 獲取用戶信息2

可以使用uni.getUserInfo請求用戶授權(quán)獲取用戶信息

頁面一樣,js部分:

   getUserInfo() {
      uni.getUserProfile({
        desc: '登錄后可同步數(shù)據(jù)',
        lang: 'zh_CN',
        success: (res) => {
          console.log('getUserProfile', res);
        },
      });
    },

獲取的用戶基礎(chǔ)數(shù)據(jù)(無openid=》微信用戶唯一標(biāo)識)

在這里插入圖片描述

總結(jié):uni.getUserProfile和uni.getUserInfo 二個api獲取的用戶數(shù)據(jù)基本一樣,都無openid=》微信用戶唯一標(biāo)識。

三、調(diào)用登錄api

3.1. 登錄api

使用uni.login方法,provider參數(shù)輸入’weixin’,成功的返回值中如果errMsg=“login:ok” 代表成功,
微信小程序端會返回一個code字符串

在這里插入圖片描述

3.2. 案例代碼

      uni.login({
            provider: 'weixin',
            success: (res) => {
              console.log('res-login', res);
              this.code = res.code;
              console.log('code', res.code);
              if (res.errMsg == 'login:ok') {
              //TODO 獲取code 攜帶code參數(shù)調(diào)用后端接口}

四、獲取唯一標(biāo)識信息

4.1. 官網(wǎng)文檔

官網(wǎng)文檔
使用獲取到的code請求微信登錄接口,獲取 openid 和 session_key

在這里插入圖片描述

4.2. 接口簡述

在這里插入圖片描述

請求方式:GET
APPID:小程序唯一標(biāo)識,上面有獲取方式
SECRET:小程序唯一標(biāo)識的秘鑰,上面參考APPID獲取方式,就在他的下面
JSCODE:這個前端調(diào)用  uni.login獲取

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

在這里插入圖片描述

五、綁定用戶 實(shí)現(xiàn)登錄

獲取到微信用戶的唯一id后,就可以綁定至自己系統(tǒng)中的用戶,我的做法是在用戶表中加入weixinId字段,跳轉(zhuǎn)至自己的用戶綁定界面,如果用戶選擇綁定微信,則更新該行用戶數(shù)據(jù)的weixinId。下次用戶使用微信登錄時,如果通過openId能夠查詢到一條用戶數(shù)據(jù),說明已經(jīng)綁定,則登錄該用戶

5.1. 代碼案例(未封裝)

前端部分:

 /**
     *
     * 獲取用戶信息
     */
    getUserInfo() {
      // 展示加載框
      uni.showLoading({
        title: '加載中',
      });
      uni.getUserProfile({
        desc: '登錄后可同步數(shù)據(jù)',
        success: async (obj) => {
          console.log('obj', obj);
          // 調(diào)用 action ,請求登錄接口
          // await this.login(obj);
          uni.login({
            provider: 'weixin',
            success: (res) => {
              console.log('res-login', res);
              this.code = res.code;
              console.log('code', res.code);
              if (res.errMsg == 'login:ok') {
                uni
                  .request({
                    url:
                      'http://127.0.0.1:8080/wxh5/wx/user/' +
                      'wx55822xxxx75e422' +
                      '/login/',
                    data: {
                      code: this.code,
                    },
                  })
                  .then((res) => {
                  //獲取到 openid 和 session_k后,自己的邏輯
                    console.log('授權(quán)登錄', res[1].data);
                    console.log(res[1].data.openid);
                    console.log(res[1].data.session_key);
                    // DoSomeThing.................
                  });
                console.log('res', res);
              }
            },
          });
        },
        fail: () => {
          uni.showToast({
            title: '授權(quán)已取消',
            icon: 'error',
            mask: true,
          });
        },
        complete: () => {
          // 隱藏loading
          uni.hideLoading();
        },
      });
    },

后端部分

   @GetMapping("/login")
    public String login(@PathVariable String appid, String code) {
        if (StringUtils.isBlank(code)) {
            return "empty jscode";
        }

        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            this.logger.info(session.getSessionKey());
            this.logger.info(session.getOpenid());
            //TODO 可以增加自己的邏輯,關(guān)聯(lián)業(yè)務(wù)相關(guān)數(shù)據(jù)
            return JsonUtils.toJson(session);
        } catch (WxErrorException e) {
            this.logger.error(e.getMessage(), e);
            return e.toString();
        }
    }

5.2. 代碼案例(封裝)

  /**
     *
     * 獲取用戶信息
     */
    getUserInfo() {
      // 展示加載框
      uni.showLoading({
        title: '加載中',
      });
      uni.getUserProfile({
        desc: '登錄后可同步數(shù)據(jù)',
        success: async (obj) => {
          // this.userInfo = obj.userInfo;
          // 調(diào)用 action ,請求登錄接口
          uni.login({
            provider: 'weixin',
            success: async (res) => {
              this.code = res.code;
              // console.log('登錄獲取code', res.code);
              if (res.errMsg == 'login:ok') {
                await this.loginAuth({
                  userProfile: obj,
                  appid: 'wx558xxxxxxxxxxxxxxx2',
                  code: this.code,
                });
              }
            },
          });
        },
        fail: () => {
          uni.showToast({
            title: '授權(quán)已取消',
            icon: 'error',
            mask: true,
          });
        },
        complete: () => {
          // 隱藏loading
          uni.hideLoading();
        },
      });
    },
  },

user.js

/**
 * 微信用戶授權(quán)登錄,攜帶appid和code參數(shù),調(diào)用后端接口獲取Openid
 */
export function loginAuth(data) {
  return request({
    url: '/wx/user/' + data.appid + '/login/',
    data: {
      code: data.code,
    },
  });
}

vuex user模塊(user.js)

  // 微信用戶授權(quán)登錄,攜帶appid和code參數(shù),調(diào)用后端接口獲取Openid
    async loginAuth(context, data) {
      console.log('data', data);
      const userInfo = data.userProfile;
      const { content: res } = await loginAuth({
        appid: data.appid,
        code: data.code,
      });

      // 解析后端傳送過來的json對象
      const userAuthInfo = JSON.parse(res);
      const openid = userAuthInfo.openid;
      // console.log('sessionKey', userAuthInfo.sessionKey);
      console.log('openid', openid);

      // 保存到vuex中,通過commit
      this.commit('user/setOpenid', userAuthInfo.openid);
      this.commit('user/setUserInfo', JSON.parse(userInfo.rawData));
    },

在這里插入圖片描述

在這里插入圖片描述

六、項(xiàng)目開源地址

6.1. 前端

applet-chock-in

6.2. 后端

weixin-java-miniapp

到此這篇關(guān)于uni-app 微信小程序授權(quán)登錄的文章就介紹到這了,更多相關(guān)uni-app 微信小程序授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • javascript使用activex控件的代碼

    javascript使用activex控件的代碼

    最近公司項(xiàng)目用到了avtivex控件。以前從來沒有用過。我把最近到處找到的使用方法整理一下。
    2011-01-01
  • JS中Promise的使用及封裝方式

    JS中Promise的使用及封裝方式

    這篇文章主要介紹了JS中Promise的使用及封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 微信小程序點(diǎn)擊左上角返回彈窗提示解決思路

    微信小程序點(diǎn)擊左上角返回彈窗提示解決思路

    當(dāng)頁面表單沒有提交直接返回時,要提示用戶是否保存當(dāng)前信息,如果已經(jīng)提交就不提示了,下面小編給大家?guī)砹宋⑿判〕绦螯c(diǎn)擊左上角返回彈窗提示解決思路,感興趣的朋友一起看看吧
    2024-06-06
  • JavaScript遍歷數(shù)組和對象的元素簡單操作示例

    JavaScript遍歷數(shù)組和對象的元素簡單操作示例

    這篇文章主要介紹了JavaScript遍歷數(shù)組和對象的元素簡單操作,結(jié)合實(shí)例形式分析了javascript數(shù)組與對象元素遍歷相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • JavaScript實(shí)現(xiàn)的DOM樹遍歷方法詳解【二叉DOM樹、多叉DOM樹】

    JavaScript實(shí)現(xiàn)的DOM樹遍歷方法詳解【二叉DOM樹、多叉DOM樹】

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)的DOM樹遍歷方法,結(jié)合實(shí)例形式詳細(xì)分析了二叉DOM樹、多叉DOM樹的前序、中序與后序遍歷,以及多叉樹深度優(yōu)先、廣度優(yōu)先等相關(guān)遍歷操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05
  • 基于JavaScript實(shí)現(xiàn)簡單掃雷游戲

    基于JavaScript實(shí)現(xiàn)簡單掃雷游戲

    這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)簡單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • JS使用cookie實(shí)現(xiàn)只出現(xiàn)一次的廣告代碼效果

    JS使用cookie實(shí)現(xiàn)只出現(xiàn)一次的廣告代碼效果

    我們上網(wǎng)經(jīng)常會遇到第一次需要登錄而之后不用再登錄的網(wǎng)站的情況,其實(shí)是運(yùn)用了Cookie 存儲 web 頁面的用戶信息,Cookie 以名/值對形式存儲,當(dāng)瀏覽器從服務(wù)器上請求 web 頁面時, 屬于該頁面的 cookie 會被添加到該請求中
    2017-04-04
  • js實(shí)現(xiàn)使用鼠標(biāo)拖拽切換圖片的方法

    js實(shí)現(xiàn)使用鼠標(biāo)拖拽切換圖片的方法

    這篇文章主要介紹了js實(shí)現(xiàn)使用鼠標(biāo)拖拽切換圖片的方法,涉及javascript操作圖片實(shí)現(xiàn)輪播效果的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-05-05
  • Javascript的表單驗(yàn)證-初識正則表達(dá)式

    Javascript的表單驗(yàn)證-初識正則表達(dá)式

    JavaScript 可用來在數(shù)據(jù)被送往服務(wù)器前對 HTML 表單中的這些輸入數(shù)據(jù)進(jìn)行驗(yàn)證。接下來通過本文給大家介紹Javascript的表單驗(yàn)證-初識正則表達(dá)式,對js表單驗(yàn)證正則表達(dá)式相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • jscript之List Excel Color Values

    jscript之List Excel Color Values

    jscript之List Excel Color Values...
    2007-06-06

最新評論

木兰县| 杭锦旗| 九江市| 称多县| 武汉市| 河东区| 朝阳市| 岑溪市| 澎湖县| 广丰县| 仙桃市| 玛多县| 林口县| 怀化市| 雷州市| 英山县| 衡东县| 陇川县| 普定县| 邵东县| 长岭县| 女性| 宁海县| 牙克石市| 邹平县| 长兴县| 祁连县| 苗栗县| 祥云县| 吉安市| 玉龙| 永州市| 郸城县| 龙泉市| 辛集市| 汝城县| 安陆市| 胶州市| 溧阳市| 韶山市| 尼玛县|