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

uni-app微信小程序登錄并使用vuex存儲登錄狀態(tài)的思路詳解

 更新時(shí)間:2019年11月04日 08:21:15   作者:houqq  
這篇文章主要介紹了uni-app微信小程序登錄并使用vuex存儲登錄態(tài)的思路,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

微信小程序注冊登錄思路

(這是根據(jù)自身的項(xiàng)目的思路,不一定每個(gè)項(xiàng)目都適用)

1.制作授權(quán)登錄框,引導(dǎo)用戶點(diǎn)擊按鈕

2.uni.login獲取code

3.把code傳給后端接口,后端返回如下數(shù)據(jù)

openid: "ogtVM5RWdfadfasdfadfadV5s"
status: 1
// 狀態(tài)碼:status==0(該用戶未注冊,需調(diào)用注冊接口) status==1(該用戶已注冊)

4.判斷用戶是否注冊,并調(diào)用用戶信息接口

(1)若已注冊則提示登錄成功,并調(diào)用后臺給的獲取用戶信息的接口,并把數(shù)據(jù)保存到vuex

(2)若未注冊則調(diào)用注冊接口,注冊成功之后調(diào)獲取用戶信息接口,并把數(shù)據(jù)保存到vuex

制作授權(quán)登錄框

我把它寫到一個(gè)組件里,在需要調(diào)用的頁面注冊該組件

authorization.vue組件

<template>
  <view class="pop">
    <view class="pop-main">
      <i class="iconfont cancel" @click="cancel"></i>
      <view class="pop-title">需要您的授權(quán)</view>
      <view class="pop-text">
        <view>為了提供更好的服務(wù)</view> 
        <view>請?jiān)谏院蟮奶崾究蛑悬c(diǎn)擊允許</view>
      </view>
      <image class="pop-imgsize" src="../static/images/aut.png" mode=""></image>
      <!-- 微信小程序的引導(dǎo)按鈕,可以獲取到用戶信息 getuserinfo函數(shù)調(diào)用請求下面會講到-->
      <button type="primary" class="reserve-btn" open-type="getUserInfo" @getuserinfo="getuserinfo">我知道了</button>
    </view>
  </view>
</template>

<script>
  import app from '../common/config.js'
  export default {
    data() {
      return {
        
      };
    },
    methods: {
      getuserinfo(res) {
      // 調(diào)用封裝在app的_getuserinfo函數(shù),_getuserinfo函數(shù)執(zhí)行用戶獲取code,傳code給后臺,調(diào)用注冊接口
        app._getuserinfo(res)
      },
      cancel() {
        this.$emit('cancelChild',false)
      }
    }
  }
</script>

<style scoped>
.pop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,.3);
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
}
.pop-main {
  width: 690upx;
  height: 800upx;
  box-sizing: border-box;
  background-color: #FFFFFF;
  border-radius: 20upx;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 14px;
  padding: 64upx 0upx 40upx 0upx;
}
/* 刪除按鈕 */
.cancel {
  position: absolute;
  top: 20upx;
  right: 20upx;
  font-size: 20px;
}
.pop-title {
  padding-bottom: 40upx;
  width: 100%;
  border-bottom: 1px solid #BFBFBF;
  font-size: 16px;
  letter-spacing: 2px;
  text-align: center;
}
.pop-imgsize {
  width: 484upx;
  height: 406upx;
}
.pop-text {
  width: 75%;
  text-align: center;
  font-size: 15px;
  margin: 30upx 0upx;
  letter-spacing: 1px;
}
button {
  background-color: #08BF00;
  color: #FFFFFF;
  text-align: center;
  height: 88upx;
  line-height: 88upx;
  font-size: 14px;
  border-radius: 50upx;
  width: 78%;
}
</style>

在個(gè)人中心頁注冊該組件

