Member.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\constant\CommonConstant;
  16. use app\common\service\DingtalkService;
  17. use app\common\service\UserService;
  18. use app\common\model\User as model;
  19. use library\Controller;
  20. /**
  21. * 员工
  22. */
  23. class Member extends Controller
  24. {
  25. /**
  26. * 绑定数据表
  27. * @var string
  28. */
  29. protected $table = 'StoreMember';
  30. /**
  31. * 控制器初始化
  32. */
  33. protected function initialize()
  34. {
  35. $this->get_status_list = CommonConstant::get_status_list();
  36. $this->get_signature_status_list = CommonConstant::get_signature_status_list();
  37. }
  38. /**
  39. * 列表
  40. * @auth true
  41. * @menu true
  42. * @throws \think\Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function index()
  48. {
  49. $status = input('status');
  50. $signature_status = input('signature_status');
  51. $this->title = '员工列表';
  52. $query = $this->_query($this->table)
  53. ->field('is_deleted',true)
  54. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  55. ->when(array_key_exists($status, $this->get_status_list), function ($query) use ($status) {
  56. $query->where('status', $status);
  57. })
  58. ->when(array_key_exists($signature_status, $this->get_signature_status_list), function ($query) use ($signature_status) {
  59. $query->where('signature_status', $signature_status);
  60. })
  61. ->like('name,mobile,title');
  62. $query->page();
  63. }
  64. /**
  65. * 列表数据处理
  66. * @param array $data
  67. * @throws \Exception
  68. */
  69. protected function _index_page_filter(&$data)
  70. {
  71. if($data){
  72. $department_list = UserService::get_department_column();
  73. $this->user_list = UserService::get_list(1);
  74. $user_object = array_column($this->user_list->toArray(),null,'userid');
  75. foreach ($data as &$value) {
  76. // 所属部门
  77. $department_ids = explode(',',$value['department']);
  78. $department_text = '';
  79. foreach ($department_ids as $val){
  80. if(array_key_exists($val,$department_list)){
  81. $department_text .= $department_list[$val].',';
  82. }
  83. }
  84. $value['department_text'] = $department_text;
  85. // 员工的直属主管
  86. $manager_text = !empty($value['manager_userid']) && array_key_exists($value['manager_userid'],$user_object) ? $user_object[$value['manager_userid']]['name'] : '';
  87. $value['manager_text'] = $manager_text;
  88. }
  89. }
  90. }
  91. /**
  92. * 编辑
  93. * @auth true
  94. * @throws \think\Exception
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. * @throws \think\exception\PDOException
  99. */
  100. public function edit()
  101. {
  102. $this->title = '编辑';
  103. $this->_form($this->table, 'form');
  104. }
  105. /**
  106. * 表单处理
  107. * @param array $data
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. */
  112. protected function _form_filter(&$data)
  113. {
  114. if($this->request->isGet()) {
  115. if($data){
  116. // 编辑获取数据时
  117. $department_list = UserService::get_department_column();
  118. $this->user_list = UserService::get_list(1);
  119. // 所属部门
  120. $department_ids = explode(',', $data['department']);
  121. $department_text = '';
  122. foreach ($department_ids as $val) {
  123. if (array_key_exists($val, $department_list)) {
  124. $department_text .= $department_list[$val] . ',';
  125. }
  126. }
  127. $data['department_text'] = $department_text;
  128. }
  129. }
  130. if($this->request->isPost()){
  131. if($data) {
  132. // 编辑提交数据时
  133. if (isset_full($data, 'signature_status')) {
  134. if (!in_array($data['signature_status'], [CommonConstant::SIGNATURE_STATUS_3, CommonConstant::SIGNATURE_STATUS_4])) {
  135. unset($data['signature_status']);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * 启用
  143. * @auth true
  144. * @throws \think\Exception
  145. * @throws \think\exception\PDOException
  146. */
  147. public function resume()
  148. {
  149. $this->applyCsrfToken();
  150. $this->_save($this->table, ['status' => CommonConstant::STATUS_NORMAL]);
  151. }
  152. /**
  153. * 禁用
  154. * @auth true
  155. * @throws \think\Exception
  156. * @throws \think\exception\PDOException
  157. */
  158. public function forbid()
  159. {
  160. $this->applyCsrfToken();
  161. $this->_save($this->table, ['status' => CommonConstant::STATUS_FROZEN]);
  162. }
  163. /**
  164. * 审核签名
  165. * @auth true
  166. * @throws \think\Exception
  167. * @throws \think\exception\PDOException
  168. */
  169. public function signature_status()
  170. {
  171. $signature_status = input('signature_status');
  172. if(!array_key_exists($signature_status,CommonConstant::get_signature_status_list())){
  173. $this->controller->error('审核状态参数错误');
  174. }
  175. $this->applyCsrfToken();
  176. $this->_save($this->table, ['signature_status' => $signature_status]);
  177. }
  178. /**
  179. * 导出
  180. **/
  181. public function export(){
  182. $data = model::field('is_deleted',true)
  183. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  184. ->select()
  185. ->toArray();
  186. if(!$data) $this->error('暂无可以导出的数据');
  187. $field=array(
  188. 'A' => array('name', '用户姓名'),
  189. 'B' => array('mobile', '手机号码'),
  190. 'C' => array('title', '职位'),
  191. );
  192. $this->phpExcelList($field,$data,'会员列表');
  193. }
  194. public function phpExcelList($field=[],$list=[],$title='文件'){
  195. $PHPExcel=new \PHPExcel();
  196. $PHPSheet=$PHPExcel->getActiveSheet();
  197. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  198. foreach($list as $key=>$value)
  199. {
  200. foreach($field as $k=>$v){
  201. if($key == 0){
  202. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  203. }
  204. $i=$key+2;
  205. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  206. }
  207. }
  208. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  209. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  210. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  211. header('Cache-Control: max-age=0'); //禁止缓存
  212. $PHPWriter->save("php://output"); //输出到浏览器
  213. }
  214. /**
  215. * 更新员工和部门
  216. **/
  217. public function renew(){
  218. DingtalkService::renew();
  219. $this->success('更新员工和部门成功');
  220. }
  221. }