Skip to content

Commit

Permalink
添加商品模块
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Nov 21, 2020
1 parent add5e14 commit 10a2641
Show file tree
Hide file tree
Showing 50 changed files with 1,995 additions and 9 deletions.
6 changes: 6 additions & 0 deletions appback/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ public function actionConsts()
];
return ResponseHelper::success($data, '商品对应的常量定义');
}

public function actionDelete()
{
Product::deleteAll(['id' => Yii::$app->request->post('id')]);
return ResponseHelper::success([], '删除成功');
}
}
9 changes: 3 additions & 6 deletions appback/models/forms/ProductForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function rules()
{
return [
[['name', 'count', 'sort', 'price'], 'required'],
[['is_featured', 'audit_status', 'id'], 'safe'],
[['is_featured', 'audit_status', 'id', 'type', 'product_status', 'featured_position', 'featured_position_sort'], 'safe'],
[['show_price', 'price', 'featured_price'], 'integer'],
];
}
Expand Down Expand Up @@ -86,15 +86,12 @@ public function save()
}
}

$data['name'] = $this->name;
$product->load($data, '');
// $product->load($this->toArray(), '');
var_dump($product->toArray());
die;
$product->load($this->toArray(), '');
if (!$product->save()) {
$transaction->rollBack();
throw new ModelException(ModelHelper::errorStr($product));
}

