Member.php 12 KB

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