Member.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\user\controller;
  15. use app\common\model\User;
  16. use app\common\model\UserFacility;
  17. use app\common\model\UserGroup;
  18. use app\common\model\UserLevel;
  19. use app\common\model\UserLevelRank;
  20. use app\common\model\UserTag;
  21. use app\common\model\UserWallet;
  22. use library\Controller;
  23. use library\tools\Data;
  24. use think\Db;
  25. use function AlibabaCloud\Client\value;
  26. /**
  27. * 会员信息管理
  28. * Class Member
  29. * @package app\Member\controller
  30. */
  31. class Member extends Controller
  32. {
  33. /**
  34. * 绑定数据表
  35. * @var string
  36. */
  37. protected $table = 'StoreMember';
  38. /**
  39. * 会员信息管理
  40. * @auth true
  41. * @menu true
  42. * @throws \think\Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. * @throws \think\exception\PDOException
  47. */
  48. public function index()
  49. {
  50. $this->search_url = strtolower($this->request->controller()).'/index_search';
  51. $this->title = '会员信息管理';
  52. $this->tag_arr = UserTag::column('title','id');
  53. $this->level_arr = UserLevel::column('name','id');
  54. $where = [];
  55. $where[] = ['m.is_deleted','=',0];
  56. if($name = input('get.name')) $where[] = ['m.name','like','%'.$name.'%'];
  57. if($phone = input('get.phone')) $where[] = ['m.phone','like','%'.$phone.'%'];
  58. if($level_id = input('get.level_id')) $where[] = ['m.level_id','=',$level_id];
  59. if($label_id = input('get.label_id')) $where[] = ['m.label','like','%|'.$label_id."|%"];
  60. if($account_type = input('get.account_type')) $where[] = ['m.account_type','=',$account_type];
  61. $field="m.id,m.openid,m.name,m.headimg,m.level_exp,m.phone,m.status,m.create_at,m.account_type,w.growth,w.integral,w.money,m.tag_id,label";
  62. $create_at = input('create_at');
  63. $query = $this->_query($this->table)
  64. ->field($field)
  65. ->alias('m')
  66. ->leftJoin('UserWallet w','m.id =w.user_id')
  67. ->where($where)
  68. ->when($create_at,function ($q)use ($create_at){
  69. if($create_at){
  70. $time = explode(' - ',$_GET['create_at']);
  71. $start_date_time = $time[0];
  72. $end_date_time = $time[1];
  73. $q->whereBetweenTime('m.create_at',$start_date_time,$end_date_time);
  74. }
  75. })->order('id desc')->page();
  76. }
  77. /**
  78. * 数据列表处理
  79. * @auth true
  80. * @menu true
  81. * @param array $data
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. protected function _index_page_filter(&$data)
  87. {
  88. $level_arr = UserLevel::column('name','id');
  89. foreach ($data as $k=>&$v){
  90. $level_rank= UserLevelRank::getUserVipInfo($v['id']);
  91. $v['level_id'] = $level_rank ? $level_rank['level_id'] : 0 ;
  92. $v['level_name'] = $level_rank ? $level_arr[$level_rank['level_id']] : '--';
  93. $v['end_date'] = $level_rank ?$level_rank['end_date'] :'--';
  94. $label_ids = implode(',',explode("|",trim($v['label'],'|')));
  95. $v['label_arr'] = $v['label'] ? UserTag::where('id','in',$label_ids)->column('title') : [];
  96. }
  97. }
  98. /**
  99. * 删除
  100. * @auth true
  101. * @menu true
  102. * @param array $data
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. * @throws \think\exception\DbException
  106. */
  107. public function remove()
  108. {
  109. $this->_save($this->table, ['is_deleted' => '1','phone'=>'','email'=>'']);
  110. }
  111. /**
  112. * 禁用
  113. * @auth true
  114. * @menu true
  115. * @param array $data
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. */
  120. public function forbid()
  121. {
  122. $this->_save($this->table, ['status' => '0']);
  123. }
  124. /**
  125. * 启用
  126. * @auth true
  127. * @menu true
  128. * @param array $data
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. */
  133. public function resume()
  134. {
  135. $this->_save($this->table, ['status' => '1']);
  136. }
  137. /**
  138. * 添加
  139. * @auth true
  140. * @menu true
  141. * @param array $data
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. */
  146. public function add(){
  147. $this->title = '添加';
  148. $this->level_arr = UserLevel::column('name','id');
  149. if($this->request->isPost())
  150. {
  151. list($data) = [$this->request->post()];
  152. $check_where =[];
  153. $check_where[] = $data['account_type'] == 1 ? ['email','=',$data['account']] : ['phone','=',$data['account']];
  154. $user_info = User::where($check_where)->find();
  155. if($user_info) $this->error('用户已存在');
  156. $user_add= [];
  157. $data['account_type'] == 1 ? $user_add['email'] = $data['account'] : $user_add['phone'] = $data['account'];
  158. $user_add['name'] = $data['name'];
  159. $user_add['account_type'] = $data['account_type'];
  160. $select_label = [];
  161. if(isset($data['label']) && !empty($data['label'])){
  162. foreach ($data['label'] as $key=>$value){
  163. if($value) $select_label[] = $key;
  164. }
  165. }
  166. $data['label'] = '|'.implode('|',$select_label).'|';
  167. $new_user = User::create($user_add);
  168. if($data['level_id'] && intval($data['days']) > 0) {
  169. UserLevelRank::create(['user_id'=>$new_user->id,'level_id'=>$data['level_id'],'start_time'=>time(),'end_time'=>time()+86400* intval($data['days']),'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($data['days']))]);
  170. }
  171. $this->success('添加成功');
  172. }
  173. $this->_form($this->table, 'add');
  174. }
  175. /**
  176. *
  177. * 编辑
  178. * @auth true
  179. * @menu true
  180. * @param array $data
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. */
  185. public function edit()
  186. {
  187. $this->title = '编辑';
  188. $this->level_arr = UserLevel::column('name','id');
  189. $this->_form($this->table, 'form');
  190. }
  191. /**
  192. * 钱包管理
  193. * @auth true
  194. * @menu true
  195. * @param array $data
  196. * @throws \think\db\exception\DataNotFoundException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. * @throws \think\exception\DbException
  199. */
  200. public function wallet()
  201. {
  202. $this->title = '钱包';
  203. $this->_form($this->table, 'wallet');
  204. }
  205. protected function _form_filter(&$data){
  206. //选中的服务标签
  207. $this->tag_arr = UserTag::column('title','id');
  208. if($this->request->isPost() && $this->request->action() == 'add')
  209. {
  210. if($data['account_type'] == 1) {
  211. $is_reg = User::where('email',$data['account'])->value('id');
  212. $data['email'] = $data['account'];
  213. }else{
  214. $is_reg = User::where('phone',$data['account'])->value('id');
  215. $data['phone'] = $data['account'];
  216. }
  217. if($is_reg)$this->error('账号已存在');
  218. }
  219. if($this->request->isPost() && $this->request->action() == 'edit') {
  220. if($data['level_id'] > 0 && $data['over_time']) {
  221. Data::save('UserLevelRank',[
  222. 'user_id'=>$data['id'],
  223. 'level_id'=>$data['level_id'],
  224. 'start_time'=>time(),
  225. 'end_time'=>strtotime($data['over_time']) ,
  226. 'end_date'=>$data['over_time']
  227. ],'user_id',['user_id'=>$data['id']]);
  228. $data['level_exp'] = $data['over_time'];
  229. } else if($data['level_id'] == 0){
  230. Data::save('UserLevelRank',[
  231. 'user_id'=>$data['id'],
  232. 'level_id'=>$data['level_id'],
  233. 'start_time'=>time(),
  234. 'end_time'=> time(),
  235. 'end_date'=>date('Y-m-d H:i:s')
  236. ],'user_id',['user_id'=>$data['id']]);
  237. $data['level_exp'] = date('Y-m-d H:i:s');
  238. }
  239. }
  240. //
  241. if($this->request->isPost()) {
  242. $select_label = [];
  243. if(isset($data['label']) && !empty($data['label'])){
  244. foreach ($data['label'] as $key=>$value){
  245. if($value) $select_label[] = $key;
  246. }
  247. }
  248. $data['label'] = '|'.implode('|',$select_label).'|';
  249. }
  250. if($this->request->isGet() && $this->request->action() =='edit') {
  251. $label_arr = isset($data['label']) ? explode('|',trim($data['label'],'|')) : [];
  252. $this->label_arr = $label_arr;
  253. $data['rank_id'] = UserLevelRank::getUserVip($data['id']);
  254. $level_rank= UserLevelRank::where([['user_id','=',$data['id']]])->find();
  255. $data['end_date'] = $level_rank ? $level_rank->end_date:'--';
  256. }
  257. }
  258. public function facility()
  259. {
  260. if($this->request->isGet())
  261. {
  262. $user_id = input('get.user_id');
  263. $list = UserFacility::where('user_id',$user_id)->column('facility','type');
  264. $this->fetch('', ['list' =>$list,'user_id'=>$user_id]);
  265. }else{
  266. list($data) = [$this->request->post()];
  267. for ($i=1;$i<=4;$i++) {
  268. $check_facility = UserFacility::where($data['user_id'],$i,$data['facility_'.$i]);
  269. if(isset($data['facility_'.$i]) && !$check_facility){
  270. Data::save(
  271. 'UserFacility',
  272. ['user_id'=>$data['user_id'],'type'=>$i,'facility'=>$data['facility_'.$i],'set_time'=>date('Y-m-d H:i:s')],
  273. 'user_id',['user_id'=>$data['user_id'],'type'=>$i]);
  274. }
  275. }
  276. $this->success('编辑成功');
  277. }
  278. }
  279. public function export(){
  280. $tag_arr = UserTag::column('title','id');
  281. $get_data = $this->request->get();
  282. $time = explode(' - ',$get_data['create_at']);
  283. $phone = $get_data['phone'];
  284. $name = $get_data['name'];
  285. $where = [];
  286. $where[] = ['status',1];
  287. $where[] = ['is_deleted',0];
  288. $where_str = ' status = 1 AND is_deleted = 0';
  289. if($name) $where_str .=' AND name like '."'%".$name."%'";
  290. if($phone) $where_str .=' AND phone like '."'%".$phone."%'";
  291. if($level_id = input('get.level_id')) $where_str .=' AND level_id = '.$level_id;
  292. if($label_id = input('get.label_id')) $where_str .=' AND label like '."'%|".$label_id."|%'";
  293. if($get_data['create_at']) $where_str.=" AND create_at > '".$time[0]."'AND create_at <'".$time[1]."'";
  294. $data = Db::query("SELECT name,headimg,email,tag_id,phone,create_at,account_type,label FROM dd_store_member WHERE".$where_str.' ORDER BY id DESC');
  295. if(empty($data)) $this->error('暂无可以导出的数据');
  296. foreach ($data as $k=>&$v) {
  297. if(!$v) $v = '--';
  298. $v['account_desc'] = $v['account_type'] == 1 ? '公司':'个人';
  299. $label_ids = implode(',',explode("|",trim($v['label'],'|')));
  300. $label_arr = $label_ids ? UserTag::where('id','in',$label_ids)->column('title') : [];
  301. $v['label_name'] = $label_arr ? implode('|',$label_arr) : "--";
  302. }
  303. $field=array(
  304. 'A' => array('name', '昵称'),
  305. 'B' => array('email', '邮箱'),
  306. 'C' => array('phone', '电话'),
  307. 'D' => array('account_desc','类型'),
  308. 'E' => array('label_name','标签'),
  309. 'F' => array('create_at', '注册时间'),
  310. );
  311. $this->phpExcelList($field,$data,'会员列表');
  312. }
  313. public function phpExcelList($field=[],$list=[],$title='文件'){
  314. $PHPExcel=new \PHPExcel();
  315. $PHPSheet=$PHPExcel->getActiveSheet();
  316. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  317. foreach($list as $key=>$value)
  318. {
  319. foreach($field as $k=>$v){
  320. if($key == 0){
  321. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  322. }
  323. $i=$key+2;
  324. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  325. }
  326. }
  327. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  328. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  329. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  330. header('Cache-Control: max-age=0'); //禁止缓存
  331. $PHPWriter->save("php://output"); //输出到浏览器
  332. }
  333. /**
  334. * 导入
  335. * @auth true
  336. * @menu true
  337. * @throws \think\Exception
  338. * @throws \think\db\exception\DataNotFoundException
  339. * @throws \think\db\exception\ModelNotFoundException
  340. * @throws \think\exception\DbException
  341. * @throws \think\exception\PDOException
  342. */
  343. public function import()
  344. {
  345. $file = request()->file('file');
  346. $file_size = $_FILES['file']['size'];
  347. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  348. //限制上传表格类型
  349. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  350. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  351. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload';
  352. if (!file_exists($dir)) mkdir($dir, 0777, true);
  353. $info = $file->move($dir);
  354. $fileName = $info->getSaveName();
  355. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}";
  356. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  357. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');
  358. */
  359. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  360. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  361. $highestRow = $sheet->getHighestRow(); // 取得总行数
  362. $arr = array('A','B','C','D','E','F');
  363. // 一次读取一列
  364. $res_arr = [];
  365. for ($row = 2; $row <= $highestRow; $row++) {
  366. $row_arr = array();
  367. for ($column = 0 ;$column < count($arr) ; $column++) {
  368. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  369. $row_arr[] = $val;
  370. }
  371. $res_arr[] = $row_arr;
  372. }
  373. $success_num = 0;
  374. // name account account_type level_id days label
  375. foreach ($res_arr as $new_user) {
  376. if(!trim($new_user['1']) || empty($new_user['1']))continue;
  377. $check_where=[];
  378. $check_where[] = $new_user['2'] == 1 ? ['email','=',trim($new_user['1'])]: ['phone','=',trim($new_user['1'])];
  379. $ck_res = User::where($check_where)->value('id');
  380. if($ck_res){
  381. Data::save('UserLevelRank',[
  382. 'user_id'=>$ck_res,
  383. 'level_id'=>intval($new_user['3']),
  384. 'start_time'=>time(),
  385. 'end_time'=>time()+86400* intval($new_user['4']),
  386. 'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))],'user_id',['user_id'=>$ck_res]);
  387. $success_num++;
  388. User::where('id',$ck_res)->update(['level_id'=>intval($new_user['3']),'level_exp'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4'])),'label'=>'|'.$new_user['5'].'|']);
  389. }else{
  390. $user_add= [];
  391. $user_add['account_type'] = $new_user['2'];
  392. if($new_user['5']) $user_add['label'] = '|'.$new_user['5'].'|';
  393. if( $user_add['account_type'] == 1) {// 公司账号
  394. $group_title = explode('@', $new_user['1'])[1];
  395. $group_info = UserGroup::where(['title'=>'@'.$group_title])->find();
  396. if(empty($group_info)) continue;
  397. $user_add['name'] = 'G企业用户_'.$group_info->name.'_'.$group_title;
  398. $check_group = User::where(['group_id'=>$group_info->id,'is_first'=>1])->value('id');//该公司是否有第一个注册的
  399. $user_add['group_id'] = $group_info->id;
  400. if(!$check_group) $user_add['is_first'] =1;
  401. if($check_group) $user_add['group_first'] = $check_group;
  402. $add_res = User::create($user_add);
  403. if(isset_full_check($user_add,'is_first',1)){
  404. User::where('id',$add_res->id)->update(['group_first'=>$add_res->id]);
  405. if($new_user['3'] > 0 && intval($new_user[4]) > 0) {
  406. UserLevelRank::create(['user_id'=>$add_res->id,'level_id'=>$new_user['3'],'start_time'=>time(),'end_time'=>time()+86400* intval($new_user['4']),'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))]);
  407. User::where('id',$add_res->id)->update(['level_id'=>intval($new_user['3']),'level_exp'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))]);
  408. $success_num++;
  409. }
  410. }
  411. }else{
  412. $user_add['phone'] = trim($new_user['1']);
  413. $user_add['name'] = 'G'. substr_replace( $new_user['1'],'****',3,4);
  414. $add_res = User::create($user_add);
  415. if($new_user['3'] > 0 && intval($new_user[4]) > 0) {
  416. UserLevelRank::create(['user_id'=>$add_res->id,'level_id'=>$new_user['3'],'start_time'=>time(),'end_time'=>time()+86400* intval($new_user['4']),'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))]);
  417. User::where('id',$add_res->id)->update(['level_id'=>intval($new_user['3']),'level_exp'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))]);
  418. $success_num++;
  419. }
  420. }
  421. }
  422. }
  423. $this->success('成功导入会员:'.$success_num);
  424. }
  425. }