文件
mashangyoujiu/Application/Retail/Controller/VipImportController.class.php

608 行
27 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Retail\Controller;
/**
* Class VipImportController 会员信息导入
* @package Retail\BasetwoController
*/
use Common\Model\RetailStoreModel;
use Common\Model\VipModel;
use Think\Controller;
//[1.8.85]
class VipImportController extends BaseController
{
public function index()
{
$user = session("ls_userinfo");
$stores = M("retail_store")->where(array('merchantid' => $user['id'], 'state' => 0))->select();
$this->assign("info", $stores);
$arr = array(
'vipImport' => '/index.php/Retail/VipImport/vipImport',
'downfile' => '/index.php/Retail/VipImport/downfile',
'delgoodsitem' => '/index.php/Retail/VipImport/delgoodsitem',
'submitVips' => '/index.php/Retail/VipImport/submitVips',
);
$authArr = checkFunction($arr);
$this->assign('authArr', $authArr);
$this->display();
}
public function vipImport()
{
if (IS_POST) {
$platform_type = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
$upload = new \Think\Upload (); // 实例化上传类
$upload->maxSize = 1048576 * 10; // 设置附件上传大小10M
$upload->exts = array(
'xls'
); // 设置附件上传类型
$upload->rootPath = 'Uploads'; // 设置附件上传根目录
$upload->savePath = '/Temp/'; // 设置附件上传(子)目录
$upload->saveName = array(
'uniqid',
''
);
// 上传文件
$info = $upload->upload();
if (empty($info)) {
$rt['status'] = 3;
$rt['msg'] = '表格类型不正确,应为.xls后缀的数据表格,或者数据表格大于10M';
$this->ajaxReturn($rt);
exit;
}
$path['path'] = '/' . 'Uploads' . $info['file']['savepath'] . $info['file']['savename'];
$arr = $this->importExecl("." . $path['path']);
$myrt['arr'] = $arr;
if ($arr['error'] == 0) {
$myrt['status'] = 2;
$myrt['msg'] = '数据表格格式不正确,请下载导入模板,按模板填入数据。';
$this->ajaxReturn($myrt);
exit;
} else {
//第三方平台
if ($platform_type == 2) {
$headArr = array(
'name' => '姓名',
'mobile_phone' => '手机号',
'store_name' => '门店',
'source_channel' => '来源渠道',
'grade_name' => '会员等级',
'integral' => '积分',
);
} else {
$headArr = array(
'mobile_phone' => '手机号码/会员卡号',
'name' => '姓名',
'nickname' => '昵称',
'sex' => '性别',
'password' => '密码',
'balance' => '余额',
'card_key' => '实体卡ID',
'integral' => '积分'
);
}
array_flip($headArr);
header('Content-Type: text/html; charset=utf-8'); //网页编码
$viparr = $arr['data'][0]['Content'];
$viparr = json_decode(json_encode($viparr), true);
$titlearr = $viparr[1];
$keyarr = array();
foreach ($titlearr as $k => $v) {
foreach ($headArr as $k2 => $v2) {
if ($v == $v2) {
$keyarr[$k2] = $k;
}
}
}
$rt['viparr'] = $viparr;
$faultarr = array();
$merchant_id = $this->merchant_id;
$logData = [];//为日志功能新建数组:用于存放合格上传的会员信息,并记录日志
$time = $_SERVER['REQUEST_TIME'];
//第三方平台
if ($platform_type == 2) {
$StoreModel = new RetailStoreModel();
$list = $StoreModel->getSelect([
'state' => 0,
'merchantid' => $merchant_id
], 'id,storetag');
$list = array_column($list, null, 'storetag');
}
foreach ($viparr as $key => $val) {
if ($key == 1) {
continue;
}
if (empty($val[$keyarr['mobile_phone']]) && empty($val[$keyarr['card_key']])) {
$val['reson'] = '手机号与实体卡号不能都为空';
$faultarr[] = $val;
continue;
}
if (mb_strlen($val[$keyarr['mobile_phone']], 'UTF-8') > 11) {
$val['reson'] = "手机号" . $val[$keyarr['mobile_phone']] . "有误";
$faultarr[] = $val;
continue;
}
if (trim($val[$keyarr['mobile_phone']], ' ') && !empty($val[$keyarr['mobile_phone']])) {
$where = [
'mobile_phone' => $val[$keyarr['mobile_phone']],
'merchantid' => $merchant_id,
'status' => ['neq', 2]
];
$vip = M('retail_vipimport')->where($where)->find();
if (!empty($vip)) {
$val['reson'] = "手机号" . $val[$keyarr['mobile_phone']] . "已存在";
$faultarr[] = $val;
continue;
}
}
//第三方平台
if ($platform_type == 2) {
if (empty($val[$keyarr['store_name']])) {
$val['reson'] = $val[$keyarr['mobile_phone']] . "门店名称有误";
$faultarr[] = $val;
continue;
}
if (empty($list[$val[$keyarr['store_name']]]['id'])) {
$val['reson'] = $val[$keyarr['mobile_phone']] . "未找到该门店";
$faultarr[] = $val;
continue;
}
if (empty($val[$keyarr['source_channel']])) {
$val['reson'] = $val[$keyarr['mobile_phone']] . "请完善来源渠道";
$faultarr[] = $val;
continue;
}
}
if (trim($val[$keyarr['card_key']], ' ') != 0 && !empty($val[$keyarr['card_key']])) {
$where = [
'card_key' => $val[$keyarr['card_key']],
'merchantid' => $merchant_id,
'status' => ['neq', 2]
];
$vip2 = M('retail_vipimport')->where($where)->find();
if (!empty($vip2)) {
$val['reson'] = "实体卡ID" . $val[$keyarr['card_key']] . "已存在";// 未找到正常的
$faultarr[] = $val;
continue;
}
}
$newInfo['merchantid'] = $merchant_id;
$newInfo['mobile_phone'] = $val[$keyarr['mobile_phone']];
$newInfo['name'] = $val[$keyarr['name']];
$newInfo['nickname'] = $val[$keyarr['nickname']] ? $val[$keyarr['nickname']] : '';
$newInfo['sex'] = $val[$keyarr['sex']] == '男' ? 1 : 2;
$newInfo['balance'] = $val[$keyarr['balance']] ? $val[$keyarr['balance']] : 0;
$newInfo['card_key'] = $val[$keyarr['card_key']] ? $val[$keyarr['card_key']] : '';
$newInfo['integral'] = $val[$keyarr['integral']];
if (!empty($val[$keyarr['password']])) {
$password = $val[$keyarr['password']];
} else {
$password = substr($newInfo['mobile_phone'], -6);
}
$newInfo['password'] = $password;
$newInfo['status'] = 0;
$newInfo['addtime'] = $time;
//第三方平台
if ($platform_type == 2) {
$newInfo['source_channel'] = $val[$keyarr['source_channel']];
$newInfo['storeid'] = $list[$val[$keyarr['store_name']]]['id'];
}
M('retail_vipimport')->add($newInfo);
$logData[] = $newInfo;
}
}
$rt['headArr'] = $headArr;
$rt['titlearr'] = $titlearr;
$rt['faultarr'] = $faultarr;
if (count($faultarr) > 0) {
// 生成错误表格,并返回路径。
$path2 = './Uploads/Temp/Vipimport' . $merchant_id . '.txt';
file_put_contents($path2, json_encode($faultarr));
$rt['status'] = 1;
$rt['path'] = $path2;
$rt['msg'] = '导入成功,但有' . count($faultarr) . '项未导入成功';
} else {
$rt['status'] = 1;
$rt['msg'] = '导入成功';
}
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($logData);//请求参数 json数据
$desc = '上传会员表格';//操作行为描述
action_log($source, $type, $request_data, $desc);
//end
$this->ajaxReturn($rt);
exit();
}
}
// 下载导入模板。
public function downfile()
{
$platform_type = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
if ($platform_type == 2) {
//第三方平台
$url = './Public/flow/zip/thirdImportExamples.zip';
} else {
$url = './Public/flow/zip/importExamples.zip';
}
if (!file_exists($url)) { //判断文件是否存在
echo "文件不存在";
exit();
}
ob_clean();
$file = fopen($url, "r");
Header("Content-type:application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: " . filesize($url));
header("Content-Disposition: attachment; filename=导入模板.zip");
echo fread($file, filesize($url));
fclose($file);
}
// 上传导入文件
public function importExecl($file)
{
if (!file_exists($file)) {
return array("error" => 0, 'message' => 'file not found!');
}
Vendor("PHPExcel.PHPExcel.IOFactory");
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
try {
$PHPReader = $objReader->load($file);
} catch (\Exception $e) {
}
if (!isset($PHPReader)) return array("error" => 0, 'message' => 'read error!');
$allWorksheets = $PHPReader->getAllSheets();
$i = 0;
foreach ($allWorksheets as $objWorksheet) {
$sheetname = $objWorksheet->getTitle();
$allRow = $objWorksheet->getHighestRow();//how many rows
$highestColumn = $objWorksheet->getHighestColumn();//how many columns
$allColumn = \PHPExcel_Cell::columnIndexFromString($highestColumn);
$array[$i]["Title"] = $sheetname;
$array[$i]["Cols"] = $allColumn;
$array[$i]["Rows"] = $allRow;
$arr = array();
$isMergeCell = array();
foreach ($objWorksheet->getMergeCells() as $cells) {//merge cells
foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) {
$isMergeCell[$cellReference] = true;
}
}
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
$row = array();
for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn++) {
;
$cell = $objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow);
$afCol = \PHPExcel_Cell::stringFromColumnIndex($currentColumn + 1);
$bfCol = \PHPExcel_Cell::stringFromColumnIndex($currentColumn - 1);
$col = \PHPExcel_Cell::stringFromColumnIndex($currentColumn);
$address = $col . $currentRow;
$value = $objWorksheet->getCell($address)->getValue();
if (substr($value, 0, 1) == '=') {
return array("error" => 0, 'message' => 'can not use the formula!');
exit;
}
if ($isMergeCell[$col . $currentRow] && $isMergeCell[$afCol . $currentRow] && !empty($value)) {
$temp = $value;
} elseif ($isMergeCell[$col . $currentRow] && $isMergeCell[$col . ($currentRow - 1)] && empty($value)) {
$value = $arr[$currentRow - 1][$currentColumn];
} elseif ($isMergeCell[$col . $currentRow] && $isMergeCell[$bfCol . $currentRow] && empty($value)) {
$temp = $value;
}
$row[$currentColumn] = $value;
}
$arr[$currentRow] = $row;
}
$array[$i]["Content"] = $arr;
$i++;
}
unset($objWorksheet);
unset($PHPReader);
unset($PHPExcel);
unlink($file);
return array("error" => 1, "data" => $array);
}
// 导入数据。
public function getgoodsarr()
{
if (IS_POST) {
$platform_type = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
$merchantid = session('ls_userinfo')['merchantid'];
$status = I('post.statustype');
if (!$merchantid) {
$rt['status'] = 0;
$rt['msg'] = '查询失败';
} else {
$w['merchantid'] = $merchantid;
if ($status != -1) {
$w['status'] = $status;
} else {
$w['status'] = array('neq', 2);
}
$data = M('retail_vipimport')->where($w)->order('status asc')->select();
if (!empty($data)) {
$StoreModel = new RetailStoreModel();
$list = $StoreModel->getSelect([
'merchantid' => $merchantid,
'state' => 0
], 'id,storetag');
if (!empty($list)) {
$list = array_column($list, null, 'id');
}
foreach ($data as $k => $v) {
$data[$k]['card_key'] = $v['card_key'] == 0 || $v['card_key'] == null ? '未绑定' : $v['card_key'];
if ($v['status'] == 0) {
$statusname = '未导入';
} else if ($v['status'] == 1) {
$statusname = '已导入';
} else if ($v['status'] == 2) {
$statusname = '已删除';
} else if ($v['status'] == 3) {
$statusname = '手机号重复';
} else if ($v['status'] == 4) {
$statusname = '实体卡已被使用';
}
$data[$k]['statusname'] = $statusname;
if ($v['sex'] == 1) {
$data[$k]['sex_name'] = '男';
} else {
$data[$k]['sex_name'] = '女';
}
//第三方平台
if ($platform_type == 2) {
$data[$k]['store_name'] = $list[$v['storeid']]['storetag'];
}
}
$rt['status'] = 1;
$rt['msg'] = '查询成功';
$rt['data'] = $data;
} else {
$rt['status'] = 0;
$rt['msg'] = '查询失败';
}
}
$this->ajaxReturn($rt);
}
}
/**
* 导入信息
*/
public function submitVips()
{
if ($_POST) {
$platform_type = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
$merchantid = $this->merchant_id;
$storeid = I('post.storeid');
$checkVipIds = I('post.checkgoodsarr');
//系统默认流程必须选择门店
if ($platform_type == 1) {
if (empty($storeid)) {
$result['status'] = 0;
$result['msg'] = '请选择门店';
$this->ajaxReturn($result);
}
}
if (empty($checkVipIds)) {
$result['status'] = 0;
$result['msg'] = '请选择会员';
$this->ajaxReturn($result);
}
$successNum = 0;
$resArr = array();
$VipModel = new VipModel();
$vipImportList = M("retail_vipimport")->where(array('id' => array('in', $checkVipIds)))->select();
foreach ($vipImportList as $k => $v) {
$find_phone = false;
if (!empty($v['mobile_phone'])) {
$map = array(
'mobile_phone' => $v['mobile_phone'],
'merchantid' => $merchantid
);
$find_phone = $VipModel->getFind($map);
if (!empty($find_phone)) {
//修改导入表状态(手机号重复)
M("retail_vipimport")->where(array("id" => $v['id']))->save(array("status" => 3));
$resArr[] = '第' . ($k + 1) . '条数据手机号 ' . $v['mobile_phone'] . ' 在会员库里已存在';
}
}
$find_card = false;
if (empty($find_phone)) {
if ($v['card_key'] != 0) {
$map = array(
'card_key' => $v['card_key'],
'merchantid' => $merchantid
);
$find_card = $VipModel->getFind($map);
if (!empty($find_card)) {
//修改导入表状态(实体卡重复)
M("retail_vipimport")->where(array("id" => $v['id']))->save(array("status" => 4));
$resArr[] = '第' . ($k + 1) . '条数据实体卡' . $v['mobile_phone'] . '在会员库里已存在';
}
}
}
if (empty($find_phone) && empty($find_card)) {
if (!empty($v['password'])) {
$vipPassword = $v['password'];
} else {
$vipPassword = substr($v['mobile_phone'], -6);
}
$CryptrModel = new \Addons\Api\Crypto ();
$password = $CryptrModel->encrypt($vipPassword, date('YmdHis', intval($v['addtime'])));
$vip = $v;
unset($vip['id']);
$vip['addtime'] = $v['addtime'];
$vip['password'] = $password;
$vip['cardnum'] = $v['mobile_phone'];
$vip['integral'] = $v['integral'] ? $v['integral'] : 0;
$vip['grade_integral'] = $v['integral'];
$vip['status'] = 1;
$vip['sex'] = $v['sex']?$v['sex']:1;
$vip['source_channel'] = $v['source_channel']?$v['source_channel']:'';
if($platform_type == 2){
$vip['storeid'] = $v['storeid'];
}else{
$vip['storeid'] = $storeid;
}
$res = $VipModel->addVip($vip);
if ($res) {
$successNum++;
M("retail_vipimport")->where(array("id" => $v['id']))->save(array("status" => 1));
$resArr[] = '第' . ($k + 1) . '条数据导入成功';
} else {
$resArr[] = '第' . ($k + 1) . '条数据导入失败';
}
}
}
$result['data'] = $resArr;
if ($successNum == count($vipImportList)) {
$result['status'] = 1;
$result['msg'] = '全部导入成功';
} else {
$result['status'] = 2;
$result['msg'] = '部分导入失败';
}
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST);//请求参数 json数据
$desc = '对上传会员进行导入';//操作行为描述
action_log($source, $type, $request_data, $desc);
$this->ajaxReturn($result);
}
}
public function delgoodsitem()
{
if (IS_POST) {
$id = I('post.id');
$merchantid = session('ls_userinfo')['merchantid'];
if ($id && $merchantid) {
$ret = M('retail_vipimport')->where(array('merchantid' => $merchantid, 'id' => array('in', $id)))->save(array('status' => 2));
}
if ($ret) {
$rt['status'] = 1;
$rt['msg'] = '删除成功';
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST);//请求参数 json数据
$desc = '对上传会员进行删除';//操作行为描述
action_log($source, $type, $request_data, $desc);
//end
} else {
$rt['status'] = 0;
$rt['msg'] = '删除失败';
}
$this->ajaxReturn($rt);
}
}
// 下载导入失败的数据表格
public function downfaultfile()
{
$platform_type = $this->role_info['platform_type'] ? $this->role_info['platform_type'] : 1;
$merchant_id = $this->merchant_id;
$path2 = './Uploads/Temp/Vipimport' . $merchant_id . '.txt';
//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
import("Vendor.PHPExcel.PHPExcel");
import("Vendor.PHPExcel.Writer.Excel5");
import("Vendor.PHPExcel.IOFactory.php");
$arr = file_get_contents($path2);
$arr = json_decode($arr, true);
$filename = "导入失败会员信息";
if ($platform_type == 2) {
$headArr = array(
'0' => '姓名',
'1' => '手机号',
'2' => '门店',
'3' => '来源渠道',
'4' => '会员等级',
'5' => '积分',
'reson' => '失败原因',
);
} else {
$headArr = array(
'0' => '手机号码/会员卡号',
'1' => '姓名',
'2' => '昵称',
'3' => '性别',
'4' => '密码',
'5' => '实体卡ID',
'6' => '余额',
'reson' => '失败原因',
);
}
$headArr2 = array();
foreach ($headArr as $k => $v) {
$headArr2[] = array($k, $v);
}
unlink($path2);
$this->getExcel($filename, $headArr2, $arr);
}
private function getExcel($expTitle, $expCellName, $expTableData, $str = "")
{
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $expTitle . date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$common = $str;
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $common);
// $objPHPExcel->setActiveSheetIndex(1)->setCellValue('A1', $str);
for ($i = 0; $i < $cellNum; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
}
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), ' ' . $expTableData[$i][$expCellName[$j][0]]);
}
}
foreach ($cellName as $value) {
$objPHPExcel->getActiveSheet()->getColumnDimension($value)->setWidth(20);
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit();
}
// 修改积分
public function getVip()
{
$vips = M('retail_vip')->select();
foreach ($vips as &$val) {
if ($val['integral'] == '' || $val['integral'] == null) {
M('retail_vip')->where(array('id' => $val['id']))->save(array('integral' => 0));
}
if ($val['grade_integral'] == '' || $val['grade_integral'] == null) {
M('retail_vip')->where(array('id' => $val['id']))->save(array('grade_integral' => 0));
}
}
}
}