Staff.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\library\Wechat;
  4. use addons\qingdongams\library\wx\WxBizDataCrypt;
  5. use addons\qingdongams\model\Contract;
  6. use addons\qingdongams\model\Customer;
  7. use addons\qingdongams\model\Flow;
  8. use addons\qingdongams\model\Receivables;
  9. use addons\qingdongams\model\Staff as StaffModel;
  10. use addons\qingdongams\model\StaffDepartment;
  11. use addons\qingdongams\model\StaffSignIn;
  12. use addons\qingdongams\model\Achievement;
  13. use addons\qingdongams\model\Workorder;
  14. use addons\qingdongams\model\WorkReport;
  15. use app\admin\model\AuthGroup;
  16. use fast\Http;
  17. use think\Exception;
  18. use think\Log;
  19. use think\Validate;
  20. /**
  21. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  22. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  23. * @desc 售后微信:qingdong_crm
  24. */
  25. /**
  26. * 员工登录 注册接口
  27. */
  28. class Staff extends StaffApi
  29. {
  30. protected $noNeedLogin = ['test', 'wxLogin','login','sendSms','mobileLogin','logintest'];
  31. protected $noNeedRight = ['getInfo', 'editInfo', "use_phone"];
  32. public function _initialize()
  33. {
  34. parent::_initialize();
  35. }
  36. //是否登录
  37. public function isLogin()
  38. {
  39. $this->success('已登录', ['userinfo' => $this->auth->getUserinfo()]);
  40. }
  41. //获取销售信息
  42. public function getStaffInfo()
  43. {
  44. $staff=StaffModel::where(['id'=>$this->auth->id])->find();
  45. $this->success('已登录', ['userinfo' => $staff]);
  46. }
  47. /**
  48. * 会员登录
  49. *
  50. * @ApiMethod (POST)
  51. * @param string $account 账号
  52. * @param string $password 密码
  53. */
  54. public function login()
  55. {
  56. $account = $this->request->post('account');
  57. $password = $this->request->post('password');
  58. if (!$account || !$password) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. $ret = $this->auth->login($account, $password);
  62. if ($ret) {
  63. $data = ['userinfo' => $this->auth->getUserinfo()];
  64. $this->success(__('Logged in successful'), $data);
  65. } else {
  66. $this->error($this->auth->getError());
  67. }
  68. }
  69. /**
  70. * 会员手机号登录
  71. *
  72. * @ApiMethod (POST)
  73. * @param string $account 账号
  74. * @param string $password 密码
  75. */
  76. public function mobileLogin()
  77. {
  78. $mobile = $this->request->post('mobile');
  79. $code = $this->request->post('code');
  80. $mobilecode = $this->request->post('mobilecode');
  81. if (empty($mobile)) {
  82. $this->error('手机号不能为空');
  83. }
  84. if (empty($code)) {
  85. $this->error('微信code不能为空');
  86. }
  87. $cacheCode = cache($mobile . '_code');
  88. if (empty($cacheCode)) {
  89. $this->error('请先发送验证码');
  90. }
  91. if ($mobilecode != $cacheCode) {
  92. $this->error('验证码错误');
  93. }
  94. $staff = StaffModel::get(['mobile' => $mobile]);
  95. if (!$staff) {
  96. $this->error('账户不正确');
  97. }
  98. if ($staff->status != 1) {
  99. $this->error('账户已经被锁定');
  100. }
  101. //直接登录会员
  102. $this->auth->direct($staff->id);
  103. $wechat = new Wechat('wxMiniProgram');
  104. $decryptSession = $wechat->code($code);
  105. if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
  106. $this->error(__('登录失败!'));
  107. }
  108. $openid = $decryptSession['openid'];
  109. $unionid = $decryptSession['unionid'] ?? '';
  110. if (empty($staff['openid'])) {
  111. $userInfo = input('userInfo/a', '');
  112. if (empty($userInfo)) {
  113. $this->error('参数错误');
  114. }
  115. $update = [];
  116. $update['openid'] = $openid;
  117. $update['unionid'] = $unionid;
  118. $update['img'] = $userInfo['avatarUrl'];
  119. $update['nickname'] = $userInfo['nickName'];
  120. $update['status'] = 0;
  121. StaffModel::where(['id' => $this->auth->id])->update($update);
  122. $this->success('绑定账号成功,等待管理员审核', ['userinfo' => $this->auth->getUserinfo()], 405);
  123. }
  124. if ($openid != $staff['openid']) {
  125. $this->error('绑定微信账号不一致');
  126. }
  127. $data = ['userinfo' => $this->auth->getUserinfo()];
  128. $this->success(__('Logged in successful'), $data);
  129. }
  130. /**
  131. * 获取个人基本信息
  132. */
  133. public function getInfo()
  134. {
  135. $staff = StaffModel::where(['id' => $this->auth->id])->with(['parent'])->find();
  136. $this->success('请求成功', $staff);
  137. }
  138. /**
  139. * 修改个人基本信息
  140. */
  141. public function editInfo()
  142. {
  143. $name = input('name');
  144. $img = input('img');
  145. $email = input('email');
  146. $sex = input('sex');
  147. $num = input('num');
  148. $post = input('post');
  149. if (StaffModel::where(['id' => $this->auth->id])->update([
  150. 'name' => $name,
  151. 'img' => $img,
  152. 'email' => $email,
  153. 'sex' => $sex,
  154. 'num' => $num,
  155. 'post' => $post,
  156. 'updatetime' => time()
  157. ]) == false) {
  158. $this->error('修改失败');
  159. }
  160. $this->success('修改成功');
  161. }
  162. //绑定微信
  163. public function wechatBind(){
  164. $type=input('type');//applet小程序 web 网页授权
  165. $code = input('code');
  166. if($type == 'web'){
  167. $wechat = new Wechat('wxOfficialAccount');
  168. if(empty($wechat->config['app_id'])){
  169. $this->error('微信appid未设置');
  170. }
  171. $decryptSession = $wechat->getOfficialAccessToken($code);
  172. if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
  173. $this->error(__('解析失败!'));
  174. }
  175. $openid = $decryptSession['openid'];
  176. $unionid = $decryptSession['unionid'] ?? '';
  177. Log::info(json_encode($decryptSession,JSON_UNESCAPED_UNICODE));
  178. //判断用户是否存在
  179. $staff = StaffModel::where(['wx_openid' => $openid])->find();
  180. if($staff){
  181. $this->error('该微信号已绑定账号');
  182. }
  183. $staffModel=new StaffModel();
  184. $result=$staffModel->save(['wx_openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
  185. if($result){
  186. $this->success('绑定成功');
  187. }
  188. $this->error('绑定失败');
  189. }elseif($type == 'applet'){
  190. $wechat = new Wechat('wxMiniProgram');
  191. if(empty($wechat->config['app_id'])){
  192. $this->error('微信appid未设置');
  193. }
  194. $decryptSession = $wechat->code($code);
  195. if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
  196. $this->error(__('解析失败!'));
  197. }
  198. $openid = $decryptSession['openid'];
  199. $unionid = $decryptSession['unionid'] ?? '';
  200. //判断用户是否存在
  201. $staff = StaffModel::where(['openid' => $openid])->find();
  202. if($staff){
  203. $this->error('该微信号已绑定账号');
  204. }
  205. $staffModel=new StaffModel();
  206. $result=$staffModel->save(['openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
  207. if($result){
  208. $this->success('绑定成功');
  209. }
  210. $this->error('绑定失败');
  211. }
  212. }
  213. //解绑微信
  214. public function unboundWechat()
  215. {
  216. $staffId = $this->auth->id;
  217. $model = new StaffModel();
  218. $staff = $model->where(['id' => $staffId])->find();
  219. if (empty($staff)) {
  220. $this->error('员工不存在');
  221. }
  222. if ($model->isUpdate(true)->save(['id' => $staffId, 'wx_openid' => null,
  223. 'unionid' => null, 'openid' => null])) {
  224. $this->success('解绑微信成功');
  225. }
  226. $this->error('操作失败');
  227. }
  228. //授权手机号
  229. public function use_phone()
  230. {
  231. $post = $this->request->post();
  232. $code = $post['code'];
  233. $encryptedData = $post['encryptedData'];
  234. $iv = $post['iv'];
  235. $config = new Wechat('wxMiniProgram');
  236. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config->config['app_id'] . "&secret=" . $config->config['secret'] . "&js_code=" . $code . "&grant_type=authorization_code";
  237. $user = Http::get($url);
  238. $arr = json_decode($user, true);
  239. if (isset($arr['errcode']) && $arr['errcode'] != 0) {
  240. $this->error('微信小程序appid或secret错误!');
  241. }
  242. if (!isset($arr['openid']) || !isset($arr['session_key'])) {
  243. $this->error('授权失败,请重新授权1!');
  244. }
  245. $openId = $arr['openid'];
  246. $pc = new WxBizDataCrypt($config->config['app_id'], $arr['session_key']);
  247. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  248. if ($errCode != 0) {
  249. $this->error('授权失败,请重新授权2!');
  250. }
  251. $decryptUserInfo = json_decode($data, true);
  252. if (isset($decryptUserInfo['phoneNumber']) && !empty($decryptUserInfo['phoneNumber'])) {
  253. $user_id = $this->auth->id;
  254. $oldStaff= StaffModel::where(array('mobile' => $decryptUserInfo['phoneNumber']))->find();
  255. if($oldStaff && $oldStaff['id'] != $user_id){
  256. $this->error('手机号已存在!');
  257. }
  258. $oldStaff= StaffModel::where(array('mobile' => $decryptUserInfo['phoneNumber']))->find();
  259. if($oldStaff && empty($oldStaff['openid'])){
  260. StaffModel::where(['id'=>$oldStaff['id']])->update(['openid'=>$openId]);
  261. }
  262. $userInfo = StaffModel::where(array('id' => $user_id))->update(array('mobile' => $decryptUserInfo['phoneNumber']));
  263. if ($userInfo === false) {
  264. $this->error('绑定失败!');
  265. }
  266. $data = ['mobile' => $decryptUserInfo['phoneNumber']];
  267. $this->success('绑定成功', $data);
  268. } else {
  269. $this->error('授权失败!');
  270. }
  271. }
  272. /**
  273. * 修改密码
  274. */
  275. public function changepwd()
  276. {
  277. if ($this->request->isPost()) {
  278. $oldpassword = $this->request->post("oldpassword");
  279. $newpassword = $this->request->post("newpassword");
  280. $renewpassword = $this->request->post("renewpassword");
  281. $rule = [
  282. 'oldpassword' => 'require|length:6,30',
  283. 'newpassword' => 'require|length:6,30',
  284. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  285. ];
  286. $msg = [
  287. 'renewpassword.confirm' =>'两次输入的密码不一致'
  288. ];
  289. $data = [
  290. 'oldpassword' => $oldpassword,
  291. 'newpassword' => $newpassword,
  292. 'renewpassword' => $renewpassword,
  293. ];
  294. $field = [
  295. 'oldpassword' => '旧密码',
  296. 'newpassword' => '新密码',
  297. 'renewpassword' => '确认密码'
  298. ];
  299. $validate = new Validate($rule, $msg, $field);
  300. $result = $validate->check($data);
  301. if (!$result) {
  302. $this->error(__($validate->getError()));
  303. return false;
  304. }
  305. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  306. if ($ret) {
  307. $this->success('修改密码成功');
  308. } else {
  309. $this->error($this->auth->getError());
  310. }
  311. }
  312. }
  313. /**
  314. * 退出登录
  315. */
  316. public function logout()
  317. {
  318. //退出本站
  319. $this->auth->logout();
  320. $this->success(__('Logout successful'), url('user/index'));
  321. }
  322. //获取下级员工列表
  323. public function getStaffList()
  324. {
  325. $ids = StaffModel::getLowerStaffId();
  326. $staff = StaffModel::where([
  327. 'id' => ['in', $ids],
  328. 'status' => 1
  329. ])->with(['parent'])->field('id,name,nickname,img,num,mobile,post,parent_id')->select();
  330. $this->success('请求成功', $staff);
  331. }
  332. //获取团队列表
  333. public function getStaffTeamList()
  334. {
  335. $name = input('name','');
  336. $ids = StaffModel::getMyStaffIds();
  337. $where=[];
  338. if($name){
  339. $where['name'] = array('like','%'.$name.'%');
  340. }
  341. $staff = StaffModel::where([
  342. 'id' => ['in', $ids],
  343. 'status' => 1
  344. ])->where($where)->with(['parent'])->field('id,name,nickname,img,num,mobile,post,parent_id')->select();
  345. $this->success('请求成功', $staff);
  346. }
  347. //获取员工详情
  348. public function getStaffDetail()
  349. {
  350. $id = input('id');
  351. if (empty($id)) {
  352. $this->error('员工不存在');
  353. }
  354. $staff = StaffModel::with([ 'parent'])->where([
  355. 'id' => $id,
  356. ])->find();
  357. $roles = [1 => '最高管理员', 2 => '公司管理员', 3 => '部门经理', 4 => '部门主管', 5 => '组长', 6 => '员工'];
  358. $staff['role'] = $roles[$staff['role']] ?? '';
  359. $this->success('请求成功', $staff);
  360. }
  361. //待审核员工列表
  362. public function getCheckStaffList()
  363. {
  364. $staff = StaffModel::where([
  365. 'status' => 0,
  366. 'name' => ['neq', '']
  367. ])->field('id,name,nickname,img,num,mobile,post,status')->select();
  368. $this->success('请求成功', $staff);
  369. }
  370. //修改员工信息
  371. public function updateStaff()
  372. {
  373. $name = input('name', '');
  374. $img = input('img', '');
  375. $email = input('email', '');
  376. $sex = input('sex', 0);
  377. $num = input('num', '');
  378. $post = input('post', '');
  379. $department_id = input('department_id', 0);
  380. $parent_id = input('parent_id', 0);
  381. $id = input('id', 0, 'intval');
  382. if (empty($department_id)) {
  383. $this->error('请选择员工部门!');
  384. }
  385. if (StaffModel::where(['id' => $id])->update([
  386. 'name' => $name,
  387. 'img' => $img,
  388. 'email' => $email,
  389. 'sex' => $sex,
  390. 'num' => $num,
  391. 'post' => $post,
  392. 'parent_id' => $parent_id,
  393. 'department_id' => $department_id,
  394. 'updatetime' => time()
  395. ]) == false) {
  396. $this->error('修改失败');
  397. }
  398. $this->success('修改成功');
  399. }
  400. //审核员工成功
  401. public function checkStaffSuccess()
  402. {
  403. $name = input('name', '');
  404. $img = input('img', '');
  405. $email = input('email', '');
  406. $sex = input('sex', 0);
  407. $role = input('role', 0);
  408. $num = input('num', '');
  409. $post = input('post', '');
  410. $mobile = input('mobile', '');
  411. $department_id = input('department_id', 0);
  412. $parent_id = input('parent_id', 0);
  413. $id = input('id', 0, 'intval');
  414. if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
  415. $this->error('待审核员工不存在');
  416. }
  417. if (empty($department_id)) {
  418. $this->error('请选择员工部门!');
  419. }
  420. if (StaffModel::where(['id' => $id])->update([
  421. 'name' => $name,
  422. 'img' => $img,
  423. 'email' => $email,
  424. 'sex' => $sex,
  425. 'num' => $num,
  426. 'role' => $role,
  427. 'mobile' => $mobile,
  428. 'post' => $post,
  429. 'parent_id' => $parent_id,
  430. 'department_id' => $department_id,
  431. 'status' => 1,
  432. 'updatetime' => time()
  433. ]) == false) {
  434. $this->error('审核失败');
  435. }
  436. $this->success('审核成功');
  437. }
  438. //员工绑定信息
  439. public function staffBind(){
  440. $staff=StaffModel::get($this->auth->id);
  441. $data=[
  442. 'is_bind_wechat'=>$staff['openid']?1:($staff['wx_openid']?1:0)
  443. ];
  444. $this->success('请求成功',$data);
  445. }
  446. //审核员工拒绝
  447. public function checkStaffError()
  448. {
  449. $id = input('id');
  450. if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
  451. $this->error('待审核员工不存在');
  452. }
  453. if (StaffModel::where(['id' => $id, 'status' => 0])->update(['deletetime' => time()]) == false) {
  454. $this->error('拒绝失败');
  455. }
  456. $this->success('审核成功');
  457. }
  458. //获取员工统计
  459. public function getStaffStatistics()
  460. {
  461. $id = input('id');
  462. if (empty($id)) {
  463. $this->error('员工不存在');
  464. }
  465. $staff=StaffModel::where(['id'=>$id])->find();
  466. if (empty($staff)) {
  467. $this->error('员工不存在');
  468. }
  469. $times=input('times','');
  470. if(empty($times)){
  471. $this->error('筛选时间不能为空');
  472. }
  473. if ($times) {//创建时间
  474. $times = explode(',', $times);
  475. $date = strtotime($times[0]);
  476. //月底
  477. $endDate = strtotime('+1 month', strtotime(date($times[1] . '-1')));
  478. }
  479. //客户
  480. $customer = Customer::where([
  481. 'create_staff_id' => $id,
  482. 'createtime' => ['between', [$date, $endDate]],
  483. ])->count();
  484. //合同
  485. $contract = Contract::where([
  486. 'create_staff_id' => $id,
  487. 'createtime' => ['between', [$date, $endDate]],
  488. 'check_status' => 2
  489. ])->count();
  490. //订单金额 业绩
  491. $contract_money = Contract::where([
  492. 'create_staff_id' => $id,
  493. 'createtime' => ['between', [$date, $endDate]],
  494. 'check_status' => 2
  495. ])->sum('money');
  496. //回款次数
  497. $receivables = Receivables::where([
  498. 'create_staff_id' => $id,
  499. 'createtime' => ['between', [$date, $endDate]],
  500. 'check_status' => 2
  501. ])->count();
  502. //回款金额
  503. $receivables_money = Receivables::where([
  504. 'create_staff_id' => $id,
  505. 'createtime' => ['between', [$date, $endDate]],
  506. 'check_status' => 2
  507. ])->sum('money');
  508. //拜访次数
  509. $baifang = \addons\qingdongams\model\Event::where([
  510. 'type'=>3,
  511. 'staff_id' => $id,
  512. 'actual_end_time' => ['between', [$date, $endDate]],
  513. 'status'=>2
  514. ])->count();
  515. //销售
  516. $contractAchievement = Achievement::getStaffTeamAchievement(date('Y-m',$date),date('Y-m',$endDate),$staff,1);
  517. //回款
  518. $receivablesAchievement = Achievement::getStaffTeamAchievement(date('Y-m',$date),date('Y-m',$endDate),$staff,2);
  519. //新建工单
  520. $workorder = Workorder::where([
  521. 'create_staff_id' => $id,
  522. 'createtime' => ['between', [$date, $endDate]]
  523. ])->count();
  524. //完成工单数量
  525. $completeWorkorder = Workorder::where([
  526. 'owner_staff_id' => $id,
  527. 'status' => 3,
  528. 'end_time' => ['between', [$date, $endDate]]
  529. ])->count();
  530. //好评工单
  531. $goodWorkorder = Workorder::where([
  532. 'comment_score' => ['in', [4, 5]],
  533. 'owner_staff_id' => $id,
  534. 'status' => 3,
  535. 'end_time' => ['between', [$date, $endDate]]
  536. ])->count();
  537. //工单金额
  538. $workorderMoney = Workorder::where([
  539. 'comment_score' => ['in', [4, 5]],
  540. 'owner_staff_id' => $id,
  541. 'status' => 3,
  542. 'end_time' => ['between', [$date, $endDate]]
  543. ])->sum('money');
  544. $this->success('请求成功', [
  545. 'customer' => $customer,//获客数量
  546. 'baifang' => $baifang,//拜访客户次数
  547. 'contract' => $contract,//订单数量
  548. 'receivables' => $receivables,//回款次数
  549. 'contractAchievement' => $contractAchievement ?: 0,//业绩目标
  550. 'contract_money' => $contract_money,//订单金额
  551. 'receivablesAchievement' => $receivablesAchievement ?: 0,//回款目标
  552. 'receivables_money' => $receivables_money,//回款金额
  553. 'workorder' => $workorder,//新建工单
  554. 'completeWorkorder' => $completeWorkorder,//完成工单数量
  555. 'goodWorkorder' => $goodWorkorder,//好评工单
  556. 'workorderMoney' => $workorderMoney,//工单金额
  557. ]);
  558. }
  559. //获取工单报告
  560. public function getWorkReport(){
  561. $id = input('id');
  562. if (empty($id)) {
  563. $this->error('员工不存在');
  564. }
  565. $staff=StaffModel::where(['id'=>$id])->find();
  566. if (empty($staff)) {
  567. $this->error('员工不存在');
  568. }
  569. $times=input('times','');
  570. if(empty($times)){
  571. $this->error('筛选时间不能为空');
  572. }
  573. if ($times) {//创建时间
  574. $times = explode(',', $times);
  575. $date = date('Y-m-d',strtotime($times[0])) ;
  576. //月底
  577. $endDate = date('Y-m-d',strtotime('+1 month', strtotime(date($times[1] . '-1'))));
  578. }
  579. $where=[
  580. 'staff_id' => $id,
  581. 'report_date' => ['between', [$date, $endDate]],
  582. ];
  583. $workreport= WorkReport::where($where)->paginate();
  584. $this->success('请求成功',$workreport);
  585. }
  586. //获取员工所属客户
  587. public function getStaffCustomer()
  588. {
  589. $id = input('id');
  590. if (empty($id)) {
  591. $this->error('员工不存在');
  592. }
  593. $limit = input("limit/d", 10);
  594. $where = [];
  595. $where['owner_staff_id'] = $id;
  596. $list = Customer::where($where)->with([
  597. 'ownerStaff',
  598. 'contacts'
  599. ])->field('id,name,next_time,owner_staff_id,level,follow')->order('id desc')->paginate($limit);
  600. $this->success('请求成功', $list);
  601. }
  602. //获取部门列表
  603. public function getDepartment()
  604. {
  605. $department = AuthGroup::where([])->field('id,name,pid')->select();
  606. $data = [];
  607. foreach ($department as $v) {
  608. $data[$v['pid']][] = $v;
  609. }
  610. $data = self::getChilds($data, 0);
  611. $this->success('请求成功', $data);
  612. }
  613. //生成树状结构
  614. private static function getChilds($data, $pid) {
  615. $list = [];
  616. if (isset($data[$pid])) {
  617. $list = $data[$pid];
  618. foreach ($list as $k => $v) {
  619. $v['children'] = self::getChilds($data, $v['id']);
  620. $list[$k] = $v;
  621. }
  622. }
  623. return $list;
  624. }
  625. //获取部门列表
  626. public function getDepartmentList()
  627. {
  628. $this->success('请求成功', AuthGroup::where([])->select());
  629. }
  630. //获取上级列表
  631. public function getParentList()
  632. {
  633. $id = input('id');
  634. if (empty($id)) {
  635. $this->error('参数错误');
  636. }
  637. $this->success('请求成功', StaffModel::getList( [$id]));
  638. }
  639. //获取角色列表
  640. public function getRoles()
  641. {
  642. //1最高管理员 2公司管理员 3部门经理 4部门主管 5组长 6员工
  643. $roles = [1 => '最高管理员', 2 => '公司管理员', 3 => '部门经理', 4 => '部门主管', 5 => '组长', 6 => '员工'];
  644. $data = [];
  645. foreach ($roles as $id => $name) {
  646. $data[] = ['id' => $id, 'name' => $name];
  647. }
  648. $this->success('请求成功', $data);
  649. }
  650. //获取用户上级IDs
  651. public function getStaffIds()
  652. {
  653. $type = input('type');
  654. if (empty($type)) {
  655. $this->success('请求成功', []);
  656. }
  657. $staff = StaffModel::where(['id' => ['in', 1 ]])->field('id,name,img')->select();
  658. $this->success('请求成功', $staff);
  659. }
  660. //获取员工地理位置
  661. public function getStaffPosition()
  662. {
  663. $list = StaffModel::where([])->field('id,name,nickname,img,post,mobile')->select();
  664. $list = collection($list)->toArray();
  665. foreach ($list as $k => $v) {
  666. $v['signin'] = StaffSignIn::where(['staff_id' => $v['id']])->limit(2)
  667. ->field('staff_id,location,lng,lat,createtime')->select();
  668. $list[$k] = $v;
  669. }
  670. $this->success('请求成功', $list);
  671. }
  672. //获取员工地理
  673. public function getStaffLocation()
  674. {
  675. $staff_id=input('id');
  676. if(empty($staff_id)){
  677. $this->error('参数不能为空');
  678. }
  679. $list = StaffSignIn::where(['staff_id' =>$staff_id,'lng'=>['neq','']])
  680. ->field('lng,lat')->order('id desc')->select();
  681. $this->success('请求成功', $list);
  682. }
  683. //获取全部员工地址
  684. public function getAllStaffLocation(){
  685. try{
  686. \think\Db::execute("SET @@sql_mode='';");
  687. $list = StaffSignIn::where(['lng'=>['neq','']])
  688. ->field('staff_id,lng,lat')
  689. ->with(['staff'])->group('staff_id')->order('id desc')->select();
  690. }catch (Exception $e){
  691. }
  692. $this->success('请求成功', $list);
  693. }
  694. //禁用账号
  695. public function disable_user()
  696. {
  697. $id = input('id');
  698. $status = 2;
  699. $model = new StaffModel();
  700. $staff = $model->where(['id' => $id])->find();
  701. if (empty($staff)) {
  702. $this->error('员工不存在');
  703. }
  704. if ($model->isUpdate(true)->save(['id' => $id, 'status' => $status])) {
  705. $this->success('操作成功');
  706. }
  707. $this->error('操作失败');
  708. }
  709. //多少任务没有完成,已用时多长时间, 完成了多少任务,用时多长时间,
  710. //获客多少,签单多少,回款多少,完成工单多少,花费费用多少
  711. //简报
  712. public function briefing()
  713. {
  714. $showStaffIds = StaffModel::getLowerStaffId();
  715. $staff = StaffModel::where(['id' => ['in', $showStaffIds]])->with([
  716. 'contract',
  717. 'receivables',
  718. 'customer',
  719. 'visit',
  720. 'workorder',
  721. 'consume',
  722. ])->select();
  723. $this->success('请求成功', $staff);
  724. }
  725. // 邮箱注册
  726. public function registerEmail(){
  727. $email=input('email','');
  728. $passcode=input('passcode','');
  729. if(!$email || !$passcode){
  730. $this->error('邮箱账号或授权码不能为空');
  731. }
  732. if (StaffModel::where(['id' => $this->auth->id])->update([
  733. 'email' => $email,
  734. 'email_code' => $passcode,
  735. 'updatetime' => time()
  736. ]) == false) {
  737. $this->error('修改失败');
  738. }
  739. $this->success('修改成功');
  740. }
  741. // 获取邮箱列表
  742. public function getEmails() {
  743. $name = input('name','');
  744. $where['email'] = [['not null',''],['neq',''],'and'];
  745. if(isset($name) && $name){
  746. $where['name|nickname'] = ['like','%'.$name.'%'];
  747. }
  748. $where['status'] = 1;
  749. $list = \addons\qingdongams\model\Staff::where($where)->field('id,name,nickname,email')->select();
  750. $this->success('',$list);
  751. }
  752. //直接体验
  753. public function logintest()
  754. {
  755. $staff = StaffModel::where([])->order('id asc')->find();
  756. if(!$staff){
  757. $this->error('员工不存在');
  758. }
  759. $stafflogin= $this->auth->direct($staff['id']);
  760. if ($stafflogin) {
  761. $data = ['userinfo' => $this->auth->getUserinfo()];
  762. $this->success(__('Logged in successful'), $data);
  763. } else {
  764. $this->error('登录失败');
  765. }
  766. }
  767. /**
  768. * 获取审批列表
  769. */
  770. public function getsteplist()
  771. {
  772. // record 跟进 contract 合同 consume 费用 receivables 回款 formapproval_1 审批
  773. $type = input('type');
  774. //获取审批
  775. $data = Flow::getsteplist($type);
  776. if (empty($data)) {
  777. $this->error('无可用审批流,请联系管理员');
  778. }
  779. $this->success('请求成功', $data);
  780. }
  781. /**
  782. * 获取审批详情
  783. */
  784. public function getstepdetail()
  785. {
  786. // record 跟进 contract 合同 consume 费用 receivables 回款
  787. $type = input('type');
  788. $relation_id=input('relation_id');
  789. $data = Flow::getstepdetail($type,$relation_id);
  790. if (empty($data)) {
  791. $this->success('请求成功', []);
  792. }
  793. $this->success('请求成功', $data);
  794. }
  795. }