User.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use app\common\model\Leave;
  7. use fast\Random;
  8. use think\Config;
  9. use think\Validate;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['login', 'mobile_login', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. if (!Config::get('fastadmin.usercenter')) {
  21. $this->error(__('User center already closed'));
  22. }
  23. }
  24. /**
  25. * 用户信息
  26. * @ApiMethod (POST)
  27. * @ApiReturnParams (name=code,type="integer",description=错误码:0=失败1=成功401=未登录403=没有权限)
  28. * @ApiReturnParams (name=msg,type="string",description=提示信息)
  29. * @ApiReturnParams (name=data,type="array",description=要返回的数据)
  30. * @ApiReturnParams (name=data.id,type="integer",description=用户ID)
  31. * @ApiReturnParams (name=data.username,type="string",description=用户姓名)
  32. * @ApiReturnParams (name=data.email,type="string",description=邮箱)
  33. * @ApiReturnParams (name=data.mobile,type="integer",description=手机号)
  34. * @ApiReturnParams (name=data.avatar,type="string",description=头像地址)
  35. * @ApiReturnParams (name=data.gender,type="string",description=性别)
  36. * @ApiReturnParams (name=data.birthday,type="string",description=出生年月)
  37. * @ApiReturnParams (name=data.rights,type="integer",description=是否有权益:0=没有1=有)
  38. */
  39. public function get_user_info()
  40. {
  41. $user = $this->auth->getUserinfo();
  42. $this->success('用户信息', $user);
  43. }
  44. /**
  45. * 手机验证码登录
  46. * @ApiMethod (POST)
  47. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  48. * @ApiParams (name="captcha", type="string", required=true, description="验证码")
  49. * @ApiReturnParams (name=code,type="integer",description=错误码:0=失败1=成功401=未登录403=没有权限)
  50. * @ApiReturnParams (name=msg,type="string",description=提示信息)
  51. * @ApiReturnParams (name=data,type="array",description=要返回的数据)
  52. * @ApiReturnParams (name=data.token,type="string",description=token值)
  53. */
  54. public function mobile_login()
  55. {
  56. $mobile = $this->request->post('mobile');
  57. $captcha = $this->request->post('captcha');
  58. if (!$mobile || !$captcha) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. if (!Validate::regex($mobile, "^1\d{10}$")) {
  62. $this->error(__('Mobile is incorrect'));
  63. }
  64. if (!Sms::check($mobile, $captcha, 'register')) {
  65. $this->error(__('Captcha is incorrect'));
  66. }
  67. $user = \app\common\model\User::getByMobile($mobile);
  68. if ($user) {
  69. if ($user->status != 'normal') {
  70. $this->error(__('Account is locked'));
  71. }
  72. //如果已经有账号则直接登录
  73. $ret = $this->auth->direct($user->id);
  74. } else {
  75. $ret = $this->auth->register('植提桥用户'.substr($mobile,-4), Random::alnum(), '', $mobile, []);
  76. }
  77. if ($ret) {
  78. Sms::flush($mobile, 'register');
  79. $data = ['token' => $this->auth->getUserinfo()['token']];
  80. $this->success(__('Logged in successful'), $data);
  81. } else {
  82. $this->error($this->auth->getError());
  83. }
  84. }
  85. /**
  86. * 修改用户信息
  87. * @ApiMethod (POST)
  88. * @ApiParams (name="avatar", type="string", required=false, description="头像地址")
  89. * @ApiParams (name="username", type="string", required=false, description="姓名")
  90. * @ApiParams (name="gender", type="string", required=false, description="性别")
  91. * @ApiParams (name="birthday", type="string", required=false, description="出生年月")
  92. * @ApiParams (name="email", type="string", required=false, description="邮箱地址")
  93. */
  94. public function edit_user_info()
  95. {
  96. $user = $this->auth->getUser();
  97. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  98. $username = $this->request->post('username');
  99. $gender = $this->request->post('gender');
  100. $birthday = $this->request->post('birthday');
  101. $email = $this->request->post('email');
  102. if(empty($avatar) && empty($username) && empty($gender) && empty($birthday) && empty($email)){
  103. $this->error('参数错误');
  104. }
  105. if($avatar){
  106. $user->avatar = $avatar;
  107. }
  108. if ($username) {
  109. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  110. if ($exists) {
  111. $this->error(__('Username already exists'));
  112. }
  113. $user->username = $username;
  114. }
  115. if($gender){
  116. $user->gender = $gender;
  117. }
  118. if($birthday){
  119. $user->birthday = $birthday;
  120. }
  121. if($email){
  122. if (!Validate::is($email, "email")) {
  123. $this->error(__('Email is incorrect'));
  124. }
  125. $exists = \app\common\model\User::where('username', $email)->where('id', '<>', $this->auth->id)->find();
  126. if ($exists) {
  127. $this->error('邮箱已存在');
  128. }
  129. $user->email = $email;
  130. }
  131. $user->save();
  132. $this->success('编辑成功');
  133. }
  134. /**
  135. * 第三方登录
  136. * @ApiMethod (POST)
  137. * @param string $platform 平台名称
  138. * @param string $code Code码
  139. */
  140. public function third()
  141. {
  142. $url = url('user/index');
  143. $platform = $this->request->post("platform",'Wechat');
  144. $code = $this->request->post("code");
  145. $config = get_addon_config('third');
  146. if (!$config || !isset($config[$platform])) {
  147. $this->error(__('Invalid parameters'));
  148. }
  149. $app = new \addons\third\library\Application($config);
  150. //通过code换access_token和绑定会员
  151. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  152. if ($result) {
  153. $openid = $result['openid'];
  154. $user = \app\common\model\User::getByOpenid($openid);
  155. if($user){
  156. //如果已经有账号则直接登录
  157. $ret = $this->auth->direct($user->id);
  158. }
  159. $loginret = \addons\third\library\Service::connect($platform, $result);
  160. if ($loginret) {
  161. $data = [
  162. 'userinfo' => $this->auth->getUserinfo(),
  163. 'thirdinfo' => $result
  164. ];
  165. $this->success(__('Logged in successful'), $data);
  166. }
  167. }
  168. $this->error(__('Operation failed'), $url);
  169. }
  170. /**
  171. * 留言内容
  172. *
  173. */
  174. public function leavelist(){
  175. $this->success('请求成功',Leave::where('uid',$this->auth->id)->field('id,type,content,createtime')->selectOrFail());
  176. }
  177. /**
  178. * 留言
  179. * @ApiMethod (POST)
  180. * @param string $content 留言内容
  181. */
  182. public function leave(){
  183. $input = $this->_validate(['content|留言内容'=>'require']);
  184. $data = [
  185. 'uid' => $this->auth->id,
  186. 'content' => $input['content'],
  187. 'type' => 1
  188. ];
  189. $inc = Leave::insert($data);
  190. if($inc){
  191. $this->success('留言成功',$inc);
  192. }else{
  193. $this->error('留言失败');
  194. }
  195. }
  196. /**
  197. * 退出登录
  198. * @ApiMethod (POST)
  199. */
  200. public function logout()
  201. {
  202. if (!$this->request->isPost()) {
  203. $this->error(__('Invalid parameters'));
  204. }
  205. $this->auth->logout();
  206. $this->success(__('Logout successful'));
  207. }
  208. }