User.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @ApiParams (name="code", type="string", required=true, description="code码")
  138. * @ApiReturnParams (name=code,type="integer",description=错误码:0=失败1=成功401=未登录403=没有权限)
  139. * @ApiReturnParams (name=msg,type="string",description=提示信息)
  140. * @ApiReturnParams (name=data,type="array",description=要返回的数据)
  141. * @ApiReturnParams (name=data.return_type,type="integer",description=返回类型:1=登录成功2=授权成功)
  142. * @ApiReturnParams (name=data.token,type="string",description=token值(返回类型为1时返回的值))
  143. * @ApiReturnParams (name=data.openid,type="string",description=token值(返回类型为2时返回的值))
  144. */
  145. public function third()
  146. {
  147. $url = url('user/index');
  148. $platform = $this->request->post("platform",'Wechat');
  149. $code = $this->request->post("code");
  150. $config = get_addon_config('third');
  151. if (!$config || !isset($config[$platform])) {
  152. $this->error(__('Invalid parameters'));
  153. }
  154. $app = new \addons\third\library\Application($config);
  155. //通过code换access_token和绑定会员
  156. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  157. if ($result) {
  158. $openid = $result['openid'];
  159. $user = \app\common\model\User::getByOpenid($openid);
  160. if($user){
  161. //如果已经有账号则直接登录
  162. $this->auth->direct($user->id);
  163. $data = ['return_type'=>1,'token' => $this->auth->getUserinfo()['token']];
  164. $this->success(__('Logged in successful'), $data);
  165. }else{
  166. $data = ['return_type'=>2,'openid' => $openid];
  167. $this->success('授权成功', $data);
  168. }
  169. /*$loginret = \addons\third\library\Service::connect($platform, $result);
  170. if ($loginret) {
  171. $data = [
  172. 'userinfo' => $this->auth->getUserinfo(),
  173. 'thirdinfo' => $result
  174. ];
  175. $this->success(__('Logged in successful'), $data);
  176. }*/
  177. }
  178. $this->error(__('Operation failed'), $url);
  179. }
  180. /**
  181. * 留言内容
  182. *
  183. */
  184. public function leavelist(){
  185. $this->success('请求成功',Leave::where('uid',$this->auth->id)->field('id,type,content,createtime')->selectOrFail());
  186. }
  187. /**
  188. * 留言
  189. * @ApiMethod (POST)
  190. * @param string $content 留言内容
  191. */
  192. public function leave(){
  193. $input = $this->_validate(['content|留言内容'=>'require']);
  194. $data = [
  195. 'uid' => $this->auth->id,
  196. 'content' => $input['content'],
  197. 'type' => 1
  198. ];
  199. $inc = Leave::insert($data);
  200. if($inc){
  201. $this->success('留言成功',$inc);
  202. }else{
  203. $this->error('留言失败');
  204. }
  205. }
  206. /**
  207. * 退出登录
  208. * @ApiMethod (POST)
  209. */
  210. public function logout()
  211. {
  212. if (!$this->request->isPost()) {
  213. $this->error(__('Invalid parameters'));
  214. }
  215. $this->auth->logout();
  216. $this->success(__('Logout successful'));
  217. }
  218. }