User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 = ['wechatlogin', 'bindmobile'];
  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 wechatlogin(){
  70. $code = input('code','');
  71. if(!$code){
  72. $this->error();
  73. }
  74. //微信
  75. $wechat = new Wechat();
  76. $wxuserinfo = $wechat->getAccessToken($code);
  77. if(!$wxuserinfo){
  78. $this->error('openid获取失败');
  79. }
  80. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  81. $this->error('openid获取失败');
  82. }
  83. $openid = $wxuserinfo['openid'];
  84. //检查用户
  85. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  86. if ($user) {
  87. if ($user['status'] == -1) {
  88. $this->error('账户已注销');
  89. }
  90. if ($user['status'] != 1) {
  91. $this->error(__('Account is locked'));
  92. }
  93. //如果已经有账号则直接登录
  94. $ret = $this->auth->direct($user['id']);
  95. if ($ret) {
  96. $userInfo = $this->auth->getUserinfo_simple();
  97. $userInfo['is_register'] = 0;
  98. $userInfo['code'] = $code;
  99. $this->success(__('Logged in successful'), $userInfo);
  100. } else {
  101. $this->error($this->auth->getError());
  102. }
  103. } else {
  104. //记录code和openid,绑定手机号的时候更新openid
  105. $wechatCodeData = [
  106. 'code' => $code,
  107. 'openid' => $openid,
  108. 'createtime' => time(),
  109. ];
  110. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  111. if (empty($wechatCode)) {
  112. Db::name('wechat_code')->insertGetId($wechatCodeData);
  113. } else {
  114. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  115. }
  116. //直接返回
  117. $userInfo = [];
  118. $userInfo['is_register'] = 1;
  119. $userInfo['code'] = $code;
  120. $this->success('获取信息成功', $userInfo);
  121. }
  122. }
  123. /**
  124. * 微信注册来的,绑定手机号
  125. *
  126. * @ApiMethod (POST)
  127. * @param string $mobile 手机号
  128. * @param string $captcha 验证码
  129. */
  130. public function bindmobile()
  131. {
  132. $mobile = input('mobile');
  133. $captcha = input('captcha');
  134. $code = input('code');
  135. if (!$mobile || !$captcha || !$code) {
  136. $this->error(__('Invalid parameters'));
  137. }
  138. if (!Validate::regex($mobile, "^1\d{10}$")) {
  139. $this->error(__('Mobile is incorrect'));
  140. }
  141. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  142. $this->error(__('Captcha is incorrect'));
  143. }
  144. $wechatCodeWhere['code'] = $code;
  145. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  146. if (empty($wechatCode)) {
  147. $this->error('请先微信登录');
  148. }
  149. //检查appid绑定的用户
  150. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  151. if ($user) {
  152. if ($user['status'] == -1) {
  153. $this->error('账户已注销');
  154. }
  155. if ($user['status'] != 1) {
  156. $this->error(__('Account is locked'));
  157. }
  158. //如果已经有账号则直接登录
  159. $ret = $this->auth->direct($user['id']);
  160. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  161. }
  162. //新的openid用户
  163. $where = [];
  164. $where['mobile'] = $mobile;
  165. $userData = Db::name('user')->where($where)->find();//老用户
  166. if (!empty($userData)) {
  167. if (empty($userData['wechat_openid'])) {
  168. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  169. } else {
  170. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  171. $this->error('该手机号已被其他用户绑定');
  172. }
  173. }
  174. $ret = $this->auth->direct($userData['id']);
  175. } else {
  176. $extend = [
  177. 'wechat_openid' => $wechatCode['openid'],
  178. ];
  179. $ret = $this->auth->register('', '','', $mobile, $extend);
  180. }
  181. if (!$ret) {
  182. $this->error($this->auth->getError());
  183. }
  184. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  185. }
  186. //用户详细资料
  187. public function userInfo(){
  188. $info = $this->auth->getUserinfo();
  189. $this->success(__('success'),$info);
  190. }
  191. /**
  192. * 退出登录
  193. * @ApiMethod (POST)
  194. */
  195. public function logout()
  196. {
  197. if (!$this->request->isPost()) {
  198. $this->error(__('Invalid parameters'));
  199. }
  200. $this->auth->logout();
  201. $this->success(__('Logout successful'));
  202. }
  203. /**
  204. * 修改会员个人信息
  205. *
  206. * @ApiMethod (POST)
  207. * @param string $avatar 头像地址
  208. * @param string $username 用户名
  209. * @param string $nickname 昵称
  210. * @param string $bio 个人简介
  211. */
  212. public function profile()
  213. {
  214. $field_array = [
  215. 'avatar','nickname'
  216. ];
  217. $data = [];
  218. foreach($field_array as $key => $field){
  219. //前端传不了post,改了
  220. /*if(!request()->has($field,'post')){
  221. continue;
  222. }*/
  223. if(!input('?'.$field)){
  224. continue;
  225. }
  226. $newone = input($field);
  227. if($field == 'avatar'){
  228. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  229. }
  230. $data[$field] = $newone;
  231. }
  232. if(empty($data)){
  233. $this->success();
  234. }
  235. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  236. $this->success();
  237. }
  238. /**
  239. * 修改手机号
  240. *
  241. * @ApiMethod (POST)
  242. * @param string $mobile 手机号
  243. * @param string $captcha 验证码
  244. */
  245. public function changemobile()
  246. {
  247. $user = $this->auth->getUser();
  248. $oldcaptcha = input('oldcaptcha');
  249. $mobile = input('mobile');
  250. $captcha = input('captcha');
  251. if (!$oldcaptcha || !$mobile || !$captcha) {
  252. $this->error(__('Invalid parameters'));
  253. }
  254. if (!Validate::regex($mobile, "^1\d{10}$")) {
  255. $this->error(__('Mobile is incorrect'));
  256. }
  257. if($user->mobile == $mobile){
  258. $this->error('新手机号不能与旧手机号相同');
  259. }
  260. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  261. $this->error(__('Mobile already exist'));
  262. }
  263. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  264. if (!$result) {
  265. $this->error('原手机号验证码错误');
  266. }
  267. $result = Sms::check($mobile, $captcha, 'changemobile');
  268. if (!$result) {
  269. $this->error('新手机号验证码错误');
  270. }
  271. Sms::flush($user->mobile, 'changemobile');
  272. Sms::flush($mobile, 'changemobile');
  273. $user->mobile = $mobile;
  274. $user->save();
  275. $this->success();
  276. }
  277. //假注销
  278. public function cancleUser(){
  279. /*$captcha = input('captcha','');
  280. if (!$captcha) {
  281. $this->error(__('Invalid parameters'));
  282. }
  283. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  284. $this->error(__('Captcha is incorrect'));
  285. }*/
  286. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  287. $this->auth->logout();
  288. $this->success('注销成功');
  289. }
  290. }