




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】微信小程序中商城開發(fā)的示例分析
這篇文章給大家分享的是有關(guān)微信小程序中商城開發(fā)的示例分析的內(nèi)容。在下覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨在下過來看看吧。最近小程序特別火,所以我們公司也針對(duì)ecshop平臺(tái)對(duì)接了小程序包括完整的用戶系統(tǒng)和購物體統(tǒng)用戶系統(tǒng):收貨地址,訂單管理,消息管理,優(yōu)惠券管理等等購物系統(tǒng)支付購物車管理,微信支付等等相信有很多小伙伴都用的是ecshop作為自己的商城,最近小程序又火了,于是就有人問ecshop對(duì)接小程序怎么做。正好最近在開發(fā)一個(gè)對(duì)接ecshop的小程序項(xiàng)目,就將我的一些開發(fā)經(jīng)驗(yàn)分享一下。一:掃描小程序二維碼后的用戶信息的獲取和緩存獲取用戶信息需要用到兩個(gè)apiwx.login(OBJECT)調(diào)用接口獲取登錄憑證(code)進(jìn)而換取用戶登錄態(tài)信息,包括用戶的唯一標(biāo)識(shí)(openid)及本次登錄的會(huì)話密鑰(session_key)。用戶數(shù)據(jù)的加解密通訊需要依賴會(huì)話密鑰完成。wx.getUserInfo(OBJECT)獲取用戶信息,需要先調(diào)用wx.login接口。獲取緩存需要用到的apiwx.setStorageSync(KEY,DATA)將data存儲(chǔ)在本地緩存中指定的key中,會(huì)覆蓋掉原來該key對(duì)應(yīng)的內(nèi)容,這是一個(gè)同步接口。下面就是具體實(shí)例代碼:我們可以將這段寫在公共的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
{
//調(diào)用登錄接口
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)獲取當(dāng)前的地理位置、速度。當(dāng)用戶離開小程序后,此接口無法調(diào)用;當(dāng)用戶點(diǎn)擊“顯示在聊天頂部”時(shí),此接口可繼續(xù)調(diào)用。具體實(shí)例代碼://獲取緯度,經(jīng)度
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.addressCvince
//console.log(province)
var
city
=
res.data.result.addressComponent.city
var
district
=
res.data.result.addressComponent.district
var
diqu
=
province+city+district
//緩存當(dāng)前所在地區(qū)
wx.setStorageSync('dq_diqu',
diqu)
wx.setStorageSync('dq_district',
district)
}
})
}
})
if($act=="get_dq"){
//獲取當(dāng)然城市
///geocoder/v2/?ak=327381a342077a8f3d584251b811cce5&callback=renderReverse&location=30.593099,114.305393&output=json
//緯度
$latitude
=
$_REQUEST['latitude'];
//經(jīng)度
$longitude
=
$_REQUEST['longitude'];
$url
=
'/geocoder/v2/?ak=327381a342077a8f3d584251b8
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年開封運(yùn)輸從業(yè)資格證考試技巧
- 農(nóng)業(yè)設(shè)備供貨合同范本
- 勞動(dòng)合同范本大全
- 2025年運(yùn)城貨運(yùn)上崗證模擬考試試題
- 專利侵權(quán)檢索合同范本
- 買賣廢鋼材合同范本
- 勞務(wù)合同范本無社保
- 臨泉教師合同范本
- 三人合作協(xié)議合同范本
- 交電產(chǎn)品銷售合同范例
- 編輯出版史考試重點(diǎn)整理
- (74)-17.3手性高效液相色譜法
- 淺談新生兒肺透明膜病的影像學(xué)診斷
- SMT失效模式分析PFMEA
- 國際貿(mào)易地理全套課件
- 家校共育-助孩子成長-家長會(huì)課件
- 叉形件工藝及車床夾具設(shè)計(jì)說明書
- GB/T 5916-2008產(chǎn)蛋后備雞、產(chǎn)蛋雞、肉用仔雞配合飼料
- 《中小學(xué)教育懲戒規(guī)則》(試用)試題及答案
- GB/T 23723.1-2009起重機(jī)安全使用第1部分:總則
- 安全測試工具、蹭網(wǎng)利器wifiphisher新增漢化版
評(píng)論
0/150
提交評(píng)論