Company.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. <?php
  2. namespace app\adminall\controller;
  3. use app\BaseController;
  4. use app\model\ConstructionStep;
  5. use app\model\WeworksingleCompanySetting;
  6. use think\facade\View;
  7. use think\facade\Request;
  8. use app\model\Company as ModelCompany;
  9. use app\model\CompanyBrand;
  10. use app\model\Employee;
  11. use app\model\Miniprogram;
  12. use app\model\Org;
  13. use Exception;
  14. use think\facade\Db;
  15. use wx\miniprogram\Qrcode;
  16. use app\model\OperateLog;
  17. use app\model\TrainClassCate;
  18. use app\model\TalkskillUsersCate;
  19. use app\model\CompanyStrengthCate;
  20. use app\model\TalkskillCates;
  21. use app\model\Company as CompanyModel;
  22. use app\model\Wechat;
  23. class Company extends BaseController
  24. {
  25. /**
  26. * 列表
  27. */
  28. public function index()
  29. {
  30. if (!Request::isAjax()) {
  31. return View::fetch();
  32. }
  33. $param = Request::only(['page', 'limit', 'keyword']);
  34. $where = [];
  35. if (!empty($param['keyword'])) {
  36. $where[] = ['company_name', 'like', '%' . $param['keyword'] . '%'];
  37. }
  38. $list = ModelCompany::where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  39. $column = array_column($list, 'root_id');
  40. $mini = Miniprogram::where('root_id', 'in', $column)->column('company', 'root_id');
  41. foreach ($list as &$m) {
  42. $m['bind_mini'] = isset($mini[$m['root_id']]) ? 1 : 0;
  43. }
  44. $count = ModelCompany::where($where)->count();
  45. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  46. }
  47. /**
  48. * 添加视图
  49. */
  50. public function add()
  51. {
  52. return View::fetch();
  53. }
  54. /**
  55. * 修改视图
  56. */
  57. public function edit($id)
  58. {
  59. $company = ModelCompany::where('id', $id)->find();
  60. $company['phone'] = Employee::where([['top_one', '=', 1], ['root_id', '=', $company->root_id]])->value('phone');
  61. View::assign('data', $company);
  62. return View::fetch();
  63. }
  64. /**
  65. * 添加企业
  66. */
  67. public function addSave()
  68. {
  69. $param = request()->param();
  70. $org_id = Org::insertGetId([
  71. 'name' => $param['company_name'],
  72. 'pid' => 0,
  73. 'level' => 0,
  74. 'info' => $param['company_name'],
  75. 'status' => 1
  76. ]);
  77. Db::startTrans();
  78. try {
  79. Org::where('id', $org_id)->save(['path' => $org_id . '-']);
  80. ModelCompany::create([
  81. 'company_name' => $param['company_name'],
  82. 'start_date' => date('Y-m-d'),
  83. 'end_date' => $param['end_date'],
  84. 'logo' => $param['logo'] ?? '',
  85. 'root_id' => $org_id,
  86. 'tel' => $param['tel'],
  87. 'mini' => $param['mini'],
  88. 'shop_number' => $param['shop_number'] ?? 0
  89. ]);
  90. $msg = $this->miniInit($org_id, str_rand(6));
  91. if ($msg) return json(['code' => 1, 'msg' => $msg]);
  92. Employee::create([
  93. 'phone' => $param['phone'],
  94. 'opt_name' => $param['company_name'],
  95. 'root_id' => $org_id,
  96. 'org_id' => $org_id,
  97. 'grant_id' => 2,
  98. 'is_manager' => 1,
  99. 'verified' => 1,
  100. 'state' => '在职',
  101. 'top_one' => 1
  102. ]);
  103. } catch (Exception $e) {
  104. // 回滚事务
  105. Db::rollback();
  106. return json(['code' => 1, 'msg' => $e->getMessage()]);
  107. }
  108. Db::commit();
  109. return json(['code' => 0, 'msg' => '添加成功']);
  110. }
  111. /**
  112. * 修改企业
  113. */
  114. public function editSave()
  115. {
  116. $param = request()->only(['phone', 'company_name', 'end_date', 'id', 'root_id', 'logo', 'tel', 'mini']);
  117. $msg = $this->setInfo($param['id'], $param['company_name'], 'company_name');
  118. $employee = Employee::where([['top_one', '=', 1], ['root_id', '=', $param['root_id']]])->find();
  119. if ($param['phone'] != $employee['phone']) {
  120. $employee->phone = $param['phone'];
  121. $employee->save();
  122. }
  123. unset($param['phone']);
  124. $company = ModelCompany::where('id', $param['id'])->find();
  125. $old = $company->mini;
  126. $company->save($param);
  127. if ($param['mini'] != $old) $this->miniInit($company->root_id);
  128. return json(['code' => 0, 'msg' => '修改成功']);
  129. }
  130. /**
  131. * 取消用户授权
  132. */
  133. public function auth()
  134. {
  135. $id = input('post.id/d');
  136. $company = ModelCompany::where('id', $id)->find();
  137. $company->status = $company->status == 1 ? 0 : 1;
  138. $company->save();
  139. return json(['code' => 0, 'msg' => '更新成功']);
  140. }
  141. /**
  142. * 更新到期有效时间
  143. */
  144. public function endDate()
  145. {
  146. $id = input('post.id/d');
  147. $date = input('post.value');
  148. ModelCompany::where('id', $id)->update(['end_date' => $date]);
  149. return json(['code' => 0, 'msg' => '更新成功']);
  150. }
  151. /**
  152. * 修改名称
  153. */
  154. public function setInfo($id, $set_value, $field)
  155. {
  156. $rs = ModelCompany::where('id', $id)->find();
  157. if (!$rs) return json(['code' => 1, 'msg' => '修改失败']);
  158. //修改名称
  159. if ($field == 'company_name') {
  160. //修改org里的分组名称
  161. $path = Org::where('id', $rs['root_id'])->find();
  162. $aimedones = Org::where([['path', 'like', $path['path'] . '%']])->select();
  163. foreach ($aimedones as $item) {
  164. $rightinfo = str_replace($rs->company_name, $set_value, $item->info);
  165. Org::where(['id' => $item['id']])->update(['info' => $rightinfo]);
  166. }
  167. $path->name = $set_value;
  168. $path->save();
  169. //修改企业名称
  170. $rs->company_name = $set_value;
  171. $rs->save();
  172. //修改miniprogram表名称
  173. Miniprogram::where('root_id', $rs['root_id'])->update(['company' => $set_value]);
  174. }
  175. //修改店面数量
  176. if ($field == 'shop_number') {
  177. $join = ModelCompany::where('company_group', $rs['id'])->count();
  178. if ($set_value < $join) return json(['code' => 1, 'msg' => '修改失败,当前已存在'.$join.'家店面']);
  179. $rs->shop_number = $set_value;
  180. $rs->save();
  181. }
  182. return json(['code' => 0, 'msg' => '修改成功']);
  183. }
  184. /*
  185. * 企业授权信息修改
  186. */
  187. public function setting($root_id)
  188. {
  189. if (!Request::isAjax()) {
  190. $mini = Miniprogram::where('root_id', $root_id)->find();
  191. View::assign('mini', $mini);
  192. View::assign('root_id', $root_id);
  193. $notify = empty($mini['notify']) ? str_rand(6) : $mini['notify'];
  194. View::assign('notify', $notify);
  195. $url = url('api/miniEventPush/' . $notify, [], true, true)->build();
  196. View::assign('url', $url);
  197. return View::fetch();
  198. }
  199. $param = request()->param();
  200. $msg = $this->miniprogram($param);
  201. return $msg;
  202. }
  203. /**
  204. * 小程序绑定
  205. */
  206. private function miniprogram($param)
  207. {
  208. $program = Miniprogram::where([['root_id', '=', $param['root_id']]])->findOrEmpty();
  209. if (!empty($param['appid']) && !empty($param['secret'])) {
  210. $accessToken = getAccessToken($param['appid'], $param['secret']);
  211. if (empty($accessToken)) return json(['code' => 1, 'msg' => '配置失败,appid,secret不正确或小程序业务域名未配置']);
  212. // 邀请二维码生成
  213. $miniQr = new Qrcode();
  214. // 1)管理员二维码
  215. $qr = $miniQr->getUnlimited($accessToken, [
  216. 'scene' => 'manager=' . $param['root_id'],
  217. 'page' => 'mycustomer/pages/invite/invite',
  218. 'width' => '280px',
  219. 'check_path' => false
  220. ]);
  221. $rs = json_decode($qr, true);
  222. if (!is_null($rs)) {
  223. trace($rs, 'error');
  224. return json(['code' => 1, 'msg' => '配置失败,appid,secret不正确或小程序业务域名未配置']);
  225. }
  226. $managerQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  227. ossContentUpload($managerQr, $qr);
  228. // 2)员工二维码
  229. $qr = $miniQr->getUnlimited($accessToken, [
  230. 'scene' => 'employee=' . $param['root_id'],
  231. 'page' => 'mycustomer/pages/invite/invite',
  232. 'width' => '280px',
  233. 'check_path' => false
  234. ]);
  235. $rs = json_decode($qr, true);
  236. if (!is_null($rs)) {
  237. return json(['code' => 1, 'msg' => '配置失败,appid,secret不正确或小程序业务域名未配置']);
  238. }
  239. $employeeQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  240. ossContentUpload($employeeQr, $qr);
  241. } else { // 公开版志远装饰
  242. $accessToken = '';
  243. // 二维码生成到企业小程序设置中
  244. $defaultAccesstoken = Miniprogram::where([['notify', '=', config('app.cgi')]])->find()->accesstoken;
  245. $miniQr = new Qrcode();
  246. // 1)管理员二维码
  247. $qr = $miniQr->getUnlimited($defaultAccesstoken, [
  248. 'scene' => 'cgi=' . $param['notify'] . '&manager=' . $param['root_id'],
  249. 'page' => 'mycustomer/pages/invite/invite',
  250. 'width' => '280px',
  251. 'check_path' => false
  252. ]);
  253. $rs = json_decode($qr, true);
  254. if (!is_null($rs)) {
  255. trace($rs, 'error');
  256. return json(['code' => 1, 'msg' => '配置失败,appid,secret不正确或小程序业务域名未配置']);
  257. }
  258. $managerQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  259. ossContentUpload($managerQr, $qr);
  260. // 2)员工二维码
  261. $qr = $miniQr->getUnlimited($defaultAccesstoken, [
  262. 'scene' => 'cgi=' . $param['notify'] . '&employee=' . $param['root_id'],
  263. 'page' => 'mycustomer/pages/invite/invite',
  264. 'width' => '280px',
  265. 'check_path' => false
  266. ]);
  267. $rs = json_decode($qr, true);
  268. if (!is_null($rs)) {
  269. return json(['code' => 1, 'msg' => '配置失败,appid,secret不正确或小程序业务域名未配置']);
  270. }
  271. $employeeQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  272. ossContentUpload($employeeQr, $qr);
  273. }
  274. $company_name = ModelCompany::where('root_id', $param['root_id'])->value('company_name');
  275. $data = [
  276. 'appid' => $param['appid'],
  277. 'secret' => $param['secret'],
  278. 'account' => $param['account'],
  279. 'encodingAesKey' => $param['encodingAesKey'],
  280. 'token' => $param['token'],
  281. 'manager_qr' => $managerQr,
  282. 'employee_qr' => $employeeQr,
  283. 'accesstoken' => $accessToken,
  284. 'expire' => time() + 7100,
  285. 'root_id' => $param['root_id'],
  286. 'notify' => $param['notify'],
  287. 'company' => $company_name
  288. ];
  289. $program->save($data);
  290. return json(['code' => 0, 'msg' => '保存成功']);
  291. }
  292. /**
  293. * 初始化小程序配置
  294. */
  295. private function miniInit($rootId, $notify = null)
  296. {
  297. $program = Miniprogram::where([['root_id', '=', $rootId]])->findOrEmpty();
  298. if ($program->isEmpty()) {
  299. if (empty($notify)) return '缺少参数notify';
  300. $data = [
  301. 'appid' => '',
  302. 'secret' => '',
  303. 'account' => '',
  304. 'encodingAesKey' => '',
  305. 'token' => '',
  306. 'manager_qr' => '',
  307. 'employee_qr' => '',
  308. 'accesstoken' => '',
  309. 'expire' => 0,
  310. 'root_id' => $rootId,
  311. 'company' => '',
  312. 'notify' => $notify
  313. ];
  314. $program->save($data);
  315. }
  316. $notify = $program->notify;
  317. // 二维码生成到企业小程序设置中
  318. $defaultAccesstoken = $program->accesstoken;
  319. if (empty($defaultAccesstoken)) return '默认accesstoken获取失败';
  320. $miniQr = new Qrcode();
  321. // 1)管理员二维码
  322. $qr = $miniQr->getUnlimited($defaultAccesstoken, [
  323. 'scene' => 'cgi=' . $notify . '&manager=' . $rootId,
  324. 'page' => 'mycustomer/pages/invite/invite',
  325. 'width' => '280px',
  326. 'check_path' => false
  327. ]);
  328. $rs = json_decode($qr, true);
  329. if (!is_null($rs)) {
  330. trace($rs, 'error');
  331. return '管理员二维码生成失败';
  332. }
  333. $managerQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  334. ossContentUpload($managerQr, $qr);
  335. // 2)员工二维码
  336. $qr = $miniQr->getUnlimited($defaultAccesstoken, [
  337. 'scene' => 'cgi=' . $notify . '&employee=' . $rootId,
  338. 'page' => 'mycustomer/pages/invite/invite',
  339. 'width' => '280px',
  340. 'check_path' => false
  341. ]);
  342. $rs = json_decode($qr, true);
  343. if (!is_null($rs)) return '员工二维码生成失败';
  344. $employeeQr = 'invite' . DIRECTORY_SEPARATOR . uniqid() . '.jpeg';
  345. ossContentUpload($employeeQr, $qr);
  346. $company_name = ModelCompany::where('root_id', $rootId)->value('company_name');
  347. $data = [
  348. 'manager_qr' => $managerQr,
  349. 'employee_qr' => $employeeQr,
  350. 'company' => $company_name
  351. ];
  352. $program->save($data);
  353. }
  354. /**
  355. * 集团列表
  356. */
  357. public function group()
  358. {
  359. /** 页面请求 */
  360. if (!$this->request->isAjax()) {
  361. $miniprogram = Wechat::field('id, type')->select();
  362. View::assign('miniprogram', $miniprogram);
  363. return View::fetch();
  364. }
  365. /** ajax请求获取数据 */
  366. // 请求参数获取
  367. $param = $this->request->only(['page', 'limit', 'keyword']);
  368. // 只取集团企业信息
  369. $where = [['company_group', '=', 0]];
  370. // 查询条件
  371. if (!empty($param['keyword'])) {
  372. $where[] = ['company_name', 'like', '%' . $param['keyword'] . '%'];
  373. }
  374. // 列表获取
  375. $list = ModelCompany::with(['mini' => function ($query) {
  376. $query->bind(['type']);
  377. }])->field('id,company_name,start_date,end_date,status,tel,addtime,root_id,company_group,mini,shop_number')->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select();
  378. // 列表总数获取
  379. $count = ModelCompany::where($where)->count();
  380. // 查询结果返回
  381. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  382. }
  383. /**
  384. * 登录日志
  385. */
  386. public function loginlog()
  387. {
  388. $id = input('id', 0);
  389. $page = input('page', 1);
  390. $limit = input('limit', 10);
  391. if (!Request::isAjax()) {
  392. //店面
  393. $shops = ModelCompany::where([['company_group', '=', $id]])->field('id,company_name')->select()->toArray();
  394. $data['group'] = $shops;
  395. // 人员
  396. view::assign('data', $data);
  397. view::assign('id', $id);
  398. return View::fetch();
  399. }
  400. $group = input('group', 0);
  401. $cw[] = ['company_group', '=', $id];
  402. if ($group) {
  403. $cw[] = ['id', '=', $group];
  404. }
  405. //所有店面
  406. $shops = ModelCompany::where($cw)->column('root_id');
  407. // var_dump($shops);die;
  408. $w[] = ['root_id', 'in', $shops];
  409. // $w = [];
  410. $list = OperateLog::with(['employee', 'company'])->where($w)->page($page, $limit)->order('id desc')->select()->toArray();
  411. if ($list) {
  412. foreach ($list as $k => $v) {
  413. $list[$k]['company_name'] = isset($v['company']) && $v['company'] ? $v['company']['company_name'] : '';
  414. $list[$k]['company_group'] = isset($v['company']) && $v['company'] ? $v['company']['company_group'] : 0;
  415. $list[$k]['company_brand'] = isset($v['company']) && $v['company'] ? $v['company']['company_brand'] : 0;
  416. $list[$k]['name'] = isset($v['employee']) && $v['employee'] ? $v['employee']['opt_name'] : '';
  417. $list[$k]['nickname'] = isset($v['employee']) && $v['employee'] ? $v['employee']['nickname'] : '';
  418. $list[$k]['phone'] = isset($v['employee']) && $v['employee'] ? $v['employee']['phone'] : '';
  419. }
  420. //dianpu
  421. $bw[] = ['id', 'in', array_column($list, 'company_brand')];
  422. $brand = CompanyBrand::where($bw)->column('brand_name', 'id');
  423. foreach ($list as $k => $v) {
  424. $list[$k]['brand_name'] = isset($brand[$v['company_brand']]) ? $brand[$v['company_brand']] : '';
  425. unset($list[$k]['employee']);
  426. unset($list[$k]['company']);
  427. }
  428. }
  429. $count = OperateLog::with(['employee', 'company'])->where($w)->count();
  430. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  431. }
  432. /**
  433. * 品牌列表
  434. */
  435. public function brand($id)
  436. {
  437. $data = CompanyBrand::field('id,logo,brand_name,company_group')->where([['company_group', '=', $id]])->select();
  438. return json(['code' => 0, 'data' => $data]);
  439. }
  440. /**
  441. * 品牌添加
  442. */
  443. public function addBrand()
  444. {
  445. $data = $this->request->only(['brand_name', 'logo', 'company_group']);
  446. CompanyBrand::create($data);
  447. return json(['code' => 0, 'msg' => '添加成功']);
  448. }
  449. /**
  450. * 品牌编辑
  451. */
  452. public function editBrand($id)
  453. {
  454. $brand = CompanyBrand::find($id);
  455. $data = $this->request->only(['brand_name', 'logo', 'company_group']);
  456. $brand->save($data);
  457. return json(['code' => 0, 'msg' => '修改成功']);
  458. }
  459. /**
  460. * 集团店面
  461. */
  462. public function shop($id)
  463. {
  464. $shops = ModelCompany::with(['mini' => function ($query) {
  465. $query->bind(['type']);
  466. }])->field('id,company_name,company_brand,start_date,end_date,city,tel,company_group,root_id,mini')
  467. ->where([
  468. ['company_group', '=', $id]
  469. ])->select();
  470. // 查询店面负责人
  471. $leaders = Employee::where([['top_one', '=', 1], ['root_id', 'in', array_column($shops->toArray(), 'root_id')]])->column('opt_name', 'root_id');
  472. foreach ($shops as &$shop) {
  473. $shop['leader_name'] = $leaders[$shop['root_id']];
  474. }
  475. View::assign('shops', $shops);
  476. return View::fetch();
  477. }
  478. /**
  479. * 添加店面
  480. */
  481. public function addShop()
  482. {
  483. $param = $this->request->param();
  484. // $param['end_date'] = ModelCompany::where(['id'=>$param['company_group']])->value('end_date');
  485. $shop_number = ModelCompany::where([['id', '=', $param['company_group']]])->value('shop_number');
  486. $count = ModelCompany::where([['company_group', '=', $param['company_group']]])->count();
  487. if ($count >= $shop_number) return json(['code' => 1, 'msg' => '超出开通店面数量']);
  488. Db::startTrans();
  489. try {
  490. $org_id = Org::insertGetId([
  491. 'name' => $param['company_name'],
  492. 'pid' => 0,
  493. 'level' => 0,
  494. 'info' => $param['company_name'],
  495. 'status' => 1
  496. ]);
  497. Org::where('id', $org_id)->save(['path' => $org_id . '-']);
  498. ModelCompany::create([
  499. 'company_name' => $param['company_name'],
  500. 'start_date' => date('Y-m-d'),
  501. 'end_date' => $param['end_date'],
  502. 'logo' => $param['logo'] ?? '',
  503. 'root_id' => $org_id,
  504. 'tel' => $param['tel'],
  505. 'company_group' => $param['company_group'],
  506. 'city' => $param['city'],
  507. 'company_brand' => $param['company_brand'],
  508. 'province_city' => !empty($param['province_city']) ? $param['province_city'] : '',
  509. 'province_city_code' => !empty($param['province_city_code']) ? $param['province_city_code'] : ''
  510. ]);
  511. $msg = $this->miniInit($org_id, str_rand(6));
  512. if ($msg) return json(['code' => 1, 'msg' => $msg]);
  513. Employee::create([
  514. 'phone' => $param['phone'],
  515. 'opt_name' => $param['leader_name'],
  516. 'root_id' => $org_id,
  517. 'grant_id' => 1,
  518. 'top_one' => 1
  519. ]);
  520. $max_end_date = ModelCompany::where(['company_group' => $param['company_group']])->order('end_date desc')->value('end_date');
  521. ModelCompany::where(['id' => $param['company_group']])->update(['end_date' => $max_end_date]);
  522. ConstructionStep::insertAll([
  523. ['name' => '开工大吉', 'order' => 1, 'root_id' => $org_id],
  524. ['name' => '前期施工', 'order' => 2, 'root_id' => $org_id],
  525. ['name' => '中期施工', 'order' => 3, 'root_id' => $org_id],
  526. ['name' => '后期施工', 'order' => 4, 'root_id' => $org_id],
  527. ['name' => '竣工', 'order' => 5, 'root_id' => $org_id]
  528. ]);
  529. //课程培训默认分类
  530. TrainClassCate::insertAll([
  531. ['name' => '销冠分享', 'root_id' => $org_id],
  532. ['name' => '总裁讲堂', 'root_id' => $org_id],
  533. ['name' => '营销工具', 'root_id' => $org_id],
  534. ['name' => '工艺标准', 'root_id' => $org_id],
  535. ['name' => '营销管理', 'root_id' => $org_id],
  536. ['name' => '业务技能', 'root_id' => $org_id],
  537. ['name' => '设计常识', 'root_id' => $org_id],
  538. ['name' => '产品知识', 'root_id' => $org_id],
  539. ['name' => '企业文化', 'root_id' => $org_id],
  540. ['name' => '通用知识', 'root_id' => $org_id],
  541. ['name' => '其他', 'root_id' => $org_id]
  542. ]);
  543. //话术默认使用部门
  544. TalkskillUsersCate::insertAll([
  545. ['name' => '业务部', 'root_id' => $org_id, 'show' => 0],
  546. ['name' => '网销部', 'root_id' => $org_id, 'show' => 0],
  547. ['name' => '设计师', 'root_id' => $org_id, 'show' => 0],
  548. ['name' => '客户经理', 'root_id' => $org_id, 'show' => 0],
  549. ['name' => '市场部', 'root_id' => $org_id, 'show' => 0],
  550. ['name' => '第三方端', 'root_id' => $org_id, 'show' => 0],
  551. ['name' => '社群部', 'root_id' => $org_id, 'show' => 0],
  552. ['name' => '其它', 'root_id' => $org_id, 'show' => 1]
  553. ]);
  554. //公司实力默认数据 发展历程 产品介绍 材料实验室 老板访谈 工艺标准 团队承诺 荣誉奖项
  555. CompanyStrengthCate::insertAll([
  556. ['name' => '发展历程', 'root_id' => $org_id],
  557. ['name' => '产品介绍', 'root_id' => $org_id],
  558. ['name' => '材料实验室', 'root_id' => $org_id],
  559. ['name' => '老板访谈', 'root_id' => $org_id],
  560. ['name' => '工艺标准', 'root_id' => $org_id],
  561. ['name' => '团队承诺', 'root_id' => $org_id],
  562. ['name' => '荣誉奖项', 'root_id' => $org_id]
  563. ]);
  564. //话术默认一级和二级分类
  565. $cates = [
  566. '开场邀约' => ['直播活动', '样板征集', '团购活动', '量房设计'],
  567. '添加微信' => ['加微验证', '加微短信', '电话加微'],
  568. '引导挖需' => ['基础信息', '谁来做主', '过往经验', '竞品情况', '装修需求'],
  569. '建立信任' => ['企业文化', '产品模式', '客户见证', '设计实力', '材料实力', '施工实力', '性价比优', '售后承诺'],
  570. '产品介绍' => ['产品内容', '产品优势', '装修流程'],
  571. '异议处理' => ['价格问题', '考虑问题', '对比问题', '不急问题'],
  572. '其他' => []
  573. ];
  574. $saves = [];
  575. foreach ([$org_id] as $v) {
  576. foreach ($cates as $k2 => $v2) {
  577. $psave = [
  578. 'pid' => 0,
  579. 'type' => 'cate',
  580. 'root_id' => $v,
  581. 'name' => $k2
  582. ];
  583. $pid = TalkskillCates::insertGetId($psave);
  584. $save = [];
  585. foreach ($v2 as $k3 => $v3) {
  586. $save[] = [
  587. 'pid' => $pid,
  588. 'type' => 'cate',
  589. 'root_id' => $v,
  590. 'name' => $v3
  591. ];
  592. }
  593. if ($save) (new TalkskillCates())->saveAll($save);
  594. unset($save);
  595. unset($psave);
  596. }
  597. }
  598. //公司实力默认数据 发展历程 产品介绍 材料实验室 老板访谈 工艺标准 团队承诺 荣誉奖项
  599. TalkskillCates::insertAll([
  600. ['name' => '毛坯期房', 'type' => 'house_type', 'root_id' => $org_id],
  601. ['name' => '毛坯现房', 'type' => 'house_type', 'root_id' => $org_id],
  602. ['name' => '毛坯准现房', 'type' => 'house_type', 'root_id' => $org_id],
  603. ['name' => '老房翻新', 'type' => 'house_type', 'root_id' => $org_id],
  604. ['name' => '精装房期房', 'type' => 'house_type', 'root_id' => $org_id],
  605. ['name' => '精装房准现房', 'type' => 'house_type', 'root_id' => $org_id],
  606. ['name' => '精装房现房', 'type' => 'house_type', 'root_id' => $org_id]
  607. ]);
  608. } catch (Exception $e) {
  609. // 回滚事务
  610. Db::rollback();
  611. return json(['code' => 1, 'msg' => $e->getMessage()]);
  612. }
  613. Db::commit();
  614. return json(['code' => 0, 'msg' => '添加成功']);
  615. }
  616. public function editShop($id)
  617. {
  618. $param = $this->request->only(['company_name', 'tel', 'city', 'company_brand', 'leader_name', 'end_date','mini','province_city'=>'','province_city_code'=>'','phone']);
  619. $data = ModelCompany::findOrEmpty($id);
  620. if ($data->isEmpty()) return json(['code' => 1, 'msg' => '保存失败,数据不存在']);
  621. $old = $data->mini;
  622. $data->save($param);
  623. if ($param['mini'] != $old) $this->miniInit($data->root_id);
  624. //Employee::where([['top_one', '=', 1], ['root_id', '=', $data->root_id]])->update(['opt_name' => $param['leader_name']]);
  625. $empdata = Employee::where([['top_one', '=', 1], ['root_id', '=', $data->root_id]])->find();
  626. $empdata->opt_name = $param['leader_name'];
  627. if(!empty($param['phone']) && $param['phone'] != $empdata['phone']){
  628. $check = Employee::where([['root_id', '=', $data->root_id],['phone','=',cypherphone(trim($param['phone']))]])->find();
  629. if(!empty($check)){
  630. $check->grant_id = 1;
  631. $check->top_one = 1;
  632. $check->save();
  633. $empdata->top_one = 0;
  634. $empdata->grant_id = 0;
  635. $empdata->save();
  636. return json(['code' => 0, 'msg' => '保存成功']);
  637. }
  638. $empdata->phone = $param['phone'];
  639. }
  640. $empdata->save();
  641. $max_end_date = ModelCompany::where(['company_group' => $data->company_group])->order('end_date desc')->value('end_date');
  642. ModelCompany::where(['id' => $data->company_group])->update(['end_date' => $max_end_date,'province_city'=>$param['province_city'],'province_city_code'=>$param['province_city_code']]);
  643. return json(['code' => 0, 'msg' => '保存成功']);
  644. }
  645. /**
  646. * 企微设置列表
  647. */
  648. public function wework_index()
  649. {
  650. $company_id = input('company_id', '', 'intval');
  651. $list = WeworksingleCompanySetting::where('company_id', '=', $company_id)->select();
  652. View::assign('company_id', $company_id);
  653. View::assign('list', $list);
  654. return View::fetch();
  655. }
  656. public function wework()
  657. {
  658. if (!request()->isAjax()) {
  659. $id = input('id', '', 'intval');
  660. $company_id = input('company_id', '', 'intval');
  661. View::assign('company_id', $company_id);
  662. $have_setting = true;
  663. if ($id) {
  664. $setting = WeworksingleCompanySetting::where('id', '=', $id)->findOrEmpty();
  665. if ($setting->isEmpty()) {
  666. $have_setting = false;
  667. }
  668. } else {
  669. $have_setting = false;
  670. }
  671. if (!$have_setting) {
  672. $ticket = md5(time());
  673. $setting['company_id'] = $company_id;
  674. $setting['ticket'] = $ticket;
  675. $setting['corp_id'] = '';
  676. $setting['agentid'] = '';
  677. $setting['agent_secret'] = '';
  678. $setting['receive_token'] = '';
  679. $setting['receive_aes_key'] = '';
  680. $setting['contact_secret'] = '';
  681. $setting['contact_token'] = '';
  682. $setting['contact_aes_key'] = '';
  683. $setting['customer_secret'] = '';
  684. $setting['customer_token'] = '';
  685. $setting['customer_aes_key'] = '';
  686. $setting['chat_secret'] = '';
  687. $setting['chat_token'] = '';
  688. $setting['chat_aes_key'] = '';
  689. $setting['chat_private_key'] = '';
  690. $setting['verify_file'] = '';
  691. $setting['verify_content'] = '';
  692. $result = WeworksingleCompanySetting::create($setting);
  693. $id = $result->id;
  694. } else {
  695. $ticket = $setting['ticket'];
  696. }
  697. View::assign('id', $id);
  698. View::assign('setting', $setting);
  699. // 应用主页
  700. $index_url = request()->domain() . '/weworksingle.html#/loading?ticket=' . $ticket;
  701. View::assign('index_url', $index_url);
  702. //应用接收消息回调链接
  703. $agent_receive_url = request()->domain() . '/weworksingle/verifyurl?ticket=' . $ticket;
  704. View::assign('agent_receive_url', $agent_receive_url);
  705. // 客户变更回调链接
  706. $customer_receive_url = request()->domain() . '/weworksingle/customerverifyurl?ticket=' . $ticket;
  707. View::assign('customer_receive_url', $customer_receive_url);
  708. // 通讯录变更回调链接
  709. $contact_receive_url = request()->domain() . '/weworksingle/contactverifyurl?ticket=' . $ticket;
  710. View::assign('contact_receive_url', $contact_receive_url);
  711. return View::fetch();
  712. } else {
  713. $id = input('id', '', 'intval');
  714. $param = $this->request->param();
  715. $find = WeworksingleCompanySetting::where('id', '=', $id)->findOrEmpty();
  716. if ($find->isEmpty()) {
  717. if (!empty($param['company_id'])) {
  718. $data['company_id'] = $param['company_id'];
  719. }
  720. $data['corp_id'] = $param['corp_id'] ?? '';
  721. $data['ticket'] = $param['ticket'] ?? '';
  722. $data['agentid'] = $param['agentid'] ?? '';
  723. $data['agent_secret'] = $param['agent_secret'] ?? '';
  724. $data['receive_token'] = $param['receive_token'] ?? '';
  725. $data['receive_aes_key'] = $param['receive_aes_key'] ?? '';
  726. $data['contact_secret'] = $param['contact_secret'] ?? '';
  727. $data['contact_token'] = $param['contact_token'] ?? '';
  728. $data['contact_aes_key'] = $param['contact_aes_key'] ?? '';
  729. $data['customer_secret'] = $param['customer_secret'] ?? '';
  730. $data['customer_token'] = $param['customer_token'] ?? '';
  731. $data['customer_aes_key'] = $param['customer_aes_key'] ?? '';
  732. $data['verify_file'] = $param['verify_file'] ?? '';
  733. $data['verify_content'] = $param['verify_content'] ?? '';
  734. $result = WeworksingleCompanySetting::create($data);
  735. if ($result) {
  736. return json(['code' => 0, 'msg' => '操作成功']);
  737. } else {
  738. return json(['code' => 1, 'msg' => '操作失败']);
  739. }
  740. } else {
  741. if (!empty($param['corp_id'])) {
  742. $find->corp_id = $param['corp_id'];
  743. }
  744. if (!empty($param['agentid'])) {
  745. $find->agentid = $param['agentid'];
  746. }
  747. if (!empty($param['agent_secret'])) {
  748. $find->agent_secret = $param['agent_secret'];
  749. }
  750. if (!empty($param['receive_token'])) {
  751. $find->receive_token = $param['receive_token'];
  752. }
  753. if (!empty($param['receive_aes_key'])) {
  754. $find->receive_aes_key = $param['receive_aes_key'];
  755. }
  756. if (!empty($param['contact_secret'])) {
  757. $find->contact_secret = $param['contact_secret'];
  758. }
  759. if (!empty($param['contact_token'])) {
  760. $find->contact_token = $param['contact_token'];
  761. }
  762. if (!empty($param['contact_aes_key'])) {
  763. $find->contact_aes_key = $param['contact_aes_key'];
  764. }
  765. if (!empty($param['customer_secret'])) {
  766. $find->customer_secret = $param['customer_secret'];
  767. }
  768. if (!empty($param['customer_token'])) {
  769. $find->customer_token = $param['customer_token'];
  770. }
  771. if (!empty($param['customer_aes_key'])) {
  772. $find->customer_aes_key = $param['customer_aes_key'];
  773. }
  774. if (!empty($param['verify_file'])) {
  775. $find->verify_file = $param['verify_file'];
  776. }
  777. if (!empty($param['verify_content'])) {
  778. $find->verify_content = $param['verify_content'];
  779. }
  780. $result = $find->save();
  781. if ($result) {
  782. return json(['code' => 0, 'msg' => '操作成功']);
  783. } else {
  784. return json(['code' => 1, 'msg' => '操作失败']);
  785. }
  786. }
  787. }
  788. }
  789. /**
  790. * 设置信息一键复制(一个企业微信多家店面)
  791. */
  792. public function wework_copy()
  793. {
  794. $company_id = input('company_id', '', 'intval');
  795. if (!$this->request->isAjax()) {
  796. $company_group = CompanyModel::where('id', '=', $company_id)->value('company_group');
  797. $company_list = [];
  798. if ($company_group) {
  799. $company_list = CompanyModel::where([['company_group', '=', $company_group], ['id', '<>', $company_id]])->select()->toArray();
  800. }
  801. foreach ($company_list as $k => $v) {
  802. $find = WeworksingleCompanySetting::where('company_id', '=', $v['id'])->find();
  803. if (empty($find)) {
  804. unset($company_list[$k]);
  805. }
  806. }
  807. View::assign('data', array_values($company_list));
  808. View::assign('company_id', $company_id);
  809. return View::fetch();
  810. } else {
  811. $copy_id = input('copy_id', '', 'intval');
  812. $setting = WeworksingleCompanySetting::where('company_id', '=', $copy_id)->findOrEmpty();
  813. if ($setting->isEmpty()) {
  814. return json(['code' => 1, 'msg' => '复制失败,企业配置不存在']);
  815. }
  816. $setting = $setting->toArray();
  817. unset($setting['id']);
  818. $setting['company_id'] = $company_id;
  819. $result = WeworksingleCompanySetting::create($setting);
  820. if ($result !== false) {
  821. return json(['code' => 0, 'msg' => '复制成功']);
  822. } else {
  823. return json(['code' => 1, 'msg' => '复制失败']);
  824. }
  825. }
  826. }
  827. /**
  828. * 删除企业微信配置
  829. */
  830. public function wework_delete()
  831. {
  832. $id = input('id', '', 'intval');
  833. $find = WeworksingleCompanySetting::find($id);
  834. if (empty($find)) {
  835. return json(['code' => 0, 'msg' => '删除成功']);
  836. }
  837. $result = $find->delete();
  838. if ($result) {
  839. return json(['code' => 0, 'msg' => '删除成功']);
  840. } else {
  841. return json(['code' => 1, 'msg' => '删除失败']);
  842. }
  843. }
  844. /**
  845. * 设置企业微信时上传校验文件
  846. */
  847. public function wework_upload()
  848. {
  849. $file = request()->file('file');
  850. $file_name = $file->getOriginalName();
  851. $value = file_get_contents($file->getPathname());
  852. if ($value) {
  853. return json(['code' => 0, 'msg' => '上传成功', 'data' => ['name' => $file_name, 'content' => $value]]);
  854. } else {
  855. return json(['code' => 1, 'msg' => '上传失败']);
  856. }
  857. }
  858. /**
  859. * 新增店铺新页面
  860. */
  861. public function new_add_shop()
  862. {
  863. $company_id = input('company_id',0);
  864. View::assign('company_id', $company_id);
  865. $miniprogram = Wechat::field('id, type')->select();
  866. View::assign('miniprogram', $miniprogram);
  867. return View::fetch();
  868. }
  869. /**
  870. * 编辑店铺新页面
  871. */
  872. public function new_edit_shop()
  873. {
  874. $id = input('id',0);
  875. View::assign('id', $id);
  876. $company_id = input('company_id',0);
  877. View::assign('company_id', $company_id);
  878. $company = ModelCompany::where('id', $id)->find();
  879. $empdata = Employee::where([['top_one', '=', 1], ['root_id', '=', $company->root_id]])->field('phone,opt_name')->find();
  880. $company['phone'] = $empdata['phone'];
  881. $company['leader_name'] = $empdata['opt_name'];
  882. // $company['phone'] = Employee::where([['top_one', '=', 1], ['root_id', '=', $company->root_id]])->value('phone');
  883. // $company['leader_name'] = Employee::where([['top_one', '=', 1], ['root_id', '=', $company->root_id]])->value('opt_name');
  884. View::assign('data', $company);
  885. $miniprogram = Wechat::field('id, type')->select();
  886. View::assign('miniprogram', $miniprogram);
  887. return View::fetch();
  888. }
  889. }