<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          微信小程序開發(四)獲取用戶openid

          2018-9-12    seo達人

          如果您想訂閱本博客內容,每天自動發到您的郵箱中, 請點這里

          在小程序里面有兩個地方獲取用戶的openid。 
          一個是wx.login(OBJECT),第二個是wx.getUserInfo(OBJECT)。 
          這里我使用的是第一種wx.login(OBJECT)。

          步驟

           wx.login({
            success: function(res) { if (res.code) { //  第一步: 獲取code //發起網絡請求 wx.request({
                  url: '后臺接口', // 獲取openid data: {
                    code: res.code
                  }
                })
              } else {
                console.log('獲取用戶登錄態失??!' + res.errMsg)
              }
            }
          }); 
          
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15

          后端的實現

          后端的實現就是后端調用這個接口:https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code。 
          這里寫圖片描述

          /*
           * 根據code獲取微信用戶的openid
           */ router.get('/api/getWxCode', function(req, res, next) { var param = req.query || req.params; var code = param.code; var urlStr = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxConfig.AppID + '&secret=' + wxConfig.Secret + '&js_code=' + code + '&grant_type=authorization_code';
              request(urlStr, function (error, response, body) { if (!error && response.statusCode == 200) { var jsBody = JSON.parse(body); 
                      jsBody.status = 100;
                      jsBody.msg = '操作成功';
                      res.end(JSON.stringify(jsBody));
                  }
              })
          }); 
          
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16

          具體實例

          /**
           * 生命周期函數--監聽頁面加載
           */ onLoad: function (options) { var self = this;
            wx.login({
              success: function (res) { if (res.code) { //發起網絡請求 wx.request({
                    url: 'https://www.hgdqdev.cn/api/getWxCode',
                    data: {
                      code: res.code
                    },
                    success: function(res){ if(res.data.status == 100){ self.setData({
                          openid: res.data.openid
                        })
                      }
                    },
                    fail: function(){ }
                  })
                } else {
                  console.log('獲取用戶登錄態失敗!' + res.errMsg)
                }
              }
            });
          },

          藍藍設計www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 、平面設計服務


          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合