123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- declare(strict_types=1);
- namespace app\index\controller;
- use app\model\Camp;
- use app\model\CampEmployee;
- use app\model\Company;
- use app\model\Employee;
- use app\model\Miniprogram;
- use app\model\Org;
- use app\model\User;
- use app\model\UserSignLog;
- use Firebase\JWT\JWT;
- use openssl\Aes;
- use think\facade\View;
- use wx\Oplatform;
- class Mini
- {
- public function index($notify)
- {
- $zhuan = [
- 'lyjczs' => 'xkIVfO',
- 'test' => 'IQB5Nr',
- 'test1' => 'ATviy6'
- ];
- if (!isset($zhuan[$notify])) abort(404, '页面不存在');
- $notify = $zhuan[$notify];
- // 判断是否合法
- $had = Miniprogram::where(['notify' => $notify])->count();
- if ($had == 0) abort(404, '企业不存在');
- $aes = new Aes(config('app.jwt_key'));
- $appid = config('app.kfweb_appid');
- $state = $aes->encrypt(rand(1000, 9999) . $notify);
- $redirect_uri = urlencode(request()->domain() . "/crm/login.html");
- $wechatUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect";
- return redirect($wechatUrl);
- }
- /**
- * 授权登录
- *
- * @param string $code
- * @return array
- */
- public function login($code, $state)
- {
- $aes = new Aes(config('app.jwt_key'));
- $state = $aes->decrypt($state);
- if (!$state) abort(404, '网址不存在');
- $notify = substr($state, 4);
- // 判断是否合法
- $root_id = Miniprogram::where(['notify' => $notify])->value('root_id');
- if (empty($root_id)) abort(404, '企业不存在');
- $oplatdata = (new Oplatform())->getAccessToken(config('app.kfweb_appid'), config('app.kfweb_secret'), $code);
- if (!$oplatdata) {
- trace($oplatdata, 'error');
- abort(404, '请求失败,请重新输入网址!');
- }
- if (isset($oplatdata['errcode'])) { // code已经被使用
- if (in_array($oplatdata['errcode'], [40029, 40163, 41008, 42003])) {
- abort(404, '请求失败,请重新输入网址!');
- } else {
- trace($oplatdata, 'error');
- abort(404, $oplatdata['errmsg']);
- }
- }
- $userIds = User::where([['unionid', '=', $oplatdata['unionid']], ['root_id', '=', $root_id]])->column('id'); // 查询用户数量
- $condition = [
- ['root_id', '=', $root_id],
- ['uid', 'in', $userIds],
- ['state', '=', '在职']
- ];
- $emp = Employee::with('user')->where($condition)->find();
- if (empty($emp)) {
- echo '<h1>未加入该企业</h1>';
- exit;
- }
- $camps = Camp::where([['root_id', '=', $root_id], ['del', '=', 0]])->column('id');
- $tw[] = ['camp_id', 'in', $camps];
- $check = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $emp->id], ['root_id', '=', $root_id]])->where($tw)->count();
- if ($check) abort(404, '您尚未完成新兵课程,请在小程序中学习完毕后再来!');
- $company = Company::where('root_id', $root_id)->find();
- if (empty($emp) || $company['end_date'] < date('Y-m-d') || $company['status'] == 1) abort(404, '页面不存在!');
- // 查询是设计部门还是销售部门
- $orgType = Org::where('id', $emp->org_id)->value('org_type');
- // token
- $token = [
- 'root_org' => $root_id,
- 'employee_id' => $emp->id,
- 'org_id' => $emp->org_id,
- 'isEmployee' => true,
- 'isManager' => $emp->is_manager,
- 'org_type' => $orgType,
- 'uid' => $emp->uid
- ];
- // 信息加密
- $data = http_build_query($token);
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => request()->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $token = JWT::encode($payload, config('app.jwt_key'));
- // 员工签到
- $userData['sign'] = UserSignLog::where([['user_id', '=', $emp->uid], ['date', '=', date('Y-m-d')]])->count();
- $url = request()->domain() . "/applet.html#/mycustomer?token=$token&platform=browser";
- return redirect($url);
- }
- }
|