1. 程式人生 > >微信小程式(十)實戰——請求後臺資料(微信小程式+laravel)

微信小程式(十)實戰——請求後臺資料(微信小程式+laravel)

1.剛開始是本地測試連結資料庫,傳遞死資料,為了將前後流程走通,也就是給定一個數據

                                        從前臺——》到後臺——》前臺顯示;

2.現在我們是實戰,直接幹;

①本地測試為了方便,頁面載入就請求後臺資料

請求伺服器:.js

onLoad: function (options) {
var that = this;//=====注意此處,要用that 指代this=====
wx.request({
url: 'http://localhost:8000/wxApp/showMentors', //伺服器地址
method: 'get',// OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
data: { }, header: {// 設定請求的 header 'content-type': 'application/json' }, success: function (res) { console.log(res); that.setData({ //======不能直接寫this.setDate====== message: res.data, //在相應的wxml頁面顯示接收到的資料 }); } }) },

這裡需要注意的是伺服器地址URL:即介面:http://localhost:8000/wxApp/showMentors

在瀏覽器中也可以直接訪問:


說明:我是在本地測試+laravel框架,所以我的URL是:本地虛擬域名+路由:http://localhost:8000/wxApp/showMentors

路由:訪問方法(laravel的路由)

Route::group(array('prefix'=>'wxApp'),function(){
    Route::get('/showMentors','WxApp\[email protected]');//老師列表});

方法:查詢所需資料傳遞到頁面(laravel中路由->方法)

public function showMentors(){
$mentorList = DB::table('mentor_infos_view')
        ->where('if_vip',1)
        ->where('user_id'
,'!=',$this->uid) ->get(); echo json_encode($mentorList); }

接收:頁面接收  .js

success: function (res) { that.setData({ //======不能直接寫this.setDate====== message: res.data, //在相應的wxml頁面顯示接收到的資料 }); }

顯示: 頁面顯示.wxml

<!-- 接收資料迴圈 --><view wx:for="{{message}}" wx:for-item="j" wx:key="id" name="info_border" ><!-- 判斷是否有頭像 --><view wx:if="{{j.mentor_image_uri==null}}"><image class="widget_arrow" src="http://dev.cfo-mentor.com/menter/public/assets/images/avatar_default.png" mode="aspectFill"></image></view><view wx:else><image class="widget_arrow" src="{{j.mentor_image_uri}}" mode="aspectFill"></image></view><!-- 顯示 --><view class='info'>姓名:{{j.mentor_name}}</view><view class='info'>職位:{{j.career}}</view><view class='info'>公司:{{j.company_name}}</view><view class='info'>地區:{{j.address}}</view><view class='info'>擅長:{{j.mentor_skills}}</view><navigator url='../../pages/info/info?user_id={{j.user_id}}'>詳情</navigator><view class='hr'></view></view>

效果:

跳轉到詳情頁面:

①首先需要建立小程式頁面(在app.js中新增"pages/info/info",

       效果:

②再建立連線:在小程式中navigator標籤相當於a標籤

<navigator url='../../pages/info/info?user_id={{j.user_id}}'>詳情</navigator>

說明:帶引數和以前一樣?XX={{XX}},URL就是前面建立的info中的info.js


③在新頁面接收資料:   

data: { user_id:"" },

④在onload中載入請求:

onLoad: function (options) { var that = this; wx.request({ url: 'http://localhost:8000/wxApp/showMentorInfo', //伺服器地址 method: 'get', data: { user_id : options.user_id, }, header: { 'content-type': 'application/json' }, success: function (res) { that.setData({ showMentorInfo: res.data, }); } }) },

請求地址: url: 'http://localhost:8000/wxApp/showMentorInfo', //在laravel中就是到路由;

請求方式:method: 'get',

請求引數: data: {
        user_id : options.user_id,

      },

請求頭:header: {

        'content-type': 'application/json'

      },

⑤在laravel中建立路由

Route::group(array('prefix'=>'wxApp'),function(){
    Route::get('/showMentorInfo','WxApp\[email protected]');//老師個人資訊
});

⑥在建立方法

public function showInfo()
{
$user_id=$_REQUEST['user_id'];
$showMentorInfo = DB::table('mentor_infos_view')
        ->where('if_vip',1)
        ->where('user_id',$user_id)
        ->first();
echo json_encode($showMentorInfo);
}

⑦在success裡面接收資料

success: function (res) { that.setData({ showMentorInfo: res.data, }); }

⑧在.wxml裡面顯示資料

<view wx:if="{{j.mentor_image_uri==null}}"><image class="widget_arrow" src="http://dev.cfo-mentor.com/menter/public/assets/images/avatar_default.png" mode="aspectFill"></image></view><view wx:else><image class="widget_arrow" src="{{j.mentor_image_uri}}" mode="aspectFill"></image></view><view class='info'>姓名:{{showMentorInfo.mentor_name}}</view><view class='info'>電話:{{showMentorInfo.mentor_phone}}</view><view class='info'>職位:{{showMentorInfo.career}}</view><view class='info'>公司:{{showMentorInfo.company_name}}</view><view class='info'>地區:{{showMentorInfo.address}}</view><view class='info'>擅長:{{showMentorInfo.mentor_skills}}</view><view class='info'>偏好:{{showMentorInfo.mentor_prefer}}</view><view class='info'>導師介紹:{{showMentorInfo.mentor_ps}}</view><view class='hr'></view>

效果:


剛入門,如有錯誤,歡迎指出,感謝!!!