User.php 15 KB

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