$request['account']])->findOrEmpty(); if ($teacher->isEmpty()) { throw new Exception('账号不存在'); } // if (empty($request['password'])) { // throw new Exception('请填写密码'); // } // // if (md5($request['password'] . $teacher->salt) != $teacher->password) { // throw new Exception('密码错误,请填写正确的密码'); // } $token_data = [ 'id' => $teacher->id, 'role' => 'teacher' ]; $token = JwtToken::generateToken($token_data); // unset($token['refresh_token']); return [ 'code' => ResponseCode::SUCCESS, 'data' => $token, 'msg' => 'success' ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 设置时区 * @param $request * @return array|void */ public function setTimeZone($request) { try { if (empty($request->teacher)) { throw new Exception('请教师登陆后再设置'); } $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); if ($teacher->isEmpty()) { throw new Exception('未找到教师信息,设置失败'); } $time_zone = TimeZone::where(['id' => $request->post('time_zone_id')])->findOrEmpty(); $res = $teacher->save([ 'time_zone_id' => $time_zone->id, 'time_zone_name' => $time_zone->name, 'time_zone_abbr' => $time_zone->abbr, 'time_zone_offset' => $time_zone->offset, ]); if (!$res) { throw new Exception('设置失败'); } return [ 'code' => ResponseCode::SUCCESS, 'msg' => '设置成功' ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 教师信息 * @param $request * @return array */ public function teacherInfo($request) { try { $teacher = Teacher::where(['id' => $request->teacher->id])->field('id,account,teacher_name,time_zone_offset,time_zone_id,time_zone_name')->findOrEmpty(); $teacher = $teacher->toArray(); $teacher['role'] = 'teacher'; return [ 'code' => ResponseCode::SUCCESS, 'data' => $teacher, ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 教师信息 * @param $request * @return array */ public function resetPassword($request) { try { if (empty($request->teacher)) { throw new Exception('请先教师登陆'); } $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); if ($teacher->isEmpty()) { throw new Exception('未找到教师信息'); } $requestData = $request->post(); if (empty($requestData['pwd']) || strlen(trim($requestData['pwd'])) < 6) { throw new Exception('请输入密码或者长度大于6位'); } $salt = random_str(16); $password = md5(trim($requestData['pwd'] . $salt)); $teacher->save([ 'password' => $password, 'salt' => $salt ]); return [ 'code' => ResponseCode::SUCCESS, 'msg' => '操作成功', ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 更新code * @param $request * @return array|void * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateOpenid($request) { try { if (empty($request->teacher)) { throw new Exception('请先教师登陆'); } $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); if ($teacher->isEmpty()) { throw new Exception('未找到教师信息'); } $code = $request->post('code'); $user_info = WechatSubscriptController::getCodeAccessToken($code); if (isset($result['code'])) { throw new Exception('获取信息失败'); } $openid = $user_info['openid']; $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); if ($teacher->openid && $teacher->openid != $openid) { throw new Exception('当前账号已绑定其它教师,不能重复绑定'); } else { $teacher->save([ 'openid' => $openid, ]); } return [ 'code' => ResponseCode::SUCCESS, 'msg' => 'success' ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } }