Custom.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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\WeiBan\Follow as WeiBanFollow;
  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,WeiBanFollow $WeiBanFollow,City $City){
  33. // 接受参数
  34. $code = request('custom_code','');
  35. $phone = request('phone','');
  36. $username = request('username','');
  37. $weibanId = request('weiban_extid','');
  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( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
  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['weiban_extid'] ) {
  66. // 通过手机号查询注册的账号
  67. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  68. // 如果存在的话,修正
  69. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  70. }
  71. // 重组
  72. $list[$key] = $value;
  73. }
  74. // 获取列表
  75. $cityList = $City->getCityList();
  76. // 分配数据
  77. $this->assign('cityList',$cityList);
  78. // 分配数据
  79. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  80. $this->assign('list',$list);
  81. // 加载模板
  82. return $this->fetch();
  83. }
  84. /**
  85. * 添加
  86. *
  87. * */
  88. public function add(Request $request,Model $Model){
  89. if( request()->isMethod('post') ){
  90. // 验证参数
  91. $request->scene('add')->validate();
  92. // 接收数据
  93. // 接收数据
  94. $data['username'] = request('username','');
  95. $data['phone'] = request('phone','');
  96. // 写入数据表
  97. $uid = $Model->add($data);
  98. // 如果操作失败
  99. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  100. // 记录行为
  101. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  102. // 告知结果
  103. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  104. }
  105. // 分配数据
  106. $this->assign('crumbs','新增');
  107. // 加载模板
  108. return $this->fetch();
  109. }
  110. /**
  111. * 修改
  112. *
  113. * */
  114. public function edit(Request $request,Model $Model,City $City){
  115. // 接收参数
  116. $uid = request('uid',0);
  117. // 查询用户
  118. $oldData = $Model->where(['uid'=>$uid])->first();
  119. // 修改
  120. if(request()->isMethod('post')){
  121. // 验证参数
  122. $request->scene('edit')->validate();
  123. // 如果用户不存在
  124. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  125. // 转数组
  126. $oldData = $oldData->toArray();
  127. // 接收数据
  128. $data['username'] = request('username','');
  129. $data['phone'] = request('phone','');
  130. $data['city_id'] = request('city_id',0);
  131. $data['weiban_extid'] = request('weiban_extid','');
  132. // 写入数据表
  133. $result = $Model->edit($uid,$data);
  134. // 如果操作失败
  135. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  136. // 记录行为
  137. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  138. // 告知结果
  139. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  140. }
  141. // 错误告知
  142. if( !$oldData ) return $this->error('查无数据');
  143. // 获取列表
  144. $cityList = $City->getCityList();
  145. // 分配数据
  146. $this->assign('cityList',$cityList);
  147. $this->assign('oldData',$oldData);
  148. $this->assign('crumbs','修改');
  149. // 加载模板
  150. return $this->fetch();
  151. }
  152. /**
  153. * 修改状态
  154. *
  155. * */
  156. public function set_status(Request $request,Model $Model){
  157. // 验证参数
  158. $request->scene('set_status')->validate();
  159. // 设置状态
  160. $uid = request('uid',0);
  161. $status = request('status',0);
  162. // 查询用户
  163. $oldData = $Model->where(['uid'=>$uid])->first();
  164. // 如果用户不存在
  165. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  166. // 执行修改
  167. $result = $Model->edit($uid,['status'=>$status]);
  168. // 提示新增失败
  169. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  170. // 记录行为
  171. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  172. // 告知结果
  173. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  174. }
  175. /**
  176. * 修改状态
  177. *
  178. * */
  179. public function set_manager(Request $request,Model $Model){
  180. // 验证参数
  181. $request->scene('set_manager')->validate();
  182. // 设置状态
  183. $uid = request('uid',0);
  184. $isManager = request('is_manager',0);
  185. // 查询用户
  186. $oldData = $Model->where(['uid'=>$uid])->first();
  187. // 如果用户不存在
  188. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  189. // 执行修改
  190. $result = $Model->edit($uid,['is_manager'=>$isManager]);
  191. // 提示新增失败
  192. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  193. // 记录行为
  194. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['is_manager'=>$isManager]);
  195. // 告知结果
  196. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  197. }
  198. /**
  199. * 表格导入
  200. *
  201. * */
  202. public function import_execl( Request $request,Model $Model,FilesManager $FilesManager,City $City,CustomAddr $CustomAddr,WeiBanExternal $WeiBanExternal){
  203. // 验证参数
  204. $request->scene('import_execl')->validate();
  205. // 获取表格信息
  206. $file = request()->file('custom_file');
  207. // 返回结果
  208. $sheetList = $FilesManager->excelToCustom($file);
  209. // 如果不存在结果
  210. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  211. // 循环表格数据
  212. foreach ($sheetList as $value) {
  213. // 获取城市ID
  214. $value['city_id'] = 0;
  215. // 存在城市名称,查询城市ID
  216. if( $value['contact_city'] ) {
  217. // 获取城市ID
  218. $value['city_id'] = (int) $City->getIdByName($value['contact_city']);
  219. // 如果城市不存在的话
  220. if ( !$value['city_id'] ) return json_send(['code'=>'error','msg'=>$value['contact_city'].' => 未找到匹配的城市,请核对城市全称']);
  221. }
  222. // 获取手机号,查询是否用客户
  223. $custom = $Model->getOneByPhone($value['phone']);
  224. // 组装数据
  225. $data = ['weiban_extid'=>$value['weiban_extid'],'username'=>$value['username']];
  226. // 如果客户不存在
  227. if( !$custom ) $data['phone'] = $value['phone'];
  228. // 如果存在城市ID,才修改城市
  229. if( $value['city_id'] ) $data['city_id'] = $value['city_id'];
  230. // 如果存在手机号
  231. $uid = $custom ? $Model->edit($custom['uid'],$data) : $Model->add($data);
  232. // 如果客户存在
  233. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['username'].'【'.$value['phone'].'】'.'用户创建或者更新失败']);
  234. // 存在详细地址,才创建地址库
  235. if( $value['contact_name'] && $value['contact_phone'] && $value['contact_province'] && $value['contact_city'] && $value['contact_area'] && $value['contact_addr'] ) {
  236. // 收件地址是否存在
  237. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  238. // 如果不存在地址
  239. 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']]);
  240. }
  241. }
  242. // 提示成功
  243. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  244. }
  245. /**
  246. * 导出表格
  247. *
  248. * */
  249. public function down_excel(Model $Model,City $City){
  250. // 接受参数
  251. $code = request('custom_code','');
  252. $phone = request('phone','');
  253. $username = request('username','');
  254. $weibanId = request('weiban_extid','');
  255. $cityId = request('city_id',0);
  256. $status = request('status');
  257. $startTime = request('start_time','');
  258. // 编码转ID
  259. $uid = $Model->codeToId($code);
  260. // 查询条件
  261. $map = [];
  262. // 编码ID
  263. if( $uid ) $map[] = ['uid','=',$uid];
  264. if( $phone ) $map[] = ['phone','=',$phone];
  265. if( $username ) $map[] = ['username','=',$username];
  266. if( $cityId ) $map[] = ['city_id','=',$cityId];
  267. if( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
  268. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  269. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  270. if( !is_null($status) ) $map[] = ['status','=',$status];
  271. // 查询数据
  272. $list = $Model->query()->where($map)->orderByDesc('uid')->get()->toArray();
  273. // 循环处理数据
  274. foreach ($list as $key => $value) {
  275. // 城市名
  276. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  277. // id转编号
  278. $value['custom_code'] = $Model->idToCode($value['uid']);
  279. // 重组
  280. $list[$key] = $value;
  281. }
  282. try {
  283. // 去下载
  284. $this->toDown($list);
  285. } catch (\Throwable $th) {
  286. echo $th->getMessage();
  287. }
  288. }
  289. /**
  290. * 去下载
  291. */
  292. private function toDown($data){
  293. $list = [];
  294. foreach ($data as $key=>$value) {
  295. // 单元格内容写入
  296. $list[$key]['custom_code'] = $value['custom_code'];
  297. $list[$key]['username'] = $value['username'];
  298. $list[$key]['phone'] = $value['phone'];
  299. $list[$key]['weiban_extid'] = $value['weiban_extid'];
  300. $list[$key]['city_name'] = $value['city_name'];
  301. $list[$key]['status'] = $value['status']?'禁用':'正常';
  302. $list[$key]['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
  303. $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
  304. }
  305. try {
  306. $config = [
  307. 'path' =>public_path().'/uploads/' // xlsx文件保存路径
  308. ];
  309. $excel = new \Vtiful\Kernel\Excel($config);
  310. $header = [
  311. '客户编码',
  312. '客户昵称',
  313. '联系方式',
  314. '微伴ID',
  315. '客户城市',
  316. '客户状态',
  317. '创建时间',
  318. '更新时间',
  319. ];
  320. $filePath = $excel->fileName('user01.xlsx', 'sheet1')
  321. ->header($header)
  322. ->data($list)
  323. ->output();
  324. $filename = '客户列表.xlsx';
  325. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  326. header('Content-Disposition: attachment;filename="' . $filename . '"');
  327. header('Content-Length: ' . filesize($filePath));
  328. header('Content-Transfer-Encoding: binary');
  329. header('Cache-Control: must-revalidate');
  330. header('Cache-Control: max-age=0');
  331. header('Pragma: public');
  332. ob_clean();
  333. flush();
  334. if (copy($filePath, 'php://output') === false) {
  335. return json_send(['code'=>'error','msg'=>'下载失败']);
  336. }
  337. @unlink($filePath);
  338. return json_send(['code'=>'success','msg'=>'下载成功','path'=>'']);
  339. }catch (\Exception $exception) {
  340. return json_send(['code'=>'error','msg'=>'下载失败']);
  341. }
  342. }
  343. /**
  344. * 设置表格样式
  345. *
  346. */
  347. private function setStyle(Spreadsheet $spreadsheet){
  348. // 选择当前活动的工作表
  349. $sheet = $spreadsheet->getActiveSheet();
  350. // 宽
  351. $sheet->getColumnDimension('A')->setWidth(15);
  352. $sheet->getColumnDimension('B')->setWidth(15);
  353. $sheet->getColumnDimension('C')->setWidth(15);
  354. $sheet->getColumnDimension('D')->setWidth(30);
  355. $sheet->getColumnDimension('E')->setWidth(15);
  356. $sheet->getColumnDimension('F')->setWidth(15);
  357. $sheet->getColumnDimension('G')->setWidth(20);
  358. $sheet->getColumnDimension('H')->setWidth(20);
  359. // 默认高度
  360. $sheet->getDefaultRowDimension()->setRowHeight(18);
  361. // 加粗第一行
  362. $sheet->getStyle('A:H')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  363. $sheet->getStyle('A1:H1')->getFont()->setBold(true);
  364. $sheet->getStyle('A1:H1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  365. // 设置表格标题
  366. $sheet
  367. ->setCellValue('A1', '客户编码')
  368. ->setCellValue('B1', '客户昵称')
  369. ->setCellValue('C1', '联系方式')
  370. ->setCellValue('D1', '微伴ID')
  371. ->setCellValue('E1', '客户城市')
  372. ->setCellValue('F1', '客户状态')
  373. ->setCellValue('G1', '创建时间')
  374. ->setCellValue('H1', '更新时间');
  375. // 返回结果
  376. return $sheet;
  377. }
  378. }