任務2-在go系統中設計並實現介面,允許小程式訪問並獲取資料顯示到頁面上
阿新 • • 發佈:2021-02-05
技術標籤:512
任務2
在go系統中設計並實現介面,允許小程式訪問該介面後,從資料庫表ExpSmallScale中隨機獲取一條內容,並將該內容顯示到任務1製作的頁面上。
完成過程
go系統
- 在map.patient.go中加入一條,表示當傳送這樣的請求呼叫這兩個方法。第一個方法為具體實現,第二個為文件記錄。
router.POST(path.Path("/data/xddquestionaire/read"),s.patientData.ReadForQuestionaire,s.patientData.ReadForQuestionaireDoc)
- 在api/data.go中加入介面說明。其中這裡需要一個filter來確定我們需要獲取的資料是哪幾條。
ReadForQuestionaire(filter *doctor.ExpSmallScaleFilter) (*doctor.ExpSmallScale,business.Error)
- 為了完成這個filter,需要在這兩個檔案裡進行相關說明。【CopyFrom這塊還不是很明白】
package sqldb
import "cdmwb/data/model/doctor"
type ExpSmallScaleFilter struct {
SerialNo uint64 `sql:"SerialNo" auto:"true" primary:"true"`
}
func (s *ExpSmallScaleFilter) CopyFrom(source *doctor.ExpSmallScaleFilter) {
if source == nil {
return
}
s.SerialNo = source.SerialNo
}
package doctor
type ExpSmallScaleFilter struct {
SerialNo uint64 `sql:"SerialNo" auto:"true" primary:"true"`
}
- 下一步就在impl/data.get.go裡實現這個介面。【這部分也還不是很明白】
//入參 出參
func (s *Data) ReadForQuestionaire(filter *doctor.ExpSmallScaleFilter) (*doctor.ExpSmallScale, business.Error) {
result := &doctor.ExpSmallScale{}
//上面這是隻獲取一條資料的寫法
dbEntity := &sqldb.ExpSmallScale{}
dbFilter := &sqldb.ExpSmallScaleFilter{}
dbFilter.CopyFrom(filter) //要寫copyform 轉移引數
sqlFilter := s.sqlDatabase.NewFilter(dbFilter,false,false)
//dbOrder := &sqldb
err := s.sqlDatabase.SelectOne(dbEntity, sqlFilter)
if err != nil {
return nil, business.NewError(errors.InternalError, err)
}
dbEntity.CopyTo(result)
return result, nil
}
- 最後在controller的兩個檔案裡實現ReadForQuestionaire和ReadForQuestionaireDoc。【為什麼有兩個檔案都要這樣寫不懂】
//程式碼都是直接仿造ListBloodPressureData寫的
func (s *Data) ReadForQuestionaire(w http.ResponseWriter, r *http.Request, p router.Params, a router.Assistant){
argument := &doctorModel.ExpSmallScaleFilter{}
err := a.GetArgument(r, argument)
if err != nil{
a.Error(errors.InputError,err)
return
}
data, be := s.patientBusiness.Data().ReadForQuestionaire(argument)
if be != nil {
a.Error(be.Error(),be.Detail())
return
}
a.Success(data)
}
//文件
func (s *Data) ReadForQuestionaireDoc(a document.Assistant) document.Function{
//now := types.Time(time.Now())
function := a.CreateFunction("獲取問卷題目")
function.SetNote("這個是什麼函式沒懂")
function.SetInputExample(&doctorModel.ExpSmallScaleFilter{
SerialNo: 1,
})
function.SetOutputExample([]*doctorModel.ExpSmallScale{
{
SerialNo: 1,
Content: "問卷題目",
},
})
s.setDocFun(a, function)
return function
}
func (s *Patient) ReadForQuestionaire(w http.ResponseWriter, r *http.Request, p router.Params, a router.Assistant){
argument := &doctorModel.ExpSmallScaleFilter{}
err := a.GetArgument(r, argument)
if err != nil{
a.Error(errors.InputError,err)
return
}
data, be := s.patientBusiness.Data().ReadForQuestionaire(argument)
if be != nil {
a.Error(be.Error(),be.Detail())
return
}
a.Success(data)
}
func (s *Patient) ReadForQuestionaireDoc(a document.Assistant) document.Function{
//now := types.Time(time.Now())
function := a.CreateFunction("獲取問卷題目")
function.SetNote("這個是什麼函式沒懂")
function.SetInputExample(&doctorModel.ExpSmallScaleFilter{
SerialNo: 1,
})
function.SetOutputExample([]*doctorModel.ExpSmallScale{
{
SerialNo: 1,
Content: "問卷題目",
},
})
s.setDocFun(a, function)
return function
}
到這裡就完成了go系統介面的設計與實現,接下來要在小程式發出這個介面並接受返回的資料顯示在介面上
小程式
- 首先在config.js裡把地址賦值給一個變數。【但是這裡很奇怪的是我用這個變數名當作url出錯了,我後來是直接寫的“”地址】
const vicoQuestionRead = vicoBaseUrl + '/patient.api/data/xddquestionaire/read'
- (下面就整個一咕嚕說明最後的情況好了,中間的艱難過程就不具體回顧)首先是task1.wxss,複製用了師兄給的scale-detail.wxss,直接套用裡面的樣式。
- 然後是task1.wxml,因為師兄給我的是一個比較完整的問卷模式了,整個問卷裡,各個題目是一個數組,然後每個題目又分別有各自的選項。在我各種刪減後最後成了這樣,因為我只從資料庫裡獲取一條資料,所以我這裡qitem這個陣列只會有一個項。因此我沒有去考慮有多個項的話,這個裡面的選項item如何處理。【然後元件的很多屬性其實我也還不太清楚,比如index】
<view class="page-body">
<view class='survey_detail_container'>
<view class='survey_detail_header' >這是一個問卷</view>
<view class='survey_detail_item' wx:for="{{qitem}}" wx:for-index="qitem.index" wx:for-item="qitem" wx:key="qitem.index" >
<view class='survey_detail_title'>{{qitem.content}}</view>
<view wx:if="{{scale.type=='singleCheck'}}">
<radio-group class="survey_detail_options" bindchange="answerChange" data-index="{{item.index}}">
<label class="survey-label" wx:for="{{item}}" wx:for-item="item" wx:key="{{item.index}}">
<radio value="{{item.value}}"/>{{item.content}}
</label>
</radio-group>
</view>
</view>
<button class="survey_btn" type="primary" bindtap="doSave">提交問卷</button>
</view>
</view>
- 最後是task1.js,這個我遇到了不少問題,因為首先我之前沒接觸過小程式開發,很多基本知識、語法都不知道,所以困擾我的東西我很多時候都不知道怎麼去查,太基礎了也查不到。最後勉勉強強寫得可以,不過還有很多問題,暫且先記錄,也許等到下一步任務時會不得不解決,進一步學習。
//這裡因為不懂,照搬照抄出現了錯誤。照搬的是../../../
//但是照抄的那個檔案比現在這個檔案目錄還低一級,所以需要去掉一個../
import { tokenRequest } from "../../utils/Request";
import { vicoQuestionRead } from "../../utils/config";
var app = getApp() ;
Page({
onShareAppMessage() {
return {
title: 'form',
path: 'page/component/pages/form/form'
}
},
//好像是如果要在wxml呼叫的資料要在data裡宣告然後給一個預設值
//所以item qitem scale(從資料庫裡獲取到的內容格式一樣)
//但是假如有很多問題的話,item qitem又該如何設定
//直接從scale拿? 因為我這裡其實scale沒怎麼用到 是冗餘的,可是value怎麼辦
data: {
question: "測試是否是這個",
scale: {barriers:[1,2,3,4], degree:[1,2,3,4],linkedItem:[],openType:0,options:[{content:"diyigexuanxiang4",index:"D"},{content:"diyigexuanxiang",index:"A"},{content:"diyigexuanxiang2",index:"B"},{content:"diyigexuanxiang3",index:"C"}],title:"平平無奇的標題一個",type:"singleCheck"},
select: [1,2,3,4],
//因為後面還需要判斷點選了哪個item,所以value這個屬性要有,但是正如scale裡的看到的所獲取的選項裡是沒有value的,所以直接把options賦值給item是不行的。後面會寫到。
item: [
{value: 'itemValue1', content: 'test30歲以下',index:'A'},
{value: 'itemValue2', content: 'test30-40歲',index:'B'},
{value: 'itemValue3', content: 'test40-50歲',index:'C'},
{value: 'itemValue4', content: 'test50-60歲',index:'D'},
],
qitem: [
{value: 'qitemValue1', content: 'question', index:'1'},
],
//把任務1裡的兩個問題都刪掉了
refresh:function(){
var that=this;
let url = 'http://localhost:8080/patient.api/data/xddquestionaire/read'
//let url = vicoQuestionRead 為啥不行
let data = {
SerialNo:1,
}
let method = "POST"
let token = wx.getStorageSync('login_token')
let header = {
"token":token,
"content-type": "application/json"
}
tokenRequest({url:url, header:header, method:method, data:data}).then(res=>{
if (res.data.code == 20001) {
console.log('relogin')
setTimeout(()=>{
that.refresh()
},700)
} else {
console.log(res.data)
//this.data是上面data裡的內容
console.log("second time")
console.log(this.data)
//把從資料庫裡獲取到的res.data賦值給我們要顯示到介面上的引數
//但是這裡,我只會一個個地賦值,這絕對是笨方法,但要怎麼解決…感覺是個語法問題…
that.setData({
scale : res.data.data.content["0"],
"item[0].content" : res.data.data.content["0"].options["0"].content,
"item[1].content" : res.data.data.content["0"].options["1"].content,
"item[2].content" : res.data.data.content["0"].options["2"].content,
"item[3].content" : res.data.data.content["0"].options["3"].content,
"qitem[0].content" : res.data.data.content["0"].title,
})
}
})
},
answerChange: function(e) {
const item = this.data.item
for (let i = 0, len = item.length; i < len; ++i) {
item[i].checked = item[i].value === e.detail.value
console.log(item[i].checked)
}
this.setData({
item,
})
},
/**
* 生命週期函式--監聽頁面顯示
*/
onShow: function () {
this.refresh();
},
/**
* 頁面相關事件處理函式--監聽使用者下拉動作
*/
onPullDownRefresh: function () {
this.refresh();
wx.stopPullDownRefresh()
},
/**
* 生命週期函式--監聽頁面隱藏
*/
onHide: function () {
this.setData({
isLoaded: false,
isDisposed: false
})
},
})
以上大致是任務2的完成情況,真的完全新手,還有好多不懂,但也基本熟悉了,感覺語法啊,還要再學習學習