FilesManager.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. /**
  7. * 文件上传记录
  8. *
  9. */
  10. class FilesManager extends Model
  11. {
  12. use HasFactory;
  13. // 与模型关联的表名
  14. protected $table = 'files_manager';
  15. // 是否主动维护时间戳
  16. public $timestamps = false;
  17. // 定义时间戳字段名
  18. // const CREATED_AT = 'insert_time';
  19. // const UPDATED_AT = 'update_time';
  20. /**
  21. * 添加数据
  22. *
  23. */
  24. public function add($data)
  25. {
  26. // 时间
  27. $data['insert_time'] = time();
  28. $data['update_time'] = time();
  29. // 写入数据表
  30. $id = $this->query()->insertGetId($data);
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id, $data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where(['id' => $id])->update($data);
  44. // 返回结果
  45. return $result;
  46. }
  47. /**
  48. * 文件上传到cos
  49. *
  50. * @param \Illuminate\Http\UploadedFile $file 文件对象
  51. *
  52. * @return Array $result 返回结果
  53. * string $file_path 文件路径
  54. * int $file_size 文件大小
  55. * string $file_name 文件名称
  56. * string $file_md5 文件Md5
  57. */
  58. public function fileStore($file, $path = 'company/license/')
  59. {
  60. // 请指定路径
  61. if (!$path) return ['error' => '请指定存储路径'];
  62. // 组装
  63. $path = trim($path, '/') . '/';
  64. // 判断文件是否有问题
  65. if (!is_object($file)) return ['error' => '文件上传失败,请重试'];
  66. // md5
  67. $fileMd5 = md5_file($file->getPathName());
  68. // 通过文件MD5查询是否已经存在
  69. $data = $this->getOneByMd5($fileMd5);
  70. // 如果存在,直接返回
  71. if ($data) return $data;
  72. // 文件名
  73. $filename = html_entity_decode(ltrim($file->getClientOriginalName(), '/'), ENT_QUOTES, 'UTF-8');
  74. // // 文件名仅支持字母数字中文-_组合
  75. // if (!preg_match('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_\-\+\.\(\)(\x20)]+$/u', $filename)) return ['error' => $filename . '文件名仅支持字母数字中文空格-_组合'];
  76. // 文件大小
  77. $ext = pathinfo($file->getClientOriginalName(),PATHINFO_EXTENSION);
  78. // 文件名
  79. $filename = md5($filename).'.'.$ext;
  80. // 文件大小
  81. $size = $file->getSize();
  82. // 保存文件
  83. $objectPath = $file->move(public_path('uploads/' . $path), $filename);
  84. // 如果有错误
  85. if (!$objectPath) return ['error' => $file->getError()];
  86. // 保存路径
  87. $data = ['file_path' => $path . $filename, 'file_size' => $size, 'file_name' => $filename, 'file_md5' => $fileMd5];
  88. // 写入数据库
  89. $data['file_id'] = $this->add($data);
  90. // 移动失败
  91. if (!$data['file_id']) return ['error' => '存储失败'];
  92. // 返回结果
  93. return $data;
  94. }
  95. /**
  96. * 通过md5值获取文件信息
  97. *
  98. * @param String $fileMd5 文件Md5值
  99. * @param String $$field 指定获取字段名称
  100. *
  101. */
  102. public function getOneByMd5($fileMd5, $field = '')
  103. {
  104. // 获取数据
  105. $data = $this->where([['file_md5', '=', $fileMd5]])->first(['id as file_id', 'file_name', 'file_path', 'file_md5', 'file_size']);
  106. // 如果
  107. $data = $data ? $data->toArray() : [];
  108. // 是否获取字段
  109. return empty($field) ? $data : (isset($data[$field]) ? $data[$field] : null);
  110. }
  111. /**
  112. * 从Excel获取产品/客户等编码
  113. *
  114. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  115. * @param Model $Model 数据模型,订单/用户模型
  116. *
  117. */
  118. public function excelToCode($file, Model $Model)
  119. {
  120. // 获取文件后缀名
  121. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  122. // 验证文件格式
  123. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  124. // 读取文件
  125. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  126. // 提取数据
  127. $sheet = $reader->getActiveSheet()->toArray();
  128. // 临时数据
  129. $list = [];
  130. // 循环处理每行
  131. foreach ($sheet as $row) {
  132. // 循环获取每行的每列
  133. foreach ($row as $col) {
  134. // code转id
  135. $col = $Model->codeToId($col);
  136. // 如果数据存在
  137. if ($col) $list[] = $col;
  138. }
  139. }
  140. // 返回结果
  141. return $list;
  142. }
  143. /**
  144. * 从Excel获取订单数据
  145. *
  146. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  147. *
  148. */
  149. public function excelToOrder($file)
  150. {
  151. // 获取文件后缀名
  152. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  153. // 验证文件格式
  154. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  155. // 读取文件
  156. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  157. // 提取数据
  158. $sheetList = $reader->getActiveSheet()->toArray('');
  159. // 表格列标题
  160. $column = array_shift($sheetList);
  161. // 列标题换字段
  162. $column = $this->columnToField($column);
  163. // 字段值
  164. if( !$column ) return ['error' => '请检查内容表头格式'];
  165. // 循环表格数据
  166. foreach ($sheetList as $row=>$value) {
  167. // 获取对应的数据
  168. $order = [];
  169. // 循环每个字段,获取值
  170. foreach ($value as $kk => $vv) {
  171. // 根据字段索引获取对应的值,转存到订单
  172. if( isset($column[$kk]) ) $order[$column[$kk]] = $vv;
  173. }
  174. // 第一条,没有订单id不可以
  175. if( $row == 0 ) {
  176. // 验证必须数据
  177. if( !$order['weizan_orderid'] ) return ['error' => ($row + 1).' 没有识别到订单ID'];
  178. if( !$order['contact_phone'] ) return ['error' => ($row + 1).' 没有识别到手机号'];
  179. if( !$order['status'] ) return ['error' => ($row + 1).' 没有识别到订单状态'];
  180. if( strtotime($order['insert_time']) === false ) return ['error' => ($row + 1).' 没有识别到下单时间'];
  181. }
  182. // 产品名称每行必填
  183. if( !$order['product_name'] ) return ['error' => ($row + 1).' 行未识别到商品名称'];
  184. if( !$order['buy_num'] ) return ['error' => ($row + 1).' 行未识别到商品数量'];
  185. if( !$order['pay_total'] ) return ['error' => ($row + 1).' 行未识别到商品金额'];
  186. // 替换一下产品名称中的数据,避免数据超长
  187. $order['product_name'] = str_ireplace(['()','()','(','('],'',str_ireplace($order['sku_attr_names'],'',$order['product_name']));
  188. $order['insert_time'] = $order['insert_time'] ? intval(strtotime($order['insert_time'])) : '';
  189. // 如果不是第一条
  190. if( $row >= 1 ) {
  191. // 如果没有订单ID 使用上一条的信息(兼容合并单元格)
  192. if( !$order['weizan_orderid'] ) $order['weizan_orderid'] = $sheetList[$row-1]['weizan_orderid'];
  193. if( !$order['insert_time'] ) $order['insert_time'] = $sheetList[$row-1]['insert_time'];
  194. if( !$order['buyer_nick'] ) $order['buyer_nick'] = $sheetList[$row-1]['buyer_nick'];
  195. if( !$order['status'] ) $order['status'] = $sheetList[$row-1]['status'];
  196. if( !$order['contact_phone'] ) $order['contact_phone'] = $sheetList[$row-1]['contact_phone'];
  197. if( !$order['contact_name'] ) $order['contact_name'] = $sheetList[$row-1]['contact_name'];
  198. if( !$order['contact_province'] ) $order['contact_province'] = $sheetList[$row-1]['contact_province'];
  199. if( !$order['contact_city'] ) $order['contact_city'] = $sheetList[$row-1]['contact_city'];
  200. if( !$order['contact_area'] ) $order['contact_area'] = $sheetList[$row-1]['contact_area'];
  201. if( !$order['contact_addr'] ) $order['contact_addr'] = $sheetList[$row-1]['contact_addr'];
  202. if( !$order['product_name'] ) $order['product_name'] = $sheetList[$row-1]['product_name'];
  203. }
  204. // 追加到订单列表
  205. $sheetList[$row] = $order;
  206. }
  207. // 返回结果
  208. return $sheetList;
  209. }
  210. /**
  211. * 从Excel获取订单数据
  212. *
  213. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  214. *
  215. */
  216. public function excelToProduct($file)
  217. {
  218. // 获取文件后缀名
  219. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  220. // 验证文件格式
  221. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  222. // 读取文件
  223. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  224. // 提取数据
  225. $sheetList = $reader->getActiveSheet()->toArray('');
  226. // 表格列标题
  227. $column = array_shift($sheetList);
  228. // 列标题换字段
  229. $column = $this->columnToProduct($column);
  230. // 字段值
  231. if( !$column ) return ['error' => '请检查内容表头格式'];
  232. // 循环表格数据
  233. foreach ($sheetList as $row=>$value) {
  234. // 获取对应的数据
  235. $order = [];
  236. // 循环每个字段,获取值
  237. foreach ($value as $kk => $vv) {
  238. // 根据字段索引获取对应的值,转存到订单
  239. if( isset($column[$kk]) ) $order[$column[$kk]] = $vv;
  240. }
  241. // 如果存在的话
  242. if( $order['grade'] ) {
  243. // 转数组
  244. $order['grade'] = explode(',',$order['grade']);
  245. // 适读
  246. $order['grade'] = empty($order['grade']) ? '' : '【适读'. $order['grade'][0].'-'.$order['grade'][count($order['grade'])-1].'年级】';
  247. }
  248. // 分类ID
  249. $value['category_id'] = 1;
  250. // 时间转换
  251. $order['insert_time'] = $order['insert_time'] ? strtotime($order['insert_time']) : 0;
  252. $order['update_time'] = $order['update_time'] ? strtotime($order['update_time']) : 0;
  253. // 库存默认
  254. $order['stock'] = 99999;
  255. // 适读
  256. $order['name'] = $order['grade'];
  257. // 描述
  258. $order['description'] = str_ireplace('http://img.duibaoduikan.com/','https://edu.dfwy.tech/',$order['description']);
  259. // 删除数据
  260. unset($order['grade']);
  261. // 追加到订单列表
  262. $sheetList[$row] = $order;
  263. }
  264. // 返回结果
  265. return $sheetList;
  266. }
  267. /**
  268. * 获取列对应的数据库字段名
  269. *
  270. */
  271. private function columnToProduct($column)
  272. {
  273. // 字段值
  274. $field = [];
  275. // 循环列标题
  276. foreach ($column as $key => $value) {
  277. if( $value == 'goods_name') $field[$key] = 'name';
  278. if( $value == 'category_id') $field[$key] = 'type_id';
  279. if( $value == 'goods_main_img')$field[$key] = 'thumb';
  280. if( $value == 'goods_specs') $field[$key] = 'spec';
  281. if( $value == 'goods_price') $field[$key] = 'price';
  282. if( $value == 'fit_grade' ) $field[$key] = 'grade';
  283. if( $value == 'magazine_info') $field[$key] = 'description';
  284. if( $value == 'goods_sort') $field[$key] = 'sort';
  285. if( $value == 'status') $field[$key] = 'status';
  286. if( $value == 'create_time') $field[$key] = 'insert_time';
  287. if( $value == 'update_time') $field[$key] = 'update_time';
  288. }
  289. // 返回字段值
  290. return $field;
  291. }
  292. /**
  293. * 获取列对应的数据库字段名
  294. *
  295. */
  296. private function columnToField($column)
  297. {
  298. // 字段值
  299. $field = [];
  300. // 循环列标题
  301. foreach ($column as $key => $value) {
  302. if( $value == '订单ID') $field[$key] = 'weizan_orderid';
  303. if( $value == '下单时间') $field[$key] = 'insert_time';
  304. if( $value == '订单状态') $field[$key] = 'status';
  305. if( $value == '收货人') $field[$key] = 'contact_name';
  306. if( $value == '收货人手机号' ) $field[$key] = 'contact_phone';
  307. if( $value == '省') $field[$key] = 'contact_province';
  308. if( $value == '市') $field[$key] = 'contact_city';
  309. if( $value == '区') $field[$key] = 'contact_area';
  310. if( $value == '收货地址') $field[$key] = 'contact_addr';
  311. if( $value == '商品名称') $field[$key] = 'product_name';
  312. if( $value == '商品规格') $field[$key] = 'sku_attr_names';
  313. if( $value == '商品数量') $field[$key] = 'buy_num';
  314. if( $value == '商品金额') $field[$key] = 'pay_total';
  315. if( $value == '买家昵称') $field[$key] = 'buyer_nick';
  316. }
  317. // 返回字段值
  318. return $field;
  319. }
  320. /**
  321. * 从Excel获取订单数据
  322. *
  323. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  324. *
  325. */
  326. public function excelToOrderStatus($file)
  327. {
  328. // 获取文件后缀名
  329. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  330. // 验证文件格式
  331. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  332. // 读取文件
  333. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  334. // 最大行数
  335. $reader = $reader->getActiveSheet();
  336. $maxRow = $reader->getHighestRow();
  337. // 验证文件格式
  338. if ( $maxRow > 500 ) return ['error' => '每次批量仅可处理500条'];
  339. // 提取数据
  340. $sheetList = $reader->toArray('');
  341. // 表格列标题
  342. $column = array_shift($sheetList);
  343. // 列标题换字段
  344. $column = $this->columnToFieldOrderStatus($column);
  345. // 字段值
  346. if( !$column ) return ['error' => '请检查内容表头格式'];
  347. // 循环表格数据
  348. foreach ($sheetList as $row=>$value) {
  349. // 获取对应的数据
  350. $order = [];
  351. // 循环每个字段,获取值
  352. foreach ($value as $kk => $vv) {
  353. // 根据字段索引获取对应的值,转存到订单
  354. if( isset($column[$kk]) ) $order[$column[$kk]] = $vv;
  355. }
  356. // 验证必须数据
  357. if( !$order['order_code'] ) return ['error' => ($row + 1).' 没有识别到订单ID'];
  358. if( !$order['status'] ) return ['error' => ($row + 1).' 没有识别到订单状态'];
  359. // 如果没有订单ID 使用上一条的信息(兼容合并单元格)
  360. if( !$order['order_code'] ) $order['order_code'] = $sheetList[$row-1]['order_code'];
  361. if( !$order['status'] ) $order['status'] = $sheetList[$row-1]['status'];
  362. // 追加到订单列表
  363. $sheetList[$row] = $order;
  364. }
  365. // 返回结果
  366. return $sheetList;
  367. }
  368. /**
  369. * 获取列对应的数据库字段名
  370. *
  371. */
  372. private function columnToFieldOrderStatus($column)
  373. {
  374. // 字段值
  375. $field = [];
  376. // 循环列标题
  377. foreach ($column as $key => $value) {
  378. if( $value == '订单编号') $field[$key] = 'order_code';
  379. if( $value == '订单状态') $field[$key] = 'status';
  380. }
  381. // 返回字段值
  382. return $field;
  383. }
  384. /**
  385. * 从Excel获取客户信息
  386. *
  387. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  388. *
  389. */
  390. public function excelToCustom($file)
  391. {
  392. // 获取文件后缀名
  393. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  394. // 验证文件格式
  395. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  396. // 读取文件
  397. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  398. // 提取数据
  399. $sheetList = $reader->getActiveSheet()->toArray('');
  400. // 表格列标题
  401. $column = array_shift($sheetList);
  402. // 列标题换字段
  403. $column = $this->customToField($column);
  404. // 字段值
  405. if( !$column ) return ['error' => '请检查内容表头格式'];
  406. // 循环表格数据
  407. foreach ($sheetList as $row=>$value) {
  408. // 获取对应的数据
  409. $custom = [];
  410. // 循环每个字段,获取值
  411. foreach ($value as $kk => $vv) {
  412. // 根据字段索引获取对应的值,转存到客户信息
  413. if( isset($column[$kk]) ) $custom[$column[$kk]] = $vv;
  414. }
  415. // 验证必须数据
  416. if( empty($custom['external_userid']) ) return ['error' => ($row + 1).' 没有识别到企微ID'];
  417. if( empty($custom['username']) ) return ['error' => ($row + 1).' 没有识别到用户昵称'];
  418. if( empty($custom['phone']) ) return ['error' => ($row + 1).' 没有识别到用户手机号'];
  419. // 没有详细地址,则默认为空
  420. if( empty($custom['contact_name']) ) $custom['contact_name'] = '';
  421. if( empty($custom['contact_phone']) ) $custom['contact_phone'] = '';
  422. if( empty($custom['contact_shop']) ) $custom['contact_shop'] = '';
  423. if( empty($custom['contact_province']) ) $custom['contact_province'] = '';
  424. if( empty($custom['contact_city']) ) $custom['contact_city'] = '';
  425. if( empty($custom['contact_area']) ) $custom['contact_area'] = '';
  426. if( empty($custom['contact_addr']) ) $custom['contact_addr'] = '';
  427. // 追加到订单列表
  428. $sheetList[$row] = $custom;
  429. }
  430. // 返回结果
  431. return $sheetList;
  432. }
  433. /**
  434. * 获取列对应的数据库字段名
  435. *
  436. */
  437. private function customToField($column)
  438. {
  439. // 字段值
  440. $field = [];
  441. // 循环列标题
  442. foreach ($column as $key => $value) {
  443. if( $value == '企微ID') $field[$key] = 'external_userid';
  444. if( $value == '用户昵称') $field[$key] = 'username';
  445. if( $value == '用户手机号' ) $field[$key] = 'phone';
  446. if( $value == '收货人') $field[$key] = 'contact_name';
  447. if( $value == '店铺信息') $field[$key] = 'contact_shop';
  448. if( $value == '收货人手机号' ) $field[$key] = 'contact_phone';
  449. if( $value == '省') $field[$key] = 'contact_province';
  450. if( $value == '市') $field[$key] = 'contact_city';
  451. if( $value == '区') $field[$key] = 'contact_area';
  452. if( $value == '收货地址') $field[$key] = 'contact_addr';
  453. }
  454. // 返回字段值
  455. return $field;
  456. }
  457. /**
  458. * 从Excel获取积分操作
  459. *
  460. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  461. *
  462. */
  463. public function excelToScore($file)
  464. {
  465. // 获取文件后缀名
  466. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  467. // 验证文件格式
  468. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  469. // 读取文件
  470. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  471. // 提取数据
  472. $sheetList = $reader->getActiveSheet()->toArray('');
  473. // 表格列标题
  474. $column = array_shift($sheetList);
  475. // 列标题换字段
  476. $column = $this->scoreToField($column);
  477. // 字段值
  478. if( !$column ) return ['error' => '请检查内容表头格式'];
  479. // 循环表格数据
  480. foreach ($sheetList as $row=>$value) {
  481. // 获取对应的数据
  482. $custom = [];
  483. // 循环每个字段,获取值
  484. foreach ($value as $kk => $vv) {
  485. // 根据字段索引获取对应的值,转存到客户信息
  486. if( isset($column[$kk]) ) $custom[$column[$kk]] = $vv;
  487. }
  488. // 验证必须数据
  489. if( empty($custom['custom_uid']) ) return ['error' => ($row + 1).' 没有识别到客户ID'];
  490. if( empty($custom['score']) ) return ['error' => ($row + 1).' 没有识别到积分数量'];
  491. if( empty($custom['description']) ) $custom['description'] = '';
  492. // 积分转整数
  493. $custom['score'] = intval($custom['score']);
  494. // 追加到订单列表
  495. $sheetList[$row] = $custom;
  496. }
  497. // 返回结果
  498. return $sheetList;
  499. }
  500. /**
  501. * 获取列对应的数据库字段名
  502. *
  503. */
  504. private function scoreToField($column)
  505. {
  506. // 字段值
  507. $field = [];
  508. // 循环列标题
  509. foreach ($column as $key => $value) {
  510. if( $value == '客户ID') $field[$key] = 'custom_uid';
  511. if( $value == '积分数量') $field[$key] = 'score';
  512. if( $value == '备注' ) $field[$key] = 'description';
  513. }
  514. // 返回字段值
  515. return $field;
  516. }
  517. /**
  518. * 从Excel获取积分操作
  519. *
  520. * @param \Illuminate\Http\UploadedFile $file 传入的文件
  521. *
  522. */
  523. public function weibanToScore($file)
  524. {
  525. // 获取文件后缀名
  526. $ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  527. // 验证文件格式
  528. if (!in_array($ext, ['csv', 'xlsx', 'xls'])) return ['error' => '请上传Excel'];
  529. // 读取文件
  530. $reader = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
  531. // 提取数据
  532. $sheetList = $reader->getActiveSheet()->toArray('');
  533. // 表格列标题
  534. $column = array_shift($sheetList);
  535. // 列标题换字段
  536. $column = $this->weibanToField($column);
  537. // 字段值
  538. if( !$column ) return ['error' => '请检查内容表头格式'];
  539. // 循环表格数据
  540. foreach ($sheetList as $row=>$value) {
  541. // 获取对应的数据
  542. $custom = [];
  543. // 循环每个字段,获取值
  544. foreach ($value as $kk => $vv) {
  545. // 根据字段索引获取对应的值,转存到客户信息
  546. if( isset($column[$kk]) ) $custom[$column[$kk]] = $vv;
  547. }
  548. // 验证必须数据
  549. if( empty($custom['external_userid']) ) return ['error' => ($row + 1).' 没有识别到企微ID'];
  550. // if( empty($custom['phone']) ) return ['error' => ($row + 1).' 没有识别到联系方式'];
  551. if( empty($custom['score']) ) return ['error' => ($row + 1).' 没有识别到积分数量'];
  552. if( empty($custom['description']) ) $custom['description'] = '';
  553. // 积分转整数
  554. $custom['score'] = intval($custom['score']);
  555. // 追加到订单列表
  556. $sheetList[$row] = $custom;
  557. }
  558. // 返回结果
  559. return $sheetList;
  560. }
  561. /**
  562. * 获取列对应的数据库字段名
  563. *
  564. */
  565. private function weibanToField($column){
  566. // 字段值
  567. $field = [];
  568. // 循环列标题
  569. foreach ($column as $key => $value) {
  570. if( $value == '企微ID') $field[$key] = 'external_userid';
  571. // if( $value == '联系方式') $field[$key] = 'phone';
  572. if( $value == '积分数量') $field[$key] = 'score';
  573. if( $value == '备注' ) $field[$key] = 'description';
  574. }
  575. // 返回字段值
  576. return $field;
  577. }
  578. }