<template>
  <view>
    <view class="info" >
      <image 
        @click="toSettings" 
        class="headimg" 
        :src="userinfo.headimgurl || defaultHeadimg" 
        >
      </image>
      <text v-if="!hasLogin" class="goLogin" @click="gologin">登錄</text>
      <text v-if="hasLogin">{{userinfo.nickname}}</text>
      <text v-if="hasLogin && userinfo.mobile" class="phone">{{userinfo.mobile}}</text>
    </view>
    <authorization v-if="tologin && !hasLogin" @cancelChild="getChild"></authorization>
  </view>
</template>

 

點(diǎn)擊"我知道了"按鈕,調(diào)用封裝_getuserinfo的請求

新建config.js,在config封裝請求

import store from '@/store'
const app = {
  apiUrl: 'https://aaa.bbbbb.com/', //請求的地址
  openidRequest(obj) {
    try {
      const openid = uni.getStorageSync('openid');
      if (openid) {
        if(obj.data) {
          obj.data["openid"] = openid;
        } else {
          obj.data = {"openid": openid};
        }
        obj.url = this.apiUrl + obj.url;
        uni.request(obj)
      } else {
        console.log("獲取不到openid")
      }
    } catch (e) {
      console.log(e)
      console.log("獲取不到openid")
    }
  },
  _getuserinfo(res) {
    var that = this
    var userinfoDetails = {} // 注冊時(shí),需要把用戶信息傳給后臺,所以定義一個(gè)對象存儲獲取到的用戶信息
    userinfoDetails = res.detail.userInfo 
    uni.getUserInfo({
     provider: 'weixin',
     success: function () {
      uni.login({
        success:function(res){
          uni.showLoading({
            title: '登陸中...',
            mask: false
          });
          uni.request({
            url: that.apiUrl + 'sdafl/ddfax/dfadf?code=' + res.code, //把code傳給后臺,后臺返回openid和status
            success: (res) => {
              console.log(res)
              if (res.data.openid) {
                uni.setStorageSync('openid', res.data.openid)
                userinfoDetails.openid = res.data.openid
                //處理一下屬性名傳遞給后臺
                userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/avatarUrl/g, "headimgurl"));
                userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/gender/g, "sex"));
                userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/nickName/g, "nickname"));
                delete userinfoDetails.language;
                userinfoDetails.ppid = store.state.ppid || ''
                console.log(userinfoDetails)
              }
              // 當(dāng)status==0時(shí)說明用戶還沒有注冊需要注冊
              if(res.data.status == 0) {
                that.sendInfo(userinfoDetails) // 調(diào)用注冊接口,并把用戶信息userinfoDetails傳給后臺
                console.log('我還沒有注冊')
              } else if (res.data.status == 1) {
                uni.showToast({
                  title: '登錄成功',
                  icon: 'success',
                  duration: 2000
                })
                that.getUserData() // 調(diào)用獲取用戶信息的接口
              } else {
                uni.hideLoading()
                uni.showToast({
                  title: '登錄失敗',
                  duration: 2000,
                  icon:'none'
                })
              }
            }
          })
        }
      })
     }
    });
  },
  // 注冊接口
  sendInfo(userinfoDetails) {
    var that = this
    uni.request({
      url: this.apiUrl + 'fdafd/ifdaffdex/fdaff',
      data: userinfoDetails,
      method: 'POST',
      success: (res) => {
        if(res.data.userinfo == 1) {
          uni.hideLoading()
          uni.showToast({
            title: '注冊成功',
            icon: 'success',
            duration: 2000
          })
          that.getUserData() // 調(diào)用獲取用戶信息的接口
        } else {
          uni.hideLoading()
          uni.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    })
  },
  // 獲取用戶信息
  getUserData() {
    uni.request({
      url: this.apiUrl + 'sfad/dfadfad/idfadffde', 
      data: {
        openid: uni.getStorageSync('openid')
      },
      method: 'POST',
      success: (res) => {
        if(res.data.status == 1) {
          console.log(res.data)
          store.commit('login', res.data.user) // vuex的方法
        } else {
          uni.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    })
  }
}
export default app;

vuex存儲登錄態(tài)的方法

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    hasLogin: false,
    userinfo: {},
    ppid: '',
  },
  mutations: {
    // 登錄
    login(state,res) {
      state.hasLogin = true
      state.userinfo = res
      uni.setStorage({
        key: 'userinfo',
        data: res
      })
      // console.log(state.userinfo)
      // console.log(state.hasLogin)
    },
    // 二維碼ppid
    saveppid(state,ppid) {
      state.ppid = ppid
      uni.setStorage({
        key: 'ppid',
        data: ppid
      })
      // console.log(state.ppid)
    },
  },
})

