OrderCommentForm.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\constants\OptionSetting;
  9. use app\models\AccountLog;
  10. use app\models\Option;
  11. use app\models\Order;
  12. use app\models\OrderComment;
  13. use app\models\OrderDetail;
  14. use app\models\OrderGoodsCancel;
  15. use app\models\Store;
  16. use app\models\WechatConfig;
  17. use app\utils\Wechat\WechatMini;
  18. use EasyWeChat\Factory;
  19. use EasyWeChat\Kernel\BaseClient;
  20. use yii\helpers\Html;
  21. use app\utils\WechatContentSafe;
  22. use Yii;
  23. use yii\base\Model;
  24. use yii\helpers\Json;
  25. class OrderCommentForm extends Model
  26. {
  27. public $store_id;
  28. public $user_id;
  29. public $order_id;
  30. public $goods_list;
  31. public $type;
  32. public $is_virtual;
  33. public function rules()
  34. {
  35. return [
  36. [['goods_list', 'order_id'], 'required'],
  37. [['type'], 'string'],
  38. [['is_virtual'], 'integer']
  39. ];
  40. }
  41. public function save()
  42. {
  43. if (!$this->validate()) {
  44. return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
  45. }
  46. $orderClass = 'app\models\Order';
  47. $commentClass = 'app\models\OrderComment';
  48. $detailClass = 'app\models\OrderDetail';
  49. $order = $orderClass::findOne([
  50. 'id' => $this->order_id,
  51. 'store_id' => $this->store_id,
  52. 'is_delete' => 0,
  53. ]);
  54. $notAllow = !in_array($this->user_id, [$order->user_id, $order->giving_gifts_received_user_id]);
  55. if (!$order || $notAllow) {
  56. return [
  57. 'code' => 1,
  58. 'msg' => '订单不存在或已删除',
  59. ];
  60. }
  61. $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $this->store_id, 'display')['value'];
  62. if (!intval($store_is_comment)) {
  63. return [
  64. 'code' => 1,
  65. 'msg' => '商城未开启评价功能',
  66. ];
  67. }
  68. $goods_list = Json::decode($this->goods_list);
  69. if (!$goods_list) {
  70. return [
  71. 'code' => 1,
  72. 'msg' => '商品信息不能为空',
  73. ];
  74. }
  75. Yii::error($goods_list);
  76. $t = \Yii::$app->db->beginTransaction();
  77. foreach ($goods_list as $goods) {
  78. $order_detail = $detailClass::findOne([
  79. 'id' => $goods['order_detail_id'],
  80. 'order_id' => $order->id,
  81. 'goods_id' => $goods['goods_id'],
  82. 'is_delete' => 0,
  83. ]);
  84. if (!$order_detail) {
  85. continue;
  86. }
  87. if (empty($goods['content'])) {
  88. return [
  89. 'code' => 1,
  90. 'msg' => '评论失败,请输入评价内容!',
  91. ];
  92. }
  93. if(is_wechat_platform()){
  94. $app = WechatMini::getWechatConfig($this->store_id);
  95. if ($app) {
  96. $res = $app->content_security->checkText($goods['content']);
  97. if ($res['errcode'] > 0) {
  98. return [
  99. 'code' => 1,
  100. 'msg' => '评论失败,内容违法!'.json_encode($res['errmsg']),
  101. ];
  102. }
  103. }
  104. }
  105. $order_comment = new $commentClass();
  106. $order_comment->store_id = $this->store_id;
  107. $order_comment->order_detail_id = $order_detail->id;
  108. $order_comment->user_id = $this->user_id;
  109. $order_comment->order_id = $this->order_id;
  110. $order_comment->goods_id = $order_detail->goods_id;
  111. $order_comment->score = $goods['score'];
  112. $order_comment->content = Html::encode($goods['content']);
  113. $order_comment->created_at = time();
  114. if ($this->is_virtual) {
  115. $order_comment->is_virtual = 1;
  116. $order_comment->virtual_user = '匿名用户';
  117. $order_comment->virtual_avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  118. }
  119. if ($order->mch_id > 0) {
  120. $order_comment->mch_id = $order->mch_id;
  121. }
  122. $order_comment->content = preg_replace('/[\xf0-\xf7].{3}/', '', $order_comment->content);
  123. $pic_list = [];
  124. foreach ($goods['pic_list'] as $pic) {
  125. if (isset($pic['url'])) {
  126. $pic_list[] = Html::encode($pic['url']);
  127. } else {
  128. $pic_list[] = Html::encode($pic);
  129. }
  130. }
  131. $order_comment->pic_list = Json::encode($pic_list);
  132. if (!$order_comment->save()) {
  133. $t->rollBack();
  134. return ['code' => 1, 'msg' => $order_comment->errors[0]];
  135. }
  136. }
  137. $order->is_comment = 1;
  138. if ($order->save()) {
  139. $store_integral_order_comment_num = Option::get(
  140. OptionSetting::STORE_INTEGRAL_ORDER_COMMENT_NUM,
  141. $this->store_id,
  142. 'gift',
  143. 0
  144. )['value'];
  145. AccountLog::saveLog(
  146. $this->user_id,
  147. $store_integral_order_comment_num,
  148. AccountLog::TYPE_INTEGRAL,
  149. AccountLog::LOG_TYPE_INCOME,
  150. 0,
  151. $order->id,
  152. "订单评价送积分");
  153. $t->commit();
  154. return [
  155. 'code' => 0,
  156. 'msg' => '提交成功',
  157. 'type' => $this->type,
  158. 'data' => [
  159. 'integral_num' => $store_integral_order_comment_num
  160. ]
  161. ];
  162. } else {
  163. $t->rollBack();
  164. return ['code' => 1, 'msg' => $order->errors[0]];
  165. }
  166. }
  167. public function orderCommentOnce() {
  168. $user_id = $this->user_id;
  169. $store_id = $this->store_id;
  170. $data = self::commonComment($user_id)->asArray()->one();
  171. $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $store_id, 'display')['value'];
  172. $data['is_comment'] = intval($store_is_comment);
  173. $data['attr'] = json_decode($data['attr'], true);
  174. return [
  175. 'code' => 0,
  176. 'msg' => '',
  177. 'data' => [
  178. 'data' => $data
  179. ]
  180. ];
  181. }
  182. //
  183. public static function commonComment($user_id) {
  184. $query = OrderDetail::find()
  185. ->alias('od')
  186. ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
  187. ->leftJoin(['oc' => OrderComment::tableName()], 'od.id = oc.order_detail_id')
  188. ->leftJoin(['ogc' => OrderGoodsCancel::tableName()], 'od.id = ogc.order_detail_id')
  189. ->where([
  190. 'o.user_id' => $user_id,
  191. 'o.trade_status' => Order::ORDER_FLOW_CONFIRM,
  192. 'is_recycle' => 0
  193. ])
  194. ->andWhere(['IS', 'oc.id', null])
  195. ->select([
  196. 'od.id AS order_detail_id',
  197. 'od.goods_id',
  198. 'od.order_id',
  199. 'od.pic AS goods_pic',
  200. 'od.goods_name AS name',
  201. 'od.attr',
  202. '(od.num - IFNULL(SUM(ogc.num), 0)) AS num',
  203. '(od.total_price - IFNULL(SUM(ogc.refund_price), 0)) AS total_price'
  204. ])
  205. ->groupBy([
  206. 'od.id',
  207. 'od.order_id',
  208. 'od.num',
  209. ])
  210. ->having('SUM(ogc.num) < od.num OR SUM(ogc.num) IS NULL')
  211. ->orderBy('od.id DESC');
  212. return $query;
  213. }
  214. public function orderCommentWaitList() {
  215. $user_id = $this->user_id;
  216. $store_id = $this->store_id;
  217. $query = self::commonComment($user_id);
  218. $pagination = pagination_make($query);
  219. foreach ($pagination['list'] as &$item) {
  220. $store_is_comment = Option::get(OptionSetting::DISPLAY_IS_COMMENT, $store_id, 'display')['value'];
  221. $item['is_comment'] = intval($store_is_comment);
  222. $item['attr'] = json_decode($item['attr'], true);
  223. }
  224. return [
  225. 'code' => 0,
  226. 'msg' => '',
  227. 'data' => $pagination
  228. ];
  229. }
  230. public function orderCommentList() {
  231. $user_id = $this->user_id;
  232. $query = OrderComment::find()->alias('oc')
  233. ->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id = od.id')
  234. ->where(['oc.user_id' => $user_id, 'oc.is_delete' => 0])
  235. ->select('oc.score, oc.content, oc.pic_list, oc.created_at, od.goods_id,
  236. od.goods_name, od.pic, od.attr')->orderBy('oc.id desc');
  237. $pagination = pagination_make($query);
  238. foreach ($pagination['list'] as &$item) {
  239. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  240. $item['attr'] = json_decode($item['attr'], true);
  241. $item['pic_list'] = json_decode($item['pic_list'], true);
  242. }
  243. return [
  244. 'code' => 0,
  245. 'msg' => '',
  246. 'data' => $pagination
  247. ];
  248. }
  249. }