UserController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers\Syj;
  3. use App\Models\Syj\UserModel;
  4. use App\Services\QQMiniApp\Login;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. class UserController extends Controller
  8. {
  9. /**
  10. * 授权注册登陆
  11. * @param Request $request
  12. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  13. * @throws \App\Services\QQMiniApp\QQException
  14. */
  15. public function auth(Request $request)
  16. {
  17. $this->validate($request, [
  18. 'js_code' => 'required',
  19. ]);
  20. $js_code = $request->get('js_code');
  21. $qqMiniApp = new Login();
  22. $user_info = $qqMiniApp->code2Session($js_code);
  23. $user = UserModel::updateOrCreate(
  24. ['openid' => $user_info->openid],
  25. ['unionid' => isset($user_info->unionid) ? $user_info->unionid : null, 'login_at' => time()]
  26. );
  27. return response([
  28. 'code' => 200,
  29. 'message' => 'OK',
  30. 'data' => $user
  31. ]);
  32. }
  33. /**
  34. * 用户信息
  35. * @param Request $request
  36. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  37. */
  38. public function user(Request $request)
  39. {
  40. $user = UserModel::where('openid', $request->header('openid'))->firstOrFail();
  41. return response([
  42. 'code' => 200,
  43. 'message' => 'OK',
  44. 'data' => $user
  45. ]);
  46. }
  47. /**
  48. * 修改用户信息
  49. * @param $request
  50. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  51. */
  52. public function updateUser(Request $request)
  53. {
  54. $this->validate($request, [
  55. 'nickname' => 'max:16',
  56. 'avatar' => 'url',
  57. 'sex' => 'integer',
  58. 'province' => '',
  59. 'city' => '',
  60. ]);
  61. $user = UserModel::where('openid', $request->header('openid'))->firstOrFail();
  62. // 补充信息并上传头像至oss
  63. if (!$user->nickname) {
  64. $user->nickname = $request->nickname;
  65. }
  66. if (!$user->avatar) {
  67. try {
  68. $avatar = str_replace('/100', '/0', $request->avatar);
  69. $avatar = $this->makeAvatar($avatar);
  70. } catch (\Exception $e) {
  71. $avatar = $request->avatar;
  72. }
  73. $user->avatar = $avatar;
  74. }
  75. if (!$user->sex) {
  76. $user->sex = $request->sex;
  77. }
  78. if (!$user->province) {
  79. $user->province = $request->province;
  80. }
  81. if (!$user->city) {
  82. $user->city = $request->city;
  83. }
  84. $user->save();
  85. return response([
  86. 'code' => 200,
  87. 'message' => 'OK',
  88. 'data' => $user
  89. ]);
  90. }
  91. private function makeAvatar($url)
  92. {
  93. $http = "http://www.koudaidaxue.com/Tools/Upload/wxHeader2Avatar";
  94. $response = \Curl::to($http)->withData([
  95. 'url' => $url
  96. ])->asJsonResponse()->post();
  97. return $response->data->url;
  98. }
  99. }