微信小程序中商城開發(fā)的示例分析

這篇文章給大家分享的是有關微信小程序中商城開發(fā)的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯是專業(yè)的瑞麗網站建設公司,瑞麗接單;提供做網站、網站設計,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行瑞麗網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!

最近小程序特別火,所以我們公司也針對ecshop平臺對接了小程序

包括完整的用戶系統(tǒng)和購物體統(tǒng)

用戶系統(tǒng):收貨地址,訂單管理,消息管理,優(yōu)惠券管理等等

購物系統(tǒng)支付購物車管理,微信支付等等

微信小程序中商城開發(fā)的示例分析

微信小程序中商城開發(fā)的示例分析

微信小程序中商城開發(fā)的示例分析

微信小程序中商城開發(fā)的示例分析

微信小程序中商城開發(fā)的示例分析

微信小程序中商城開發(fā)的示例分析

相信有很多小伙伴都用的是ecshop作為自己的商城,最近小程序又火了,于是就有人問ecshop對接小程序怎么做。

正好最近在開發(fā)一個對接ecshop的小程序項目,就將我的一些開發(fā)經驗分享一下。

一:掃描小程序二維碼后的用戶信息的獲取和緩存

獲取用戶信息需要用到兩個api

wx.login(OBJECT)

調用接口獲取登錄憑證(code)進而換取用戶登錄態(tài)信息,包括用戶的唯一標識(openid) 及本次登錄的 會話密鑰(session_key)。用戶數據的加解密通訊需要依賴會話密鑰完成。

wx.getUserInfo(OBJECT)

獲取用戶信息,需要先調用 wx.login 接口。

獲取緩存需要用到的api

wx.setStorageSync(KEY,DATA)

將 data 存儲在本地緩存中指定的 key 中,會覆蓋掉原來該 key 對應的內容,這是一個同步接口。

下面就是具體實例代碼:

我們可以將這段寫在公共的app.js頁面

//app.js 
App({ 
 onLaunch: function() { 
 }, 
 getUserInfo: function (cb) { 
 var that = this 
 if (this.globalData.userInfo) { 
  typeof cb == "function" && cb(this.globalData.userInfo) 
 } else { 
  //調用登錄接口 
  wx.login({ 
  success: function (res) { 
   if (res.code) { 
   var userid = wx.getStorageSync('scuserid') 
   var sc_session_id = wx.getStorageSync('sc_session_id') 
   var openid = wx.getStorageSync('sc_session_id') 
   if(!userid){ 
     wx.request({ 
     url: 'xxxx/data.php?action=sendCode', 
     data: { 
      code: res.code, 
     }, 
     success: function (res) { 
      //console.log(res) 
      var status = res.data.status 
      if(status == 1){ 
       wx.showToast({ 
       title: res.data.message, 
       icon: 'success', 
       duration: 2000 
       }) 
      }else if(status == 2){ 
       var scuserid = res.data.userid 
       if(scuserid > 0){ 
        //緩存user_id 
        wx.setStorageSync('scuserid', scuserid) 
        wx.setStorageSync('openid', res.data.openid) 
        wx.setStorageSync('sc_session_id', res.data.session_id) 
       } 
      }else{ 
       //緩存session_id 
       wx.setStorageSync('openid', res.data.openid) 
       wx.setStorageSync('sc_session_id', res.data.session_id) 
       //獲取用戶信息 
       wx.getUserInfo({ 
       success: function (res) { 
        that.globalData.userInfo = res.userInfo 
        typeof cb == "function" && cb(that.globalData.userInfo) 
        //console.log(res); 
        wx.request({ 
        url: 'xxxx/data.php?action=saveUserInfo', 
        data: { 
         userinfo: res.userInfo, 
         openid: wx.getStorageSync('openid'), 
        }, 
        success: function (res) { 
         //console.log(res.data) 
         var status = res.data.status 
         if(status == 1){ 
          wx.showToast({ 
           title: res.data.message, 
           icon: 'success', 
           duration: 2000 
          }) 
         }else{ 
          var scuserid = res.data.userid 
          if(scuserid > 0){ 
          //緩存user_id 
          wx.setStorageSync('scuserid', scuserid) 
          } 
         } 
        } 
        }) 
       } 
       }) 
      } 
     } 
     }) 
   } 
   } 
  } 
  }) 
 } 
 }, 
 globalData: { 
 userInfo: null 
 } 
})

二:獲取微信用戶的信息以及如何將用戶信息緩存起來

要獲取用戶的地理信息則要用到

wx.getLocation(OBJECT)

獲取當前的地理位置、速度。當用戶離開小程序后,此接口無法調用;當用戶點擊“顯示在聊天頂部”時,此接口可繼續(xù)調用。

具體實例代碼:

//獲取緯度,經度 
 wx.getLocation({ 
  type: 'wgs84', 
  success: function (res) { 
  var latitude = res.latitude 
  var longitude = res.longitude 
  wx.request({ 
   url: 'http://XXXXXX/data.php?action=get_dq', 
   data: { 
   latitude: latitude, 
   longitude: longitude 
   }, 
   headers: { 
   'Content-Type': 'application/json' 
   }, 
   success: function (res) { 
   //console.log(res.data) 
   var province = res.data.result.addressComponent.province 
   //console.log(province) 
   var city = res.data.result.addressComponent.city 
   var district = res.data.result.addressComponent.district 
   var diqu = province+city+district 
   //緩存當前所在地區(qū) 
   wx.setStorageSync('dq_diqu', diqu) 
   wx.setStorageSync('dq_district', district) 
   } 
  }) 
  } 
 }) 
if($act=="get_dq"){ 
 //獲取當然城市 
 //http://api.map.baidu.com/geocoder/v2/?ak=327381a342077a8f3d584251b811cce5&callback=renderReverse&location=30.593099,114.305393&output=json 
 //緯度 
 $latitude = $_REQUEST['latitude']; 
 //經度 
 $longitude = $_REQUEST['longitude']; 
 $url = 'http://api.map.baidu.com/geocoder/v2/?ak=327381a342077a8f3d584251b811cce5&location='.$latitude.','.$longitude.'&output=json'; 
 $result = file_get_contents($url); 
 exit($result); 
}

感謝各位的閱讀!關于“微信小程序中商城開發(fā)的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網站題目:微信小程序中商城開發(fā)的示例分析
網址分享:http://bm7419.com/article6/jdgsog.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站內鏈云服務器、定制網站動態(tài)網站、手機網站建設域名注冊

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都網站建設公司