1
0

Mini.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\index\controller;
  4. use app\model\Camp;
  5. use app\model\CampEmployee;
  6. use app\model\Company;
  7. use app\model\Employee;
  8. use app\model\Miniprogram;
  9. use app\model\Org;
  10. use app\model\User;
  11. use app\model\UserSignLog;
  12. use Firebase\JWT\JWT;
  13. use openssl\Aes;
  14. use think\facade\View;
  15. use wx\Oplatform;
  16. class Mini
  17. {
  18. public function index($notify)
  19. {
  20. $zhuan = [
  21. 'lyjczs' => 'xkIVfO',
  22. 'test' => 'IQB5Nr',
  23. 'test1' => 'ATviy6'
  24. ];
  25. if (!isset($zhuan[$notify])) abort(404, '页面不存在');
  26. $notify = $zhuan[$notify];
  27. // 判断是否合法
  28. $had = Miniprogram::where(['notify' => $notify])->count();
  29. if ($had == 0) abort(404, '企业不存在');
  30. $aes = new Aes(config('app.jwt_key'));
  31. $appid = config('app.kfweb_appid');
  32. $state = $aes->encrypt(rand(1000, 9999) . $notify);
  33. $redirect_uri = urlencode(request()->domain() . "/crm/login.html");
  34. $wechatUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect";
  35. return redirect($wechatUrl);
  36. }
  37. /**
  38. * 授权登录
  39. *
  40. * @param string $code
  41. * @return array
  42. */
  43. public function login($code, $state)
  44. {
  45. $aes = new Aes(config('app.jwt_key'));
  46. $state = $aes->decrypt($state);
  47. if (!$state) abort(404, '网址不存在');
  48. $notify = substr($state, 4);
  49. // 判断是否合法
  50. $root_id = Miniprogram::where(['notify' => $notify])->value('root_id');
  51. if (empty($root_id)) abort(404, '企业不存在');
  52. $oplatdata = (new Oplatform())->getAccessToken(config('app.kfweb_appid'), config('app.kfweb_secret'), $code);
  53. if (!$oplatdata) {
  54. trace($oplatdata, 'error');
  55. abort(404, '请求失败,请重新输入网址!');
  56. }
  57. if (isset($oplatdata['errcode'])) { // code已经被使用
  58. if (in_array($oplatdata['errcode'], [40029, 40163, 41008, 42003])) {
  59. abort(404, '请求失败,请重新输入网址!');
  60. } else {
  61. trace($oplatdata, 'error');
  62. abort(404, $oplatdata['errmsg']);
  63. }
  64. }
  65. $userIds = User::where([['unionid', '=', $oplatdata['unionid']], ['root_id', '=', $root_id]])->column('id'); // 查询用户数量
  66. $condition = [
  67. ['root_id', '=', $root_id],
  68. ['uid', 'in', $userIds],
  69. ['state', '=', '在职']
  70. ];
  71. $emp = Employee::with('user')->where($condition)->find();
  72. if (empty($emp)) {
  73. echo '<h1>未加入该企业</h1>';
  74. exit;
  75. }
  76. $camps = Camp::where([['root_id', '=', $root_id], ['del', '=', 0]])->column('id');
  77. $tw[] = ['camp_id', 'in', $camps];
  78. $check = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $emp->id], ['root_id', '=', $root_id]])->where($tw)->count();
  79. if ($check) abort(404, '您尚未完成新兵课程,请在小程序中学习完毕后再来!');
  80. $company = Company::where('root_id', $root_id)->find();
  81. if (empty($emp) || $company['end_date'] < date('Y-m-d') || $company['status'] == 1) abort(404, '页面不存在!');
  82. // 查询是设计部门还是销售部门
  83. $orgType = Org::where('id', $emp->org_id)->value('org_type');
  84. // token
  85. $token = [
  86. 'root_org' => $root_id,
  87. 'employee_id' => $emp->id,
  88. 'org_id' => $emp->org_id,
  89. 'isEmployee' => true,
  90. 'isManager' => $emp->is_manager,
  91. 'org_type' => $orgType,
  92. 'uid' => $emp->uid
  93. ];
  94. // 信息加密
  95. $data = http_build_query($token);
  96. $key = $aes->encrypt($data);
  97. // token数据设置
  98. $payload = array(
  99. "iss" => request()->domain(),
  100. "aud" => 'mini',
  101. "iat" => time(),
  102. "nbf" => time(),
  103. "data" => $key
  104. );
  105. // 自定义登陆状态
  106. $token = JWT::encode($payload, config('app.jwt_key'));
  107. // 员工签到
  108. $userData['sign'] = UserSignLog::where([['user_id', '=', $emp->uid], ['date', '=', date('Y-m-d')]])->count();
  109. $url = request()->domain() . "/applet.html#/mycustomer?token=$token&platform=browser";
  110. return redirect($url);
  111. }
  112. }