You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.php 7.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\library\Auth;
  4. use app\common\controller\Backend;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  7. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  8. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  9. use think\exception\PDOException;
  10. /**
  11. * 会员管理
  12. *
  13. * @icon fa fa-user
  14. */
  15. class User extends Backend
  16. {
  17. protected $relationSearch = true;
  18. /**
  19. * @var \app\admin\model\User
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('User');
  26. }
  27. public function import()
  28. {
  29. $file = $this->request->request('file');
  30. if (!$file) {
  31. $this->error(__('Parameter %s can not be empty', 'file'));
  32. }
  33. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  34. if (!is_file($filePath)) {
  35. $this->error(__('No results were found'));
  36. }
  37. //实例化reader
  38. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  39. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  40. $this->error(__('Unknown data format'));
  41. }
  42. if ($ext === 'csv') {
  43. $file = fopen($filePath, 'r');
  44. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  45. $fp = fopen($filePath, "w");
  46. $n = 0;
  47. while ($line = fgets($file)) {
  48. $line = rtrim($line, "\n\r\0");
  49. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  50. if ($encoding != 'utf-8') {
  51. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  52. }
  53. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  54. fwrite($fp, $line . "\n");
  55. } else {
  56. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  57. }
  58. $n++;
  59. }
  60. fclose($file) || fclose($fp);
  61. $reader = new Csv();
  62. } elseif ($ext === 'xls') {
  63. $reader = new Xls();
  64. } else {
  65. $reader = new Xlsx();
  66. }
  67. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  68. $fieldArr = [ 'nickname',
  69. 'username',
  70. 'password',
  71. 'email',
  72. 'floor'
  73. ];
  74. //加载文件
  75. $insert = [];
  76. try {
  77. if (!$PHPExcel = $reader->load($filePath)) {
  78. $this->error(__('Unknown data format'));
  79. }
  80. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  81. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  82. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  83. // $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  84. $fields = [
  85. 'nickname',
  86. 'username',
  87. 'password',
  88. 'email',
  89. 'floor'
  90. ];
  91. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  92. $values = [];
  93. $salt = \fast\Random::alnum();
  94. for ($currentColumn = 1; $currentColumn <= 5; $currentColumn++) {
  95. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  96. if ($currentColumn==3){
  97. //修改的是密码
  98. $val= \app\common\library\Auth::instance()->getEncryptPassword($val, $salt);
  99. }
  100. if ($currentColumn==5 && !$val){
  101. $val=-1;
  102. }
  103. if (!$val){
  104. continue 2;
  105. }
  106. $values[] = is_null($val) ? '' : $val;
  107. }
  108. // $row = [];
  109. $temp = array_combine($fields, $values);
  110. // foreach ($temp as $k => $v) {
  111. // if (isset($fieldArr[$k]) && $k !== '') {
  112. // $row[$fieldArr[$k]] = $v;
  113. // }
  114. // }
  115. // print_r($temp);
  116. // print_r($row);
  117. // if ($row) {
  118. $temp['status']="normal";
  119. $temp['salt']=$salt;
  120. $insert[] = $temp;
  121. // }
  122. }
  123. } catch (Exception $exception) {
  124. $this->error($exception->getMessage());
  125. }
  126. if (!$insert) {
  127. $this->error(__('No rows were updated'));
  128. }
  129. try {
  130. //是否包含admin_id字段
  131. $has_admin_id = false;
  132. foreach ($fieldArr as $name => $key) {
  133. if ($key == 'admin_id') {
  134. $has_admin_id = true;
  135. break;
  136. }
  137. }
  138. if ($has_admin_id) {
  139. $auth = Auth::instance();
  140. foreach ($insert as &$val) {
  141. if (!isset($val['admin_id']) || empty($val['admin_id'])) {
  142. $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
  143. }
  144. }
  145. }
  146. $this->model->saveAll($insert);
  147. } catch (PDOException $exception) {
  148. $msg = $exception->getMessage();
  149. if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
  150. $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
  151. };
  152. $this->error($msg);
  153. } catch (Exception $e) {
  154. $this->error($e->getMessage());
  155. }
  156. $this->success();
  157. }
  158. /**
  159. * 查看
  160. */
  161. public function index()
  162. {
  163. //设置过滤方法
  164. $this->request->filter(['strip_tags']);
  165. if ($this->request->isAjax()) {
  166. //如果发送的来源是Selectpage,则转发到Selectpage
  167. if ($this->request->request('keyField')) {
  168. return $this->selectpage();
  169. }
  170. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  171. $total = $this->model
  172. ->with('group')
  173. ->where($where)
  174. ->order($sort, $order)
  175. ->count();
  176. $list = $this->model
  177. ->with('group')
  178. ->where($where)
  179. ->order($sort, $order)
  180. ->limit($offset, $limit)
  181. ->select();
  182. foreach ($list as $k => $v) {
  183. $v->hidden(['password', 'salt']);
  184. }
  185. $result = array("total" => $total, "rows" => $list);
  186. return json($result);
  187. }
  188. return $this->view->fetch();
  189. }
  190. /**
  191. * 编辑
  192. */
  193. public function edit($ids = NULL)
  194. {
  195. $row = $this->model->get($ids);
  196. if (!$row)
  197. $this->error(__('No Results were found'));
  198. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  199. return parent::edit($ids);
  200. }
  201. }