User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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\utils\Service\Tencent\TencentIm;
  7. use think\Validate;
  8. use think\Db;
  9. use app\common\library\Wechat;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['login','mobilelogin','register','wechatlogin','bindmobile','resetpwd'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 会员中心
  23. */
  24. public function index()
  25. {
  26. }
  27. /**
  28. * 会员登录
  29. *
  30. * @ApiMethod (POST)
  31. * @param string $account 账号
  32. * @param string $password 密码
  33. */
  34. public function login()
  35. {
  36. $account = input('account');
  37. $password = input('password');
  38. if (!$account || !$password) {
  39. $this->error(__('Invalid parameters'));
  40. }
  41. $ret = $this->auth->login($account, $password);
  42. if ($ret) {
  43. $data = $this->auth->getUserinfo_smiple();
  44. $im = new TencentIm();
  45. $im->register($data['chat_id'],$data['nickname'],$data['avatar']);
  46. $this->success(__('Logged in successful'), $data);
  47. } else {
  48. $this->error($this->auth->getError());
  49. }
  50. }
  51. /**
  52. * 手机验证码登录
  53. *
  54. * @ApiMethod (POST)
  55. * @param string $mobile 手机号
  56. * @param string $captcha 验证码
  57. */
  58. public function mobilelogin()
  59. {
  60. $mobile = input('mobile');
  61. $captcha = input('captcha');
  62. if (!$mobile || !$captcha) {
  63. $this->error(__('Invalid parameters'));
  64. }
  65. if (!Validate::regex($mobile, "^1\d{10}$")) {
  66. $this->error(__('Mobile is incorrect'));
  67. }
  68. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  69. $this->error(__('Captcha is incorrect'));
  70. }
  71. $user = \app\common\model\User::getByMobile($mobile);
  72. if ($user) {
  73. if ($user->status != 1) {
  74. $this->error(__('Account is locked'));
  75. }
  76. //如果已经有账号则直接登录
  77. $ret = $this->auth->direct($user->id);
  78. } else {
  79. $this->error('不存在的用户');
  80. }
  81. if ($ret) {
  82. Sms::flush($mobile, 'mobilelogin');
  83. $data = $this->auth->getUserinfo_smiple();
  84. $im = new TencentIm();
  85. $im->register($data['chat_id'],$data['nickname'],$data['avatar']);
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  90. }
  91. /**
  92. * 注册会员
  93. *
  94. * @ApiMethod (POST)
  95. * @param string $username 用户名
  96. * @param string $password 密码
  97. * @param string $email 邮箱
  98. * @param string $mobile 手机号
  99. * @param string $code 验证码
  100. */
  101. public function register()
  102. {
  103. $mobile = input('mobile');
  104. $captcha = input('captcha');
  105. $password = input('password');
  106. if (!$mobile || !$captcha || !$password) {
  107. $this->error(__('Invalid parameters'));
  108. }
  109. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  110. $this->error(__('Mobile is incorrect'));
  111. }
  112. $ret = Sms::check($mobile, $captcha, 'register');
  113. if (!$ret) {
  114. $this->error(__('Captcha is incorrect'));
  115. }
  116. $ret = $this->auth->register('', $password, '', $mobile, []);
  117. if ($ret) {
  118. $data = $this->auth->getUserinfo_smiple();
  119. $im = new TencentIm();
  120. $im->register($data['chat_id'],$data['nickname'],$data['avatar']);
  121. $this->success(__('Sign up successful'), $data);
  122. } else {
  123. $this->error($this->auth->getError());
  124. }
  125. }
  126. //微信登录,预先假注册
  127. public function wechatlogin(){
  128. $code = input('code','');
  129. if(!$code){
  130. $this->error(__('Invalid parameters'));
  131. }
  132. $from = input('from','app');
  133. $field = $from == 'app' ? 'app_openid' : 'mini_openid';
  134. //微信
  135. $wechat = new Wechat();
  136. $wxuserinfo = $wechat->getAccessToken($code);
  137. if(!$wxuserinfo){
  138. $this->error('openid获取失败');
  139. }
  140. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid']) || !isset($wxuserinfo['unionid'])){
  141. $this->error('openid获取失败');
  142. }
  143. $openid = $wxuserinfo['openid'];
  144. $unionid = $wxuserinfo['unionid'];
  145. //检查用户
  146. $user = Db::name('user')->where('unionid',$unionid)->find();
  147. if ($user) {
  148. if ($user['status'] == -1) {
  149. $this->error('账户已注销');
  150. }
  151. if ($user['status'] != 1) {
  152. $this->error(__('Account is locked'));
  153. }
  154. /*$update = [
  155. $field => $openid,//应该就只有mini支付时用
  156. ];
  157. Db::name('user')->where('id',$user['id'])->update($update);*/
  158. //如果已经有账号则直接登录
  159. $ret = $this->auth->direct($user['id']);
  160. if ($ret) {
  161. $userInfo = $this->auth->getUserinfo_simple();
  162. $userInfo['is_register'] = 0;
  163. $userInfo['code'] = $code;
  164. $this->success(__('Logged in successful'), $userInfo);
  165. } else {
  166. $this->error($this->auth->getError());
  167. }
  168. } else {
  169. //记录code和openid,绑定手机号的时候更新openid
  170. $wechatCodeData = [
  171. 'code' => $code,
  172. 'openid' => $openid,
  173. 'unionid' => $unionid,
  174. 'from' => $from,
  175. 'createtime' => time(),
  176. ];
  177. $wechatCode = Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->find();
  178. if (empty($wechatCode)) {
  179. Db::name('wechat_code')->insertGetId($wechatCodeData);
  180. } else {
  181. Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->update($wechatCodeData);
  182. }
  183. //直接返回
  184. $userInfo = [];
  185. $userInfo['is_register'] = 1;
  186. $userInfo['code'] = $code;
  187. $this->success('获取信息成功', $userInfo);
  188. }
  189. }
  190. /**
  191. * 微信注册来的,绑定手机号
  192. *
  193. * @ApiMethod (POST)
  194. * @param string $mobile 手机号
  195. * @param string $captcha 验证码
  196. */
  197. public function bindmobile()
  198. {
  199. $mobile = input('mobile');
  200. $captcha = input('captcha');
  201. $code = input('code');
  202. $from = input('from','app');
  203. $field = $from == 'app' ? 'app_openid' : 'mini_openid';
  204. if (!$mobile || !$captcha || !$code) {
  205. $this->error(__('Invalid parameters'));
  206. }
  207. if (!Validate::regex($mobile, "^1\d{10}$")) {
  208. $this->error(__('Mobile is incorrect'));
  209. }
  210. $result = Sms::check($mobile, $captcha, 'wechatregister');
  211. if (!$result) {
  212. $this->error(__('Captcha is incorrect'));
  213. }
  214. $wechatCodeWhere['code'] = $code;
  215. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  216. if (empty($wechatCode)) {
  217. $this->error('请先微信登录');
  218. }
  219. //检查appid绑定的用户
  220. $user = Db::name('user')->where('unionid',$wechatCode['unionid'])->find();
  221. if ($user) {
  222. if ($user['status'] == -1) {
  223. $this->error('账户已注销');
  224. }
  225. if ($user['status'] != 1) {
  226. $this->error(__('Account is locked'));
  227. }
  228. //如果已经有账号则直接登录
  229. $ret = $this->auth->direct($user['id']);
  230. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  231. }
  232. //新的openid用户
  233. $where = [];
  234. $where['mobile'] = $mobile;
  235. $userData = Db::name('user')->where($where)->find();//老用户
  236. if (!empty($userData)) {
  237. if (empty($userData['unionid'])) {
  238. Db::name('user')->where('id',$userData['id'])->update([$field => $wechatCode['openid'],'unionid' => $wechatCode['unionid']]);//老用户更新openid
  239. } else {
  240. if ($userData['unionid'] != $wechatCode['unionid']) {
  241. $this->error('该手机号已被其他用户绑定');
  242. }
  243. }
  244. $ret = $this->auth->direct($userData['id']);
  245. } else {
  246. $extend = [
  247. $field => $wechatCode['openid'],
  248. 'unionid' => $wechatCode['unionid'],
  249. ];
  250. $ret = $this->auth->register('', '','', $mobile, $extend);
  251. }
  252. if (!$ret) {
  253. $this->error($this->auth->getError());
  254. }
  255. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  256. }
  257. /**
  258. * 退出登录
  259. * @ApiMethod (POST)
  260. */
  261. public function logout()
  262. {
  263. if (!$this->request->isPost()) {
  264. $this->error(__('Invalid parameters'));
  265. }
  266. $this->auth->logout();
  267. $this->success(__('Logout successful'));
  268. }
  269. //真注销
  270. public function cancleuser(){
  271. if (!$this->request->isPost()) {
  272. $this->error(__('Invalid parameters'));
  273. }
  274. //退出im
  275. // $tenIm = new Tenim();
  276. // $tenIm->loginoutim($this->auth->id);
  277. $data = [
  278. 'status' => -1,
  279. 'mobile' => 'close_'.$this->auth->mobile,
  280. 'app_openid' => 'close_'.$this->auth->app_openid,
  281. 'mini_openid' => 'close_'.$this->auth->mini_openid,
  282. // 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
  283. ];
  284. Db::name('user')->where('id',$this->auth->id)->update($data);
  285. $this->auth->logout();
  286. $this->success('注销成功');
  287. }
  288. //用户详细资料
  289. public function userinfo(){
  290. $info = $this->auth->getUserinfo();
  291. $this->success(__('success'),$info);
  292. }
  293. /**
  294. * 修改会员个人信息
  295. *
  296. * @ApiMethod (POST)
  297. * @param string $avatar 头像地址
  298. * @param string $username 用户名
  299. * @param string $nickname 昵称
  300. * @param string $bio 个人简介
  301. */
  302. public function profile()
  303. {
  304. $field_array = ['nickname','avatar'];
  305. $data = [];
  306. foreach($field_array as $key => $field){
  307. //前端传不了post,改了
  308. /*if(!request()->has($field,'post')){
  309. continue;
  310. }*/
  311. if(!input('?'.$field)){
  312. continue;
  313. }
  314. $newone = input($field);
  315. if($field == 'avatar'){
  316. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  317. }
  318. $data[$field] = $newone;
  319. }
  320. if(empty($data)){
  321. $this->success();
  322. }
  323. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  324. if($update_rs === false){
  325. $this->error('修改资料失败');
  326. }
  327. $this->success();
  328. }
  329. /**
  330. * 修改手机号
  331. *
  332. * @ApiMethod (POST)
  333. * @param string $mobile 手机号
  334. * @param string $captcha 验证码
  335. */
  336. public function changemobile()
  337. {
  338. $user = $this->auth->getUser();
  339. $mobile = input('mobile');
  340. $captcha = input('captcha');
  341. if (!$mobile || !$captcha) {
  342. $this->error(__('Invalid parameters'));
  343. }
  344. if (!Validate::regex($mobile, "^1\d{10}$")) {
  345. $this->error(__('Mobile is incorrect'));
  346. }
  347. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  348. $this->error(__('Mobile already exists'));
  349. }
  350. $result = Sms::check($mobile, $captcha, 'changemobile');
  351. if (!$result) {
  352. $this->error(__('Captcha is incorrect'));
  353. }
  354. $user->mobile = $mobile;
  355. $user->save();
  356. Sms::flush($mobile, 'changemobile');
  357. $this->success();
  358. }
  359. //修改密码
  360. public function changepwd()
  361. {
  362. $mobile = $this->auth->mobile;
  363. $captcha = input('captcha');
  364. $newpassword = input('newpassword','');
  365. if (!$mobile || !$captcha || !$newpassword) {
  366. $this->error(__('Invalid parameters'));
  367. }
  368. if (!Validate::regex($mobile, "^1\d{10}$")) {
  369. $this->error(__('Mobile is incorrect'));
  370. }
  371. $result = Sms::check($mobile, $captcha, 'changepwd');
  372. if (!$result) {
  373. $this->error(__('Captcha is incorrect'));
  374. }
  375. Sms::flush($mobile, 'changepwd');
  376. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  377. $this->error(__('Password must be 6 to 30 characters'));
  378. }
  379. $ret = $this->auth->changepwd($newpassword, '', true);
  380. if ($ret) {
  381. $this->success(__('Reset password successful'));
  382. } else {
  383. $this->error($this->auth->getError());
  384. }
  385. }
  386. /**
  387. * 重置密码
  388. *
  389. * @ApiMethod (POST)
  390. * @param string $mobile 手机号
  391. * @param string $newpassword 新密码
  392. * @param string $captcha 验证码
  393. */
  394. public function resetpwd()
  395. {
  396. $mobile = input("mobile");
  397. $captcha = input("captcha");
  398. $newpassword = input("newpassword");
  399. if (!$mobile || !$captcha || !$newpassword) {
  400. $this->error(__('Invalid parameters'));
  401. }
  402. if (!Validate::regex($mobile, "^1\d{10}$")) {
  403. $this->error(__('Mobile is incorrect'));
  404. }
  405. $user = \app\common\model\User::getByMobile($mobile);
  406. if (!$user) {
  407. $this->error('不存在的用户');
  408. }else{
  409. if ($user->status != 1) {
  410. $this->error(__('Account is locked'));
  411. }
  412. }
  413. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  414. if (!$ret) {
  415. $this->error(__('Captcha is incorrect'));
  416. }
  417. Sms::flush($mobile, 'resetpwd');
  418. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  419. $this->error(__('Password must be 6 to 30 characters'));
  420. }
  421. //模拟一次登录
  422. $this->auth->direct($user->id);
  423. $ret = $this->auth->changepwd($newpassword, '', true);
  424. if ($ret) {
  425. $this->success(__('Reset password successful'));
  426. } else {
  427. $this->error($this->auth->getError());
  428. }
  429. }
  430. }