export default store

 

總結(jié)

以上所述是小編給大家介紹的uni-app微信小程序登錄并使用vuex存儲登錄狀態(tài)的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • 原生js實(shí)現(xiàn)無縫輪播圖效果

    原生js實(shí)現(xiàn)無縫輪播圖效果

    本文主要分享了原生js實(shí)現(xiàn)無縫輪播圖效果的示例代碼,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • 微信小程序全局狀態(tài)的深入講解

    微信小程序全局狀態(tài)的深入講解

    這篇文章主要介紹了微信小程序全局狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • JS如何獲取地址欄的參數(shù)實(shí)例講解

    JS如何獲取地址欄的參數(shù)實(shí)例講解

    本篇文章給大家?guī)砹岁P(guān)于JS如何獲取地址欄的參數(shù)實(shí)例講解,有需要的朋友們參考下。
    2018-10-10
  • javascript判斷元素存在和判斷元素存在于實(shí)時(shí)的dom中的方法

    javascript判斷元素存在和判斷元素存在于實(shí)時(shí)的dom中的方法

    本文主要介紹了javascript判斷元素存在和判斷元素存在于實(shí)時(shí)的dom中的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • JS實(shí)現(xiàn)頁面導(dǎo)航與內(nèi)容相互錨定實(shí)例詳解

    JS實(shí)現(xiàn)頁面導(dǎo)航與內(nèi)容相互錨定實(shí)例詳解

    這篇文章主要為大家介紹了JS實(shí)現(xiàn)頁面導(dǎo)航與內(nèi)容相互錨定實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • js判斷頁面中是否有指定控件的簡單實(shí)例

    js判斷頁面中是否有指定控件的簡單實(shí)例

    本篇文章主要是對js判斷頁面中是否有指定控件的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-03-03
  • uniapp界面新增水印實(shí)現(xiàn)示例詳解

    uniapp界面新增水印實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了uniapp界面新增水印實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Bootstrap基本模板的使用和理解1

    Bootstrap基本模板的使用和理解1

    這篇文章主要為大家詳細(xì)介紹了關(guān)于Bootstrap基本模板的使用和理解的學(xué)習(xí)記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 在uniapp中如何去掉一些不想要的權(quán)限

    在uniapp中如何去掉一些不想要的權(quán)限

    在uniapp中,云打包以后會自動(dòng)增加一個(gè)電話權(quán)限,并且在manifest.json里面也沒有添加這個(gè)權(quán)限,怎么添加都添加不上,下面小編給大家分享在uniapp中如何去掉一些不想要的權(quán)限,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • uniapp如何編寫含有后端的登錄注冊頁面

    uniapp如何編寫含有后端的登錄注冊頁面

    uniapp是一個(gè)使用html5標(biāo)準(zhǔn)的,一次開發(fā),可以發(fā)布到安卓,ios,小程序的多端框架,非常方便,下面這篇文章主要給大家介紹了關(guān)于uniapp如何編寫含有后端的登錄注冊頁面的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評論

南雄市| 土默特右旗| 疏勒县| 广昌县| 读书| 庄河市| 宜丰县| 兴仁县| 新沂市| 封丘县| 金秀| 贵阳市| 府谷县| 康马县| 乌兰浩特市| 晋宁县| 丰顺县| 巴楚县| 梅州市| 怀安县| 兴义市| 新津县| 青海省| 安顺市| 沙坪坝区| 沙洋县| 大新县| 邹平县| 广饶县| 香港 | 丹巴县| 赣州市| 永宁县| 郑州市| 馆陶县| 仲巴县| 静宁县| 浮梁县| 宁乡县| 时尚| 固始县|