123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- namespace App\Http\Controllers;
- use http\Env\Response;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Auth;
- use App\Models\User;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use EasyWeChat\Factory;
- class UserController extends Controller
- {
- public function __construct()
- {
- }
- public function getOpenidToken(Request $request)
- {
- $code = $request->code;
- $config = config('wechat.defaults.mini_program.default');
- $mini = Factory::miniProgram($config);
- $info = $mini->auth->session($code);
- if (isset($info['errcode'])) {
- return response()->json([
- 'error_code' => '411111',
- 'msg' => 'code 不正确'
- ]);
- }
- $openid = $info['openid'];
- $where['openid'] = $openid;
- $where['del_is'] = 0;
- $userInfo = User::where($where)->first();
- if ($userInfo) {
- $token = Auth::guard('api')->fromUser($userInfo);
- return response()->json([
- 'error_code' => 200,
- 'msg' => '获取成功',
- 'token' => $token,
- 'openid' => $openid
- ]);
- } else {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '没有认证',
- 'cert' => '0',
- 'openid' => $openid
- ]);
- }
- }
- public function login(Request $request)
- {
- $input = $request->all();
- $rules = [
- 'cre_num' => 'required',
- 'name' => 'required|max:16',
- ];
- $messages = [
- 'name.required' => '党员名称不能为空.',
- 'name.max' => '党员名称不能超过16个字符.',
- 'cre_num.required' => '手机号或档案号不能为空.',
- ];
- $validator = Validator::make($input, $rules, $messages);
- if ($validator->fails()) {
- return response()->json([
- 'error_code' => 41113,
- 'msg' => $validator->errors()->first()
- ]);
- }
- $where['name'] = $input['name'];
- $where['del_is'] = 0;
- $cre_num = $input['cre_num'];
- $openid = $input['openid'];
- $user = User::where($where)
- ->where(function ($query) use ($cre_num) {
- $query->where('cre_num', $cre_num)
- ->orWhere('telphone', $cre_num);
- })
- ->first();
- if (isset($user)) {
- $teamInfo = DB::table('team')->where('id', $user->team_id)->first();
- if ($teamInfo) {
- $data['teamid'] = $teamInfo->id;
- $data['teamname'] = $teamInfo->name;
- } else {
- $data['teamid'] = '0';
- $data['teamname'] = '无支部';
- }
- if ($user->openid) {
- return response()->json([
- 'error_code' => 400014,
- 'msg' => '该账号信息已绑定',
- ]);
- }
- $row = DB::table('users')
- ->where($where)
- ->where(function ($query) use ($cre_num) {
- $query->where('cre_num', $cre_num)
- ->orWhere('telphone', $cre_num);
- })
- ->update(['openid' => $openid]);
- if ($row) {
- if ($token = Auth::guard('api')->fromUser($user)) {
- $data['token'] = $token;
- return response()->json([
- 'error_code' => 200,
- 'msg' => '认证成功',
- 'data' => $data,
- 'openid' => $openid
- ]);
- } else {
- return response()->json([
- 'error_code' => 203,
- 'msg' => '获取token失败',
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 202,
- 'msg' => '认证失败',
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 42201,
- 'msg' => '用户信息不存在'
- ]);
- }
- }
- public function userRemoveBinding(Request $request)
- {
- $userid = Auth::user()->id;
- $row = DB::table('users')->where('id', $userid)->update(['cert' => '0', 'openid' => '']);
- if ($row) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '解绑成功'
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '解绑失败'
- ]);
- }
- }
- public function certTeam(Request $request)
- {
- $userid = Auth::user()->id;
- if ((int)Auth::user()->cert === 2) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '认证通过'
- ]);
- }
- $row = DB::table('users')->where('id', $userid)->update(['cert' => '2']);
- if ($row) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '认证通过'
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '认证失败'
- ]);
- }
- }
- /* 获取信息 */
- public function getUserInfo(Request $request)
- {
- $userid = Auth::user()->id;
- $data = DB::table('users')
- ->leftJoin('team', 'users.team_id', '=', 'team.id')
- ->where('users.id', $userid)
- ->select('users.*', 'team.name as teamname')
- ->first();
- if ($data) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '获取成功',
- 'data' => $data
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '获取失败'
- ]);
- }
- }
- /* 修改信息 */
- public function changeUserInfo(Request $request)
- {
- $rules = [
- 'telphone' => 'required|regex:/^1[345789]\d{9}$/',
- 'name' => 'required|max:16',
- ];
- $messages = [
- 'name.required' => '党员名称不能为空.',
- 'name.max' => '党员名称不能超过16个字符.',
- 'telphone.required' => '手机号不能为空.',
- 'telphone.regex' => '手机号格式有误.',
- ];
- $validator = Validator::make($request->all(), $rules, $messages);
- if ($validator->fails()) {
- return response()->json([
- 'error_code' => 41113,
- 'msg' => $validator->errors()->first()
- ]);
- }
- $info['name'] = $request->name;
- $info['telphone'] = $request->telphone;
- $userid = Auth::user()->id;
- $row = DB::table('users')->where('id', $userid)->update($info);
- if ($row) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '修改成功'
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '修改失败'
- ]);
- }
- }
- /*下载承诺书*/
- public function downloadCommitBook(Request $request)
- {
- $url = 'http://' . $request->server('SERVER_NAME') . '/images/commit/承诺书模板.doc';
- return response()->json([
- 'error_code' => 200,
- 'url' => $url
- ]);
- }
- /** 上传承诺书 */
- public function uploadCommitBook(Request $request)
- {
- $file = $request->file('file');//获取上传的文件
- if ($file->isValid()) {
- $filename = $file->getClientOriginalName();//获取上传文件的文件名(带后缀,如abc.png)
- $fileextension = $file->getClientOriginalExtension();//获取上传文件的后缀(如abc.png,获取到的为png)
- if (!preg_match('/(jpg|jpeg|png|gif)/', strtolower($filename))) {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '您只能上传通用的图片格式'
- ]);
- }
- $filesize = $file->getSize();//获取上传文件的大小
- if ($filesize / 1024 > 2000) {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '请检查您上传的文件不能大于2000KB'
- ]);
- }
- $newfilename = date("YmdHis") . mt_rand(1000, 9999) . substr(md5(uniqid(rand(1000, 9999))), 6, 18) . '.' . $fileextension;
- $newpath = $request->server('DOCUMENT_ROOT') . '/images/commit';
- $path = $file->move($newpath, $newfilename);
- if ($path) {
- $imgurl = 'http://' . $request->server('SERVER_NAME') . '/images/commit/' . $newfilename;
- $userid = Auth::user()->id;
- $row = DB::table('upload_commit_log')->insert([
- 'uid' => $userid,
- 'imageUrl' => $imgurl,
- 'created_at' => date("Y-m-d H:i:s")
- ]);
- if ($row) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '承诺书上传成功',
- 'location' => $imgurl
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '承诺书上传失败'
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '承诺书图片上传失败'
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '承诺书图片上传失败'
- ]);
- }
- }
- /* 获取承诺书*/
- public function getCommitList(Request $request)
- {
- $uid = Auth::user()->id;
- $page_size = $request->page_size;
- $page_index = $request->page_index;
- $num = ($page_index - 1) * $page_size;
- $count = DB::table('upload_commit_log')
- ->where('uid', $uid)
- ->count();
- if ($count > 0) {
- $list = DB::table('upload_commit_log')
- ->where('uid', $uid)
- ->orderBy('id', 'desc')
- ->skip($num)->take($page_size)->get();
- if ($list) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '获取承诺书成功',
- 'data' => $list,
- 'count' => $count
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '获取承诺书失败'
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '没有承诺书信息',
- 'data' => [],
- 'count' => $count
- ]);
- }
- }
- /** 删除承诺书 */
- public function destoryCommit(Request $request)
- {
- $rules = [
- 'id' => 'required',
- ];
- $messages = [
- 'id.required' => '请选择要删除的承诺书.'
- ];
- $validator = Validator::make($request->all(), $rules, $messages);
- if ($validator->fails()) {
- return response()->json([
- 'error_code' => 41113,
- 'msg' => $validator->errors()->first()
- ]);
- }
- $where['id'] = $request->id;
- $commit = DB::table('upload_commit_log')->where($where)->first();
- if (empty($commit)) {
- return response()->json([
- 'error_code' => 400010,
- 'msg' => '承诺书不存在'
- ]);
- }
- $row = DB::table('upload_commit_log')
- ->where($where)
- ->delete();
- if ($row) {
- $filePath = $commit->imageUrl;
- $fileName = explode('/', $filePath);
- $commitName = end($fileName);
- $path = $request->server('DOCUMENT_ROOT') . '/images/commit/' . $commitName;
- Log::info('ID为 ' . $commit->uid . ' 用户的承诺书被删除');
- unlink($path);
- return response()->json([
- 'error_code' => 200,
- 'msg' => '删除成功'
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '删除失败'
- ]);
- }
- }
- public function getUserTeamInfo()
- {
- $uid = Auth::user()->id;
- $teamid = DB::table('users')->where('id', $uid)->value('team_id');
- if ($teamid) {
- $data = DB::table('team')->where('id', $teamid)->first();
- if ($data) {
- return response()->json([
- 'error_code' => 200,
- 'msg' => '获取支部信息成功',
- 'data' => $data
- ]);
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '获取支部信息失败'
- ]);
- }
- } else {
- return response()->json([
- 'error_code' => 0,
- 'msg' => '获取支部信息失败'
- ]);
- }
- }
- /**
- * Get the authenticated User
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function me()
- {
- return response()->json($this->guard()->user());
- }
- /**
- * Log the user out (Invalidate the token)
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function logout()
- {
- $this->guard()->logout();
- return response()->json(['message' => 'Successfully logged out']);
- }
- /**
- * Refresh a token.
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function refresh()
- {
- return $this->respondWithToken($this->guard()->refresh());
- }
- /**
- * Get the token array structure.
- *
- * @param string $token
- *
- * @return \Illuminate\Http\JsonResponse
- */
- protected function respondWithToken($token)
- {
- return response()->json([
- 'access_token' => $token,
- 'token_type' => 'bearer',
- 'expires_in' => $this->guard()->factory()->getTTL() * 60
- ]);
- }
- /**
- * Get the guard to be used during authentication.
- *
- * @return \Illuminate\Contracts\Auth\Guard
- */
- public function guard()
- {
- return Auth::guard();
- }
- }
|