try {
$transaction->commit();
} catch (Exception $e) {
Expand Down
4 changes: 4 additions & 0 deletions appwap/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM yiisoftware/yii2-php:7.2-apache

# Change document root for Apache
RUN sed -i -e 's|/app/web|/app/appwap/web|g' /etc/apache2/sites-available/000-default.conf
23 changes: 23 additions & 0 deletions appwap/assets/AppAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace appwap\assets;

use yii\web\AssetBundle;

/**
* Main appwap application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
15 changes: 15 additions & 0 deletions appwap/codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace: appwap\tests
actor_suffix: Tester
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
bootstrap: _bootstrap.php
settings:
colors: true
memory_limit: 1024M
modules:
config:
Yii2:
configFile: 'config/codeception-local.php'
4 changes: 4 additions & 0 deletions appwap/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
codeception-local.php
main-local.php
params-local.php
test-local.php
1 change: 1 addition & 0 deletions appwap/config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
49 changes: 49 additions & 0 deletions appwap/config/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);

return [
'id' => 'app-appwap',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'appwap\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'csrfParam' => '_csrf-appwap',
],
'user' => [
'identityClass' => 'common\models\User',
'enableSession' => false,
'enableAutoLogin' => false,
// 'identityCookie' => ['name' => '_identity-appwap', 'httpOnly' => true],
],
// 'session' => [
// // this is the name of the session cookie used for login on the appwap
// 'name' => 'advanced-appwap',
// ],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
'params' => $params,
];
4 changes: 4 additions & 0 deletions appwap/config/params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
'adminEmail' => '[email protected]',
];
15 changes: 15 additions & 0 deletions appwap/config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
return [
'id' => 'app-appwap-tests',
'components' => [
'assetManager' => [
'basePath' => __DIR__ . '/../web/assets',
],
'urlManager' => [
'showScriptName' => true,
],
'request' => [
'cookieValidationKey' => 'test',
],
],
];
57 changes: 57 additions & 0 deletions appwap/controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace appwap\controllers;

use appwap\helpers\AccessControl;
use common\consts\YesNo;
use common\models\Fuser;
use Yii;
use yii\web\Controller;

/**
* Base controller
*/
class BaseController extends Controller
{
public function behaviors()
{
return [
// 允许vue前端的域名或ip跨域请求, 在http头加入如:Access-Control-Allow-Origin http://localhost:8081
'corsFilter' => [
'class' => \yii\filters\Cors::class,
'cors' => [
'Origin' => Yii::$app->params['cors_origin'],
'Access-Control-Request-Method' => ['GET', 'POST'],
'Access-Control-Request-Headers' => ['*'],
],
],
// 访问控制
// 'access' => [
// 'class' => AccessControl::class,
// 'allowActions' => ['site/login', 'site/error',] //不需要控制的action
// ]
];
}

/**
* 获取当前商城用户id, Fuser.id
* @return string
*/
protected function getFuserId()
{
return Yii::$app->user->id;
}

/**
* 获取当前商城用户id, Fuser
* @return string
*/
protected function getFuser()
{
$fuser = Fuser::findOne($this->getFuserId());
if ($fuser && $fuser->status == YesNo::YES) {
return $fuser;
} else {
return null;
}
}
}
83 changes: 83 additions & 0 deletions appwap/controllers/FuserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace appwap\controllers;

use appwap\models\searchs\FuserSearch;
use common\helpers\DateHelper;
use common\helpers\ResponseHelper;
use common\models\Fuser;
use Yii;

class FuserController extends BaseController
{
public function actionIndex()
{
$searchModel = new FuserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$fusers = $dataProvider->getModels();
$items = [];
/* @var Fuser $fusers */
foreach ($fusers as $fuser) {
$item = $fuser->toArray(['username', 'nickname', 'mobile', 'email', 'status']);
$item['created_at'] = DateHelper::format($fuser->created_at);
$item['updated_at'] = DateHelper::format($fuser->updated_at);
$items[] = $item;
}
$data = [
'items' => $items,
'total' => strval($dataProvider->getTotalCount())
];
return ResponseHelper::success($data);
}

public function actionView($id)
{
$model = Fuser::findOne($id);
if (!$model) {
return ResponseHelper::fail([], '用户不存在');
}

$item = $model->toArray(['username', 'nickname', 'mobile', 'email', 'status']);
$item['created_at'] = DateHelper::format($model->created_at);
$item['updated_at'] = DateHelper::format($model->updated_at);
ResponseHelper::success(['fuser' => $item]);
}

/**
* 获取用户信息,包括:用户数据,用户的菜单
*/
public function actionGetInfo()
{
$mch = $this->getFuser();

$data = [
'mch' => [
'mch_no' => $mch->mch_no,
'nickname' => $mch->nickname,
'username' => $mch->username
],
'menus' => [
[
'name' => '首页',
'route' => '/site/index',
'icon' => 'fa fa-dashboard',
],
[
'name' => '订单列表',
'route' => '/order/index',
'icon' => 'fa fa-list',
],
[
'name' => '流水列表',
'route' => '/balance-flow/index',
'icon' => 'fa fa-list-ul',
],
[
'name' => '修改密码',
'route' => '/mch/update-password',
'icon' => 'fa fa-lock',
],
],
];
return ResponseHelper::success($data);
}
}
81 changes: 81 additions & 0 deletions appwap/controllers/OrderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace appwap\controllers;

use common\consts\NotifyStatus;
use common\consts\OrderStatus;
use common\consts\ProductType;
use common\helpers\PriceHelper;
use common\helpers\DateHelper;
use common\helpers\NotifyHelper;
use common\helpers\ResponseHelper;
use common\models\Fuser;
use common\models\Payer;
use Yii;
use common\models\Order;
use appwap\models\searchs\OrderSearch;
use yii\base\Exception;
use yii\data\Pagination;

/**
* OrderController implements the CRUD actions for Order model.
*/
class OrderController extends BaseController
{
public function actionIndex()
{
$request = Yii::$app->request;
$page = $request->get('page', 1) - 1;
$page_size = $request->get('page_size', 10);
$pagination = new Pagination();
$pagination->pageSize = $page_size;
$pagination->page = $page;

$searchModel = new OrderSearch();
$dataProvider = $searchModel->search($request->queryParams);
$dataProvider->setPagination($pagination);
$orders = $dataProvider->getModels();
$totalCount = $dataProvider->getTotalCount();

$items = [];
/* @var Order $order */
foreach ($orders as $order) {
$item = $order->toArray();
$item['total_price'] = PriceHelper::format2DecimalYuan($order->total_price);
$item['origin_total_price'] = PriceHelper::format2DecimalYuan($order->origin_total_price);
$item['order_status_label'] = OrderStatus::label($order->order_status);
$item['created_at'] = DateHelper::format($order->created_at);
$items[] = $item;
}

$data = [
'items' => $items,
'total' => $totalCount
];
return ResponseHelper::success($data);
}

// 订单首页统计
public function actionStat()
{
$request = Yii::$app->request;
$searchModel = new OrderSearch();

// 总数
$selectColumns = 'COUNT(1) total_count';
$dataProvider = $searchModel->search($request->queryParams);
$total = $dataProvider->query->select($selectColumns)->asArray(true)->one();

// 成功的总数
$selectColumns = 'SUM(total_price) total_price, COUNT(1) total_count';
$dataProvider = $searchModel->search($request->queryParams);
$success = $dataProvider->query->select($selectColumns)->andFilterWhere(['order_status' => OrderStatus::SUCCESS])->asArray(true)->one();

$data = [
'total_price_success' => $success['total_price'] ? PriceHelper::format2DecimalYuan($success['total_price']) : '0',
'total_count_success' => $success['total_count'],
'total_count' => $total['total_count'],
];
return ResponseHelper::success($data);
}
}
Loading

0 comments on commit 10a2641

Please sign in to comment.