Wework.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace app\api\controller;
  3. use app\logics\SmsLogic;
  4. use app\logics\UserLogic;
  5. use app\model\Employee;
  6. use app\model\Miniprogram;
  7. use app\model\User as ModelUser;
  8. use app\model\WeworkBindTicket;
  9. use app\model\WeworkCompany;
  10. use app\model\WeworkUser;
  11. use EasyWeChat\Factory;
  12. use Firebase\JWT\JWT;
  13. use openssl\Aes;
  14. use think\facade\Config;
  15. use think\facade\Log;
  16. use app\model\Company;
  17. use toolkits\Aec;
  18. /**
  19. * 企业微信登录相关 (第三方应用)
  20. * Class Wework
  21. * @package app\apiSingle\controller
  22. */
  23. class Wework extends Base
  24. {
  25. /**
  26. * 获取授权链接
  27. */
  28. public function getAuthLink(){
  29. $config = Config::get('app.wework');
  30. $app = Factory::openWork($config);
  31. $redirectUrl = input('redirectUrl', '', 'trim');
  32. $link = $app->corp->getOAuthRedirectUrl($redirectUrl, 'snsapi_userinfo', $state = null);
  33. return json(['code' => self::success, 'data' => $link, 'msg' => '获取成功']);
  34. }
  35. /**
  36. * 根据授权code换取用户信息,查询到信息则返回token,没有则返回对应的状态节点信息。
  37. * @return \think\response\Json
  38. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getUserByCode(){
  44. $code = input('code', '', 'trim');
  45. $config = Config::get('app.wework');
  46. $app = Factory::openWork($config);
  47. $info = $app->corp->getUserByCode($code);
  48. if ($info['errcode'] == 0) {
  49. $where['corpid'] = $info['CorpId'];
  50. $where['userid'] = $info['UserId'];
  51. $find = (new WeworkUser())->where($where)->find();
  52. $wx_uid = 0;
  53. if (empty($find)) {
  54. $data['corpid'] = $info['CorpId'];
  55. $data['userid'] = $info['UserId'];
  56. $data['deviceid'] = !empty($info['DeviceId']) ? $info['DeviceId'] : '';
  57. $data['open_userid'] = !empty($info['open_userid']) ? $info['open_userid'] : '';
  58. $wx_result = WeworkUser::create($data);
  59. $wx_uid = $wx_result->id;
  60. } else {
  61. $wx_uid = $find['id'];
  62. }
  63. // 查询用户所在企业是否有开通应用
  64. $wework_company = WeworkCompany::where('auth_corpid', '=', $info['CorpId'])->find();
  65. $company_find = Company::where(['wework_company'=> $wework_company['id']])->find();
  66. if (empty($company_find)) {
  67. // return json(['code'=> 2, 'msg'=> '所在企业未绑定装企公司']);
  68. return json(['code'=> 3, 'msg'=> '请先登录', 'data'=> ['wx_cid'=> $wework_company['id'], 'wx_uid'=> $wx_uid]]);
  69. }
  70. // 查询是否有绑定员工
  71. $employee = Employee::where('wework_uid', '=', $wx_uid)->find();
  72. if (empty($employee)) {
  73. return json(['code'=> 3, 'msg'=> '请先登录', 'data'=> ['wx_cid'=> $wework_company['id'], 'wx_uid'=> $wx_uid]]);
  74. } else {
  75. // 查询到员工,返回token
  76. $token = [
  77. 'root_org' => $employee['root_id'],
  78. 'isEmployee' => true,
  79. 'uid' => $employee['uid'],
  80. 'employee_id'=> $employee['id'],
  81. 'org_id' => $employee['org_id']
  82. ];
  83. // 信息加密
  84. $data = http_build_query($token);
  85. $aes = new Aes(config('app.jwt_key'));
  86. $key = $aes->encrypt($data);
  87. // token数据设置
  88. $payload = array(
  89. "iss" => "https://" . request()->domain(),
  90. "aud" => 'mini',
  91. "iat" => time(),
  92. "nbf" => time(),
  93. "data" => $key
  94. );
  95. // 自定义登陆状态
  96. $token = JWT::encode($payload, config('app.jwt_key'));
  97. $logic = new UserLogic();
  98. $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
  99. $company = Company::where('root_id', $employee['root_id'])->find();
  100. $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
  101. $returnData = ['code' => self::success, 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
  102. return json($returnData);
  103. }
  104. } else {
  105. return json(['code'=> self::error_msg, 'msg'=> '获取授权信息失败', 'data'=> '']);
  106. }
  107. }
  108. /**
  109. * 获取手机验证码
  110. */
  111. public function sms($phone)
  112. {
  113. $content = '验证码:%code%(请勿转告他人),有效时间15分钟,请尽快完成验证';
  114. $smsLogic = new SmsLogic();
  115. $rs = $smsLogic->sendSms($phone, 'employee_login_phone', $content);
  116. if (!$rs) return json(['code' => 1, 'msg' => '短信发送失败']);
  117. return json(['code' => 0, 'msg' => '短信发送成功']);
  118. }
  119. /**
  120. * 手机号验证码登陆
  121. */
  122. public function phone_login($phone)
  123. {
  124. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  125. $phone = $aec->encrypt($phone);
  126. $wx_cid = input('wx_cid', '', 'intval');
  127. $wx_uid = input('wx_uid', '', 'intval');
  128. // 判断是否是违法请求
  129. $wx_company = WeworkCompany::find($wx_cid);
  130. $wx_user = WeworkUser::find($wx_uid);
  131. if ($wx_company['auth_corpid'] !== $wx_user['corpid']){
  132. return json(['code'=> self::error_msg, 'msg'=> '登录失败,请联系管理员']);
  133. }
  134. $company = Company::where(['wework_company'=> $wx_cid])->find();
  135. // 无绑定公司,判断是否是管理员
  136. if (empty($company)) {
  137. $companys = Employee::with(['company', 'companys'])->where('phone', '=', $phone)->where('grant_id', '<>', 0)->count();
  138. if ($companys > 0) {
  139. $ticket = md5(time());
  140. $t_data['wx_uid'] = $wx_uid;
  141. $t_data['wx_cid'] = $wx_cid;
  142. $t_data['phone'] = $phone;
  143. $t_data['ticket'] = $ticket;
  144. $t_data['status'] = 0;
  145. WeworkBindTicket::create($t_data);
  146. return json(['code'=> 2, 'msg'=> '请绑定企业', 'data'=> $ticket]);
  147. } else {
  148. return json(['code'=> self::error_msg, 'msg'=> '登录失败,请联系管理员']);
  149. }
  150. } else {
  151. if ($company['status'] == 1) {
  152. return json(['code' => self::error_msg, 'msg' => '账号被禁用,请联系管理员']);
  153. } elseif ($company['end_date'] . ' 23:59:59' < date('Y-m-d H:i:s', time())) {
  154. return json(['code' => self::error_msg, 'msg' => '账号已过期,请联系管理员']);
  155. }
  156. $employee = Employee::where([['phone', '=', $phone], ['root_id', '=', $company['root_id']]])->find();
  157. if (empty($employee)) return json(['code' => self::error_msg, 'msg' => '账户不存在']);
  158. // 关联企业微信用户信息到系统用户
  159. Employee::where(['id'=> $employee['id']])->update(['wework_uid'=> $wx_uid]);
  160. // 查询到员工,返回token
  161. $token = [
  162. 'root_org' => $employee['root_id'],
  163. 'isEmployee' => true,
  164. 'uid' => $employee['uid'],
  165. 'employee_id'=> $employee['id'],
  166. 'org_id' => $employee['org_id']
  167. ];
  168. // 信息加密
  169. $data = http_build_query($token);
  170. $aes = new Aes(config('app.jwt_key'));
  171. $key = $aes->encrypt($data);
  172. // token数据设置
  173. $payload = array(
  174. "iss" => "https://" . request()->domain(),
  175. "aud" => 'mini',
  176. "iat" => time(),
  177. "nbf" => time(),
  178. "data" => $key
  179. );
  180. // 自定义登陆状态
  181. $token = JWT::encode($payload, config('app.jwt_key'));
  182. $logic = new UserLogic();
  183. $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
  184. $company = Company::where('root_id', $employee['root_id'])->find();
  185. $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
  186. $returnData = ['code' => self::success, 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
  187. return json($returnData);
  188. }
  189. }
  190. /**
  191. * 获取手机号所管理企业列表
  192. */
  193. public function getCompanys(){
  194. $ticket = input('ticket', '', 'trim');
  195. $find = WeworkBindTicket::where(['ticket'=> $ticket, 'status'=> 0])->find();
  196. if (empty($find)) {
  197. return json(['code' => self::error_msg, 'msg' => '没有可绑定的企业']);
  198. }
  199. $employees = Employee::where([['phone', '=', $find['phone']], ['state', '=', '在职'], ['is_manager', '=', 1]])->select()->toArray();
  200. $companyList = Company::with(['brand'])->where([['root_id', 'in', array_column($employees, 'root_id')]])->select()->visible(['id','company_name', 'logo','root_id','status','end_date'])->toArray();
  201. foreach ($companyList as &$item) {
  202. $item['client_type'] = Miniprogram::where('root_id', '=', $item['root_id'])->value('notify');
  203. if ($item['status']==1) {
  204. $item['off'] = 1;
  205. $item['off_remark'] = '账号被禁用';
  206. }elseif ($item['end_date']<date('Y-m-d')) {
  207. $item['off'] = 1;
  208. $item['off_remark'] = '账号已过期';
  209. }else{
  210. $item['off'] = 0;
  211. $item['off_remark'] = '账号正常';
  212. }
  213. }
  214. return json(['code'=> self::success, 'data'=> $companyList, 'msg'=> '请求成功']);
  215. }
  216. /**
  217. * 绑定企业
  218. */
  219. public function bindCompany(){
  220. $ticket = input('ticket', '', 'trim');
  221. $company_id = input('company_id', '', 'intval');
  222. $t_find = WeworkBindTicket::where('ticket', '=', $ticket)->find();
  223. $t_find->status = 1;
  224. $t_find->save();
  225. $result = Company::where('id', '=', $company_id)->update(['wework_company'=> $t_find['wx_cid']]);
  226. if ($result !== false) {
  227. $company = Company::find($company_id);
  228. $employee = Employee::where([['phone', '=', $t_find['phone']], ['root_id', '=', $company['root_id']]])->find();
  229. if (empty($employee)) return json(['code' => self::error_msg, 'msg' => '账户不存在']);
  230. // 关联企业微信用户信息到系统用户
  231. Employee::where(['id'=> $employee['id']])->update(['wework_uid'=> $t_find['wx_uid']]);
  232. // 查询到员工,返回token
  233. $token = [
  234. 'root_org' => $employee['root_id'],
  235. 'isEmployee' => true,
  236. 'uid' => $employee['uid'],
  237. 'employee_id'=> $employee['id'],
  238. 'org_id' => $employee['org_id']
  239. ];
  240. // 信息加密
  241. $data = http_build_query($token);
  242. $aes = new Aes(config('app.jwt_key'));
  243. $key = $aes->encrypt($data);
  244. // token数据设置
  245. $payload = array(
  246. "iss" => "https://" . request()->domain(),
  247. "aud" => 'mini',
  248. "iat" => time(),
  249. "nbf" => time(),
  250. "data" => $key
  251. );
  252. // 自定义登陆状态
  253. $token = JWT::encode($payload, config('app.jwt_key'));
  254. $company = Company::where('root_id', $employee['root_id'])->find();
  255. $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
  256. $logic = new UserLogic();
  257. $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
  258. $returnData = ['code' => self::success, 'msg'=> '绑定成功', 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
  259. return json($returnData);
  260. } else {
  261. return json(['code'=> self::error_msg, 'msg'=> '绑定失败']);
  262. }
  263. }
  264. }