軟件功能及核心代碼
4.1.1 用戶登錄注冊(cè)?
首先是在用戶進(jìn)入平臺(tái)時(shí)獲取用戶微信昵稱己肮,保存作為用戶名息罗。至于用戶ID,是通過java中的UUID方法隨機(jī)分配一唯一值洪鸭。最后將這兩項(xiàng)必填信息寫入數(shù)據(jù)庫(kù)完成注冊(cè)
核心代碼如下:
(1)獲取用戶微信昵稱
// 登錄
letthat =this
wx.login({
success:res=>{
// 發(fā)送 res.code 到后臺(tái)換取 openId, sessionKey, unionId
? ? },
})
// 獲取用戶信息
wx.getSetting({
success:res=>{
if(res.authSetting['scope.userInfo']) {
// 已經(jīng)授權(quán)挡毅,可以直接調(diào)用 getUserInfo 獲取頭像昵稱蒜撮,不會(huì)彈框
wx.getUserInfo({
success:res=>{
// 可以將 res 發(fā)送給后臺(tái)解碼出 unionId
that.globalData.userInfo = res.userInfo//獲取用戶基本信息并保存為全局變量
that.globalData.userName = res.userInfo.nickName//獲取用戶微信昵稱并保存為全局變量
(2)UUID方法隨機(jī)分配一唯一值
//獲取UUID生成的32位數(shù)的前8位作為用戶ID
Stringuid = UUID.randomUUID().toString().substring(0,8);
4.1.2 商品分類導(dǎo)航
商品總共有4中分類,系統(tǒng)為每種分類分配一ID值慷嗜,內(nèi)容欄將會(huì)根據(jù)不同ID值顯示不同類型的商品淀弹。
核心代碼如下:
//商品類別標(biāo)簽切換事件
switchTab:function(e){
letid = e.currentTarget.dataset.id//獲取前端商品類別的ID值
lettype//定義商品類別變量
letnum =0//定義保存商品類別的數(shù)組初始下標(biāo)
claArray = []//定義保存商品類別數(shù)組的臨時(shí)變量
varthat =this
this.setData({
curNavId: id,//給前端商品類別賦值
})
//通過id分類List頁(yè)面內(nèi)容
switch(id){
case1:
type="推薦"
break;
case2:
type="學(xué)習(xí)"
break;
case3:
type="運(yùn)動(dòng)"
break;
case4:
type="生活"
break;
case5:
type="智能"
break;
}
if(GoodContent.length >0){
for(leti =0; i < GoodContent.length; i++) {
letcontent = GoodContent[i]
if(content.GoodType ==type||type=="推薦") {//content.GoodType很關(guān)鍵
? ? ? ? ? ? claArray[num++] = content
? ? ? ? }
}
this.setData({
? ? listContent: claArray
})
4.1.3 熱門商品展示
熱門商品展示主要是本系統(tǒng)會(huì)根據(jù)商城各商品的點(diǎn)擊瀏覽量,自動(dòng)推薦瀏覽量最高的前5款商品展示在主頁(yè)庆械。
核心代碼如下:
//連接數(shù)據(jù)庫(kù)獲取商品信息
wx.request({
url:'http://localhost:8080/Ajax-Servlet-Test1/StoreMsg',//服務(wù)器地址
data: {
xx:'selectAll',
},
header: {
'Content-Type':'application/json'
},
success:function(res){
that.globalData.GoodMessage = res.data//將獲取的數(shù)據(jù)保存在GoodMessage 全局變量
? ? }
})
varurlList =newArray()//定義保存熱門商品圖片Url地址的數(shù)組
for(leti =0; i <5; i++) {
urlList[i] = app.globalData.GoodMessage[i].GoodImgUrl
}
4.1.4 模糊搜索商品
? 模糊搜索商品主要是根據(jù)用戶輸入的關(guān)鍵字進(jìn)行檢索薇溃,用戶輸入商品信息關(guān)鍵字,與已發(fā)布的商品信息進(jìn)行匹配缭乘,搜索到符合要求的商品
核心代碼如下:
//獲取服務(wù)器數(shù)據(jù)
getdata:function(){
letthat =this
? ? wx.request({
url:'http://localhost:8080/Ajax-Servlet-Test1/inputMsg',
data: {
xx:'select',
//用戶輸入的關(guān)鍵字
yy: that.data.inputValue
? ? },
header: {
'Content-Type':'application/json'
? ? },
success:function(res){
? ? ? ? that.setData({
//將服務(wù)器返回的匹配信息保存
list:res.data
? ? ? ? })
}
})
},
//服務(wù)器端與數(shù)據(jù)庫(kù)進(jìn)行交互
if(str.equals(goodName)){
Stringsql ="SELECT * From good WHERE GoodTitle LIKE '%"+goodName+"'%";
PreparedStatement prep =null;
try{
? ? ? ? prep = (PreparedStatement) connection.prepareStatement(sql);
? ? ? ? ResultSet result = prep.executeQuery();
while(result.next()){
Good good =newGood();
StringGoodTitle = result.getString("GoodTitle");
? ? ? ? ? ? good.setUserName(GoodTitle);
? ? ? ? ? ? goodList.add(good);
? ? ? ? }
}catch(SQLException e) {
? ? ? ? e.printStackTrace();
? ? }
}
Gson gson =newGson();
Stringgoods = gson.toJson(goodList);
write.write(goods);
4.1.5 發(fā)布商品?
有效用戶在發(fā)布商品信息時(shí)沐序,填寫商品名稱、商品圖片堕绩、商品類別策幼、商品介紹、聯(lián)系人ID奴紧、聯(lián)系人姓名等信息特姐,然后通過request訪問服務(wù)器將數(shù)據(jù)寫進(jìn)數(shù)據(jù)庫(kù)。
核心代碼如下:
(1)獲取本地圖片黍氮,并將圖片臨時(shí)地址保存在數(shù)據(jù)庫(kù)
//上傳圖片
bindSendImage:function(res){
letthat =this
? ? wx.chooseImage({
count:1,//定義本地選擇圖片時(shí)只能選擇一張
sizeType:['original','compressed'],//定義選擇的圖片可以被壓縮
sourceType:['album','camera'],//圖片既可以從相冊(cè)中選擇也可以拍照獲取
success:function(res){
? ? ? ? that.setData({
imgUrl:res.tempFilePaths[0]//獲取本地圖片url地址
? ? })
4.1.6 商品收藏
用戶在進(jìn)入商品的詳細(xì)信息界面時(shí)唐含,如果喜歡該物品但是又不想立刻購(gòu)買的話,可以收藏該物品沫浆,收藏好的商品也可以點(diǎn)擊取消捷枯。
核心代碼如下:
//收藏事件
switchSC:function(id){
letthat =this
letuserId = app.globalData.userId//獲取登錄用戶ID
switch(id){
case0:
this.setData({
shoucang:"收藏"
? ? ? ? ? ? })
//連接數(shù)據(jù)庫(kù)刪除用戶收藏信息
? ? ? ? ? ? wx.request({
url:'http://localhost:8080/Ajax-Servlet-Test1/StoreMsg',
data: {
xx:'deleteSC',
//用戶ID
yy: userId,
//商品ID
zz: that.data.GoodId,
//店家ID
ss: that.data.DjId
? ? ? ? ? ? ? ? },
header: {
'Content-Type':'application/json'
? ? ? ? ? ? ? ? },
success:function(res){
console.log("刪除成功")
? ? ? ? ? ? ? ? }
? ? ? ? ? ? })
break;
case1:
this.setData({
shoucang:"收藏成功"
? ? ? ? ? ? })
//連接數(shù)據(jù)庫(kù)添加用戶收藏信息
? ? ? ? ? ? wx.request({
url:'http://localhost:8080/Ajax-Servlet-Test1/StoreMsg',
data: {
xx:'insertSC',
//用戶ID
yy: userId,
//商品ID
zz: that.data.GoodId,
//店家ID
ss: that.data.DjId
? ? ? ? ? ? ? ? },
header: {
'Content-Type':'application/json'
? ? ? ? ? ? ? ? },
success:function(res){
console.log("收藏成功")
? ? ? ? ? ? ? ? }
? ? ? ? ? ? })
break;
}
}
案例展示