Users.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. namespace app\api\controller;
  3. use app\event\Msg;
  4. use app\logics\EmployeeLogic;
  5. use app\logics\OrgLogic;
  6. use app\model\Company;
  7. use app\model\CustomerClue;
  8. use app\model\Employee;
  9. use app\model\Miniprogram;
  10. use app\model\Org;
  11. use app\model\User as ModelUser;
  12. use app\model\UserSignLog;
  13. use Firebase\JWT\JWT;
  14. use openssl\Aes;
  15. use app\model\CampEmployee;
  16. use app\model\Camp as CampModel;
  17. use wx\miniprogram\User as MiniprogramUser;
  18. use app\model\AgentUser;
  19. use app\model\Wechat;
  20. use app\model\CreditsSetting;
  21. use app\model\AgentIntegral;
  22. use toolkits\Aec;
  23. use app\model\AgentType;
  24. class Users extends Base
  25. {
  26. /**
  27. * 用户登陆
  28. * @return json [code:状态,token:jwt_token, exist:是否已注册]
  29. *
  30. * 装修宝mini是薛鹊主体下的小程序,unionid 不同
  31. */
  32. public function code2session($code)
  33. {
  34. // 小程序类型获取
  35. $miniType = $this->request->param('client_type');
  36. $client = $this->request->param('client');
  37. // 查找小程序根部门
  38. if (!empty($miniType)) {
  39. $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
  40. $rootOrg = $miniprogram->root_id;
  41. $miniprogramId = Company::where('root_id', $rootOrg)->value('mini');
  42. $miniprogram = Wechat::findOrEmpty($miniprogramId);
  43. } else {
  44. $defaultRootOrg = Miniprogram::where([['notify', '=', config('app.cgi')]])->value('root_id');
  45. if (empty($client)) {
  46. $miniprogram = Wechat::findOrEmpty(1);
  47. } else {
  48. $miniprogram = Wechat::where('appid', $client)->findOrEmpty();
  49. }
  50. }
  51. if ($miniprogram->isEmpty()) return json(['code' => self::error_msg, 'msg' => '未授权的小程序']);
  52. // code获取用户信息
  53. $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
  54. if (isset($rs['errcode']) && $rs['errcode'] != 0) {
  55. return json(['code' => self::error_debug_msg, 'msg' => $rs['errmsg']]);
  56. }
  57. // 初始token
  58. $token = [
  59. 'openid' => $rs['openid'],
  60. 'unionid' => $rs['unionid'],
  61. 'session_key' => $rs['session_key'],
  62. 'client_type' => $miniType,
  63. 'isEmployee' => false,
  64. 'mini' => $miniprogram->id
  65. ];
  66. $multi = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->count(); // 查询用户数量
  67. if ($multi == 1) {
  68. $user = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->find();
  69. if (!empty($rootOrg) && $user->root_id != $rootOrg) $user = null;
  70. } elseif ($multi > 1) {
  71. if (!empty($rootOrg)) {
  72. $uidList = ModelUser::whereOr([[['unionid', '=', $rs['unionid']], ['root_id', '=', $rootOrg]], [['mini_openid', '=', $rs['openid']], ['root_id', '=', $rootOrg]]])->column('id');
  73. $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职', '待确认']]];
  74. } else { // 未标记登录的企业
  75. /** 检查是否是员工 */
  76. $uidList = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->column('id');
  77. $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职']], ['disable', '=', 0]];
  78. }
  79. // 查询所属企业并判断企业是否正常
  80. $rootIdList = Employee::where($condition)->column('root_id');
  81. if (!empty($rootIdList)) {
  82. $rootIdList = Company::where([['root_id', 'in', $rootIdList], ['status', '=', 0], ['end_date', '>=', date('Y-m-d')]])->column('root_id');
  83. }
  84. // 获取最近登录的uid;
  85. if (!empty($rootIdList)) {
  86. $uidList = Employee::where([['uid', 'in', $uidList], ['state', 'in', ['在职']], ['root_id', 'in', $rootIdList]])->column('uid');
  87. }
  88. $user = ModelUser::where([['id', 'in', $uidList]])->order('updatetime desc')->find();
  89. }
  90. if (empty($user)) {
  91. $data = [
  92. 'mini_openid' => $rs['openid'],
  93. 'unionid' => $rs['unionid'],
  94. 'root_id' => $rootOrg ?? $defaultRootOrg,
  95. 'updatetime' => date('Y-m-d H:i:s')
  96. ];
  97. $user = ModelUser::create($data);
  98. } else {
  99. $user->updatetime = date('Y-m-d H:i:s');
  100. if (empty($user->unionid)) $user->unionid = $rs['unionid'];
  101. $user->mini_openid = $rs['openid'];
  102. $user->save();
  103. }
  104. $rootOrg = $user->root_id;
  105. $token['root_org'] = $rootOrg;
  106. $token['client_type'] = Miniprogram::where(['root_id' => $rootOrg])->value('notify');
  107. $token['uid'] = $user->id;
  108. $userData = $user->visible(['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone'])->toArray();
  109. $userData['sign'] = UserSignLog::where([['user_id', '=', $user->id], ['date', '=', date('Y-m-d')]])->count();
  110. $info = Company::where('root_id', $rootOrg)->find();
  111. if ($info['end_date'] < date('Y-m-d') || $info['status'] == 1) { //账号过期或关闭
  112. $employee = false;
  113. } else {
  114. $root_id = Company::where("status=0 and end_date>='" . date('Y-m-d') . "'")->column('root_id');
  115. $ew[] = ['root_id', 'in', $root_id];
  116. $employee = Employee::where($ew)->where([['uid', '=', $user->id], ['state', 'in', ['在职', '待审核']]])->order('state asc')->find();
  117. }
  118. if ($employee) {
  119. $updateData = ['updatetime' => date('Y-m-d H:i:s')];
  120. if (empty($employee->unionid)) $updateData['unionid'] = $user->unionid;
  121. $employee->save($updateData);
  122. //更新uid关联所有店面的公众号id
  123. $this->updateOfficialOpenid($user->unionid, $employee->id);
  124. // 查询是设计部门还是销售部门
  125. $orgType = Org::where('id', $employee->org_id)->value('org_type');
  126. // 判断是否小程序禁入
  127. if ($employee->disable == 0) {
  128. $userData['binded'] = [
  129. 'id' => $employee->id,
  130. 'name' => $employee->name,
  131. 'org_name' => $employee->org->name,
  132. 'org_id' => $employee->org_id,
  133. 'isManager' => $employee->is_manager,
  134. 'isNewbie' => $employee->is_newbie,
  135. 'state' => $employee->state,
  136. 'qrcode' => $employee->qrcode,
  137. 'org_type' => $orgType
  138. ];
  139. }
  140. $token['isEmployee'] = true;
  141. $token['employee_id'] = $employee->id;
  142. $token['org_id'] = $employee->org_id;
  143. $token['org_type'] = $orgType;
  144. $token['isManager'] = $employee->is_manager;
  145. $token['name'] = $employee->name;
  146. }
  147. $check = $token;
  148. $shareuser = null;
  149. $share = $this->request->param('share');
  150. $share_agent_id = $this->request->param('share_agent_id'); //通过经纪人分享打开的
  151. $share_agent_id = isset($share_agent_id) && !empty($share_agent_id) ? $share_agent_id : 0;
  152. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  153. if ($share) {
  154. $shareuser = ModelUser::where(['id' => $share, 'root_id' => $rootOrg])->field('id,nickname,headimgurl,phone')->find();
  155. if ($shareuser) {
  156. $shareBind = Employee::where(['uid' => $shareuser->id, 'state' => '在职', 'root_id' => $rootOrg])->find();
  157. if (!$shareBind) {
  158. // 如果分享人已离职
  159. $shareBind = Employee::where([['uid', '=', $shareuser->id], ['state', 'in', ['离职', '待审核']], ['root_id', '=', $rootOrg]])->order('id desc')->find();
  160. if ($shareBind) {
  161. $shareBind = Employee::with(['user'])->where(['id' => $shareBind['left_to_empid']])->find();
  162. $shareuser->nickname = $shareBind->name;
  163. $shareuser->phone = $shareBind->phone;
  164. $shareuser->headimgurl = $shareBind->user->headimgrul;
  165. $shareuser->id = $shareBind->user->id;
  166. $shareuser->str = '';
  167. }
  168. } else {
  169. $shareuser->str = $aec->encrypt($rootOrg.'#'.$shareBind->id);
  170. $shareuser->nickname = $shareBind->name;
  171. $shareuser->phone = $shareBind->phone;
  172. }
  173. $token['share'] = $shareuser->id;
  174. if ($shareBind && $shareBind['state'] == '在职') {
  175. $token['share_employee'] = $shareBind->id;
  176. $token['share_org'] = $shareBind->org_id;
  177. $token['share_agent_id'] = $share_agent_id; //经纪人分享的
  178. $shareuser['name'] = $shareBind->name;
  179. $shareuser['qrcode'] = $shareBind->qrcode;
  180. }
  181. if (!$token['isEmployee']) {
  182. $this->addClue([
  183. 'uid' => $userData['id'],
  184. 'employee_id' => $token['share_employee'] ?? 0,
  185. 'org_id' => $token['share_org'] ?? $token['root_org'],
  186. 'agent_id' => $share_agent_id
  187. ], $token['root_org']);
  188. }
  189. }
  190. }
  191. $company = Company::where('root_id', $rootOrg)->find()->toArray();
  192. $company['client_type'] = $token['client_type'];
  193. // 信息加密
  194. $data = http_build_query($token);
  195. $aes = new Aes(config('app.jwt_key'));
  196. $key = $aes->encrypt($data);
  197. // token数据设置
  198. $payload = array(
  199. "iss" => "https://" . $this->request->domain(),
  200. "aud" => 'mini',
  201. "iat" => time(),
  202. "nbf" => time(),
  203. "data" => $key
  204. );
  205. // 自定义登陆状态
  206. $token = JWT::encode($payload, config('app.jwt_key'));
  207. $share_str = '';
  208. if ($check['isEmployee']) $share_str = $aec->encrypt($rootOrg.'#'.$check['employee_id']);
  209. $returnData = ['code' => self::success, 'token' => $token, 'user' => $userData, 'company' => $company['company_name'], 'company_data' => $company,'str'=>$share_str];
  210. empty($shareuser) ?: $returnData['share'] = $shareuser;
  211. //是否在训练中
  212. if ($check['isEmployee']) {
  213. $camps = CampModel::where([['root_id', '=', $check['root_org']], ['del', '=', 0]])->column('id');
  214. if (!$camps) {
  215. $returnData['camp'] = 0;
  216. } else {
  217. $tw[] = ['camp_id', 'in', $camps];
  218. $returnData['camp'] = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $check['employee_id']], ['root_id', '=', $check['root_org']]])->where($tw)->count();
  219. }
  220. } else {
  221. $returnData['camp'] = 0;
  222. }
  223. //查询是否是经纪人
  224. $agent = AgentUser::where([['uid', '=', $check['uid']], ['root_id', '=', $check['root_org']], ['status', '=', 1]])->find();
  225. $returnData['isagent'] = !empty($agent) ? $agent['id'] : 0;
  226. $returnData['agent_name'] = !empty($agent) ? $agent['agent_name'] : '';
  227. //如果是经纪人返回关联业务员的信息,用于在经纪人端使用
  228. if (!empty($agent)) {
  229. $agent_employee = Employee::with(['user'])->where(['id' => $agent['agent_employee_id']])->find();
  230. $returnData['share'] = [
  231. 'id' => $agent_employee->user->id,
  232. 'nickname' => $agent_employee->user->nickname,
  233. 'name' => $agent_employee->name,
  234. 'phone' => $agent_employee->phone,
  235. 'headimgurl' => $agent_employee->user->headimgrul,
  236. 'qrcode' => $agent_employee->qrcode
  237. ];
  238. $returnData['agent_employee_id'] = !empty($agent_employee) ? $agent_employee->uid : 0;
  239. }
  240. //是否关注公众号
  241. if ($check['isEmployee']) {
  242. $returnData['official_openid'] = Employee::where('id', $check['employee_id'])->value('official_openid') ? 1 : 0;
  243. } else {
  244. $returnData['official_openid'] = 1;
  245. }
  246. return json($returnData);
  247. }
  248. /**
  249. * 用户登陆
  250. * @return json [code:状态,token:jwt_token, exist:是否已注册]
  251. *
  252. * 装修宝mini是薛鹊主体下的小程序,unionid 不同
  253. */
  254. public function code2sessionmini($code)
  255. {
  256. // 小程序类型获取
  257. $miniType = $this->request->param('client_type');
  258. $client = $this->request->param('client');
  259. if (!empty($miniType) && !empty($client)) {
  260. //同时传client 和 client_type 表示 进入 client 小程序下的 client_type 店面
  261. $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
  262. $rootOrg = $miniprogram->root_id;
  263. $miniprogram = Wechat::where('id', $client)->findOrEmpty();
  264. }elseif (!empty($miniType)) {
  265. // 查找小程序根部门
  266. $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
  267. $rootOrg = $miniprogram->root_id;
  268. $miniprogramId = Company::where('root_id', $rootOrg)->value('mini');
  269. $miniprogram = Wechat::findOrEmpty($miniprogramId);
  270. } else {
  271. $defaultRootOrg = Miniprogram::where([['notify', '=', config('app.cgi')]])->value('root_id');
  272. if (empty($client)) {
  273. $miniprogram = Wechat::findOrEmpty(1);
  274. } else {
  275. $miniprogram = Wechat::where('id', $client)->findOrEmpty();
  276. }
  277. }
  278. if ($miniprogram->isEmpty()) return json(['code' => self::error_msg, 'msg' => '未授权的小程序']);
  279. // code获取用户信息
  280. $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
  281. if (isset($rs['errcode']) && $rs['errcode'] != 0) {
  282. return json(['code' => self::error_debug_msg, 'msg' => $rs['errmsg']]);
  283. }
  284. // 初始token
  285. $token = [
  286. 'openid' => $rs['openid'],
  287. 'unionid' => $rs['unionid'],
  288. 'session_key' => $rs['session_key'],
  289. 'client_type' => $miniType,
  290. 'isEmployee' => false,
  291. 'mini' => $miniprogram->id
  292. ];
  293. //原有主体
  294. $unionid_field = 'unionid';
  295. $openid_field = 'mini_openid';
  296. $multi = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->count(); // 查询用户数量
  297. if ($multi == 1) {
  298. $user = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->find();
  299. if (!empty($rootOrg) && $user->root_id != $rootOrg) $user = null;
  300. } elseif ($multi > 1) {
  301. if (!empty($rootOrg)) {
  302. $uidList = ModelUser::whereOr([[[$unionid_field, '=', $rs['unionid']], ['root_id', '=', $rootOrg]], [[$openid_field, '=', $rs['openid']], ['root_id', '=', $rootOrg]]])->column('id');
  303. $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职', '待确认']]];
  304. } else { // 未标记登录的企业
  305. /** 检查是否是员工 */
  306. $uidList = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->column('id');
  307. $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职']], ['disable', '=', 0]];
  308. }
  309. // 查询所属企业并判断企业是否正常
  310. $rootIdList = Employee::where($condition)->column('root_id');
  311. if (!empty($rootIdList)) {
  312. $rootIdList = Company::where([['root_id', 'in', $rootIdList], ['status', '=', 0], ['end_date', '>=', date('Y-m-d')]])->column('root_id');
  313. }
  314. // 获取最近登录的uid;
  315. if (!empty($rootIdList)) {
  316. $uidList = Employee::where([['uid', 'in', $uidList], ['state', 'in', ['在职']], ['root_id', 'in', $rootIdList]])->column('uid');
  317. }
  318. $user = ModelUser::where([['id', 'in', $uidList]])->order('updatetime desc')->find();
  319. }
  320. if (empty($user)) {
  321. $data = [
  322. $openid_field => $rs['openid'],
  323. $unionid_field => $rs['unionid'],
  324. 'root_id' => $rootOrg ?? $defaultRootOrg,
  325. 'updatetime' => date('Y-m-d H:i:s')
  326. ];
  327. $user = ModelUser::create($data);
  328. } else {
  329. $user->updatetime = date('Y-m-d H:i:s');
  330. if (empty($user->$unionid_field)) $user->$unionid_field = $rs['unionid'];
  331. $user->$openid_field = $rs['openid'];
  332. $user->save();
  333. }
  334. $rootOrg = $user->root_id;
  335. $token['root_org'] = $rootOrg;
  336. $token['client_type'] = Miniprogram::where(['root_id' => $rootOrg])->value('notify');
  337. $token['uid'] = $user->id;
  338. $userData = $user->visible(['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone'])->toArray();
  339. $userData['sign'] = UserSignLog::where([['user_id', '=', $user->id], ['date', '=', date('Y-m-d')]])->count();
  340. $info = Company::where('root_id', $rootOrg)->find();
  341. if ($info['end_date'] < date('Y-m-d') || $info['status'] == 1) { //账号过期或关闭
  342. $employee = false;
  343. } else {
  344. $root_id = Company::where("status=0 and end_date>='" . date('Y-m-d') . "'")->column('root_id');
  345. $ew[] = ['root_id', 'in', $root_id];
  346. $employee = Employee::where($ew)->where([['uid', '=', $user->id], ['state', 'in', ['在职', '待审核']]])->order('state asc')->find();
  347. }
  348. if ($employee) {
  349. $updateData = ['updatetime' => date('Y-m-d H:i:s')];
  350. if (empty($employee->$unionid_field)) $updateData[$unionid_field] = $user->$unionid_field;
  351. $employee->save($updateData);
  352. //更新uid关联所有店面的公众号id
  353. $this->xqupdateOfficialOpenid($user->$unionid_field, $employee->id);
  354. // 查询是设计部门还是销售部门
  355. $orgType = Org::where('id', $employee->org_id)->value('org_type');
  356. // 判断是否小程序禁入
  357. if ($employee->disable == 0) {
  358. $userData['binded'] = [
  359. 'id' => $employee->id,
  360. 'name' => $employee->name,
  361. 'org_name' => $employee->org->name,
  362. 'org_id' => $employee->org_id,
  363. 'isManager' => $employee->is_manager,
  364. 'isNewbie' => $employee->is_newbie,
  365. 'state' => $employee->state,
  366. 'qrcode' => $employee->qrcode,
  367. 'org_type' => $orgType
  368. ];
  369. }
  370. $token['isEmployee'] = true;
  371. $token['employee_id'] = $employee->id;
  372. $token['org_id'] = $employee->org_id;
  373. $token['org_type'] = $orgType;
  374. $token['isManager'] = $employee->is_manager;
  375. $token['name'] = $employee->name;
  376. }
  377. $check = $token;
  378. $shareuser = null;
  379. $share = $this->request->param('share');
  380. $share_agent_id = $this->request->param('share_agent_id'); //通过经纪人分享打开的
  381. $share_agent_id = isset($share_agent_id) && !empty($share_agent_id) ? $share_agent_id : 0;
  382. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  383. if ($share) {
  384. $shareuser = ModelUser::where(['id' => $share, 'root_id' => $rootOrg])->field('id,nickname,headimgurl,phone')->find();
  385. if ($shareuser) {
  386. $shareBind = Employee::where(['uid' => $shareuser->id, 'state' => '在职', 'root_id' => $rootOrg])->find();
  387. if (!$shareBind) {
  388. // 如果分享人已离职
  389. $shareBind = Employee::where([['uid', '=', $shareuser->id], ['state', 'in', ['离职', '待审核']], ['root_id', '=', $rootOrg]])->order('id desc')->find();
  390. if ($shareBind) {
  391. $shareBind = Employee::with(['user'])->where(['id' => $shareBind['left_to_empid']])->find();
  392. $shareuser->nickname = $shareBind->name;
  393. $shareuser->phone = $shareBind->phone;
  394. $shareuser->headimgurl = $shareBind->user->headimgrul;
  395. $shareuser->id = $shareBind->user->id;
  396. $shareuser->str = '';
  397. }
  398. } else {
  399. $shareuser->str = $aec->encrypt($rootOrg.'#'.$shareBind->id);
  400. $shareuser->nickname = $shareBind->name;
  401. $shareuser->phone = $shareBind->phone;
  402. }
  403. $token['share'] = $shareuser->id;
  404. if ($shareBind && $shareBind['state'] == '在职') {
  405. $token['share_employee'] = $shareBind->id;
  406. $token['share_org'] = $shareBind->org_id;
  407. $token['share_agent_id'] = $share_agent_id; //经纪人分享的
  408. $shareuser['name'] = $shareBind->name;
  409. $shareuser['qrcode'] = $shareBind->qrcode;
  410. }
  411. if (!$token['isEmployee']) {
  412. $this->addClue([
  413. 'uid' => $userData['id'],
  414. 'employee_id' => $token['share_employee'] ?? 0,
  415. 'org_id' => $token['share_org'] ?? $token['root_org'],
  416. 'agent_id' => $share_agent_id
  417. ], $token['root_org']);
  418. }
  419. }
  420. }
  421. $company = Company::where('root_id', $rootOrg)->find()->toArray();
  422. $company['client_type'] = $token['client_type'];
  423. // 信息加密
  424. $data = http_build_query($token);
  425. $aes = new Aes(config('app.jwt_key'));
  426. $key = $aes->encrypt($data);
  427. // token数据设置
  428. $payload = array(
  429. "iss" => "https://" . $this->request->domain(),
  430. "aud" => 'mini',
  431. "iat" => time(),
  432. "nbf" => time(),
  433. "data" => $key
  434. );
  435. // 自定义登陆状态
  436. $share_str = '';
  437. $token = JWT::encode($payload, config('app.jwt_key'));
  438. if ($check['isEmployee']) $share_str = $aec->encrypt($rootOrg.'#'.$check['employee_id']);
  439. $returnData = ['code' => self::success, 'token' => $token, 'user' => $userData, 'company' => $company['company_name'], 'company_data' => $company,'str'=>$share_str];
  440. empty($shareuser) ?: $returnData['share'] = $shareuser;
  441. //是否在训练中
  442. if ($check['isEmployee']) {
  443. $camps = CampModel::where([['root_id', '=', $check['root_org']], ['del', '=', 0]])->column('id');
  444. if (!$camps) {
  445. $returnData['camp'] = 0;
  446. } else {
  447. $tw[] = ['camp_id', 'in', $camps];
  448. $returnData['camp'] = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $check['employee_id']], ['root_id', '=', $check['root_org']]])->where($tw)->count();
  449. }
  450. } else {
  451. $returnData['camp'] = 0;
  452. }
  453. //查询是否是经纪人
  454. $agent = AgentUser::where([['uid', '=', $check['uid']], ['root_id', '=', $check['root_org']], ['is_review', '=', 2], ['status', '=', 1]])->find();
  455. $returnData['isagent'] = !empty($agent) ? $agent['id'] : 0;
  456. //如果是经纪人返回关联业务员的信息,用于在经纪人端使用
  457. if (!empty($agent)) {
  458. $agent_employee = Employee::with(['user'])->where(['id' => $agent['agent_employee_id']])->find();
  459. $returnData['share'] = [
  460. 'id' => $agent_employee->user->id,
  461. 'nickname' => $agent_employee->user->nickname,
  462. 'name' => $agent_employee->name,
  463. 'phone' => $agent_employee->phone,
  464. 'headimgurl' => $agent_employee->user->headimgrul,
  465. 'qrcode' => $agent_employee->qrcode
  466. ];
  467. $returnData['agent_employee_id'] = !empty($agent_employee) ? $agent_employee->uid : 0;
  468. }
  469. //是否关注公众号
  470. if ($check['isEmployee']) {
  471. $returnData['official_openid'] = Employee::where('id', $check['employee_id'])->value('official_openid') ? 1 : 0;
  472. if (isset($employee)) event('SysOperate', [$employee, '小程序登录']);//登录日志
  473. } else {
  474. $returnData['official_openid'] = 1;
  475. }
  476. //test测试号无法关联小程序
  477. $returnData['official_openid'] = 1;
  478. return json($returnData);
  479. }
  480. /**
  481. * 关注公众号
  482. */
  483. private function updateOfficialOpenid($unionid, $eid)
  484. {
  485. $where[] = ['unionid', '=', $unionid];
  486. $where[] = ['official_openid', '<>', ''];
  487. $official_openid = Employee::where($where)->order('id desc')->value('official_openid');
  488. if ($official_openid) {
  489. Employee::where('id', $eid)->update(['official_openid' => $official_openid]);
  490. }
  491. return;
  492. }
  493. /**
  494. * 关注公众号
  495. */
  496. private function xqupdateOfficialOpenid($unionid, $eid)
  497. {
  498. return;
  499. }
  500. private function addClue($data, $root_id)
  501. {
  502. // 如果线索是员工(包含运维,已离职,待审核)
  503. $isEmployee = Employee::where([['uid', '=', $data['uid']], ['root_id', '=', $root_id], ['state', 'in', ['在职', '待审核']]])->find();
  504. if ($isEmployee) return;
  505. // 如果线索已经获取过,更新时间
  506. if ($data['employee_id'] == 0) { // 判断是否有员工已获取
  507. CustomerClue::where([['uid', '=', $data['uid']]])->update(['updatetime' => date('Y-m-d H:i:s')]);
  508. } else { // 判读是否重复获取
  509. $clue = CustomerClue::where([['uid', '=', $data['uid']], ['employee_id', '=', $data['employee_id']]])->find();
  510. if (empty($clue) && $data['employee_id'] !== 0) {
  511. CustomerClue::create($data);
  512. //增加经纪人分享的产生线索增加积分功能
  513. if(!empty($data['agent_id'])){
  514. $this->agent_clue($data,$root_id);
  515. }
  516. } else {
  517. $clue->updatetime = date('Y-m-d H:i:s');
  518. $clue->save();
  519. }
  520. }
  521. //查询是否获取过手机号
  522. $phone = ModelUser::where('id',$data['uid'])->value('phone');
  523. if ($phone) {
  524. CustomerClue::where([['uid', '=', $data['uid']],['phone','NULL',null]])->update(['phone' =>$phone]);
  525. }
  526. }
  527. /**
  528. * 经纪人分享产生线索计算
  529. */
  530. public function agent_clue($data,$root_id)
  531. {
  532. $rule_type = 'produceclue_integral';
  533. $agent = AgentUser::where([['root_id','=',$root_id],['id','=',$data['agent_id']]])->field('id,type')->find();
  534. $rule = AgentType::where([['root_id','=',$root_id],['id','=',$agent['type']]])->find();
  535. $rule_list = !empty($rule['count']) ? json_decode($rule['count'],true) : '';
  536. $itg_rule = !empty($rule_list) ? $rule_list[$rule_type] : 0;
  537. //$itg_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $root_id]])->value('value');
  538. $state = '经纪人分享产生线索';
  539. $add = array(
  540. 'agent_id' => $data['agent_id'],
  541. 'type' => 6,
  542. 'integral' => $itg_rule,
  543. 'addtime' => time(),
  544. 'state' => $state,
  545. 'customer_id' => 0
  546. );
  547. //如果设置了产生线索送积分再增加记录
  548. if(!empty($itg_rule)){
  549. AgentIntegral::insert($add);
  550. }
  551. }
  552. /**
  553. * 授权获取用户信息(客户小程序调用)
  554. */
  555. public function setUserInfo($encryptedData, $iv, $signature, $rawData)
  556. {
  557. $request = request();
  558. $token = $request->token;
  559. // 计算签名是否正确
  560. $sign = sha1($rawData . $token['session_key']);
  561. if ($sign != $signature) {
  562. return json(['code' => self::error_debug_msg, 'msg' => '签名验证失败']);
  563. }
  564. $miniprogram = Wechat::where([['id', '=', $token['mini']]])->find();
  565. // 解析获取用户数据
  566. $mini = new MiniprogramUser();
  567. $data = $mini->getInfo($miniprogram->appid, $token['session_key'], $encryptedData, $iv);
  568. ModelUser::where([['unionid', '=', $request->token['unionid']], ['root_id', '=', $token['root_org']]])->update([
  569. 'city' => $data['city'],
  570. 'province' => $data['province'],
  571. 'country' => $data['country'],
  572. 'sex' => $data['gender'],
  573. 'nickname' => $data['nickName'],
  574. 'headimgurl' => $data['avatarUrl']
  575. ]);
  576. // 更新token
  577. if (!$token['isEmployee']) {
  578. // 添加客户线索xg
  579. $this->addClue([
  580. 'uid' => $token['uid'],
  581. 'employee_id' => $token['share_employee'] ?? 0,
  582. 'org_id' => $token['share_org'] ?? $token['root_org'],
  583. 'agent_id' => $token['share_agent_id'] ?? 0
  584. ], $token['root_org']);
  585. }
  586. return json(['code' => self::success, 'msg' => '信息保存成功']);
  587. }
  588. /**
  589. * 授权获取并保存用户手机号
  590. */
  591. public function setUserMobile($encryptedData, $iv)
  592. {
  593. $request = request();
  594. $miniprogram = Wechat::where([['id', '=', $request->token['mini']]])->find();
  595. $mini = new MiniprogramUser();
  596. $data = $mini->getInfo($miniprogram->appid, $request->token['session_key'], $encryptedData, $iv);
  597. ModelUser::where([['unionid|xq_unionid', '=', $request->token['unionid']], ['root_id', '=', $request->token['root_org']]])->update(['phone' => $data['phoneNumber']]);
  598. // 线索更新手机号
  599. if (!$request->token['isEmployee'] && !empty($request->token['share_employee'])) {
  600. CustomerClue::where([
  601. 'uid' => $request->token['uid'],
  602. 'employee_id' => $request->token['share_employee']
  603. ])->update(['phone' => $data['phoneNumber']]);
  604. }
  605. return json(['code' => self::success, 'msg' => '信息保存成功', 'mobile' => $data['phoneNumber']]);
  606. }
  607. /**
  608. * 员工注册
  609. */
  610. public function register()
  611. {
  612. $params = request()->param(['is_manager', 'name', 'orgid', 'recruit' => '']);
  613. $result = EmployeeLogic::addemployee($params, $msg);
  614. if ($result) {
  615. $leader = Employee::where(['org_id' => $params['orgid'], 'is_manager' => 1])->column('id');
  616. event(new Msg($leader, '您接收到一条新审批,请点击前往审阅', 'register'));
  617. return json(['code' => 0, 'msg' => '成功提交', 'data' => $result]);
  618. } else {
  619. return json(['code' => 1, 'msg' => $msg]);
  620. }
  621. }
  622. /**
  623. * 组织结构
  624. */
  625. public function org()
  626. {
  627. $token = request()->token;
  628. $data = OrgLogic::struc($token['root_org']);
  629. return json(['code' => 0, 'data' => $data]);
  630. }
  631. /**
  632. * 装修宝mini 装修宝pro数据同步
  633. */
  634. public function synchronizationData()
  635. {
  636. $code = input('code','');
  637. $encryptedData = input('encrypted','');
  638. $iv = input('iv','');
  639. //装修宝mini 配置
  640. $miniprogram = Wechat::where([['id', '=', 3]])->find();
  641. //code 获取用户信息
  642. $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
  643. if(!isset($rs['session_key'])) return json(['code' => 1, 'data' => 'code无效', 'msg' => 'code无效']);
  644. $mini = new MiniprogramUser();
  645. $data = $mini->getInfo($miniprogram->appid, $rs['session_key'], $encryptedData, $iv);
  646. if(!isset($data['phoneNumber'])) return json(['code' => 1, 'data' => '解析失败', 'msg' => '解析失败']);
  647. //手机号查询 装修宝pro 账号信息
  648. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  649. $phone = $aec->encrypt($data['phoneNumber']);
  650. $where = [['phone','=',$phone],['uid','>',0]];
  651. $emp = Employee::where($where)->column('id,uid');
  652. if($emp){
  653. $eids = array_column($emp,'id');
  654. $uids = array_column($emp,'uid');
  655. Employee::where([['id','in',$eids]])->update(['xq_unionid'=>$rs['unionid'],'xq_mini_openid'=>$rs['openid']]);
  656. ModelUser::where([['id','in',$uids]])->update(['xq_unionid'=>$rs['unionid'],'xq_mini_openid'=>$rs['openid']]);
  657. }else{
  658. return json(['code' => 0, 'msg' => '未查询到关联账号']);
  659. }
  660. return json(['code' => 0, 'data' => '同步完成']);
  661. }
  662. }