Staff.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Contacts;
  4. use addons\qingdong\model\Contract;
  5. use addons\qingdong\model\Customer;
  6. use addons\qingdong\model\Flow;
  7. use addons\qingdong\model\Leads;
  8. use addons\qingdong\model\Receivables;
  9. use addons\qingdong\model\Remind;
  10. use addons\qingdong\model\Staff as StaffModel;
  11. use addons\qingdong\model\StaffDepartment;
  12. use addons\qingdong\library\Wechat;
  13. use addons\qingdong\model\Achievement;
  14. use addons\qingdong\model\Record;
  15. use fast\Http;
  16. use think\Exception;
  17. use think\Log;
  18. use think\Validate;
  19. /**
  20. * * 操作文档:https://doc.fastadmin.net/qingdong
  21. * 软件介绍:https://www.fastadmin.net/store/qingdong.html
  22. * 售后微信:qingdong_crm
  23. * 员工登录 注册接口
  24. */
  25. class Staff extends StaffApi
  26. {
  27. protected $noNeedLogin = ['test', 'wxLogin', 'login','logintest'];
  28. protected $noNeedRight = ['getInfo', 'editInfo', "use_phone"];
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. }
  33. //是否登录
  34. public function isLogin()
  35. {
  36. $this->success('已登录', ['userinfo' => $this->auth->getUserinfo()]);
  37. }
  38. /**
  39. * 会员登录
  40. *
  41. * @ApiMethod (POST)
  42. * @param string $account 账号
  43. * @param string $password 密码
  44. */
  45. public function login()
  46. {
  47. $account = $this->request->post('account');
  48. $password = $this->request->post('password');
  49. if (!$account || !$password) {
  50. $this->error(__('Invalid parameters'));
  51. }
  52. $ret = $this->auth->login($account, $password);
  53. if ($ret) {
  54. $data = ['userinfo' => $this->auth->getUserinfo()];
  55. $this->success(__('Logged in successful'), $data);
  56. } else {
  57. $this->error($this->auth->getError());
  58. }
  59. }
  60. //获取个人基本信息
  61. public function getInfo()
  62. {
  63. $staff = StaffModel::where(['id' => $this->auth->id])->with(['department', 'parent'])->find();
  64. $group_ids=explode(',',$staff['group_ids']);
  65. $staff['role_type'] = 1;//基础和团队
  66. if (in_array(1, $group_ids)) {//超级管理员
  67. $staff['role_type'] = 9;
  68. }
  69. $this->success('请求成功', $staff);
  70. }
  71. //员工绑定信息
  72. public function staffBind(){
  73. $staff=StaffModel::get($this->auth->id);
  74. $data=[
  75. 'is_bind_wechat'=>$staff['openid']?1:($staff['wx_openid']?1:0)
  76. ];
  77. $this->success('请求成功',$data);
  78. }
  79. //绑定微信
  80. public function wechatBind(){
  81. $type=input('type');//applet小程序 web 网页授权
  82. $code = input('code');
  83. if($type == 'web'){
  84. $wechat = new Wechat('wxOfficialAccount');
  85. if(empty($wechat->config['app_id'])){
  86. $this->error('微信公众号appid未设置');
  87. }
  88. if(empty($wechat->config['secret'])){
  89. $this->error('微信公众号secret未设置');
  90. }
  91. $decryptSession = $wechat->getOfficialAccessToken($code);
  92. if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
  93. $this->error(__('解析失败!'));
  94. }
  95. $openid = $decryptSession['openid'];
  96. $unionid = $decryptSession['unionid'] ?? '';
  97. Log::info(json_encode($decryptSession,JSON_UNESCAPED_UNICODE));
  98. //判断用户是否存在
  99. $staff = StaffModel::where(['wx_openid' => $openid])->find();
  100. if($staff){
  101. $this->error('该微信号已绑定账号');
  102. }
  103. $staffModel=new StaffModel();
  104. $result=$staffModel->save(['wx_openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
  105. if($result){
  106. $this->success('绑定成功');
  107. }
  108. $this->error('绑定失败');
  109. }elseif($type == 'applet'){
  110. $wechat = new Wechat('wxMiniProgram');
  111. if(empty($wechat->config['app_id'])){
  112. $this->error('微信小程序appid未设置');
  113. }
  114. if(empty($wechat->config['secret'])){
  115. $this->error('微信小程序secret未设置');
  116. }
  117. $decryptSession = $wechat->code($code);
  118. if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
  119. $this->error(__('解析失败!'));
  120. }
  121. $openid = $decryptSession['openid'];
  122. $unionid = $decryptSession['unionid'] ?? '';
  123. //判断用户是否存在
  124. $staff = StaffModel::where(['openid' => $openid])->find();
  125. if($staff){
  126. $this->error('该微信号已绑定账号');
  127. }
  128. $staffModel=new StaffModel();
  129. $result=$staffModel->save(['openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
  130. if($result){
  131. $this->success('绑定成功');
  132. }
  133. $this->error('绑定失败');
  134. }
  135. }
  136. //解绑微信
  137. public function unboundWechat()
  138. {
  139. $staffId = $this->auth->id;
  140. $model = new StaffModel();
  141. $staff = $model->where(['id' => $staffId])->find();
  142. if (empty($staff)) {
  143. $this->error('员工不存在');
  144. }
  145. if ($model->isUpdate(true)->save(['id' => $staffId, 'wx_openid' => null,
  146. 'unionid' => null, 'openid' => null])) {
  147. $this->success('解绑微信成功');
  148. }
  149. $this->error('操作失败');
  150. }
  151. //修改个人基本信息
  152. public function editInfo()
  153. {
  154. $name = input('name');
  155. $img = input('img');
  156. $email = input('email');
  157. $sex = input('sex');
  158. $num = input('num');
  159. $post = input('post');
  160. $mobile = input('mobile');
  161. $staffs = StaffModel::where(array('id' => $this->auth->id))->find();
  162. $staffs->mobile = $mobile;
  163. $staffs->img = $img;
  164. $staffs->email = $email;
  165. $staffs->sex = $sex;
  166. $staffs->num = $num;
  167. $staffs->post = $post;
  168. $staffs->updatetime = time();
  169. $result = $staffs->save();
  170. if (!$result) {
  171. $this->error('修改失败');
  172. }
  173. $this->success('修改成功');
  174. }
  175. //授权手机号
  176. public function use_phone()
  177. {
  178. $code = $this->request->post('code','',null);
  179. $encryptedData = $this->request->post('encryptedData','',null);
  180. $iv = $this->request->post('iv','',null);
  181. $config = new Wechat('wxMiniProgram');
  182. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config->config['app_id'] . "&secret=" . $config->config['secret'] . "&js_code=" . $code . "&grant_type=authorization_code";
  183. $user = curl_getinfo($url);
  184. $arr = json_decode($user, true);
  185. if (isset($arr['errcode']) && $arr['errcode'] != 0) {
  186. $this->error('操作失败!');
  187. }
  188. if (!isset($arr['openid']) || !isset($arr['session_key'])) {
  189. $this->error('授权失败,请重新授权!');
  190. }
  191. $openId = $arr['openid'];
  192. $pc = new Wxbizdatacrypt($config->config['app_id'], $arr['session_key']);
  193. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  194. if ($errCode != 0) {
  195. $this->error('授权失败,请重新授权!');
  196. }
  197. $decryptUserInfo = json_decode($data, true);
  198. if (isset($decryptUserInfo['phoneNumber']) && !empty($decryptUserInfo['phoneNumber'])) {
  199. $user_id = $this->auth->id;
  200. $userInfo = StaffModel::where(array('id' => $user_id))->update(array('mobile' => $decryptUserInfo['phoneNumber']));
  201. if ($userInfo === false) {
  202. $this->error('绑定失败!');
  203. }
  204. $data = ['mobile' => $decryptUserInfo['phoneNumber']];
  205. $this->success('绑定成功', $data);
  206. } else {
  207. $this->error('授权失败!');
  208. }
  209. }
  210. //获取下级员工列表
  211. public function getStaffList()
  212. {
  213. $ids = StaffModel::getLowerStaffId();
  214. $staff = StaffModel::where([
  215. 'id' => ['in', $ids],
  216. 'status' => 1
  217. ])->with(['parent'])->field('id,name,nickname,img,num,mobile,post,parent_id')->select();
  218. $this->success('请求成功', $staff);
  219. }
  220. //获取员工详情
  221. public function getStaffDetail()
  222. {
  223. $id = input('id');
  224. if (empty($id)) {
  225. $this->error('员工不存在');
  226. }
  227. $staff = StaffModel::with(['department', 'parent'])->where([
  228. 'id' => $id,
  229. ])->find();
  230. $this->success('请求成功', $staff);
  231. }
  232. //待审核员工列表
  233. public function getCheckStaffList()
  234. {
  235. $staff = StaffModel::where([
  236. 'status' => 0,
  237. 'name' => ['neq', '']
  238. ])->field('id,name,nickname,img,num,mobile,post,status')->select();
  239. $this->success('请求成功', $staff);
  240. }
  241. //修改员工信息
  242. public function updateStaff()
  243. {
  244. $name = input('name', '');
  245. $img = input('img', '');
  246. $email = input('email', '');
  247. $sex = input('sex', 0);
  248. $num = input('num', '');
  249. $post = input('post', '');
  250. $parent_id = input('parent_id', 0);
  251. $id = input('id', 0, 'intval');
  252. if (StaffModel::where(['id' => $id])->update([
  253. 'name' => $name,
  254. 'img' => $img,
  255. 'email' => $email,
  256. 'sex' => $sex,
  257. 'num' => $num,
  258. 'post' => $post,
  259. 'parent_id' => $parent_id,
  260. 'updatetime' => time()
  261. ]) == false) {
  262. $this->error('修改失败');
  263. }
  264. $this->success('修改成功');
  265. }
  266. //审核员工成功
  267. public function checkStaffSuccess()
  268. {
  269. $name = input('name', '');
  270. $img = input('img', '');
  271. $email = input('email', '');
  272. $sex = input('sex', 0);
  273. $role = input('role', 0);
  274. $num = input('num', '');
  275. $post = input('post', '');
  276. $mobile = input('mobile', '');
  277. $department_id = input('department_id', 0);
  278. $parent_id = input('parent_id', 0);
  279. $id = input('id', 0, 'intval');
  280. if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
  281. $this->error('待审核员工不存在');
  282. }
  283. if (empty($department_id)) {
  284. $this->error('请选择员工部门!');
  285. }
  286. if (StaffModel::where(['id' => $id])->update([
  287. 'name' => $name,
  288. 'img' => $img,
  289. 'email' => $email,
  290. 'sex' => $sex,
  291. 'num' => $num,
  292. 'role' => $role,
  293. 'mobile' => $mobile,
  294. 'post' => $post,
  295. 'parent_id' => $parent_id,
  296. 'department_id' => $department_id,
  297. 'status' => 1,
  298. 'updatetime' => time()
  299. ]) == false) {
  300. $this->error('审核失败');
  301. }
  302. $this->success('审核成功');
  303. }
  304. //审核员工拒绝
  305. public function checkStaffError(){
  306. $id = input('id');
  307. if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
  308. $this->error('待审核员工不存在');
  309. }
  310. if (StaffModel::where(['id' => $id, 'status' => 0])->update(['deletetime' => time()]) == false) {
  311. $this->error('拒绝失败');
  312. }
  313. $this->success('审核成功');
  314. }
  315. //获取员工统计
  316. public function getStaffStatistics()
  317. {
  318. $id = input('id');
  319. if (empty($id)) {
  320. $this->error('员工不存在');
  321. }
  322. $date = input('date', date('Y-m'));
  323. //月底
  324. $endDate = strtotime('+1 month', strtotime(date($date . '-1')));
  325. $date = strtotime($date);
  326. //客户 线索 联系人 合同 回款 跟进次数 处理审批
  327. $leads = Leads::where([
  328. 'create_staff_id' => $id,
  329. 'createtime' => ['between', [$date, $endDate]],
  330. ])->count();
  331. $customer = Customer::where([
  332. 'create_staff_id' => $id,
  333. 'createtime' => ['between', [$date, $endDate]],
  334. ])->count();
  335. $contacts = Contacts::where([
  336. 'create_staff_id' => $id,
  337. 'createtime' => ['between', [$date, $endDate]],
  338. ])->count();
  339. $contract = Contract::where([
  340. 'create_staff_id' => $id,
  341. 'createtime' => ['between', [$date, $endDate]],
  342. 'check_status' => 2
  343. ])->count();
  344. $contract_money = Contract::where([
  345. 'create_staff_id' => $id,
  346. 'createtime' => ['between', [$date, $endDate]],
  347. 'check_status' => 2
  348. ])->sum('money');
  349. $receivables = Receivables::where([
  350. 'create_staff_id' => $id,
  351. 'createtime' => ['between', [$date, $endDate]],
  352. 'check_status' => 2
  353. ])->count();
  354. $receivables_money = Receivables::where([
  355. 'create_staff_id' => $id,
  356. 'createtime' => ['between', [$date, $endDate]],
  357. 'check_status' => 2
  358. ])->sum('money');
  359. $record = Record::where([
  360. 'create_staff_id' => $id,
  361. 'createtime' => ['between', [$date, $endDate]],
  362. ])->count();
  363. $field = Achievement::getMonthField(date('Y-m', $date));
  364. $contractAchievement = Achievement::where([
  365. 'year' => date('Y', $date),
  366. 'type' => 3,
  367. 'obj_id' => $id,
  368. 'status' => 1
  369. ])->value($field);
  370. $receivablesAchievement = Achievement::where([
  371. 'year' => date('Y', $date),
  372. 'type' => 3,
  373. 'obj_id' => $id,
  374. 'status' => 2
  375. ])->value($field);
  376. $this->success('请求成功', [
  377. 'leads' => $leads,
  378. 'customer' => $customer,
  379. 'contacts' => $contacts,
  380. 'contract' => $contract,
  381. 'contract_money' => $contract_money,
  382. 'receivables' => $receivables,
  383. 'receivables_money' => $receivables_money,
  384. 'record' => $record,
  385. 'contractAchievement' => $contractAchievement ?: 0,
  386. 'receivablesAchievement' => $receivablesAchievement ?: 0,
  387. ]);
  388. }
  389. //获取员工所属客户
  390. public function getStaffCustomer()
  391. {
  392. $id = input('id');
  393. $limit = input("limit/d", 10);
  394. $row = StaffModel::get($id);
  395. if (empty($row)) {
  396. $this->error('员工不存在');
  397. }
  398. $where = [];
  399. $where['owner_staff_id'] = $id;
  400. $list = Customer::where($where)->with([
  401. 'ownerStaff',
  402. 'contacts'
  403. ])->field('id,name,next_time,owner_staff_id,level,follow')->order('id desc')->paginate($limit);
  404. $this->success('请求成功', $list);
  405. }
  406. //获取部门列表
  407. public function getDepartment()
  408. {
  409. $this->success('请求成功', StaffDepartment::getDepartmentList());
  410. }
  411. //获取上级列表
  412. public function getParentList()
  413. {
  414. $id = input('id');
  415. if (empty($id)) {
  416. $this->error('参数错误');
  417. }
  418. $this->success('请求成功', StaffModel::getList([$id]));
  419. }
  420. /**
  421. * 获取用户上级IDs
  422. */
  423. public function getStaffIds()
  424. {
  425. // record 跟进 contract 合同 consume 费用 receivables 回款
  426. $type = input('type');
  427. $remind=Remind::where(['type'=>$type])->find();
  428. if($remind){
  429. $staff = StaffModel::where(['id' => ['in', $remind['staff_ids']]])->field('id,name,img')->select();
  430. $this->success('请求成功', $staff);
  431. }
  432. $pid = StaffModel::where(['id' => $this->auth->id])->value('parent_id');
  433. $pids = [$pid];
  434. $staff = StaffModel::where(['id' => ['in', $pids]])->field('id,name,img')->select();
  435. $this->success('请求成功', $staff);
  436. }
  437. /**
  438. * 获取审批列表
  439. */
  440. public function getsteplist()
  441. {
  442. // record 跟进 contract 合同 consume 费用 receivables 回款 formapproval_1 审批
  443. $type = input('type');
  444. //获取审批
  445. $data = Flow::getsteplist($type);
  446. if (empty($data)) {
  447. $this->error('无可用审批流,请联系管理员');
  448. }
  449. $this->success('请求成功', $data);
  450. }
  451. /**
  452. * 获取审批详情
  453. */
  454. public function getstepdetail()
  455. {
  456. // record 跟进 contract 合同 consume 费用 receivables 回款
  457. $type = input('type');
  458. $relation_id=input('relation_id');
  459. $data = Flow::getstepdetail($type,$relation_id);
  460. if (empty($data)) {
  461. $this->error('无可用审批流,请联系管理员');
  462. }
  463. $this->success('请求成功', $data);
  464. }
  465. //禁用账号
  466. public function disable_user()
  467. {
  468. $id = input('id');
  469. $status = 2;
  470. $model = new StaffModel();
  471. $staff = $model->where(['id' => $id])->find();
  472. if (empty($staff)) {
  473. $this->error('员工不存在');
  474. }
  475. if ($model->isUpdate(true)->save(['id' => $id, 'status' => $status])) {
  476. $this->success('操作成功');
  477. }
  478. $this->error('操作失败');
  479. }
  480. /**
  481. * 退出登录
  482. * @ApiMethod (POST)
  483. */
  484. public function logout()
  485. {
  486. if (!$this->request->isPost()) {
  487. $this->error(__('Invalid parameters'));
  488. }
  489. $this->auth->logout();
  490. $this->success(__('Logout successful'));
  491. }
  492. /**
  493. * 修改密码
  494. */
  495. public function changepwd()
  496. {
  497. if ($this->request->isPost()) {
  498. $oldpassword = $this->request->post("oldpassword");
  499. $newpassword = $this->request->post("newpassword");
  500. $renewpassword = $this->request->post("renewpassword");
  501. $rule = [
  502. 'oldpassword' => 'require|length:6,30',
  503. 'newpassword' => 'require|length:6,30',
  504. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  505. ];
  506. $msg = [
  507. 'renewpassword.confirm' =>'两次输入的密码不一致'
  508. ];
  509. $data = [
  510. 'oldpassword' => $oldpassword,
  511. 'newpassword' => $newpassword,
  512. 'renewpassword' => $renewpassword,
  513. ];
  514. $field = [
  515. 'oldpassword' => '旧密码',
  516. 'newpassword' => '新密码',
  517. 'renewpassword' => '确认密码'
  518. ];
  519. $validate = new Validate($rule, $msg, $field);
  520. $result = $validate->check($data);
  521. if (!$result) {
  522. $this->error(__($validate->getError()));
  523. return false;
  524. }
  525. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  526. if ($ret) {
  527. $this->success('修改密码成功');
  528. } else {
  529. $this->error($this->auth->getError());
  530. }
  531. }
  532. }
  533. //直接体验
  534. public function logintest()
  535. {
  536. $staff = StaffModel::where([])->order('id asc')->find();
  537. if(!$staff){
  538. $this->error('员工不存在');
  539. }
  540. $stafflogin= $this->auth->direct($staff['id']);
  541. if ($stafflogin) {
  542. $data = ['userinfo' => $this->auth->getUserinfo()];
  543. $this->success(__('Logged in successful'), $data);
  544. } else {
  545. $this->error('登录失败');
  546. }
  547. }
  548. }