Member.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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\cache\driver\Redis;
  25. use think\Db;
  26. use function AlibabaCloud\Client\value;
  27. /**
  28. * 会员信息管理
  29. * Class Member
  30. * @package app\Member\controller
  31. */
  32. class Member extends Controller
  33. {
  34. /**
  35. * 绑定数据表
  36. * @var string
  37. */
  38. protected $table = 'StoreMember';
  39. /**
  40. * 会员信息管理
  41. * @auth true
  42. * @menu true
  43. * @throws \think\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. * @throws \think\exception\PDOException
  48. */
  49. public function index()
  50. {
  51. $this->search_url = strtolower($this->request->controller()).'/index_search';
  52. $this->title = '会员信息管理';
  53. $this->tag_arr = UserTag::column('title','id');
  54. $this->level_arr = UserLevel::column('name','id');
  55. $where = [];
  56. $where[] = ['m.is_deleted','=',0];
  57. if($name = input('get.name')) $where[] = ['m.name','like','%'.$name.'%'];
  58. if($phone = input('get.phone')) $where[] = ['m.phone|m.email','like','%'.$phone.'%'];
  59. if($level_id = input('get.level_id')) $where[] = ['m.level_id','=',$level_id];
  60. if($label_id = input('get.label_id')) $where[] = ['m.label','like','%|'.$label_id."|%"];
  61. if($account_type = input('get.account_type')) {
  62. if($account_type == 3) {
  63. $where[] = ['m.is_first','=',1];
  64. }else{
  65. $where[] = ['m.account_type','=',$account_type];
  66. }
  67. }
  68. $field="m.id,m.openid,m.name,m.is_first,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";
  69. $create_at = input('create_at');
  70. $query = $this->_query($this->table)
  71. ->field($field)
  72. ->alias('m')
  73. ->leftJoin('UserWallet w','m.id =w.user_id')
  74. ->where($where)
  75. ->when($create_at,function ($q)use ($create_at){
  76. if($create_at){
  77. $time = explode(' - ',$_GET['create_at']);
  78. $start_date_time = $time[0];
  79. $end_date_time = $time[1];
  80. $q->whereBetweenTime('m.create_at',$start_date_time,$end_date_time);
  81. }
  82. })->order('id desc')->page();
  83. }
  84. /**
  85. * 数据列表处理
  86. * @auth true
  87. * @menu true
  88. * @param array $data
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @throws \think\exception\DbException
  92. */
  93. protected function _index_page_filter(&$data)
  94. {
  95. $level_arr = UserLevel::column('name','id');
  96. foreach ($data as $k=>&$v){
  97. $level_rank= UserLevelRank::getUserVipInfo($v['id']);
  98. $v['level_id'] = $level_rank ? $level_rank['level_id'] : 0 ;
  99. $v['level_name'] = $level_rank ? $level_arr[$level_rank['level_id']] : '--';
  100. $v['end_date'] = $level_rank ?$level_rank['end_date'] :'--';
  101. $label_ids = implode(',',explode("|",trim($v['label'],'|')));
  102. $v['label_arr'] = $v['label'] ? UserTag::where('id','in',$label_ids)->column('title') : [];
  103. }
  104. }
  105. /**
  106. * 删除
  107. * @auth true
  108. * @menu true
  109. * @param array $data
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. public function remove()
  115. {
  116. $this->_save($this->table, ['is_deleted' => '1','phone'=>'','email'=>'','bind_id'=>0,'is_first'=>0]);
  117. }
  118. /**
  119. * 禁用
  120. * @auth true
  121. * @menu true
  122. * @param array $data
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public function forbid()
  128. {
  129. $this->_save($this->table, ['status' => '0']);
  130. }
  131. /**
  132. * 启用
  133. * @auth true
  134. * @menu true
  135. * @param array $data
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. */
  140. public function resume()
  141. {
  142. $this->_save($this->table, ['status' => '1']);
  143. }
  144. /**
  145. * 添加
  146. * @auth true
  147. * @menu true
  148. * @param array $data
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. */
  153. public function add(){
  154. $this->title = '添加';
  155. $this->level_arr = UserLevel::column('name','id');
  156. if($this->request->isPost())
  157. {
  158. list($data) = [$this->request->post()];
  159. $check_where =[];
  160. $check_where[] = $data['account_type'] == 1 ? ['email','=',$data['account']] : ['phone','=',$data['account']];
  161. $user_info = User::where($check_where)->find();
  162. if($user_info) $this->error('用户已存在');
  163. $user_add= [];
  164. $data['account_type'] == 1 ? $user_add['email'] = $data['account'] : $user_add['phone'] = $data['account'];
  165. $user_add['name'] = $data['name'];
  166. $user_add['account_type'] = $data['account_type'];
  167. $select_label = [];
  168. if(isset($data['label']) && !empty($data['label'])){
  169. foreach ($data['label'] as $key=>$value){
  170. if($value) $select_label[] = $key;
  171. }
  172. }
  173. $data['label'] = '|'.implode('|',$select_label).'|';
  174. $new_user = User::create($user_add);
  175. if($data['level_id'] && intval($data['days']) > 0) {
  176. 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']))]);
  177. }
  178. $this->success('添加成功');
  179. }
  180. $this->_form($this->table, 'add');
  181. }
  182. /**
  183. *
  184. * 编辑
  185. * @auth true
  186. * @menu true
  187. * @param array $data
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. * @throws \think\exception\DbException
  191. */
  192. public function edit()
  193. {
  194. $this->title = '编辑';
  195. $this->level_arr = UserLevel::column('name','id');
  196. $this->_form($this->table, 'form');
  197. }
  198. /**
  199. * 钱包管理
  200. * @auth true
  201. * @menu true
  202. * @param array $data
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\ModelNotFoundException
  205. * @throws \think\exception\DbException
  206. */
  207. public function wallet()
  208. {
  209. $this->title = '钱包';
  210. $this->_form($this->table, 'wallet');
  211. }
  212. protected function _form_filter(&$data){
  213. //选中的服务标签
  214. $this->tag_arr = UserTag::column('title','id');
  215. if($this->request->isPost() && $this->request->action() == 'add')
  216. {
  217. if($data['account_type'] == 1) {
  218. $is_reg = User::where('email',$data['account'])->value('id');
  219. $data['email'] = $data['account'];
  220. }else{
  221. $is_reg = User::where('phone',$data['account'])->value('id');
  222. $data['phone'] = $data['account'];
  223. }
  224. if($is_reg)$this->error('账号已存在');
  225. }
  226. if($this->request->isPost() && $this->request->action() == 'edit') {
  227. if($data['level_id'] > 0 && $data['over_time']) {
  228. Data::save('UserLevelRank',[
  229. 'user_id'=>$data['id'],
  230. 'level_id'=>$data['level_id'],
  231. 'start_time'=>time(),
  232. 'end_time'=>strtotime($data['over_time']) ,
  233. 'end_date'=>$data['over_time']
  234. ],'user_id',['user_id'=>$data['id']]);
  235. $data['level_exp'] = $data['over_time'];
  236. } else if($data['level_id'] == 0){
  237. Data::save('UserLevelRank',[
  238. 'user_id'=>$data['id'],
  239. 'level_id'=>$data['level_id'],
  240. 'start_time'=>time(),
  241. 'end_time'=> time(),
  242. 'end_date'=>date('Y-m-d H:i:s')
  243. ],'user_id',['user_id'=>$data['id']]);
  244. $data['level_exp'] = date('Y-m-d H:i:s');
  245. }
  246. }
  247. //
  248. if($this->request->isPost()) {
  249. $select_label = [];
  250. if(isset($data['label']) && !empty($data['label'])){
  251. foreach ($data['label'] as $key=>$value){
  252. if($value) $select_label[] = $key;
  253. }
  254. }
  255. $data['label'] = '|'.implode('|',$select_label).'|';
  256. if(isset($data['bind_phone'])) {
  257. $bind_id = User::where('phone',$data['bind_phone'])->value('id');
  258. if(!$bind_id) $this->error('该用户未注册');
  259. $check_bind = User::where(['bind_id'=>$bind_id])->value('id');
  260. if($data['bind_phone'] && $check_bind) $this->error('该用户被绑定');
  261. $data['bind_id'] = $bind_id;
  262. }
  263. }
  264. if($this->request->isGet() && $this->request->action() =='edit') {
  265. $label_arr = isset($data['label']) ? explode('|',trim($data['label'],'|')) : [];
  266. $this->label_arr = $label_arr;
  267. $data['rank_id'] = UserLevelRank::getUserVip($data['id']);
  268. $level_rank= UserLevelRank::where([['user_id','=',$data['id']]])->find();
  269. $data['end_date'] = $level_rank ? $level_rank->end_date:'--';
  270. $data['bind_phone'] = $data['bind_id'] ? User::where('id',$data['bind_id'])->value('phone'):'';
  271. }
  272. }
  273. public function facility()
  274. {
  275. if($this->request->isGet())
  276. {
  277. $user_id = input('get.user_id');
  278. $list = UserFacility::where('user_id',$user_id)->column('facility','type');
  279. $this->fetch('', ['list' =>$list,'user_id'=>$user_id]);
  280. }else{
  281. list($data) = [$this->request->post()];
  282. for ($i=1;$i<=4;$i++) {
  283. // $check_facility = UserFacility::where(['user_id'=>$data['user_id'],'type'=>$i,'facility'=>$data['facility_'.$i]])->value('id');
  284. $redis = new Redis();
  285. if(isset($data['facility_'.$i])){
  286. Data::save(
  287. 'UserFacility',
  288. ['user_id'=>$data['user_id'],'type'=>$i,'facility'=>$data['facility_'.$i],'set_time'=>date('Y-m-d H:i:s')],
  289. 'user_id',['user_id'=>$data['user_id'],'type'=>$i]);
  290. if($data['facility_'.$i] == ''){
  291. $redis->rm('TOKEN_'.$data['user_id'].'_'.$i);
  292. // var_dump('TOKEN_'.$data['user_id'].'_'.$i);die;
  293. $aliases = $this->aliases($data['user_id'],[],'GET','?new_format=true');
  294. if(isset($aliases['data']) && $aliases['data'] != '' && $aliases['data'] != null){
  295. if(count($aliases['data']) > 0){
  296. //别名下有绑定的Registration ID
  297. $registration_ids = [];
  298. foreach ($aliases['data'] as $k => $v){
  299. if($i == 1 || $i == 3){
  300. //安卓
  301. if($v['platform'] == "android"){
  302. $registration_ids[] = $v['registration_id'];
  303. }
  304. }else if($i == 2 || $i == 4){
  305. //ios
  306. if($v['platform'] == "ios"){
  307. $registration_ids[] = $v['registration_id'];
  308. }
  309. }
  310. }
  311. if(count($registration_ids) > 0){
  312. $body = [
  313. "registration_ids"=>[
  314. "remove"=> $registration_ids
  315. ]
  316. ];
  317. //var_dump($body);die;
  318. // var_dump($registration_ids);die;
  319. $aliases_del = $this->aliases($data['user_id'],$body,'POST');
  320. // var_dump($aliases_del);die;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. $this->success('编辑成功');
  328. }
  329. }
  330. /**
  331. * @title 推送查询
  332. * @desc 推送查询
  333. */
  334. public function aliases($id = 66,$registration_ids = [],$type = 'GET',$new_format = ''){
  335. $data = input();
  336. // if(!$data['url'] || !$data['from'] || !$data['to'] || !$data['url']){
  337. //
  338. // }
  339. $arr = [
  340. 'url'=>'https://device.jpush.cn/v3/aliases/'.$id.$new_format,
  341. 'AppKey'=>config('app.jiguang')['AppKey'],
  342. 'MasterSecret'=>config('app.jiguang')['MasterSecret'],
  343. ];
  344. // $hx_voice = hx_voice(24);
  345. // $url= $arr['url'].$arr['org_name'].'/'.$arr['app_name'].'/'.'messages/users';
  346. // $body['from'] = $data['form']; //'member2200'
  347. // $body['to'] = $data['to']; //worker1000
  348. // $body['type'] = 'audio';
  349. // $body['body'] = [
  350. // 'filename' => $data['filename'], //'testaudio.amr', //语音文件的名称
  351. // 'url' => $data['url'] //'http://tmp/bQo7Udv1FETr01c4bb0ecee3a2edb86b00a393176fcc.durationTime=2790.aac', //语音文件的url地址
  352. // ];
  353. $body = $registration_ids;
  354. $options['type']="audio";
  355. $options['msg']='发送';
  356. $b=json_encode($body);
  357. // return $b;
  358. $str = $arr['AppKey'].':'.$arr['MasterSecret'];
  359. //$str = '7d431e42dfa6a6d693ac2d04:5e987ac6d2e04d95a9d8f0d1';
  360. $base64 = base64_encode($str);
  361. //$header=['Authorization' => ];
  362. $header = array("Authorization: Basic ".$base64);
  363. // return json($header);
  364. $result=$this->postCurl($arr['url'],$b,$header,$type);
  365. // $result=$this->postCurl($url,$body,$header);
  366. //dump($result);
  367. return $result;
  368. // return $hx_voice;
  369. }
  370. public function postCurl($url,$body,$header,$type="POST"){
  371. //1.创建一个curl资源
  372. $ch = curl_init();
  373. //2.设置URL和相应的选项
  374. curl_setopt($ch,CURLOPT_URL,$url);//设置url
  375. //1)设置请求头
  376. // array_push($header, 'Accept:application/json');
  377. // array_push($header,'Content-Type:application/json');
  378. // array_push($header, 'http:multipart/form-data');
  379. //设置为false,只会获得响应的正文(true的话会连响应头一并获取到)
  380. curl_setopt($ch,CURLOPT_HEADER,0);
  381. // curl_setopt ( $ch, CURLOPT_TIMEOUT,5); // 设置超时限制防止死循环
  382. //设置发起连接前的等待时间,如果设置为0,则无限等待。
  383. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
  384. //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  385. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  386. //2)设备请求体
  387. if (strlen($body)>0) {
  388. //$b=json_encode($body,true);
  389. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//全部数据使用HTTP协议中的"POST"操作来发送。
  390. }
  391. //设置请求头
  392. // if(count($header)>0){
  393. // curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  394. // }
  395. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  396. //上传文件相关设置
  397. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  398. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  399. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
  400. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算
  401. //3)设置提交方式
  402. switch($type){
  403. case "GET":
  404. curl_setopt($ch,CURLOPT_HTTPGET,true);
  405. break;
  406. case "POST":
  407. curl_setopt($ch,CURLOPT_POST,true);
  408. break;
  409. case "PUT"://使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。这对于执行"DELETE" 或者其他更隐蔽的HTT
  410. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");
  411. break;
  412. case "DELETE":
  413. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"DELETE");
  414. break;
  415. }
  416. //4)在HTTP请求中包含一个"User-Agent: "头的字符串。-----必设
  417. // curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
  418. // curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  419. //curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
  420. //5)
  421. //3.抓取URL并把它传递给浏览器
  422. $res=curl_exec($ch);
  423. $result=json_decode($res,true);
  424. //4.关闭curl资源,并且释放系统资源
  425. curl_close($ch);
  426. if(empty($result))
  427. return $res;
  428. else
  429. return $result;
  430. }
  431. public function export(){
  432. $tag_arr = UserTag::column('title','id');
  433. $get_data = $this->request->get();
  434. $time = explode(' - ',$get_data['create_at']);
  435. $phone = $get_data['phone'];
  436. $name = $get_data['name'];
  437. $where = [];
  438. $where[] = ['status',1];
  439. $where[] = ['is_deleted',0];
  440. $where_str = ' status = 1 AND is_deleted = 0';
  441. if($name) $where_str .=' AND name like '."'%".$name."%'";
  442. if($phone) $where_str .=' AND phone like '."'%".$phone."%'";
  443. if($level_id = input('get.level_id')) $where_str .=' AND level_id = '.$level_id;
  444. if($label_id = input('get.label_id')) $where_str .=' AND label like '."'%|".$label_id."|%'";
  445. if($get_data['create_at']) $where_str.=" AND create_at > '".$time[0]."'AND create_at <'".$time[1]."'";
  446. $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');
  447. if(empty($data)) $this->error('暂无可以导出的数据');
  448. foreach ($data as $k=>&$v) {
  449. if(!$v) $v = '--';
  450. $v['account_desc'] = $v['account_type'] == 1 ? '公司':'个人';
  451. $label_ids = implode(',',explode("|",trim($v['label'],'|')));
  452. $label_arr = $label_ids ? UserTag::where('id','in',$label_ids)->column('title') : [];
  453. $v['label_name'] = $label_arr ? implode('|',$label_arr) : "--";
  454. }
  455. $field=array(
  456. 'A' => array('name', '昵称'),
  457. 'B' => array('email', '邮箱'),
  458. 'C' => array('phone', '电话'),
  459. 'D' => array('account_desc','类型'),
  460. 'E' => array('label_name','标签'),
  461. 'F' => array('create_at', '注册时间'),
  462. );
  463. $this->phpExcelList($field,$data,'会员列表');
  464. }
  465. public function phpExcelList($field=[],$list=[],$title='文件'){
  466. $PHPExcel=new \PHPExcel();
  467. $PHPSheet=$PHPExcel->getActiveSheet();
  468. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  469. foreach($list as $key=>$value)
  470. {
  471. foreach($field as $k=>$v){
  472. if($key == 0){
  473. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  474. }
  475. $i=$key+2;
  476. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  477. }
  478. }
  479. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  480. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  481. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  482. header('Cache-Control: max-age=0'); //禁止缓存
  483. $PHPWriter->save("php://output"); //输出到浏览器
  484. }
  485. /**
  486. * 导入
  487. * @auth true
  488. * @menu true
  489. * @throws \think\Exception
  490. * @throws \think\db\exception\DataNotFoundException
  491. * @throws \think\db\exception\ModelNotFoundException
  492. * @throws \think\exception\DbException
  493. * @throws \think\exception\PDOException
  494. */
  495. public function import()
  496. {
  497. $file = request()->file('file');
  498. $file_size = $_FILES['file']['size'];
  499. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  500. //限制上传表格类型
  501. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  502. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  503. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload';
  504. if (!file_exists($dir)) mkdir($dir, 0777, true);
  505. $info = $file->move($dir);
  506. $fileName = $info->getSaveName();
  507. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}";
  508. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  509. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');
  510. */
  511. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  512. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  513. $highestRow = $sheet->getHighestRow(); // 取得总行数
  514. $arr = array('A','B','C','D','E','F');
  515. // 一次读取一列
  516. $res_arr = [];
  517. for ($row = 2; $row <= $highestRow; $row++) {
  518. $row_arr = array();
  519. for ($column = 0 ;$column < count($arr) ; $column++) {
  520. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  521. $row_arr[] = $val;
  522. }
  523. $res_arr[] = $row_arr;
  524. }
  525. $success_num = 0;
  526. // name account account_type level_id days label
  527. foreach ($res_arr as $new_user) {
  528. if(!trim($new_user['1']) || empty($new_user['1']))continue;
  529. $check_where=[];
  530. $check_where[] = $new_user['2'] == 1 ? ['email','=',trim($new_user['1'])]: ['phone','=',trim($new_user['1'])];
  531. $ck_res = User::where($check_where)->value('id');
  532. if($ck_res){
  533. Data::save('UserLevelRank',[
  534. 'user_id'=>$ck_res,
  535. 'level_id'=>intval($new_user['3']),
  536. 'start_time'=>time(),
  537. 'end_time'=>time()+86400* intval($new_user['4']),
  538. 'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))],'user_id',['user_id'=>$ck_res]);
  539. $success_num++;
  540. 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'].'|']);
  541. }else{
  542. $user_add= [];
  543. $user_add['account_type'] = $new_user['2'];
  544. if($new_user['5']) $user_add['label'] = '|'.$new_user['5'].'|';
  545. if( $user_add['account_type'] == 1) {// 公司账号
  546. $group_title = explode('@', $new_user['1'])[1];
  547. $group_info = UserGroup::where(['title'=>'@'.$group_title])->find();
  548. if(empty($group_info)) continue;
  549. $user_add['name'] = 'G企业用户_'.$group_info->name.'_'.$group_title;
  550. $check_group = User::where(['group_id'=>$group_info->id,'is_first'=>1])->value('id');//该公司是否有第一个注册的
  551. $user_add['group_id'] = $group_info->id;
  552. if(!$check_group) $user_add['is_first'] =1;
  553. if($check_group) $user_add['group_first'] = $check_group;
  554. $add_res = User::create($user_add);
  555. if(isset_full_check($user_add,'is_first',1)){
  556. User::where('id',$add_res->id)->update(['group_first'=>$add_res->id]);
  557. if($new_user['3'] > 0 && intval($new_user[4]) > 0) {
  558. 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']))]);
  559. 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']))]);
  560. $success_num++;
  561. }
  562. }
  563. }else{
  564. $user_add['phone'] = trim($new_user['1']);
  565. $user_add['name'] = 'G'. substr_replace( $new_user['1'],'****',3,4);
  566. $add_res = User::create($user_add);
  567. if($new_user['3'] > 0 && intval($new_user[4]) > 0) {
  568. 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']))]);
  569. 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']))]);
  570. $success_num++;
  571. }
  572. }
  573. }
  574. }
  575. $this->success('成功导入会员:'.$success_num);
  576. }
  577. }