Staff.php 30 KB

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