02課開(kāi)發(fā)網(wǎng)頁(yè)端瑞客論壇_第1頁(yè)
02課開(kāi)發(fā)網(wǎng)頁(yè)端瑞客論壇_第2頁(yè)
02課開(kāi)發(fā)網(wǎng)頁(yè)端瑞客論壇_第3頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、_網(wǎng)頁(yè)端知識(shí)要點(diǎn)1. 網(wǎng)頁(yè)資料npm庫(kù)阮一峰的OAuth2 OAuth2.0的 碼模式 main&id=mp1421140842 2 0.html碼模式(authorization code)是功能最完整、流程最嚴(yán)密的 模式。它的特點(diǎn)就是通過(guò)客戶(hù)端的后臺(tái)服務(wù)器,與服務(wù)提供商的認(rèn)證服務(wù)器進(jìn)行互動(dòng)。(A) 用戶(hù) 客戶(hù)端,后者將前者導(dǎo)向認(rèn)證服務(wù)器。(B) 用戶(hù)選擇是否給予客戶(hù)端 。(C) 假設(shè)用戶(hù)給予 ,認(rèn)證服務(wù)器將用戶(hù)導(dǎo)向客戶(hù)端事先指定的重定向URI(redirection URI),同時(shí)附上一個(gè) 碼。(D) 客戶(hù)端收到 碼,附上早先的重定向URI,向認(rèn)證服務(wù)器申請(qǐng)令牌。這一步是在客戶(hù)端的 的服務(wù)

2、器上完成的,對(duì)用戶(hù)不可見(jiàn)。開(kāi)課吧web全棧架構(gòu)師(E)認(rèn)證服務(wù)器核對(duì)了 碼和重定向URI,確認(rèn)無(wú)誤后,向客戶(hù)端 令牌(access token)和更新令牌(refresh token)。獲取用戶(hù)信息 - 相當(dāng)于普通網(wǎng)頁(yè)的用戶(hù)登錄生成認(rèn)證URL跳轉(zhuǎn) 認(rèn)證服務(wù)回調(diào)服務(wù)端 獲取code調(diào)用 接口 AccessToken與OpenId調(diào)用服務(wù)器端接口開(kāi)課吧web全棧架構(gòu)師1. 配置網(wǎng)頁(yè)回調(diào)2. 配置JS安全接口1 網(wǎng)頁(yè)端網(wǎng)頁(yè) 登錄獲取用戶(hù)信息/ wechat/index.html async auth () window.location.href = /wxAuthorize,1. 初始化Oauth

3、開(kāi)課吧web全棧架構(gòu)師/ index.jsconst OAuth = require(co-wechat-oauth);const oauth = new OAuth(conf.appid, conf.appsecret);2. 生成用戶(hù)URL/ index.js/ 生成引導(dǎo)用戶(hù)點(diǎn)擊的 URLrouter.get(/wxAuthorize, async (ctx, next) = const state = ctx.query.id/ const target = ctx.hrefconsole.log(ctx. + ctx.href)/ 目標(biāo)地址redirectUrl = ctx.hrefr

4、edirectUrl = redirectUrl.replace(wxAuthorize, wxCallback) const scope = snsapi_userinfovar url = oauth.getAuthorizeURL(redirectUrl, state, scope); console.log(url + url)ctx.redirect(url)3. 獲取用戶(hù)回調(diào) AccessToken與OpenId/ index.js/ 獲取AccessTokenrouter.get(/wxCallback, async (ctx, next) = const code = ctx.

5、query.code / 碼console.log(getAccessToken, code)var token = await oauth.getAccessToken(code); var accessToken = token.data.access_token; var openid = token.data.openid; console.log(getAccessToken.)console.log(accessToken, accessToken) console.log(openid, openid)/ ctx.body = tokenctx.redirect(/?openid

6、= + openid)4. 用戶(hù)信息開(kāi)課吧web全棧架構(gòu)師/ index.html async getUser()const qs = Qs.parse(window.location.search.substr(1) const openid = qs.openidconst res = await axios.get(/getUser, params:openid)console.log(User,res.data),/ index.jsrouter.get(/getUser, async (ctx, next) = const openid = ctx.query.openid cons

7、ole.log(getUser, openid)var userInfo = await oauth.getUser(openid); console.log(userInfo:, userInfo) ctx.body = userInfo)5. AccessToken緩存問(wèn)題開(kāi)課吧web全棧架構(gòu)師/ mongo.js/ ClientAccessToken/ mongoose.jsconst mongoose = require(mongoose) const Schema = mongoosemongoose.connect(mongodb:/localhost:27017/weixin,

8、useNewUrlParser: true, () = console.log(Mongodb connected.)exports.ServerToken = mongoose.m (ServerToken, accessToken: String);/ ClientAccessToken schema = new Schema(access_token: String, expires_in: Number, refresh_token: String, openid: String,scope: String, create_at: String);const OAuth = requi

9、re(co-wechat-oauth);const oauth = new OAuth(conf.appid, conf.appsecret, async function (openid) return await ClientToken.getToken(openid),async function (openid, token) return await ClientToken.setToken(openid, token)2. JSSDK資料:npm庫(kù):是開(kāi)發(fā)者在網(wǎng)頁(yè)上通過(guò)JavaScript代碼使用 main&id=mp1421141115(獲取JSCong)原生功能的工具包,開(kāi)發(fā)者

10、可以使用它在網(wǎng)頁(yè)上錄制和 微信語(yǔ)音、上傳 本地圖片、拍照等許多能力運(yùn)行于 內(nèi)置瀏覽器的網(wǎng)頁(yè)調(diào)用 原生應(yīng)用如:拍照、語(yǔ)音、掃一掃功能 查到的數(shù)據(jù)不同圖像接口音頻接口開(kāi)課吧web全棧架構(gòu)師/ 自定義getToken方法schema.statics.getToken = async function (openid) return await this.findOne(openid: openid);schema.statics.setToken = async function (openid, token) / 有則更新,無(wú)則添加const query = openid: openid;cons

11、t options = upsert: true;return await this.updateOne(query, token, options);exports.ClientToken = mongoose.m (ClientToken, schema);/ index.js/ 獲取JSConfig router.get(/getJsConfig, async ctx = console.log(getJSSDK., ctx.query)var res = await api.getJsConfig(ctx.query); console.log(res, res)ctx.body = res)/ index.html/ index.htmlgetJSConfig: async function () console.log(wx:, wx)let res = await axios.get(/getJSConfig, params: url: window.location.href)console.log(res., res.data)res.data.jsApiList = on ShareTimeline, on ShareAppMessage wx.config(res.data);wx.ready(function () console.log(wx.read

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論