萤火php端: 查询数据的时候报错: “message“: “Undefined index: pay_status“,
代码:getGoodsFromHistory
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\service;
use app\api\model\Order as OrderModel;
use app\api\model\order\Delivery as DeliveryModel;
use app\api\model\OrderAddress as OrderAddressModel;
use app\api\service\User as UserService;
use app\common\service\Order as OrderService;
use app\common\service\Express as ExpressService;
use app\common\enum\order\OrderStatus as OrderStatusEnum;
use cores\exception\BaseException;
/**
* 订单服务类
* Class Order
* @package app\common\service
*/
class Order extends OrderService
{
/**
* 获取物流信息
* @param int $orderId 订单ID
* @return mixed
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function express(int $orderId)
{
// 获取发货单列表
$model = new DeliveryModel;
$list = $model->getList($orderId);
// 整理物流跟踪信息
return $this->getExpressTraces($orderId, $list);
}
/**
* 整理物流跟踪信息
* @param int $orderId 订单ID
* @param $list
* @return mixed
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getExpressTraces(int $orderId, $list)
{
// 订单收货地址
$address = OrderAddressModel::detail(['order_id' => $orderId]);
// 整理物流跟踪信息
$Express = new ExpressService;
foreach ($list as $item) {
if (!empty($item['express'])) {
$item['traces'] = $Express->traces(
$item['express'],
$item['express_no'],
$address,
$this->getStoreId()
);
}
}
return $list;
}
/**
* 获取某商品的购买件数
* @param int $userId 用户ID
* @param int $goodsId 商品ID
* @param int $orderSource
* @return int
*/
public static function getGoodsBuyNum(int $userId, int $goodsId, int $orderSource): int
{
return (int) (new OrderModel)->setBaseQuery('order', [['order_goods', 'order_id']])
->where('order_goods.user_id', '=', $userId)
->where('order_goods.goods_id', '=', $goodsId)
->where('order.order_source', '=', $orderSource)
->where('order.order_status', '<>', OrderStatusEnum::CANCELLED)
->where('order.is_delete', '=', 0)
->sum('order_goods.total_num');
}
/**
* 根据用户id 来判断是否是新用户
* @return \app\api\model\Goods[]|array|\think\Collection
* @throws \cores\exception\BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getGoodsList()
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
// 查询列表记录
return (new orderModel)
->where('user_id', '=', $userId)
->where('is_delete', '=', 0)
->select();
}
public function getGoodsFromHistory()
{
$userId = UserService::getCurrentLoginUserId();
return (new OrderModel())
->alias('a')
->join('order_goods b', 'a.order_id = b.order_id')
->where('a.user_id', '=', $userId)
->where('a.is_delete', '=', 0)
//->field('b.goods_id,a.order_status')
->field('b.goods_id,a.order_status,a.pay_status,a.delivery_status,a.receipt_status') // 选择商品ID和购买总数
->select()->toArray();
}
}
表结构:
CREATE TABLE `yoshop_order` (
`order_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单ID',
`order_no` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '订单号',
`order_type` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '订单类型(10实物订单)',
`total_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '商品总金额(不含优惠折扣)',
`order_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '订单金额(含优惠折扣)',
`coupon_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '优惠券ID',
`coupon_money` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '优惠券抵扣金额',
`points_money` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '积分抵扣金额',
`points_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '积分抵扣数量',
`pay_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '实际付款金额(包含运费)',
`update_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '后台修改的订单金额(差价)',
`buyer_remark` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '买家留言',
`pay_method` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '支付方式(余额/微信/支付宝)',
`pay_status` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '付款状态(10未付款 20已付款)',
`pay_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '付款时间',
`trade_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '第三方交易记录ID',
`delivery_type` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '配送方式(10快递配送 20门店自提 30无需配送)',
`express_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '运费金额',
`delivery_status` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '发货状态(10未发货 20已发货 30部分发货)',
`delivery_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '发货时间',
`receipt_status` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '收货状态(10未收货 20已收货)',
`receipt_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '收货时间',
`order_status` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '订单状态(10进行中 20取消 21待取消 30已完成)',
`points_bonus` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '赠送的积分数量',
`merchant_remark` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '商家备注',
`is_settled` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '订单是否已结算(0未结算 1已结算)',
`settled_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '订单结算时间',
`is_comment` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '是否已评价(0否 1是)',
`order_source` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '订单来源(10普通订单 20砍价订单 30秒杀订单)',
`order_source_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '来源记录ID',
`order_source_data` varchar(500) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '来源记录的参数 (json格式)',
`platform` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '来源客户端 (APP、H5、小程序等)',
`user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
`is_delete` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否删除',
`store_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商城ID',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`order_id`),
UNIQUE KEY `order_no` (`order_no`),
KEY `store_id` (`store_id`),
KEY `user_id` (`user_id`),
KEY `pay_status` (`pay_status`),
KEY `delivery_status` (`delivery_status`),
KEY `receipt_status` (`receipt_status`),
KEY `order_status` (`order_status`),
KEY `is_settled` (`is_settled`),
KEY `order_source` (`order_source`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单记录表';
CREATE TABLE `yoshop_order_goods` (
`order_goods_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单商品ID',
`goods_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商品ID',
`goods_type` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '商品类型(10实物商品)',
`goods_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '商品名称',
`image_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商品封面图ID',
`deduct_stock_type` tinyint(3) unsigned NOT NULL DEFAULT '20' COMMENT '库存计算方式(10下单减库存 20付款减库存)',
`spec_type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '规格类型(10单规格 20多规格)',
`goods_sku_id` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '商品sku唯一标识',
`goods_props` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'SKU的规格属性(json格式)',
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '商品详情',
`goods_no` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '商品编码',
`goods_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '商品价格(单价)',
`line_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '商品划线价',
`goods_weight` double unsigned NOT NULL DEFAULT '0' COMMENT '商品重量(Kg)',
`is_user_grade` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否存在会员等级折扣',
`grade_ratio` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '会员折扣比例(0-10)',
`grade_goods_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '会员折扣的商品单价',
`grade_total_money` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '会员折扣的总额差',
`coupon_money` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '优惠券折扣金额',
`points_money` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '积分金额',
`points_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '积分抵扣数量',
`points_bonus` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '赠送的积分数量',
`total_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '购买数量',
`total_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '商品总价(数量×单价)',
`total_pay_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '实际付款价(折扣和优惠后)',
`delivery_status` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '发货状态(10未发货 20已发货 30部分发货)',
`delivery_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '已发货数量',
`is_comment` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '是否已评价(0否 1是)',
`order_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '订单ID',
`user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
`goods_source_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '来源记录ID',
`store_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商城ID',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
PRIMARY KEY (`order_goods_id`),
KEY `goods_id` (`goods_id`),
KEY `order_id` (`order_id`),
KEY `user_id` (`user_id`),
KEY `store_id` (`store_id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单商品记录表';
我只想查到用户下单的商品的id 。 然后filed 中只写了 goods_id 报错如上。
然后发现在Order 模型中有一个将 订单的付款状态返回成 0-》 未支付 的函数 ,因为我查询出来的数据没有这个字段所以报错。 加上所需要的字段就正常了
Order 模型代码:getStateTextAttr
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\common\model;
use app\api\model\Order as OrderModel;
use app\api\service\User as UserService;
use cores\BaseModel;
use app\common\service\Order as OrderService;
use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\enum\order\OrderStatus as OrderStatusEnum;
use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
use app\common\library\helper;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;
use think\model\relation\HasMany;
use think\model\relation\BelongsTo;
/**
* 订单模型
* Class Order
* @package app\common\model
*/
class Order extends BaseModel
{
// 定义表名
protected $name = 'order';
// 定义表名(外部引用)
public static string $tableName = 'order';
// 定义主键
protected $pk = 'order_id';
// 定义别名
protected string $alias = 'order';
/**
* 追加字段
* @var array
*/
protected $append = [
'state_text', // 售后单状态文字描述
];
/**
* 订单商品列表
* @return HasMany
*/
public function goods(): HasMany
{
$module = self::getCalledModule();
return $this->hasMany("app\\{$module}\\model\\OrderGoods")->withoutField('content');
}
/**
* 关联订单发货单
* @return hasMany
*/
public function delivery(): HasMany
{
$module = self::getCalledModule();
return $this->hasMany("app\\{$module}\\model\\order\\Delivery");
}
/**
* 关联订单收货地址表
* @return HasOne
*/
public function address(): HasOne
{
$module = self::getCalledModule();
return $this->hasOne("app\\{$module}\\model\\OrderAddress");
}
/**
* 关联用户表
* @return BelongsTo
*/
public function user(): BelongsTo
{
$module = self::getCalledModule();
return $this->belongsTo("app\\{$module}\\model\\User");
}
/**
* 关联物流公司表 (仅用于兼容旧物流数据)
* @return BelongsTo
*/
public function express(): BelongsTo
{
$module = self::getCalledModule();
return $this->belongsTo("app\\{$module}\\model\\Express");
}
/**
* 关联模型:第三方交易记录
* @return BelongsTo
*/
public function trade(): BelongsTo
{
$module = self::getCalledModule();
return $this->belongsTo("app\\{$module}\\model\\PaymentTrade", 'trade_id', 'trade_id');
}
/**
* 获取器:订单状态文字描述
* @param $value
* @param $data
* @return string
*/
public function getStateTextAttr($value, $data): string
{
//todo 这种设置了TextAttr 的查询的时候会加上这个
// 订单状态
if ($data['order_status'] != OrderStatusEnum::NORMAL) {
return OrderStatusEnum::data()[$data['order_status']]['name'];
}
// 付款状态
if ($data['pay_status'] == PayStatusEnum::PENDING) {
return '待支付';
}
// 发货状态
if ($data['delivery_status'] != DeliveryStatusEnum::DELIVERED) {
$enum = [DeliveryStatusEnum::NOT_DELIVERED => '待发货', DeliveryStatusEnum::PART_DELIVERED => '部分发货'];
return $enum[$data['delivery_status']];
}
// 收货状态
if ($data['receipt_status'] == ReceiptStatusEnum::NOT_RECEIVED) {
return '待收货';
}
return $value;
}
/**
* 获取器:订单金额(含优惠折扣)
* @param $value
* @param $data
* @return string
*/
public function getOrderPriceAttr($value, $data): string
{
// 兼容旧数据:订单金额
if ($value == 0) {
return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
}
return $value;
}
/**
* 获取器:改价金额(差价)
* @param $value
* @return array
*/
public function getUpdatePriceAttr($value): array
{
return [
'symbol' => $value < 0 ? '-' : '+',
'value' => sprintf('%.2f', abs((float)$value))
];
}
/**
* 获取器:付款时间
* @param $value
* @return false|string
*/
public function getPayTimeAttr($value)
{
return format_time($value);
}
/**
* 获取器:发货时间
* @param $value
* @return false|string
*/
public function getDeliveryTimeAttr($value)
{
return format_time($value);
}
/**
* 获取器:收货时间
* @param $value
* @return false|string
*/
public function getReceiptTimeAttr($value)
{
return format_time($value);
}
/**
* 获取器:来源记录的参数
* @param $json
* @return array
*/
public function getOrderSourceDataAttr($json): array
{
return $json ? helper::jsonDecode($json) : [];
}
/**
* 修改器:来源记录的参数
* @param array $data
* @return string
*/
public function setOrderSourceDataAttr(array $data): string
{
return helper::jsonEncode($data);
}
/**
* 生成订单号
* @return string
*/
public function orderNo(): string
{
return OrderService::createOrderNo();
}
/**
* 订单详情
* @param $where
* @param array $with
* @return static|array|null
*/
public static function detail($where, array $with = [])
{
is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
return self::get($filter, $with);
}
/**
* 批量获取订单列表
* @param array $orderIds
* @param array $with
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getListByIds(array $orderIds, array $with = []): array
{
$data = $this->getListByInArray('order_id', $orderIds, $with);
return helper::arrayColumn2Key($data, 'order_id');
}
/**
* 批量获取订单列表
* @param $field
* @param $data
* @param array $with
* @return \think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getListByInArray($field, $data, array $with = []): \think\Collection
{
return $this->with($with)
->where($field, 'in', $data)
->where('is_delete', '=', 0)
->select();
}
/**
* 根据订单号批量查询
* @param $orderNos
* @param array $with
* @return \think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getListByOrderNos($orderNos, array $with = []): \think\Collection
{
return $this->getListByInArray('order_no', $orderNos, $with);
}
/**
* 批量更新订单
* @param $orderIds
* @param $data
* @return bool|false
*/
public function onBatchUpdate($orderIds, $data): bool
{
return static::updateBase($data, [['order_id', 'in', $orderIds]]);
}
}