Member.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\UserLevel;
  17. use app\common\model\UserLevelRank;
  18. use app\common\model\UserTag;
  19. use app\common\model\UserWallet;
  20. use library\Controller;
  21. use library\tools\Data;
  22. use think\Db;
  23. use function AlibabaCloud\Client\value;
  24. /**
  25. * 会员信息管理
  26. * Class Member
  27. * @package app\Member\controller
  28. */
  29. class Member extends Controller
  30. {
  31. /**
  32. * 绑定数据表
  33. * @var string
  34. */
  35. protected $table = 'StoreMember';
  36. /**
  37. * 会员信息管理
  38. * @auth true
  39. * @menu true
  40. * @throws \think\Exception
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. * @throws \think\exception\PDOException
  45. */
  46. public function index()
  47. {
  48. $this->search_url = strtolower($this->request->controller()).'/index_search';
  49. $this->title = '会员信息管理';
  50. $this->tag_arr = UserTag::column('title','id');
  51. $this->level_arr = UserLevel::column('name','id');
  52. $where = [];
  53. $where[] = ['m.is_deleted','=',0];
  54. if($name = input('get.name')) $where[] = ['m.name','like','%'.$name.'%'];
  55. if($phone = input('get.phone')) $where[] = ['m.phone','like','%'.$phone.'%'];
  56. if($level_id = input('get.level_id')) $where[] = ['m.level_id','=',$level_id];
  57. if($account_type = input('get.account_type')) $where[] = ['m.account_type','=',$account_type];
  58. $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";
  59. $query = $this->_query($this->table)
  60. ->field($field)
  61. ->alias('m')
  62. ->leftJoin('UserWallet w','m.id =w.user_id')
  63. ->where($where)
  64. ->dateBetween('create_at')->order('id desc')->page();
  65. }
  66. /**
  67. * 数据列表处理
  68. * @auth true
  69. * @menu true
  70. * @param array $data
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. protected function _index_page_filter(&$data)
  76. {
  77. $level_arr = UserLevel::column('name','id');
  78. foreach ($data as $k=>&$v){
  79. $level_rank= UserLevelRank::where([['user_id','=',$v['id']],['end_time','>',time()]])->order('level_id desc')->find();
  80. $v['level_id'] = $level_rank ? $level_rank->level_id : 0 ;
  81. $v['level_name'] = $level_rank ? $level_arr[$level_rank->level_id] : '--';
  82. $v['end_date'] = $level_rank ? $level_rank->end_date:'--';
  83. }
  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. public function remove()
  95. {
  96. $this->_save($this->table, ['is_deleted' => '1','phone'=>'','email'=>'']);
  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 forbid()
  108. {
  109. $this->_save($this->table, ['status' => '0']);
  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 resume()
  121. {
  122. $this->_save($this->table, ['status' => '1']);
  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 add(){
  134. $this->title = '添加';
  135. $this->level_arr = UserLevel::column('name','id');
  136. $this->tag_arr = UserTag::column('title','id');
  137. if($this->request->isPost())
  138. {
  139. list($data) = [$this->request->post()];
  140. $check_where =[];
  141. $check_where[] = $data['account_type'] == 1 ? ['email','=',$data['account']] : ['phone','=',$data['account']];
  142. $user_info = User::where($check_where)->find();
  143. if($user_info) $this->error('用户已存在');
  144. $user_add= [];
  145. $data['account_type'] == 1 ? $user_add['email'] = $data['account'] : $user_add['phone'] = $data['account'];
  146. $user_add['name'] = $data['name'];
  147. $user_add['account_type'] = $data['account_type'];
  148. $user_add['tag_id'] = $data['tag_id'];
  149. $new_user = User::create($user_add);
  150. if($data['level_id'] && intval($data['days']) > 0) {
  151. 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']))]);
  152. }
  153. $this->success('添加成功');
  154. }
  155. $this->_form($this->table, 'add');
  156. }
  157. /**
  158. *
  159. * 编辑
  160. * @auth true
  161. * @menu true
  162. * @param array $data
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. */
  167. public function edit()
  168. {
  169. $this->title = '编辑';
  170. $this->tag_arr = UserTag::column('title','id');
  171. $this->level_arr = UserLevel::column('name','id');
  172. $this->_form($this->table, 'form');
  173. }
  174. /**
  175. * 钱包管理
  176. * @auth true
  177. * @menu true
  178. * @param array $data
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. */
  183. public function wallet()
  184. {
  185. $this->title = '钱包';
  186. $this->_form($this->table, 'wallet');
  187. }
  188. protected function _form_filter(&$data){
  189. if($this->request->isPost() && $this->request->action() == 'add')
  190. {
  191. if($data['account_type'] == 1) {
  192. $is_reg = User::where('email',$data['account'])->value('id');
  193. $data['email'] = $data['account'];
  194. }else{
  195. $is_reg = User::where('phone',$data['account'])->value('id');
  196. $data['phone'] = $data['account'];
  197. }
  198. if($is_reg)$this->error('账号已存在');
  199. }
  200. if($this->request->isPost() && $this->request->action() == 'edit') {
  201. if($data['level_id'] > 0 && $data['over_time']) {
  202. Data::save('UserLevelRank',[
  203. 'user_id'=>$data['id'],
  204. 'level_id'=>$data['level_id'],
  205. 'start_time'=>time(),
  206. 'end_time'=>strtotime($data['over_time']) ,
  207. 'end_date'=>$data['over_time']
  208. ],'user_id',['user_id'=>$data['id']]);
  209. $data['level_exp'] = $data['over_time'];
  210. } else if($data['level_id'] == 0){
  211. Data::save('UserLevelRank',[
  212. 'user_id'=>$data['id'],
  213. 'level_id'=>$data['level_id'],
  214. 'start_time'=>time(),
  215. 'end_time'=> time(),
  216. 'end_date'=>date('Y-m-d H:i:s')
  217. ],'user_id',['user_id'=>$data['id']]);
  218. $data['level_exp'] = date('Y-m-d H:i:s');
  219. }
  220. }
  221. if($this->request->isGet() && $this->request->action() =='edit') {
  222. $data['rank_id'] = UserLevelRank::getUserVip($data['id']);
  223. }
  224. }
  225. public function export(){
  226. $tag_arr = UserTag::column('title','id');
  227. $get_data = $this->request->get();
  228. $time = explode(' - ',$get_data['create_at']);
  229. $phone = $get_data['phone'];
  230. $name = $get_data['name'];
  231. $where = [];
  232. $where[] = ['status',1];
  233. $where[] = ['is_deleted',0];
  234. $where_str = ' status = 1 AND is_deleted = 0';
  235. if($name) $where_str .=' AND name like '."'%".$name."%'";
  236. if($phone) $where_str .=' AND phone like '."'%".$phone."%'";
  237. if($get_data['create_at']) $where_str.=" AND create_at > '".$time[0]."'AND create_at <'".$time[1]."'";
  238. $data = Db::query("SELECT name,headimg,email,tag_id,phone,create_at,account_type FROM dd_store_member WHERE".$where_str.' ORDER BY id DESC');
  239. if(empty($data)) $this->error('暂无可以导出的数据');
  240. foreach ($data as $k=>&$v) {
  241. if(!$v) $v = '--';
  242. $v['account_desc'] = $v['account_type'] == 1 ? '公司':'个人';
  243. $v['label_name'] = $v['tag_id'] ? $tag_arr[$v['tag_id']]:'--';
  244. }
  245. $field=array(
  246. 'A' => array('name', '昵称'),
  247. 'B' => array('email', '邮箱'),
  248. 'C' => array('phone', '电话'),
  249. 'D' => array('account_desc','类型'),
  250. 'E' => array('label_name','标签'),
  251. 'F' => array('create_at', '注册时间'),
  252. );
  253. $this->phpExcelList($field,$data,'会员列表');
  254. }
  255. public function phpExcelList($field=[],$list=[],$title='文件'){
  256. $PHPExcel=new \PHPExcel();
  257. $PHPSheet=$PHPExcel->getActiveSheet();
  258. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  259. foreach($list as $key=>$value)
  260. {
  261. foreach($field as $k=>$v){
  262. if($key == 0){
  263. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  264. }
  265. $i=$key+2;
  266. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  267. }
  268. }
  269. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  270. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  271. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  272. header('Cache-Control: max-age=0'); //禁止缓存
  273. $PHPWriter->save("php://output"); //输出到浏览器
  274. }
  275. /**
  276. * 导入
  277. * @auth true
  278. * @menu true
  279. * @throws \think\Exception
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. * @throws \think\exception\DbException
  283. * @throws \think\exception\PDOException
  284. */
  285. public function import()
  286. {
  287. $file = request()->file('file');
  288. $file_size = $_FILES['file']['size'];
  289. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  290. //限制上传表格类型
  291. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  292. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls格式!');
  293. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload';
  294. if (!file_exists($dir)) mkdir($dir, 0777, true);
  295. $info = $file->move($dir);
  296. $fileName = $info->getSaveName();
  297. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}";
  298. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  299. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');
  300. */
  301. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  302. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  303. $highestRow = $sheet->getHighestRow(); // 取得总行数
  304. $arr = array('A','B','C','D','E');
  305. // 一次读取一列
  306. $res_arr = [];
  307. for ($row = 2; $row <= $highestRow; $row++) {
  308. $row_arr = array();
  309. for ($column = 0 ;$column < count($arr) ; $column++) {
  310. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  311. $row_arr[] = $val;
  312. }
  313. $res_arr[] = $row_arr;
  314. }
  315. $success_num = 0;
  316. // name account account_type level_id days
  317. foreach ($res_arr as $new_user) {
  318. $check_where[] = $new_user['2'] == 1 ? ['email','=',trim($new_user['1'])]: ['phone','=',trim($new_user['1'])];
  319. $ck_res = User::where($check_where)->value('id');
  320. if($ck_res){
  321. Data::save('UserLevelRank',[
  322. 'user_id'=>$ck_res,
  323. 'level_id'=>intval($new_user['3']),
  324. 'start_time'=>time(),
  325. 'end_time'=>time()+86400* intval($new_user['4']),
  326. 'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))],'user_id',['user_id'=>$ck_res]);
  327. $success_num++;
  328. User::where('id',$ck_res)->update(['level_id'=>intval($new_user['3']),'level_exp'=>time()+86400* intval($new_user['4'])]);
  329. continue;
  330. }
  331. $user_add= [];
  332. $new_user['2'] == 1 ? $user_add['email'] = trim($new_user['1']) : $user_add['phone'] = trim($new_user['1']);
  333. $user_add['name'] = $new_user['0'];
  334. $user_add['account_type'] = $new_user['2'];
  335. $add_res = User::create($user_add);
  336. if($new_user['3'] > 0 && intval($new_user[4]) > 0) {
  337. 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']))]);
  338. $success_num++;
  339. }
  340. }
  341. $this->success('成功导入会员:'.$success_num);
  342. }
  343. }