User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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\Config;
  8. use think\Validate;
  9. use app\common\library\Keyworld;
  10. use think\Db;
  11. use app\common\library\Wechat;
  12. /**
  13. * 会员接口
  14. */
  15. class User extends Api
  16. {
  17. protected $noNeedLogin = ['wxmini_regmobile_login'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. }
  23. /**
  24. * 微信小程序登录+注册
  25. * code得到注册手机号,此手机号登录+注册
  26. */
  27. public function wxmini_regmobile_login(){
  28. $code = input('code');
  29. $opencode = input('opencode');
  30. if (!$code || !$opencode) {
  31. $this->error(__('Invalid parameters'));
  32. }
  33. $config = config('wxMiniProgram');
  34. $wechat = new Wechat($config['appid'],$config['secret']);
  35. $getuserphonenumber = $wechat->getuserphonenumber($code);
  36. if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
  37. $this->error('授权获取手机号失败');
  38. }
  39. //获取openid
  40. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
  41. $openidInfo = $this->getJson($getopenid);
  42. if(!isset($openidInfo['openid'])) {
  43. $this->error('用户openid获取失败',$openidInfo);
  44. }
  45. $openid = $openidInfo['openid'];
  46. if (!$openid) {
  47. $this->error('用户openid获取失败');
  48. }
  49. $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
  50. $userInfo = Db::name('user')->where('mobile',$mobile)->find();
  51. // 判断用户是否已经存在
  52. if($userInfo) { // 登录
  53. if ($userInfo['status'] != 1) {
  54. $this->error(__('Account is locked'));
  55. }
  56. //如果已经有账号则直接登录
  57. $res = $this->auth->direct($userInfo['id']);
  58. } else {
  59. $extend = ['wxmini_openid'=>$openid];
  60. $res = $this->auth->register('', '', '',$mobile, $extend);
  61. }
  62. if($res) {
  63. $this->success("登录成功!",$this->auth->getUserinfo());
  64. } else {
  65. $this->error($this->auth->getError());
  66. }
  67. }
  68. //用户详细资料
  69. public function userInfo(){
  70. $info = $this->auth->getUserinfo();
  71. $this->success(__('success'),$info);
  72. }
  73. /**
  74. * 退出登录
  75. * @ApiMethod (POST)
  76. */
  77. public function logout()
  78. {
  79. if (!$this->request->isPost()) {
  80. $this->error(__('Invalid parameters'));
  81. }
  82. $this->auth->logout();
  83. $this->success(__('Logout successful'));
  84. }
  85. /**
  86. * 修改会员个人信息
  87. *
  88. * @ApiMethod (POST)
  89. * @param string $avatar 头像地址
  90. * @param string $username 用户名
  91. * @param string $nickname 昵称
  92. * @param string $bio 个人简介
  93. */
  94. public function profile()
  95. {
  96. $field_array = [
  97. 'avatar','nickname'
  98. ];
  99. $data = [];
  100. foreach($field_array as $key => $field){
  101. //前端传不了post,改了
  102. /*if(!request()->has($field,'post')){
  103. continue;
  104. }*/
  105. if(!input('?'.$field)){
  106. continue;
  107. }
  108. $newone = input($field);
  109. if($field == 'avatar'){
  110. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  111. }
  112. $data[$field] = $newone;
  113. }
  114. if(empty($data)){
  115. $this->success();
  116. }
  117. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  118. $this->success();
  119. }
  120. //假注销
  121. public function cancleUser(){
  122. /*$captcha = input('captcha','');
  123. if (!$captcha) {
  124. $this->error(__('Invalid parameters'));
  125. }
  126. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  127. $this->error(__('Captcha is incorrect'));
  128. }*/
  129. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  130. $this->auth->logout();
  131. $this->success('注销成功');
  132. }
  133. //////////////////////////////////////////////////////
  134. //微信登录,预先假注册
  135. public function wechatlogin(){
  136. $code = input('code','');
  137. if(!$code){
  138. $this->error();
  139. }
  140. //微信
  141. $wechat = new Wechat();
  142. $wxuserinfo = $wechat->getAccessToken($code);
  143. if(!$wxuserinfo){
  144. $this->error('openid获取失败');
  145. }
  146. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  147. $this->error('openid获取失败');
  148. }
  149. $openid = $wxuserinfo['openid'];
  150. //检查用户
  151. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  152. if ($user) {
  153. if ($user['status'] == -1) {
  154. $this->error('账户已注销');
  155. }
  156. if ($user['status'] != 1) {
  157. $this->error(__('Account is locked'));
  158. }
  159. //如果已经有账号则直接登录
  160. $ret = $this->auth->direct($user['id']);
  161. if ($ret) {
  162. $userInfo = $this->auth->getUserinfo_simple();
  163. $userInfo['is_register'] = 0;
  164. $userInfo['code'] = $code;
  165. $this->success(__('Logged in successful'), $userInfo);
  166. } else {
  167. $this->error($this->auth->getError());
  168. }
  169. } else {
  170. //记录code和openid,绑定手机号的时候更新openid
  171. $wechatCodeData = [
  172. 'code' => $code,
  173. 'openid' => $openid,
  174. 'createtime' => time(),
  175. ];
  176. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  177. if (empty($wechatCode)) {
  178. Db::name('wechat_code')->insertGetId($wechatCodeData);
  179. } else {
  180. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  181. }
  182. //直接返回
  183. $userInfo = [];
  184. $userInfo['is_register'] = 1;
  185. $userInfo['code'] = $code;
  186. $this->success('获取信息成功', $userInfo);
  187. }
  188. }
  189. /**
  190. * 微信注册来的,绑定手机号
  191. *
  192. * @ApiMethod (POST)
  193. * @param string $mobile 手机号
  194. * @param string $captcha 验证码
  195. */
  196. public function bindmobile()
  197. {
  198. $mobile = input('mobile');
  199. $captcha = input('captcha');
  200. $code = input('code');
  201. if (!$mobile || !$captcha || !$code) {
  202. $this->error(__('Invalid parameters'));
  203. }
  204. if (!Validate::regex($mobile, "^1\d{10}$")) {
  205. $this->error(__('Mobile is incorrect'));
  206. }
  207. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  208. $this->error(__('Captcha is incorrect'));
  209. }
  210. $wechatCodeWhere['code'] = $code;
  211. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  212. if (empty($wechatCode)) {
  213. $this->error('请先微信登录');
  214. }
  215. //检查appid绑定的用户
  216. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  217. if ($user) {
  218. if ($user['status'] == -1) {
  219. $this->error('账户已注销');
  220. }
  221. if ($user['status'] != 1) {
  222. $this->error(__('Account is locked'));
  223. }
  224. //如果已经有账号则直接登录
  225. $ret = $this->auth->direct($user['id']);
  226. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  227. }
  228. //新的openid用户
  229. $where = [];
  230. $where['mobile'] = $mobile;
  231. $userData = Db::name('user')->where($where)->find();//老用户
  232. if (!empty($userData)) {
  233. if (empty($userData['wechat_openid'])) {
  234. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  235. } else {
  236. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  237. $this->error('该手机号已被其他用户绑定');
  238. }
  239. }
  240. $ret = $this->auth->direct($userData['id']);
  241. } else {
  242. $extend = [
  243. 'wechat_openid' => $wechatCode['openid'],
  244. ];
  245. $ret = $this->auth->register('', '','', $mobile, $extend);
  246. }
  247. if (!$ret) {
  248. $this->error($this->auth->getError());
  249. }
  250. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  251. }
  252. /**
  253. * 修改手机号
  254. *
  255. * @ApiMethod (POST)
  256. * @param string $mobile 手机号
  257. * @param string $captcha 验证码
  258. */
  259. public function changemobile()
  260. {
  261. $user = $this->auth->getUser();
  262. $oldcaptcha = input('oldcaptcha');
  263. $mobile = input('mobile');
  264. $captcha = input('captcha');
  265. if (!$oldcaptcha || !$mobile || !$captcha) {
  266. $this->error(__('Invalid parameters'));
  267. }
  268. if (!Validate::regex($mobile, "^1\d{10}$")) {
  269. $this->error(__('Mobile is incorrect'));
  270. }
  271. if($user->mobile == $mobile){
  272. $this->error('新手机号不能与旧手机号相同');
  273. }
  274. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  275. $this->error(__('Mobile already exist'));
  276. }
  277. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  278. if (!$result) {
  279. $this->error('原手机号验证码错误');
  280. }
  281. $result = Sms::check($mobile, $captcha, 'changemobile');
  282. if (!$result) {
  283. $this->error('新手机号验证码错误');
  284. }
  285. Sms::flush($user->mobile, 'changemobile');
  286. Sms::flush($mobile, 'changemobile');
  287. $user->mobile = $mobile;
  288. $user->save();
  289. $this->success();
  290. }
  291. }