User.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/5 17:15
  5. */
  6. namespace app\api\controller;
  7. use app\api\BaseController;
  8. use app\api\exception\ApiException;
  9. use app\api\model\UserModel;
  10. use app\api\model\VisitorModel;
  11. use app\common\service\OrderService;
  12. use app\common\service\UserService;
  13. use app\common\until\Until;
  14. use think\facade\Cache;
  15. use think\facade\Session;
  16. class User extends BaseController {
  17. /**
  18. * @OA\Get (path="/api/User",
  19. * tags={"用户管理"},
  20. * summary="用户列表",
  21. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  22. * @OA\Parameter(description="页码",in="query", name="page",@OA\Schema(type="string",default="1")),
  23. * @OA\Parameter(description="页尺寸", in="query",name="pageSize",@OA\Schema(type="string",default="10")),
  24. * @OA\Parameter(description="状态 1正常 2删除",in="query", name="status",@OA\Schema(type="string")),
  25. * @OA\Parameter(description="手机号", in="query",name="mobile",@OA\Schema(type="string")),
  26. * @OA\Parameter(description="昵称", in="query",name="name",@OA\Schema(type="string")),
  27. * @OA\Parameter(description="真实姓名", in="query",name="realName",@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['realName']) || (isset($input['realName']) && $input['realName'] == '0')) {
  53. $where[] = ['u.real_name', 'like', '%' . $input['realName'] . '%'];
  54. }
  55. if (!empty($input['cardLevel'])) {
  56. $where[] = ['u.card_level', '=', $input['cardLevel']];
  57. }
  58. if (!empty($input['joinType'])) {
  59. $where[] = ['u.join_type', '=', $input['joinType']];
  60. }
  61. $model->setWhere($where);
  62. $data = $model->getUserList();
  63. Until::output($data);
  64. }
  65. /**
  66. * @OA\Post(path="/api/user/save",
  67. * tags={"用户管理"},
  68. * summary="保存用户(有id就更新,没id就新增)",
  69. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  70. * @OA\RequestBody(
  71. * @OA\MediaType(
  72. * mediaType="application/json",
  73. * @OA\Schema(
  74. * @OA\Property(description="微信小程序", property="code", type="string", default="dd"),
  75. * required={"title", "content"})
  76. * )
  77. * ),
  78. * @OA\Response(response="200", description="请求成功")
  79. * )
  80. */
  81. public function save() {
  82. $input = Until::getInput();
  83. $rule = [
  84. 'code' => 'require',
  85. ];
  86. Until::check($rule, $input);
  87. $server = new UserService();
  88. $userInfo = $server->decode($input['code']);
  89. $userServer = new UserService();
  90. $info = $userServer->saveUserInfo($input, $userInfo['openid']);
  91. $info['token'] = (new \app\common\until\Token())->getToken($info['userId'], '', false);
  92. Until::output(['userInfo' => $info]);
  93. }
  94. public function getMobile() {
  95. $input = Until::getInput();
  96. $rule = [
  97. 'code' => 'require',
  98. 'encryptedData' => 'require',
  99. 'iv' => 'require'
  100. ];
  101. Until::check($rule, $input);
  102. $server = new UserService();
  103. $mobile = $server->getPhoneMobile($input['code'],$input['iv'],$input['encryptedData']);
  104. $userModel = new UserModel();
  105. $userModel::where(['id' => $this->userId])->update(['mobile' => $mobile]);
  106. $info = $userModel->getUserInfo($this->userId);
  107. $info['token'] = (new \app\common\until\Token())->getToken($info['userId'], '', false);
  108. Until::output(['userInfo' => $info]);
  109. }
  110. /**
  111. * @OA\Post(path="/api/User/editUser",
  112. * tags={"用户管理"},
  113. * summary="修改用户信息",
  114. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  115. * @OA\RequestBody(
  116. * @OA\MediaType(
  117. * mediaType="multipart/form-data",
  118. * @OA\Schema(
  119. * @OA\Property(description="用户姓名", property="name", type="string", default="小明"),
  120. * @OA\Property(description="生日", property="birthday", type="string", default="1889-02-03"),
  121. * @OA\Property(description="头像", property="avatar", type="string", default="http://xxx.com"),
  122. * @OA\Property(description="性别 0未知 1男 2女", property="sex", type="string", default="http://xxx.com"),
  123. * required={"name", "birthday","avatar"})
  124. * )
  125. * ),
  126. * @OA\Response(response="200", description="请求成功")
  127. * )
  128. */
  129. public function editUser() {
  130. $input = Until::getInput();
  131. $rule = [
  132. 'name' => 'require',
  133. 'birthday' => 'require',
  134. 'avatar' => 'require',
  135. 'sex' => 'require'
  136. ];
  137. Until::check($rule, $input);
  138. $model = new UserModel();
  139. $model::where(['id' => $this->userId])->update([
  140. 'name' => $input['name'],
  141. 'birthday' => $input['birthday'],
  142. 'avatar' => $input['avatar'],
  143. 'sex' => $input['sex']
  144. ]);
  145. $info = $model->getUserInfo($this->userId);
  146. Until::output(['info'=>$info]);
  147. }
  148. /**
  149. * @OA\Get(path="/api/User/read",
  150. * tags={"用户管理"},
  151. * summary="读取单个用户信息",
  152. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  153. * @OA\RequestBody(
  154. * @OA\MediaType(
  155. * mediaType="multipart/form-data",
  156. * @OA\Schema(
  157. * @OA\Property(description="用户id", property="id", type="int"),
  158. * required={"id"})
  159. * )
  160. * ),
  161. * @OA\Response(response="200", description="请求成功")
  162. * )
  163. */
  164. public function read(int $id) {
  165. $info = (new UserModel())::where(['id' => $id])->find();
  166. Until::output(['info' => Until::modelToArray($info)]);
  167. }
  168. /**
  169. * @OA\Post(path="/api/User/recharge",
  170. * tags={"用户管理"},
  171. * summary="充值余额",
  172. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  173. * @OA\RequestBody(
  174. * @OA\MediaType(
  175. * mediaType="multipart/form-data",
  176. * @OA\Schema(
  177. * @OA\Property(description="充值金额", property="recharge", type="string"),
  178. * required={"recharge"})
  179. * )
  180. * ),
  181. * @OA\Response(response="200", description="请求成功")
  182. * )
  183. */
  184. public function recharge() {
  185. $input = Until::getInput();
  186. $rule = [
  187. 'recharge' => 'require',
  188. ];
  189. Until::check($rule, $input);
  190. $service = new OrderService();
  191. $data = $service->createRechargeOrder($input['recharge']);
  192. Until::output($data);
  193. }
  194. }