微信小程序的api使用和封裝


小程序登錄

天使童裝

1.微信授權

通過wx.login()獲取code值,只是一個臨時憑證
通過code利用wx.request調用小程序登錄接口來換取token,代碼如下:
   wx.login({
       success(res) {
         console.log('wx.login:',res)
         let {code}=res;
          wx.request({
            url: 'https://api.it120.cc/hjl/user/wxapp/login',
            data:{
              code
            },
            header:{
              'content-type':' application/x-www-form-urlencoded'
            },
            method:'POST',
            success(result) {
              console.log('登錄返回',result)
            }
          })

       }
     })

檢測token是否有效接口:https://api.it120.cc/hjl/user/check-token

微信檢測session_key是否有效:

獲取用戶信息:

 通過button的open-type="getUserInfo"  bindgetUserInfo="回調來獲取用戶信息的方法"
 通過wx.getUserInfo(success(res) {})

2.存入你們公司后台數據庫

 注冊接口

 1.通過微信登錄獲取code的方式注冊
    
    注冊接口:/{domain}/user/wxapp/register/complex

    請求的參數:
        code	微信登錄接口返回的 code 參數數據
        encryptedData	微信登錄接口返回的 加密用戶信息
        iv	微信登錄接口返回的加密偏移數據	


 2.通過郵箱注冊:通過用戶管理接口測試
 3.通過手機號注冊:通過用戶管理接口測試

封裝數據請求API接口

API: application interface ,即編程接口

1.提取公共的url

2. 二次封裝ajax請求(axios,$.ajax,wx.request)

3. 基於業務封裝的API:獲取商品列表,添加購物車,添加收藏,搜索,商品說情

4.通過Promise將請求的數據拋出,供用戶使用

   new Promise(回調函數)

5.對數據加載進行loading,toast處理


封裝好,如果在頁面中調用


 getGoodsList().then(res=>{

  console.log('終於等到你',res)


});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM