ImportFakerComment.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. use think\facade\Db;
  3. use think\migration\Seeder;
  4. class ImportFakerComment extends Seeder
  5. {
  6. /**
  7. * Run Method.
  8. *
  9. * Write your database seeder using this method.
  10. *
  11. * More information on writing seeders is available here:
  12. * http://docs.phinx.org/en/latest/seeding.html
  13. */
  14. public function run()
  15. {
  16. // 清空假评论
  17. // \app\common\model\Comment::where('create_time', 0)->delete();
  18. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  19. $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx');
  20. // $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx');
  21. // $excel = PHPExcel_IOFactory::load(public_path().'/uploads/import_comment.xlsx');
  22. $nameArr = $excel->getSheetNames();
  23. $sheetCount = $excel->getSheetCount();
  24. $goodsIds = [
  25. // 线上环境
  26. 10036, // 2.5kg
  27. 10035, // 5kg
  28. 10037, // 礼盒装
  29. 10041, // 2.5kg 10袋
  30. 10042, // 5kg 5袋
  31. 10040, // 5kg礼盒 4盒
  32. // 预生产测试
  33. // 10044,
  34. // 10043,
  35. // 10042,
  36. // 10038,
  37. // 10039,
  38. // 10040,
  39. ];
  40. for ($i = 0; $i < $sheetCount; $i++) {
  41. $sheet = $excel->getSheet($i);
  42. $totalRows = $sheet->getHighestRow();
  43. $totalColumn = $sheet->getHighestColumn();
  44. \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($totalColumn);
  45. for ($j = 2; $j <= $totalRows; $j++) {
  46. $nickName = trim($this->removeEmojiChar($sheet->getCell('B' . $j)->getValue()));
  47. $content = trim($this->removeEmojiChar($sheet->getCell('C' . $j)->getValue()));
  48. if (!empty($nickName) && !empty($content)) {
  49. // 写入评价
  50. \app\common\model\Comment::create([
  51. 'score' => 5,
  52. 'content' => $content,
  53. 'user_id' => 0,
  54. 'nick_name' => $nickName,
  55. 'goods_id' => $goodsIds[$i],
  56. 'store_id' => 10001,
  57. 'is_public' => 1,
  58. 'status' => 1,
  59. 'create_time' => 0,
  60. 'update_time' => 0,
  61. ]);
  62. }
  63. }
  64. }
  65. }
  66. public function removeEmojiChar($str)
  67. {
  68. $mbLen = mb_strlen($str);
  69. $strArr = [];
  70. for ($i = 0; $i < $mbLen; $i++) {
  71. $mbSubstr = mb_substr($str, $i, 1, 'utf-8');
  72. if (strlen($mbSubstr) >= 4) {
  73. continue;
  74. }
  75. $strArr[] = $mbSubstr;
  76. }
  77. return implode('', $strArr);
  78. }
  79. }