User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 bind_intro(){
  137. $introcode = input('introcode','','trim');
  138. if(!empty($introcode) && $this->auth->isLogin() && empty($this->auth->intro_uid)){
  139. $intro_user = Db::name('user')->where('introcode',$introcode)->field('id,intro_uid')->find();
  140. if(!empty($intro_user) && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id){
  141. Db::startTrans();
  142. Db::name('user')->where('id',$this->auth->id)->update(['intro_uid'=>$intro_user['id']]);
  143. Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['intro_uid'=>$intro_user['id']]);
  144. Db::commit();
  145. }
  146. }
  147. $this->success();
  148. }
  149. //假注销
  150. public function cancleUser(){
  151. /*$captcha = input('captcha','');
  152. if (!$captcha) {
  153. $this->error(__('Invalid parameters'));
  154. }
  155. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  156. $this->error(__('Captcha is incorrect'));
  157. }*/
  158. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  159. $this->auth->logout();
  160. $this->success('注销成功');
  161. }
  162. //////////////////////////////////////////////////////
  163. //微信登录,预先假注册
  164. public function wechatlogin(){
  165. $code = input('code','');
  166. if(!$code){
  167. $this->error();
  168. }
  169. //微信
  170. $wechat = new Wechat();
  171. $wxuserinfo = $wechat->getAccessToken($code);
  172. if(!$wxuserinfo){
  173. $this->error('openid获取失败');
  174. }
  175. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  176. $this->error('openid获取失败');
  177. }
  178. $openid = $wxuserinfo['openid'];
  179. //检查用户
  180. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  181. if ($user) {
  182. if ($user['status'] == -1) {
  183. $this->error('账户已注销');
  184. }
  185. if ($user['status'] != 1) {
  186. $this->error(__('Account is locked'));
  187. }
  188. //如果已经有账号则直接登录
  189. $ret = $this->auth->direct($user['id']);
  190. if ($ret) {
  191. $userInfo = $this->auth->getUserinfo_simple();
  192. $userInfo['is_register'] = 0;
  193. $userInfo['code'] = $code;
  194. $this->success(__('Logged in successful'), $userInfo);
  195. } else {
  196. $this->error($this->auth->getError());
  197. }
  198. } else {
  199. //记录code和openid,绑定手机号的时候更新openid
  200. $wechatCodeData = [
  201. 'code' => $code,
  202. 'openid' => $openid,
  203. 'createtime' => time(),
  204. ];
  205. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  206. if (empty($wechatCode)) {
  207. Db::name('wechat_code')->insertGetId($wechatCodeData);
  208. } else {
  209. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  210. }
  211. //直接返回
  212. $userInfo = [];
  213. $userInfo['is_register'] = 1;
  214. $userInfo['code'] = $code;
  215. $this->success('获取信息成功', $userInfo);
  216. }
  217. }
  218. /**
  219. * 微信注册来的,绑定手机号
  220. *
  221. * @ApiMethod (POST)
  222. * @param string $mobile 手机号
  223. * @param string $captcha 验证码
  224. */
  225. public function bindmobile()
  226. {
  227. $mobile = input('mobile');
  228. $captcha = input('captcha');
  229. $code = input('code');
  230. if (!$mobile || !$captcha || !$code) {
  231. $this->error(__('Invalid parameters'));
  232. }
  233. if (!Validate::regex($mobile, "^1\d{10}$")) {
  234. $this->error(__('Mobile is incorrect'));
  235. }
  236. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  237. $this->error(__('Captcha is incorrect'));
  238. }
  239. $wechatCodeWhere['code'] = $code;
  240. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  241. if (empty($wechatCode)) {
  242. $this->error('请先微信登录');
  243. }
  244. //检查appid绑定的用户
  245. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  246. if ($user) {
  247. if ($user['status'] == -1) {
  248. $this->error('账户已注销');
  249. }
  250. if ($user['status'] != 1) {
  251. $this->error(__('Account is locked'));
  252. }
  253. //如果已经有账号则直接登录
  254. $ret = $this->auth->direct($user['id']);
  255. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  256. }
  257. //新的openid用户
  258. $where = [];
  259. $where['mobile'] = $mobile;
  260. $userData = Db::name('user')->where($where)->find();//老用户
  261. if (!empty($userData)) {
  262. if (empty($userData['wechat_openid'])) {
  263. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  264. } else {
  265. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  266. $this->error('该手机号已被其他用户绑定');
  267. }
  268. }
  269. $ret = $this->auth->direct($userData['id']);
  270. } else {
  271. $extend = [
  272. 'wechat_openid' => $wechatCode['openid'],
  273. ];
  274. $ret = $this->auth->register('', '','', $mobile, $extend);
  275. }
  276. if (!$ret) {
  277. $this->error($this->auth->getError());
  278. }
  279. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  280. }
  281. /**
  282. * 修改手机号
  283. *
  284. * @ApiMethod (POST)
  285. * @param string $mobile 手机号
  286. * @param string $captcha 验证码
  287. */
  288. public function changemobile()
  289. {
  290. $user = $this->auth->getUser();
  291. $oldcaptcha = input('oldcaptcha');
  292. $mobile = input('mobile');
  293. $captcha = input('captcha');
  294. if (!$oldcaptcha || !$mobile || !$captcha) {
  295. $this->error(__('Invalid parameters'));
  296. }
  297. if (!Validate::regex($mobile, "^1\d{10}$")) {
  298. $this->error(__('Mobile is incorrect'));
  299. }
  300. if($user->mobile == $mobile){
  301. $this->error('新手机号不能与旧手机号相同');
  302. }
  303. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  304. $this->error(__('Mobile already exist'));
  305. }
  306. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  307. if (!$result) {
  308. $this->error('原手机号验证码错误');
  309. }
  310. $result = Sms::check($mobile, $captcha, 'changemobile');
  311. if (!$result) {
  312. $this->error('新手机号验证码错误');
  313. }
  314. Sms::flush($user->mobile, 'changemobile');
  315. Sms::flush($mobile, 'changemobile');
  316. $user->mobile = $mobile;
  317. $user->save();
  318. $this->success();
  319. }
  320. }