User.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * @ApiParams (name="openid", type="string", required=true, description="微信登录授权返回的openid")
  50. * @ApiReturnParams (name=code,type="integer",description=错误码:0=失败1=成功401=未登录403=没有权限)
  51. * @ApiReturnParams (name=msg,type="string",description=提示信息)
  52. * @ApiReturnParams (name=data,type="array",description=要返回的数据)
  53. * @ApiReturnParams (name=data.token,type="string",description=token值)
  54. */
  55. public function mobile_login()
  56. {
  57. $mobile = $this->request->post('mobile');
  58. $captcha = $this->request->post('captcha');
  59. $openid = $this->request->post('openid');
  60. if (!$mobile || !$captcha) {
  61. $this->error(__('Invalid parameters'));
  62. }
  63. if (!Validate::regex($mobile, "^1\d{10}$")) {
  64. $this->error(__('Mobile is incorrect'));
  65. }
  66. if (!Sms::check($mobile, $captcha, 'register')) {
  67. $this->error(__('Captcha is incorrect'));
  68. }
  69. $user = \app\common\model\User::getByMobile($mobile);
  70. if ($user) {
  71. if ($user->status != 'normal') {
  72. $this->error(__('Account is locked'));
  73. }
  74. //如果已经有账号则直接登录
  75. $ret = $this->auth->direct($user->id);
  76. } else {
  77. $ret = $this->auth->register('植提桥用户'.substr($mobile,-4), Random::alnum(), '', $mobile, ['openid'=>$openid]);
  78. }
  79. if ($ret) {
  80. Sms::flush($mobile, 'register');
  81. $data = ['token' => $this->auth->getUserinfo()['token']];
  82. $this->success(__('Logged in successful'), $data);
  83. } else {
  84. $this->error($this->auth->getError());
  85. }
  86. }
  87. /**
  88. * 修改用户信息
  89. * @ApiMethod (POST)
  90. * @ApiParams (name="avatar", type="string", required=false, description="头像地址")
  91. * @ApiParams (name="username", type="string", required=false, description="姓名")
  92. * @ApiParams (name="gender", type="string", required=false, description="性别")
  93. * @ApiParams (name="birthday", type="string", required=false, description="出生年月")
  94. * @ApiParams (name="email", type="string", required=false, description="邮箱地址")
  95. */
  96. public function edit_user_info()
  97. {
  98. $user = $this->auth->getUser();
  99. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  100. $username = $this->request->post('username');
  101. $gender = $this->request->post('gender');
  102. $birthday = $this->request->post('birthday');
  103. $email = $this->request->post('email');
  104. if(empty($avatar) && empty($username) && empty($gender) && empty($birthday) && empty($email)){
  105. $this->error('参数错误');
  106. }
  107. if($avatar){
  108. $user->avatar = $avatar;
  109. }
  110. if ($username) {
  111. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  112. if ($exists) {
  113. $this->error(__('Username already exists'));
  114. }
  115. $user->username = $username;
  116. }
  117. if($gender){
  118. $user->gender = $gender;
  119. }
  120. if($birthday){
  121. $user->birthday = $birthday;
  122. }
  123. if($email){
  124. if (!Validate::is($email, "email")) {
  125. $this->error(__('Email is incorrect'));
  126. }
  127. $exists = \app\common\model\User::where('username', $email)->where('id', '<>', $this->auth->id)->find();
  128. if ($exists) {
  129. $this->error('邮箱已存在');
  130. }
  131. $user->email = $email;
  132. }
  133. $user->save();
  134. $this->success('编辑成功');
  135. }
  136. /**
  137. * 微信登录
  138. * @ApiMethod (POST)
  139. * @ApiParams (name="code", type="string", required=true, description="code码")
  140. * @ApiReturnParams (name=code,type="integer",description=错误码:0=失败1=成功401=未登录403=没有权限)
  141. * @ApiReturnParams (name=msg,type="string",description=提示信息)
  142. * @ApiReturnParams (name=data,type="array",description=要返回的数据)
  143. * @ApiReturnParams (name=data.return_type,type="integer",description=返回类型:1=登录成功2=授权成功)
  144. * @ApiReturnParams (name=data.token,type="string",description=token值(返回类型为1时返回的值))
  145. * @ApiReturnParams (name=data.openid,type="string",description=token值(返回类型为2时返回的值))
  146. */
  147. public function third()
  148. {
  149. $url = url('user/index');
  150. $platform = $this->request->post("platform",'Wechat');
  151. $code = $this->request->post("code");
  152. $config = get_addon_config('third');
  153. if (!$config || !isset($config[$platform])) {
  154. $this->error(__('Invalid parameters'));
  155. }
  156. $app = new \addons\third\library\Application($config);
  157. //通过code换access_token和绑定会员
  158. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  159. if ($result) {
  160. $openid = $result['openid'];
  161. $user = \app\common\model\User::getByOpenid($openid);
  162. if($user){
  163. //如果已经有账号则直接登录
  164. $this->auth->direct($user->id);
  165. $data = ['return_type'=>1,'token' => $this->auth->getUserinfo()['token']];
  166. $this->success(__('Logged in successful'), $data);
  167. }else{
  168. $data = ['return_type'=>2,'openid' => $openid];
  169. $this->success('授权成功', $data);
  170. }
  171. /*$loginret = \addons\third\library\Service::connect($platform, $result);
  172. if ($loginret) {
  173. $data = [
  174. 'userinfo' => $this->auth->getUserinfo(),
  175. 'thirdinfo' => $result
  176. ];
  177. $this->success(__('Logged in successful'), $data);
  178. }*/
  179. }
  180. $this->error(__('Operation failed'), $url);
  181. }
  182. /**
  183. * 留言内容
  184. *
  185. */
  186. public function leavelist(){
  187. $this->success('请求成功',Leave::where('uid',$this->auth->id)->field('id,type,content,createtime')->selectOrFail());
  188. }
  189. /**
  190. * 留言
  191. * @ApiMethod (POST)
  192. * @param string $content 留言内容
  193. */
  194. public function leave(){
  195. $input = $this->_validate(['content|留言内容'=>'require']);
  196. $data = [
  197. 'uid' => $this->auth->id,
  198. 'content' => $input['content'],
  199. 'type' => 1
  200. ];
  201. $inc = Leave::insert($data);
  202. if($inc){
  203. $this->success('留言成功',$inc);
  204. }else{
  205. $this->error('留言失败');
  206. }
  207. }
  208. /**
  209. * 退出登录
  210. * @ApiMethod (POST)
  211. */
  212. public function logout()
  213. {
  214. if (!$this->request->isPost()) {
  215. $this->error(__('Invalid parameters'));
  216. }
  217. $this->auth->logout();
  218. $this->success(__('Logout successful'));
  219. }
  220. }