User.php 11 KB

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