Member.php 32 KB

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