diff --git a/app/data/controller/api/News.php b/app/data/controller/api/News.php index 730372184..9098d9b51 100644 --- a/app/data/controller/api/News.php +++ b/app/data/controller/api/News.php @@ -61,7 +61,7 @@ class News extends Controller $map = $this->_vali(['code.require' => '文章不能为空!']); $query = $this->_query('DataNewsXComment')->where($map); $result = $query->order('id desc')->page(false, false, false, 5); - NewsService::instance()->buildListByMinAndCode($result['list']); + NewsService::instance()->buildListByUidAndCode($result['list']); $this->success('获取文章评论成功!', $result); } diff --git a/app/data/controller/api/Notify.php b/app/data/controller/api/Notify.php index 4db9f8c58..623af1f7c 100644 --- a/app/data/controller/api/Notify.php +++ b/app/data/controller/api/Notify.php @@ -38,6 +38,10 @@ class Notify extends Controller * @param string $scene 支付场景 * @param string $param 支付通道 * @return string + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function alipay(string $scene = 'order', string $param = ''): string { diff --git a/app/data/controller/api/auth/News.php b/app/data/controller/api/auth/News.php index 4cf7ffae2..43f70f100 100644 --- a/app/data/controller/api/auth/News.php +++ b/app/data/controller/api/auth/News.php @@ -41,9 +41,7 @@ class News extends Auth { $map = $this->_vali(['uid.value' => $this->uuid, 'code.require' => '文章不能为空!']); $result = $this->_query('DataNewsXComment')->where($map)->order('id desc')->page(true, false); - if (count($result['list']) > 0) { - NewsService::instance()->buildListByMinAndCode($result); - } + NewsService::instance()->buildListByUidAndCode($result); $this->success('获取评论列表成功', $result); } @@ -107,9 +105,7 @@ class News extends Auth $map = ['uid' => $this->uuid, 'type' => 1]; $query = $this->_query('DataNewsXCollect')->where($map); $result = $query->order('id desc')->page(true, false, false, 15); - if (count($result['list']) > 0) { - NewsService::instance()->buildListByMinAndCode($result['list']); - } + NewsService::instance()->buildListByUidAndCode($result['list']); $this->success('获取收藏记录成功!', $result); } @@ -154,7 +150,7 @@ class News extends Auth { $query = $this->_query('DataNewsXCollect')->order('id desc'); $result = $query->where(['uid' => $this->uuid, 'type' => 2])->page(true, false, false, 15); - NewsService::instance()->buildListByMinAndCode($result['list']); + NewsService::instance()->buildListByUidAndCode($result['list']); $this->success('获取点赞记录成功!', $result); } @@ -168,7 +164,7 @@ class News extends Auth { $query = $this->_query('DataNewsXCollect')->order('id desc'); $result = $query->where(['uid' => $this->uuid, 'type' => 3])->page(true, false, false, 15); - NewsService::instance()->buildListByMinAndCode($result['list']); + NewsService::instance()->buildListByUidAndCode($result['list']); $this->success('获取浏览历史成功!', $result); } diff --git a/app/data/service/NewsService.php b/app/data/service/NewsService.php index 688e98c1f..9795ff6db 100644 --- a/app/data/service/NewsService.php +++ b/app/data/service/NewsService.php @@ -34,23 +34,19 @@ class NewsService extends Service * @param array $list 数据列表 * @return array */ - public function buildListByMinAndCode(array &$list = []): array + public function buildListByUidAndCode(array &$list = []): array { if (count($list) > 0) { - /*! 读取文章内容 */ + /*! 绑定文章内容 */ $codes = array_unique(array_column($list, 'code')); $colls = 'id,code,name,cover,mark,status,deleted,create_at,num_like,num_read,num_comment,num_collect'; $items = $this->app->db->name('DataNewsItem')->whereIn('code', $codes)->column($colls, 'code'); $marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name'); foreach ($items as &$vo) $vo['mark'] = str2arr($vo['mark'] ?: '', ',', $marks); + foreach ($list as &$vo) $vo['record'] = $items[$vo['code']] ?? []; /*! 绑定用户数据 */ - $mids = array_unique(array_column($list, 'uid')); $colls = 'id,phone,nickname,username,headimg,status'; - $users = $this->app->db->name('DataUser')->whereIn('id', $mids)->column($colls, 'id'); - foreach ($list as &$vo) { - $vo['member'] = $users[$vo['uid']] ?? []; - $vo['record'] = $items[$vo['code']] ?? []; - } + UserService::instance()->buildByUid($list, 'uid', 'member', $colls); } return $list; } diff --git a/app/data/service/OrderService.php b/app/data/service/OrderService.php index a22bc175c..617e2ae87 100644 --- a/app/data/service/OrderService.php +++ b/app/data/service/OrderService.php @@ -57,13 +57,6 @@ class OrderService extends Service */ public function buildItemData(array &$data = []): array { - // 关联用户数据 - $mids = array_unique(array_merge(array_column($data, 'uid'), array_column($data, 'from'))); - $members = $this->app->db->name('DataUser')->whereIn('id', $mids)->column('*', 'id'); - foreach ($members as &$user) { - unset($user['token'], $user['tokenv'], $user['openid1'], $user['openid2']); - unset($user['unionid'], $user['password'], $user['status'], $user['deleted']); - } // 关联发货信息 $nobs = array_unique(array_column($data, 'order_no')); $trucks = $this->app->db->name('ShopOrderSend')->whereIn('order_no', $nobs)->column('*', 'order_no'); @@ -71,17 +64,17 @@ class OrderService extends Service // 关联订单商品 $query = $this->app->db->name('ShopOrderItem')->where(['status' => 1, 'deleted' => 0]); $items = $query->withoutField('id,uid,status,deleted,create_at')->whereIn('order_no', $nobs)->select()->toArray(); + // 关联用户数据 + $fields = 'username,phone,nickname,headimg,status'; + UserService::instance()->buildByUid($data, 'uid', 'member', $fields); + UserService::instance()->buildByUid($data, 'from', 'fromer', $fields); foreach ($data as &$vo) { $vo['sales'] = 0; - $vo['member'] = $members[$vo['uid']] ?? []; - $vo['fromer'] = $members[$vo['from']] ?? []; $vo['truck'] = $trucks[$vo['order_no']] ?? []; $vo['items'] = []; - foreach ($items as $item) { - if ($vo['order_no'] === $item['order_no']) { - $vo['items'][] = $item; - $vo['sales'] += $item['stock_sales']; - } + foreach ($items as $item) if ($vo['order_no'] === $item['order_no']) { + $vo['sales'] += $item['stock_sales']; + $vo['items'][] = $item; } } return $data; diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index c679a69d5..5bfa1e0d9 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -2,6 +2,7 @@ namespace app\data\service; +use app\data\service\payment\AliPaymentService; use app\data\service\payment\JoinPaymentService; use app\data\service\payment\WechatPaymentService; use think\admin\Service; @@ -98,15 +99,14 @@ abstract class PaymentService extends Service /** * 支付服务对象 - * @var JoinPaymentService|WechatPaymentService + * @var JoinPaymentService|WechatPaymentService|AliPaymentService */ protected static $driver = []; - /** * 根据配置实例支付服务 * @param string $payid 支付通道编号 - * @return JoinPaymentService|WechatPaymentService + * @return JoinPaymentService|WechatPaymentService|AliPaymentService * @throws \think\Exception */ public static function build(string $payid): PaymentService diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 7dc490bcd..8139a5173 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -177,6 +177,23 @@ class UserService extends Service } } + /** + * 列表绑定用户数据 + * @param array $list 原数据列表 + * @param string $keys 用户UID字段 + * @param string $bind 绑定字段名称 + * @param string $column 返回用户字段 + * @return array + */ + public function buildByUid(array &$list, string $keys = 'uid', string $bind = 'user', string $column = '*'): array + { + if (count($list) < 1) return $list; + $uids = array_unique(array_column($list, $keys)); + $users = $this->app->db->name('DataUser')->whereIn('id', $uids)->column($column, 'id'); + foreach ($list as &$vo) $vo[$bind] = $users[$keys] ?? []; + return $list; + } + /** * 获取令牌的认证值 * @return string