Authored by wuxiao

获取有货商品信息

... ... @@ -3,7 +3,7 @@ namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use yii\helpers\ArrayHelper;
use common\lib\YohoApi\Client as YohoApiClient;
/**
* Live controller
... ... @@ -37,8 +37,30 @@ class ProductController extends BaseController
$list = $model
->offset($pagination->offset)->limit($pagination->limit)
->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
->indexBy('product_id')
->all();
/**
* 获取有货商品信息
*/
$productSkn = [];
foreach ($list as $model){
$productSkn[] = $model->product_id;
}
$ret = YohoApiClient::self()->h5ProductBatch($productSkn);
//var_dump(YohoApiClient::self()->h5ProductBatch([51188407,51188408,51188414,51188421,51188468]));
//var_dump(YohoApiClient::getRequestUrl());
if (empty($ret['product_list'])){
array_shift($productSkn);
$ret = YohoApiClient::self()->h5ProductBatch($productSkn);
}
if (!empty($ret['product_list'])){
foreach ($ret['product_list'] as $product){
$list[$product['product_skn']]->product_name = $product['product_name'];
$list[$product['product_skn']]->sales_price = $product['sales_price'];
}
}
return $this->render('list',[
'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
'pagination'=>$pagination,
... ... @@ -93,7 +115,7 @@ class ProductController extends BaseController
$post = Yii::$app->getRequest()->post();
if ($model->load($post,'') && $model->save()){
Yii::$app->session->setFlash('success', '编辑成功。');
$model->refresh();
return $this->redirect($this->_refer);
}else{
Yii::$app->session->setFlash('warning', current($model->getFirstErrors()));
}
... ...
... ... @@ -30,7 +30,8 @@ class RoomProduct extends \yii\db\ActiveRecord
public function rules()
{
return [
[['room_id', 'product_id', 'sort', 'create_time', 'update_time'], 'integer'],
[['room_id', 'product_id', 'sort', 'sales_price', 'create_time', 'update_time'], 'integer'],
[['product_name'], 'string', 'max' => 100],
[['room_id', 'product_id'], 'unique', 'targetAttribute' => ['room_id', 'product_id'], 'comboNotUnique' => '商品ID已存在'],
];
}
... ...
... ... @@ -106,6 +106,8 @@ $this->registerJs($this->blocks['javascript'],View::POS_END)
<tr style="white-space:nowrap">
<th><input type="checkbox" onclick="checkall(this)">选择</th>
<th>商品ID</th>
<th>商品名称</th>
<th>商品销售价</th>
<th>排序</th>
<th>操作</th>
</tr>
... ... @@ -115,6 +117,8 @@ $this->registerJs($this->blocks['javascript'],View::POS_END)
<tr data-stock="1" data-source="1" data-status="2" data-id="5011880">
<td><input type="checkbox" value="<?=$model->id?>" name="product[]"></td>
<td><?=$model->product_id?></td>
<th><?=$model->product_name?></th>
<th><?=$model->sales_price?></th>
<td><?=$model->sort?></td>
<td>
... ...
<?php
namespace common\lib\YohoApi;
use yii\helpers\ArrayHelper;
/**
* 有货接口
* @author wuxiao
*/
class Client{
private static $_domain = array(
'prod' => 'http://api.yoho.cn/',
'dev' => 'http://192.168.102.205:8080/gateway/',
);
/**
* 私钥列表
* @var array
*/
private static $privateKey = array(
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
'web' => '0ed29744ed318fd28d2c07985d3ba633',
'h5' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'other' => '6tjjbg7ecrcd3ulgqizbqavfrutixhm7',
);
/**
* 默认客户端类型
*/
const client_type = 'web';
/**
* $_requestUrl
* 请求url
* @var string
*/
protected static $_requestUrl = '';
/**
* $_rawResponse
* 原始的返回信息
* @var string
*/
protected static $_rawResponse = '';
/**
* $_timeOut
* 设置连接主机的超时时间
* @var int 数量级:秒
* */
protected static $_timeOut = 2;
/**
* getRequestUrl
* 获取请求url
*/
public static function getRequestUrl()
{
return self::$_requestUrl;
}
/**
* getRawResponse
* 获取原始的返回信息
*/
public static function getRawResponse()
{
return self::$_rawResponse;
}
/**
* 实例化自身
*/
public static function self()
{
static $self;
if (empty($self)){
$self = new self();
}
return $self;
}
private function __construct(){
;
}
/**
* 根据多个skn查询的接口
* @param type $productSkn
* @return type
*/
public function h5ProductBatch($productSkn){
$productSkn = (array)$productSkn;
$params = [
'client_type'=>self::client_type,
'method'=>'h5.product.batch',
'productSkn'=>implode(',',$productSkn),
];
return $this->send($params);
}
protected function send(array $params)
{
$params = $this->makeUrl($params);
$domain = $this->getDomain();
$ret = self::_sendRequest($domain,$params,'GET');
if (!is_array($ret) || ($ret['code'] !== 200)){
return false;
}
return $ret['data'];
}
protected function makeUrl(array $package){
$package['private_key'] = self::$privateKey[self::client_type];
ksort($package);
reset($package);
$packageList = array();
foreach ($package as $key => $val) {
$packageList[] = trim($key . '=' . $val);
}
$client_secret = strtolower(md5(implode('&', $packageList)));
$package['client_secret'] = $client_secret;
unset($package['private_key']);
return $package;
}
protected function getDomain(){
return ArrayHelper::getValue(self::$_domain, YII_ENV,self::$_domain['prod']);
}
/**
* _sendRequest
* @param string $url 请求url
* @param array $paramArray 请求参数
* @param string $method 请求方法
* @return
*/
protected static function _sendRequest($url, $paramArray, $method = 'POST')
{
$ch = curl_init();
if ($method == 'POST')
{
$paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
}
else
{
$url .= '?' . http_build_query($paramArray);
}
self::$_requestUrl = $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT,self::$_timeOut);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (false !== strpos($url, "https")) {
// 证书
// curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$resultStr = curl_exec($ch);
self::$_rawResponse = $resultStr;
$result = json_decode($resultStr, true);
if (!$result)
{
return $resultStr;
}
return $result;
}
}
\ No newline at end of file
... ...
... ... @@ -30,7 +30,8 @@ class RoomProduct extends \yii\db\ActiveRecord
public function rules()
{
return [
[['room_id', 'product_id', 'sort', 'create_time', 'update_time'], 'integer'],
[['room_id', 'product_id', 'sort', 'sales_price', 'create_time', 'update_time'], 'integer'],
[['product_name'], 'string', 'max' => 100],
[['room_id', 'product_id'], 'unique', 'targetAttribute' => ['room_id', 'product_id'], 'message' => 'The combination of Room ID and Product ID has already been taken.'],
];
}
... ...