GoodsCommentAssistantLogic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\goods;
  20. use app\common\enum\YesNoEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\Goods;
  23. use app\common\model\GoodsComment;
  24. use app\common\model\GoodsCommentImage;
  25. use app\common\model\GoodsItem;
  26. use app\common\service\FileService;
  27. use think\facade\Db;
  28. class GoodsCommentAssistantLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 添加虚拟评价
  32. * @param $params
  33. * @author Tab
  34. * @datetime 2022/1/18 9:53
  35. */
  36. public static function add($params)
  37. {
  38. Db::startTrans();
  39. try {
  40. $commentTime = strtotime($params['comment_time']);
  41. if ($commentTime === false) {
  42. throw new \Exception('评论时间格式错误');
  43. }
  44. if ($commentTime > time()) {
  45. throw new \Exception('评论时间不能超过当前时间');
  46. }
  47. $goods = Goods::findOrEmpty($params['goods_id']);
  48. if ($goods->isEmpty()) {
  49. throw new \Exception('商品不存在');
  50. }
  51. $virtual = [
  52. 'sn' => create_user_sn(),
  53. 'avatar' => FileService::setFileUrl($params['avatar']),
  54. 'nickname' => $params['nickname'],
  55. 'level_id' => $params['level_id'],
  56. 'goods_name' => $goods['name'],
  57. 'goods_image' => $goods['image']
  58. ];
  59. $item = GoodsItem::where('goods_id', $params['goods_id'])->findOrEmpty();
  60. $commentData = [
  61. 'goods_id' => $params['goods_id'],
  62. 'item_id' => $item['id'],
  63. 'spec_value_str' => $item['spec_value_str'],
  64. 'user_id' => 0,
  65. 'order_goods_id' => 0,
  66. 'goods_comment' => $params['goods_comment'],
  67. 'service_comment' => $params['goods_comment'],
  68. 'description_comment' => $params['goods_comment'],
  69. 'express_comment' => $params['goods_comment'],
  70. 'comment' => $params['comment'],
  71. 'status' => YesNoEnum::YES,
  72. 'virtual' => json_encode($virtual),
  73. 'create_time' => $commentTime
  74. ];
  75. $newComment = GoodsComment::create($commentData);
  76. $commentImagesData = [];
  77. if (isset($params['comment_images']) && is_array($params['comment_images'])) {
  78. foreach($params['comment_images'] as $item) {
  79. $commentImagesData[] = [
  80. 'comment_id' => $newComment['id'],
  81. 'uri' => FileService::setFileUrl($item),
  82. ];
  83. }
  84. }
  85. if (count($commentImagesData)) {
  86. $goodsCommentImage = new GoodsCommentImage();
  87. $goodsCommentImage->saveAll($commentImagesData);
  88. }
  89. Db::commit();
  90. return true;
  91. } catch(\Exception $e) {
  92. Db::rollback();
  93. self::$error = $e->getMessage();
  94. return false;
  95. }
  96. }
  97. }