Custom.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Custom as Request;
  3. use App\Models\City;
  4. use App\Models\Custom as Model;
  5. use App\Models\CustomAddr;
  6. use App\Models\CustomScore;
  7. use Illuminate\Support\Carbon;
  8. use App\Models\Work\External as WorkExternal;
  9. use App\Models\FilesManager;
  10. use App\Models\WeiBan\External as WeiBanExternal;
  11. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  15. use PhpOffice\PhpSpreadsheet\Style\Fill;
  16. /**
  17. * 客户管理
  18. *
  19. * @author 刘相欣
  20. *
  21. */
  22. class Custom extends Auth{
  23. protected function _initialize(){
  24. parent::_initialize();
  25. $this->assign('breadcrumb1','用户管理');
  26. $this->assign('breadcrumb2','客户管理');
  27. }
  28. /**
  29. * 列表页
  30. *
  31. * */
  32. public function index(Model $Model,CustomScore $CustomScore,City $City,WorkExternal $WorkExternal){
  33. // 接受参数
  34. $code = request('custom_code','');
  35. $phone = request('phone','');
  36. $username = request('username','');
  37. $extId = request('external_userid','');
  38. $cityId = request('city_id',0);
  39. $status = request('status');
  40. $startTime = request('start_time','');
  41. // 编码转ID
  42. $uid = $Model->codeToId($code);
  43. // 查询条件
  44. $map = [];
  45. // 编码ID
  46. if( $uid ) $map[] = ['uid','=',$uid];
  47. if( $phone ) $map[] = ['phone','=',$phone];
  48. if( $username ) $map[] = ['username','=',$username];
  49. if( $cityId ) $map[] = ['city_id','=',$cityId];
  50. if( $extId ) $map[] = ['external_userid','=',$extId];
  51. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  52. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  53. if( !is_null($status) ) $map[] = ['status','=',$status];
  54. // 查询数据
  55. $list = $Model->query()->where($map)->orderByDesc('uid')->paginate(config('page_num',10))->appends(request()->all());
  56. // 循环处理数据
  57. foreach ($list as $key => $value) {
  58. // 城市名
  59. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  60. // id转编号
  61. $value['custom_code'] = $Model->idToCode($value['uid']);
  62. // id转编号
  63. $value['custom_score'] = $CustomScore->getCustomScore($value['uid']);
  64. // 如果不存在企微ID,查询数据库
  65. if( !$value['external_userid'] ) $value['external_userid'] = (string) $WorkExternal->query()->where([['custom_uid', '=',$value['uid']]])->value('external_userid');
  66. // 重组
  67. $list[$key] = $value;
  68. }
  69. // 获取列表
  70. $cityList = $City->getCityList();
  71. // 分配数据
  72. $this->assign('cityList',$cityList);
  73. // 分配数据
  74. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  75. $this->assign('list',$list);
  76. // 加载模板
  77. return $this->fetch();
  78. }
  79. /**
  80. * 添加
  81. *
  82. * */
  83. public function add(Request $request,Model $Model){
  84. if( request()->isMethod('post') ){
  85. // 验证参数
  86. $request->scene('add')->validate();
  87. // 接收数据
  88. // 接收数据
  89. $data['username'] = request('username','');
  90. $data['phone'] = request('phone','');
  91. // 写入数据表
  92. $uid = $Model->add($data);
  93. // 如果操作失败
  94. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  95. // 记录行为
  96. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  97. // 告知结果
  98. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  99. }
  100. // 分配数据
  101. $this->assign('crumbs','新增');
  102. // 加载模板
  103. return $this->fetch();
  104. }
  105. /**
  106. * 修改
  107. *
  108. * */
  109. public function edit(Request $request,Model $Model,City $City){
  110. // 接收参数
  111. $uid = request('uid',0);
  112. // 查询用户
  113. $oldData = $Model->where(['uid'=>$uid])->first();
  114. // 修改
  115. if(request()->isMethod('post')){
  116. // 验证参数
  117. $request->scene('edit')->validate();
  118. // 如果用户不存在
  119. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  120. // 转数组
  121. $oldData = $oldData->toArray();
  122. // 接收数据
  123. $data['username'] = request('username','');
  124. $data['phone'] = request('phone','');
  125. $data['city_id'] = request('city_id',0);
  126. $data['external_userid']= request('external_userid','');
  127. // 写入数据表
  128. $result = $Model->edit($uid,$data);
  129. // 如果操作失败
  130. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  131. // 记录行为
  132. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  133. // 告知结果
  134. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  135. }
  136. // 错误告知
  137. if( !$oldData ) return $this->error('查无数据');
  138. // 获取列表
  139. $cityList = $City->getCityList();
  140. // 分配数据
  141. $this->assign('cityList',$cityList);
  142. $this->assign('oldData',$oldData);
  143. $this->assign('crumbs','修改');
  144. // 加载模板
  145. return $this->fetch();
  146. }
  147. /**
  148. * 修改状态
  149. *
  150. * */
  151. public function set_status(Request $request,Model $Model){
  152. // 验证参数
  153. $request->scene('set_status')->validate();
  154. // 设置状态
  155. $uid = request('uid',0);
  156. $status = request('status',0);
  157. // 查询用户
  158. $oldData = $Model->where(['uid'=>$uid])->first();
  159. // 如果用户不存在
  160. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  161. // 执行修改
  162. $result = $Model->edit($uid,['status'=>$status]);
  163. // 提示新增失败
  164. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  165. // 记录行为
  166. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  167. // 告知结果
  168. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  169. }
  170. /**
  171. * 表格导入
  172. *
  173. * */
  174. public function import_execl( Request $request,Model $Model,FilesManager $FilesManager,City $City,CustomAddr $CustomAddr,WeiBanExternal $WeiBanExternal){
  175. // 验证参数
  176. $request->scene('import_execl')->validate();
  177. // 获取表格信息
  178. $file = request()->file('custom_file');
  179. // 返回结果
  180. $sheetList = $FilesManager->excelToCustom($file);
  181. // 如果不存在结果
  182. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  183. // 循环表格数据
  184. foreach ($sheetList as $value) {
  185. // 获取城市ID
  186. $value['city_id'] = 0;
  187. // 存在城市名称,查询城市ID
  188. if( $value['contact_city'] ) {
  189. // 获取城市ID
  190. $value['city_id'] = (int) $City->getIdByName($value['contact_city']);
  191. // 如果城市不存在的话
  192. if ( !$value['city_id'] ) return json_send(['code'=>'error','msg'=>$value['contact_city'].' => 未找到匹配的城市,请核对城市全称']);
  193. }
  194. // 获取手机号,查询是否用客户
  195. $custom = $Model->getOneByPhone($value['phone']);
  196. // 组装数据
  197. $data = ['external_userid'=>$value['external_userid'],'username'=>$value['username']];
  198. // 如果客户不存在
  199. if( !$custom ) $data['phone'] = $value['phone'];
  200. // 如果存在城市ID,才修改城市
  201. if( $value['city_id'] ) $data['city_id'] = $value['city_id'];
  202. // 如果存在手机号
  203. $uid = $custom ? $Model->edit($custom['uid'],$data) : $Model->add($data);
  204. // 如果客户存在
  205. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['username'].'【'.$value['phone'].'】'.'用户创建或者更新失败']);
  206. // 存在详细地址,才创建地址库
  207. if( $value['contact_name'] && $value['contact_phone'] && $value['contact_province'] && $value['contact_city'] && $value['contact_area'] && $value['contact_addr'] ) {
  208. // 收件地址是否存在
  209. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  210. // 如果不存在地址
  211. if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$value['contact_name'],'contact_shop'=>$value['contact_shop'],'contact_phone'=>$value['contact_phone'],'contact_province'=>$value['contact_province'],'contact_city'=>$value['contact_city'],'contact_area'=>$value['contact_area'],'contact_addr'=>$value['contact_addr']]);
  212. }
  213. }
  214. // 提示成功
  215. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  216. }
  217. /**
  218. * 导出表格
  219. *
  220. * */
  221. public function down_excel(Model $Model,City $City){
  222. // 接受参数
  223. $code = request('custom_code','');
  224. $phone = request('phone','');
  225. $username = request('username','');
  226. $extId = request('external_userid','');
  227. $cityId = request('city_id',0);
  228. $status = request('status');
  229. $startTime = request('start_time','');
  230. // 编码转ID
  231. $uid = $Model->codeToId($code);
  232. // 查询条件
  233. $map = [];
  234. // 编码ID
  235. if( $uid ) $map[] = ['uid','=',$uid];
  236. if( $phone ) $map[] = ['phone','=',$phone];
  237. if( $username ) $map[] = ['username','=',$username];
  238. if( $cityId ) $map[] = ['city_id','=',$cityId];
  239. if( $extId ) $map[] = ['external_userid','=',$extId];
  240. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  241. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  242. if( !is_null($status) ) $map[] = ['status','=',$status];
  243. // 查询数据
  244. $list = $Model->query()->where($map)->orderByDesc('uid')->get()->toArray();
  245. // 循环处理数据
  246. foreach ($list as $key => $value) {
  247. // 城市名
  248. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  249. // id转编号
  250. $value['custom_code'] = $Model->idToCode($value['uid']);
  251. // 重组
  252. $list[$key] = $value;
  253. }
  254. try {
  255. // 去下载
  256. $this->toDown($list);
  257. } catch (\Throwable $th) {
  258. echo $th->getMessage();
  259. }
  260. }
  261. /**
  262. * 去下载
  263. */
  264. private function toDown($data){
  265. // 创建新的电子表格对象
  266. $spreadsheet = new Spreadsheet();
  267. // 设置合并单元格的行和列,例如合并A1到B2的单元格
  268. $sheet = $this->setStyle($spreadsheet);
  269. // 从第二行写入
  270. $row = 2;
  271. // 循环写入
  272. foreach ($data as $value) {
  273. // 单元格内容写入
  274. $sheet->setCellValue('A'.$row, $value['custom_code']);
  275. //避免 = + - 识别成公式
  276. $sheet->setCellValueExplicit('B'.$row, $value['username'],DataType::TYPE_STRING);
  277. $sheet->setCellValue('C'.$row, $value['phone']);
  278. $sheet->setCellValue('D'.$row, $value['external_userid']);
  279. $sheet->setCellValue('E'.$row, $value['city_name']);
  280. $sheet->setCellValue('F'.$row, $value['status']?'禁用':'正常');
  281. $sheet->setCellValue('G'.$row, date('Y-m-d H:i:s',$value['insert_time']));
  282. $sheet->setCellValue('H'.$row, date('Y-m-d H:i:s',$value['update_time']));
  283. // 行数+1
  284. $row++;
  285. }
  286. // 创建内容
  287. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  288. header('Pragma: public');
  289. header('Content-type:application/vnd.ms-excel');
  290. header('Content-Disposition: inline;filename=客户列表.xlsx');
  291. // 输出数据流
  292. return $writer->save('php://output');
  293. }
  294. /**
  295. * 设置表格样式
  296. *
  297. */
  298. private function setStyle(Spreadsheet $spreadsheet){
  299. // 选择当前活动的工作表
  300. $sheet = $spreadsheet->getActiveSheet();
  301. // 宽
  302. $sheet->getColumnDimension('A')->setWidth(15);
  303. $sheet->getColumnDimension('B')->setWidth(15);
  304. $sheet->getColumnDimension('C')->setWidth(15);
  305. $sheet->getColumnDimension('D')->setWidth(30);
  306. $sheet->getColumnDimension('E')->setWidth(15);
  307. $sheet->getColumnDimension('F')->setWidth(15);
  308. $sheet->getColumnDimension('G')->setWidth(20);
  309. $sheet->getColumnDimension('H')->setWidth(20);
  310. // 默认高度
  311. $sheet->getDefaultRowDimension()->setRowHeight(18);
  312. // 加粗第一行
  313. $sheet->getStyle('A:H')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  314. $sheet->getStyle('A1:H1')->getFont()->setBold(true);
  315. $sheet->getStyle('A1:H1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  316. // 设置表格标题
  317. $sheet
  318. ->setCellValue('A1', '客户编码')
  319. ->setCellValue('B1', '客户昵称')
  320. ->setCellValue('C1', '联系方式')
  321. ->setCellValue('D1', '企微ID')
  322. ->setCellValue('E1', '客户城市')
  323. ->setCellValue('F1', '客户状态')
  324. ->setCellValue('G1', '创建时间')
  325. ->setCellValue('H1', '更新时间');
  326. // 返回结果
  327. return $sheet;
  328. }
  329. }