User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/5 17:15
  5. */
  6. namespace app\api\controller;
  7. use app\api\model\HomeModel;
  8. use app\common\service\CommonService;
  9. use app\api\BaseController;
  10. use app\api\exception\ApiException;
  11. use app\api\model\UserModel;
  12. use app\api\model\VisitorModel;
  13. use app\common\service\UserService;
  14. use app\common\until\Until;
  15. use think\facade\Cache;
  16. use think\facade\Session;
  17. class User extends BaseController {
  18. /**
  19. * @OA\Get (path="/api/User",
  20. * tags={"用户管理"},
  21. * summary="用户列表",
  22. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  23. * @OA\Parameter(description="页码",in="query", name="page",@OA\Schema(type="string",default="1")),
  24. * @OA\Parameter(description="页尺寸", in="query",name="pageSize",@OA\Schema(type="string",default="10")),
  25. * @OA\Parameter(description="状态 1正常 2删除",in="query", name="status",@OA\Schema(type="string")),
  26. * @OA\Parameter(description="手机号", in="query",name="mobile",@OA\Schema(type="string")),
  27. * @OA\Parameter(description="昵称", in="query",name="name",@OA\Schema(type="string")),
  28. * @OA\Parameter(description="会员等级 1非会员,2金卡会员,3铂金会员",in="query",name="cardLevel",@OA\Schema(type="string")),
  29. * @OA\Parameter(description="加入方式 1小程序 2后台预约",in="query", name="joinType",@OA\Schema(type="string")),
  30. * @OA\RequestBody(
  31. * ),
  32. * @OA\Response(response="200", description="请求成功")
  33. * )
  34. */
  35. public function index() {
  36. $input = Until::getInput();
  37. $model = new UserModel();
  38. $model->setPage($input['page'] ?? 1);
  39. $model->setPageSize($input['pageSize'] ?? 10);
  40. $where = [];
  41. if (!empty($input['status'])) {
  42. $where[] = ['u.status', '=', $input['status']];
  43. } else {
  44. $where[] = ['u.status', '=', $model::NORMAL];
  45. }
  46. if (!empty($input['mobile']) || (isset($input['mobile']) && $input['mobile'] == '0')) {
  47. $where[] = ['u.mobile', 'like', '%' . $input['mobile'] . '%'];
  48. }
  49. if (!empty($input['name']) || (isset($input['name']) && $input['name'] == '0')) {
  50. $where[] = ['u.name', 'like', '%' . $input['name'] . '%'];
  51. }
  52. if (!empty($input['cardLevel'])) {
  53. $where[] = ['u.card_level', '=', $input['cardLevel']];
  54. }
  55. if (!empty($input['joinType'])) {
  56. $where[] = ['u.join_type', '=', $input['joinType']];
  57. }
  58. $model->setWhere($where);
  59. $data = $model->getUserList();
  60. Until::output($data);
  61. }
  62. /**
  63. * @OA\Post(path="/api/user/save",
  64. * tags={"用户管理"},
  65. * summary="保存用户(有id就更新,没id就新增)",
  66. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  67. * @OA\RequestBody(
  68. * @OA\MediaType(
  69. * mediaType="application/json",
  70. * @OA\Schema(
  71. * @OA\Property(description="微信小程序", property="code", type="string", default="dd"),
  72. * required={"title", "content"})
  73. * )
  74. * ),
  75. * @OA\Response(response="200", description="请求成功")
  76. * )
  77. */
  78. public function save() {
  79. $input = Until::getInput();
  80. $rule = [
  81. 'code' => 'require',
  82. ];
  83. Until::check($rule, $input);
  84. $server = new UserService();
  85. $userInfo = $server->decode($input['code']);
  86. $userServer = new UserService();
  87. $info = $userServer->saveUserInfo($input, $userInfo['openid']);
  88. $info['token'] = (new \app\common\until\Token())->getToken($info['userId'], '', false);
  89. Until::output(['userInfo' => $info]);
  90. }
  91. public function getMobile() {
  92. $input = Until::getInput();
  93. $rule = [
  94. 'code' => 'require',
  95. 'encryptedData' => 'require',
  96. 'iv' => 'require'
  97. ];
  98. Until::check($rule, $input);
  99. $server = new UserService();
  100. $mobile = $server->getPhoneMobile($input['code'],$input['iv'],$input['encryptedData']);
  101. $userModel = new UserModel();
  102. $userModel::where(['id' => $this->userId])->update(['mobile' => $mobile]);
  103. $info = $userModel->getUserInfo($this->userId);
  104. $info['token'] = (new \app\common\until\Token())->getToken($info['userId'], '', false);
  105. Until::output(['userInfo' => $info]);
  106. }
  107. /**
  108. * @OA\Post(path="/api/User/editUser",
  109. * tags={"用户管理"},
  110. * summary="修改用户信息",
  111. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  112. * @OA\RequestBody(
  113. * @OA\MediaType(
  114. * mediaType="multipart/form-data",
  115. * @OA\Schema(
  116. * @OA\Property(description="用户姓名", property="name", type="string", default="小明"),
  117. * @OA\Property(description="生日", property="birthday", type="string", default="1889-02-03"),
  118. * @OA\Property(description="头像", property="avatar", type="string", default="http://xxx.com"),
  119. * @OA\Property(description="性别 0未知 1男 2女", property="sex", type="string", default="http://xxx.com"),
  120. * required={"name", "birthday","avatar"})
  121. * )
  122. * ),
  123. * @OA\Response(response="200", description="请求成功")
  124. * )
  125. */
  126. public function editUser() {
  127. $input = Until::getInput();
  128. $rule = [
  129. 'name' => 'require',
  130. 'birthday' => 'require',
  131. 'avatar' => 'require',
  132. 'sex' => 'require'
  133. ];
  134. Until::check($rule, $input);
  135. $model = new UserModel();
  136. $model::where(['id' => $this->userId])->update([
  137. 'name' => $input['name'],
  138. 'birthday' => $input['birthday'],
  139. 'avatar' => $input['avatar'],
  140. 'sex' => $input['sex']
  141. ]);
  142. $info = $model->getUserInfo($this->userId);
  143. Until::output(['info'=>$info]);
  144. }
  145. /**
  146. * @OA\Get(path="/api/User/read",
  147. * tags={"用户管理"},
  148. * summary="读取单个用户信息",
  149. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  150. * @OA\RequestBody(
  151. * @OA\MediaType(
  152. * mediaType="multipart/form-data",
  153. * @OA\Schema(
  154. * @OA\Property(description="用户id", property="id", type="int"),
  155. * required={"id"})
  156. * )
  157. * ),
  158. * @OA\Response(response="200", description="请求成功")
  159. * )
  160. */
  161. public function read(int $id) {
  162. $info = (new UserModel())::where(['id' => $id])->find();
  163. Until::output(['info' => Until::modelToArray($info)]);
  164. }
  165. }