User.php 11 KB

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