| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\constants\OptionSetting;
- use app\models\AccountLog;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use app\models\OrderGoodsCancel;
- use app\models\Store;
- use app\models\WechatConfig;
- use app\utils\Wechat\WechatMini;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use yii\helpers\Html;
- use app\utils\WechatContentSafe;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Json;
- class OrderCommentForm extends Model
- {
- public $store_id;
- public $user_id;
- public $order_id;
- public $goods_list;
- public $type;
- public $is_virtual;
- public function rules()
- {
- return [
- [['goods_list', 'order_id'], 'required'],
- [['type'], 'string'],
- [['is_virtual'], 'integer']
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
- }
- $orderClass = 'app\models\Order';
- $commentClass = 'app\models\OrderComment';
- $detailClass = 'app\models\OrderDetail';
- $order = $orderClass::findOne([
- 'id' => $this->order_id,
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- ]);
- $notAllow = !in_array($this->user_id, [$order->user_id, $order->giving_gifts_received_user_id]);
- if (!$order || $notAllow) {
- return [
- 'code' => 1,
- 'msg' => '订单不存在或已删除',
- ];
- }
- $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $this->store_id, 'display')['value'];
- if (!intval($store_is_comment)) {
- return [
- 'code' => 1,
- 'msg' => '商城未开启评价功能',
- ];
- }
- $goods_list = Json::decode($this->goods_list);
- if (!$goods_list) {
- return [
- 'code' => 1,
- 'msg' => '商品信息不能为空',
- ];
- }
- Yii::error($goods_list);
- $t = \Yii::$app->db->beginTransaction();
- foreach ($goods_list as $goods) {
- $order_detail = $detailClass::findOne([
- 'id' => $goods['order_detail_id'],
- 'order_id' => $order->id,
- 'goods_id' => $goods['goods_id'],
- 'is_delete' => 0,
- ]);
- if (!$order_detail) {
- continue;
- }
- if (empty($goods['content'])) {
- return [
- 'code' => 1,
- 'msg' => '评论失败,请输入评价内容!',
- ];
- }
- if(is_wechat_platform()){
- $app = WechatMini::getWechatConfig($this->store_id);
- if ($app) {
- $res = $app->content_security->checkText($goods['content']);
- if ($res['errcode'] > 0) {
- return [
- 'code' => 1,
- 'msg' => '评论失败,内容违法!'.json_encode($res['errmsg']),
- ];
- }
- }
- }
- $order_comment = new $commentClass();
- $order_comment->store_id = $this->store_id;
- $order_comment->order_detail_id = $order_detail->id;
- $order_comment->user_id = $this->user_id;
- $order_comment->order_id = $this->order_id;
- $order_comment->goods_id = $order_detail->goods_id;
- $order_comment->score = $goods['score'];
- $order_comment->content = Html::encode($goods['content']);
- $order_comment->created_at = time();
- if ($this->is_virtual) {
- $order_comment->is_virtual = 1;
- $order_comment->virtual_user = '匿名用户';
- $order_comment->virtual_avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if ($order->mch_id > 0) {
- $order_comment->mch_id = $order->mch_id;
- }
- $order_comment->content = preg_replace('/[\xf0-\xf7].{3}/', '', $order_comment->content);
- $pic_list = [];
- foreach ($goods['pic_list'] as $pic) {
- if (isset($pic['url'])) {
- $pic_list[] = Html::encode($pic['url']);
- } else {
- $pic_list[] = Html::encode($pic);
- }
- }
- $order_comment->pic_list = Json::encode($pic_list);
- if (!$order_comment->save()) {
- $t->rollBack();
- return ['code' => 1, 'msg' => $order_comment->errors[0]];
- }
- }
- $order->is_comment = 1;
- if ($order->save()) {
- $store_integral_order_comment_num = Option::get(
- OptionSetting::STORE_INTEGRAL_ORDER_COMMENT_NUM,
- $this->store_id,
- 'gift',
- 0
- )['value'];
- AccountLog::saveLog(
- $this->user_id,
- $store_integral_order_comment_num,
- AccountLog::TYPE_INTEGRAL,
- AccountLog::LOG_TYPE_INCOME,
- 0,
- $order->id,
- "订单评价送积分");
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '提交成功',
- 'type' => $this->type,
- 'data' => [
- 'integral_num' => $store_integral_order_comment_num
- ]
- ];
- } else {
- $t->rollBack();
- return ['code' => 1, 'msg' => $order->errors[0]];
- }
- }
- public function orderCommentOnce() {
- $user_id = $this->user_id;
- $store_id = $this->store_id;
- $data = self::commonComment($user_id)->asArray()->one();
- $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $store_id, 'display')['value'];
- $data['is_comment'] = intval($store_is_comment);
- $data['attr'] = json_decode($data['attr'], true);
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'data' => $data
- ]
- ];
- }
- //
- public static function commonComment($user_id) {
- $query = OrderDetail::find()
- ->alias('od')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
- ->leftJoin(['oc' => OrderComment::tableName()], 'od.id = oc.order_detail_id')
- ->leftJoin(['ogc' => OrderGoodsCancel::tableName()], 'od.id = ogc.order_detail_id')
- ->where([
- 'o.user_id' => $user_id,
- 'o.trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'is_recycle' => 0
- ])
- ->andWhere(['IS', 'oc.id', null])
- ->select([
- 'od.id AS order_detail_id',
- 'od.goods_id',
- 'od.order_id',
- 'od.pic AS goods_pic',
- 'od.goods_name AS name',
- 'od.attr',
- '(od.num - IFNULL(SUM(ogc.num), 0)) AS num',
- '(od.total_price - IFNULL(SUM(ogc.refund_price), 0)) AS total_price'
- ])
- ->groupBy([
- 'od.id',
- 'od.order_id',
- 'od.num',
- ])
- ->having('SUM(ogc.num) < od.num OR SUM(ogc.num) IS NULL')
- ->orderBy('od.id DESC');
- return $query;
- }
- public function orderCommentWaitList() {
- $user_id = $this->user_id;
- $store_id = $this->store_id;
- $query = self::commonComment($user_id);
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $store_id, 'display')['value'];
- $item['is_comment'] = intval($store_is_comment);
- $item['attr'] = json_decode($item['attr'], true);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => $pagination
- ];
- }
- public function orderCommentList() {
- $user_id = $this->user_id;
- $query = OrderComment::find()->alias('oc')
- ->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id = od.id')
- ->where(['oc.user_id' => $user_id, 'oc.is_delete' => 0])
- ->select('oc.score, oc.content, oc.pic_list, oc.created_at, od.goods_id,
- od.goods_name, od.pic, od.attr')->orderBy('oc.id desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['attr'] = json_decode($item['attr'], true);
- $item['pic_list'] = json_decode($item['pic_list'], true);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => $pagination
- ];
- }
- }
|