User.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 fast\Random;
  7. use think\Db;
  8. use think\Validate;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Api
  13. {
  14. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','sendPhone'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 会员中心
  22. */
  23. public function index()
  24. {
  25. $this->success('', ['welcome' => $this->auth->nickname]);
  26. }
  27. /**
  28. * 会员登录
  29. *
  30. * @param string $account 账号
  31. * @param string $password 密码
  32. */
  33. public function login()
  34. {
  35. $account = $this->request->request('account');
  36. $password = $this->request->request('password');
  37. if (!$account || !$password) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $ret = $this->auth->login($account, $password);
  41. if ($ret) {
  42. $data = ['userinfo' => $this->auth->getUserinfo()];
  43. $this->success(__('Logged in successful'), $data);
  44. } else {
  45. $this->error($this->auth->getError());
  46. }
  47. }
  48. /**
  49. * 手机验证码登录
  50. *
  51. * @param string $mobile 手机号
  52. * @param string $captcha 验证码
  53. */
  54. public function mobilelogin()
  55. {
  56. $mobile = $this->request->request('mobile');
  57. $captcha = $this->request->request('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. $ret = session($mobile);
  65. if (!$ret || $ret != $captcha) {
  66. $this->error(__('Captcha is incorrect'));
  67. }
  68. $user = \app\common\model\User::getByMobile($mobile);
  69. if ($user) {
  70. if ($user->status != '1') {
  71. $this->error(__('Account is locked'));
  72. }
  73. //如果已经有账号则直接登录
  74. $ret = $this->auth->direct($user->id);
  75. if ($ret) {
  76. Sms::flush($mobile, 'mobilelogin');
  77. $data = ['userinfo' => $this->auth->getUserinfo()];
  78. $this->success(__('Logged in successful'), $data);
  79. } else {
  80. $this->error($this->auth->getError());
  81. }
  82. } else {
  83. return $this->error('暂无账此号请去注册');
  84. // $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  85. }
  86. }
  87. /**
  88. * 注册会员
  89. *
  90. * @param string $password 密码
  91. * @param string $group_id 身份012
  92. * @param string $mobile 手机号
  93. * @param string $code 验证码
  94. */
  95. public function register()
  96. {
  97. $password = $this->request->request('password');
  98. $mobile = $this->request->request('mobile');
  99. $group_id = $this->request->request('group_id');
  100. $username = $mobile;
  101. $code = $this->request->request('code');
  102. if (!$username || !$password) {
  103. $this->error(__('Invalid parameters'));
  104. }
  105. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  106. $this->error(__('Mobile is incorrect'));
  107. }
  108. $ret = session($mobile);
  109. if (!$ret || $ret != $code) {
  110. $this->error(__('Captcha is incorrect'));
  111. }
  112. if (!$group_id) {
  113. $group_id = 0;
  114. }
  115. $ret = $this->auth->register($username, $password, '', $mobile, [], $group_id);
  116. if ($ret) {
  117. $data = ['userinfo' => $this->auth->getUserinfo()];
  118. $this->success(__('Sign up successful'), $data);
  119. } else {
  120. $this->error($this->auth->getError());
  121. }
  122. }
  123. /**
  124. * 退出登录
  125. */
  126. public function logout()
  127. {
  128. $this->auth->logout();
  129. $this->success(__('Logout successful'));
  130. }
  131. /**
  132. * 修改会员个人信息
  133. *
  134. * @param string $avatar 头像地址
  135. * @param string $username 用户名
  136. * @param string $nickname 昵称
  137. * @param string $bio 个人简介
  138. */
  139. public function profile()
  140. {
  141. $user = $this->auth->getUser();
  142. $username = $this->request->request('username');
  143. $nickname = $this->request->request('nickname');
  144. $bio = $this->request->request('bio');
  145. $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
  146. if ($username) {
  147. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  148. if ($exists) {
  149. $this->error(__('Username already exists'));
  150. }
  151. $user->username = $username;
  152. }
  153. if ($nickname) {
  154. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  155. if ($exists) {
  156. $this->error(__('Nickname already exists'));
  157. }
  158. $user->nickname = $nickname;
  159. }
  160. $user->bio = $bio;
  161. $user->avatar = $avatar;
  162. $user->save();
  163. $this->success();
  164. }
  165. /**
  166. * 修改手机号
  167. *
  168. * @param string $mobile 手机号
  169. * @param string $captcha 验证码
  170. */
  171. public function changemobile()
  172. {
  173. $user = $this->auth->getUser();
  174. $mobile = $this->request->request('mobile');
  175. $captcha = $this->request->request('captcha');
  176. if (!$mobile || !$captcha) {
  177. $this->error(__('Invalid parameters'));
  178. }
  179. if (!Validate::regex($mobile, "^1\d{10}$")) {
  180. $this->error(__('Mobile is incorrect'));
  181. }
  182. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  183. $this->error(__('Mobile already exists'));
  184. }
  185. $result = Sms::check($mobile, $captcha, 'changemobile');
  186. if (!$result) {
  187. $this->error(__('Captcha is incorrect'));
  188. }
  189. $verification = $user->verification;
  190. $verification->mobile = 1;
  191. $user->verification = $verification;
  192. $user->mobile = $mobile;
  193. $user->save();
  194. Sms::flush($mobile, 'changemobile');
  195. $this->success();
  196. }
  197. /**
  198. * 微信登录
  199. *
  200. * @param string $code Code码
  201. */
  202. // public function third()
  203. //
  204. // {
  205. //
  206. // $wchat = new WeChat();
  207. //
  208. //
  209. // $code = request()->param('code', "");
  210. //
  211. // $user = $wchat->getUserAccessUserInfo($code);
  212. // dump($user);die;
  213. //
  214. // }
  215. //微信登录
  216. public function third(){
  217. $code = request()->param('code', "");//获取code
  218. $appid ="wxe02aa578255f9184";
  219. $secret = "39ec8add0b8d4ed794e9cb330a334538";
  220. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
  221. //通过code换取网页授权access_token
  222. $weixin = file_get_contents($url);
  223. dump($weixin);die;
  224. $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
  225. $array = get_object_vars($jsondecode);//转换成数组
  226. $openid = $array['openid'];//输出openid
  227. return $openid;
  228. }
  229. /**
  230. * 重置密码
  231. *
  232. * @param string $mobile 手机号
  233. * @param string $newpassword 新密码
  234. * @param string $captcha 验证码
  235. */
  236. public function resetpwd()
  237. {
  238. // $type = $this->request->request("type");
  239. $type = "mobile";
  240. $mobile = $this->request->request("mobile");
  241. $email = $this->request->request("email");
  242. $newpassword = $this->request->request("newpassword");
  243. $captcha = $this->request->request("captcha");
  244. if (!$newpassword || !$captcha) {
  245. $this->error(__('Invalid parameters'));
  246. }
  247. if (!Validate::regex($mobile, "^1\d{10}$")) {
  248. $this->error(__('Mobile is incorrect'));
  249. }
  250. $user = \app\common\model\User::getByMobile($mobile);
  251. if (!$user) {
  252. $this->error(__('User not found'));
  253. }
  254. $ret = session($mobile);
  255. if (!$ret || $ret != $captcha) {
  256. $this->error(__('Captcha is incorrect'));
  257. }
  258. //模拟一次登录
  259. $this->auth->direct($user->id);
  260. $rets = $this->auth->changepwd($newpassword, '', true);
  261. if ($rets) {
  262. $this->success(__('Reset password successful'));
  263. } else {
  264. $this->error($this->auth->getError());
  265. }
  266. }
  267. /**
  268. * 发送验证码
  269. *
  270. * @param string $mobile 手机号
  271. * @param string $type 1注册2忘记3修改密码
  272. */
  273. public function sendPhone()
  274. {
  275. $mobile = $this->request->param('mobile');
  276. $type = $this->request->param('type');
  277. if (!isset($type) || empty($type)) return $this->error('参数错误');
  278. if ($type == 1) {
  279. $issetphone = Db::name('user')->where('mobile', $mobile)->find();
  280. if (isset($issetphone)) return $this->error('此账号已存在');
  281. }
  282. if ($type == 3) {
  283. $user = $this->auth->getUser();
  284. $isuseourphone = Db::name('user')->where('id', $user['id'])->where('mobile', $mobile)->find();
  285. if (!$isuseourphone) return $this->error('请使用本账号手机号修改密码');
  286. }
  287. $number = rand(1000, 9999);
  288. $res = send_sms($mobile, 1, ['code' => $number]);
  289. if (isset($res['Message']) && $res['Message'] == "OK") {
  290. session($mobile, $number);
  291. return $this->success('发送成功', $number);
  292. } else {
  293. return $this->error('发送失败');
  294. }
  295. // $send->index($mobile,$number);
  296. // return $this->success('',$number);
  297. }
  298. }