写作背景:最近在写小程序拼团多商家版,从原先单商户改造。现在需要考虑在商品详情中,展示商家数据。那接口如何设计呢,是包含在商品详情,还是单独涉及一个商家详情的接口,简单数据包含复杂的数据需要调接口完成。

效果图:

路由:

Route::group(['prefix' => 'api/v1.0', 'namespace' => "Api\\Air\\v_1_0"], function () {
    # ############### start merchant ###############
    Route::get('merchant/{merchant_id}',                         'MerchantController@detail');
    # ############### end merchant ###############
});

代码:

<?php
// +----------------------------------------------------------------------
// | Description: 
// +----------------------------------------------------------------------
// | Author: beichen <notice@notestore.cn>
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Api\Air\v_1_0;
use App\Http\Controllers\Core\ApiController;
use Illuminate\Support\Facades\DB;

class MerchantController extends ApiController {
    public function detail($merchant_id) {
        $MerchantDb = DB::table('merchant');
        $GoodsDb = DB::table('goods');
        $merchant = $MerchantDb->where('merchant_id',$merchant_id)->where('in_selling',1)->select("merchant_logo", "merchant_name", "merchant_id")->first();

        if(!$merchant) {
            return json_encode(array('result'=>'fail','error_code'=>41002,'error_info'=>'商家已下架或不存在'));
        }

        $merchant["sell_count"] = $GoodsDb->where("merchant_id", $merchant_id)->sum("sell_count");
        echo json_encode(['result'=>'ok','merchant'=>$merchant]);
    }
}

小程序代码:

<view class="merchant-details" wx:if="{{ merchantData.merchant_id }}">
            <view class="merchant-details-base">
                <image class="merchant-details-logo" src="{{merchantData.merchant_logo}}" />
                <view class="merchant-details-title">
                    <view class="merchant-details-name">{{ merchantData.merchant_name }}</view>
                    <view class="merchant-details-count">已团{{ merchantData.sell_count }}件</view>
                </view>
            </view>

            <view class="merchant-details-button" bindtap="bindRedirect" data-url="./merchant?merchant_id={{merchantData.merchant_id}}">进店逛逛</view>
        </view>
.merchant-details-logo {
    width: 88rpx;
    height: 88rpx;
    margin-right: 16rpx;
}

.merchant-details {
    margin-top: 20rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fff;
    padding: 20rpx;
}

.merchant-details-base {
    display: flex;
}

.merchant-details-name {
    font-size: 30rpx;
    font-weight: 700;
    color: #151516;
}

.merchant-details-title {
    color: #58595b;
    font-size: 26rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.merchant-details-button {
    border: 1px solid #9c9c9c;
    font-size: 30rpx;
    color: #151516;
    border-radius: 6rpx;
    text-align: center;
    height: 56rpx;
    width: 152rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}

ThinkPHP
4 声望3 粉丝