User.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms;
  5. use think\Validate;
  6. use think\Db;
  7. /**
  8. * 会员接口
  9. */
  10. class User extends Api
  11. {
  12. protected $noNeedLogin = ['getUserOpenid','mobilelogin','wxMiniProgramLogin'];
  13. protected $noNeedRight = '*';
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. }
  18. //code获取openid
  19. public function getUserOpenid() {
  20. // code值
  21. $code = $this->request->param('code');
  22. if (!$code) {
  23. $this->error(__('Invalid parameters'));
  24. }
  25. $config = config('wxMiniProgram');
  26. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  27. $openidInfo = $this->getJson($getopenid);
  28. if(!isset($openidInfo['openid'])) {
  29. $this->error('用户openid获取失败',$openidInfo);
  30. }
  31. // 获取的结果存入数据库
  32. $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
  33. if($find) {
  34. $update = [];
  35. $update['openid'] = $openidInfo['openid'];
  36. $update['sessionkey'] = $openidInfo['session_key'];
  37. $update['createtime'] = time();
  38. $res = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
  39. } else {
  40. $insert = [];
  41. $insert['openid'] = $openidInfo['openid'];
  42. $insert['sessionkey'] = $openidInfo['session_key'];
  43. $insert['createtime'] = time();
  44. $res = Db::name('user_sessionkey')->insertGetId($insert);
  45. }
  46. if($res !== false) {
  47. $this->success('获取成功',$openidInfo);
  48. } else {
  49. $this->error('获取失败');
  50. }
  51. }
  52. /**
  53. * json 请求
  54. * @param $url
  55. * @return mixed
  56. */
  57. private function getJson($url){
  58. $ch = curl_init();
  59. curl_setopt($ch, CURLOPT_URL, $url);
  60. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  61. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  63. $output = curl_exec($ch);
  64. curl_close($ch);
  65. return json_decode($output, true);
  66. }
  67. /**
  68. * 手机验证码登录
  69. *
  70. * @ApiMethod (POST)
  71. * @param string $mobile 手机号
  72. * @param string $captcha 验证码
  73. */
  74. public function mobilelogin()
  75. {
  76. $mobile = $this->request->post('mobile');
  77. $captcha = $this->request->post('captcha');
  78. $openid = $this->request->post('openid');
  79. if (!$mobile || !$captcha || !$openid) {
  80. $this->error(__('Invalid parameters'));
  81. }
  82. if (!Validate::regex($mobile, "^1\d{10}$")) {
  83. $this->error(__('Mobile is incorrect'));
  84. }
  85. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  86. $this->error(__('Captcha is incorrect'));
  87. }
  88. // 获取openid和sessionkey
  89. $openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
  90. if(!$openidInfo){
  91. $this->error('openid获取失败');
  92. }
  93. //
  94. $check_user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->where('mobile','neq',$mobile)->find();
  95. if($check_user){
  96. $this->error('该微信已经绑定手机号'.$check_user['mobile'].',请使用该手机号登录');
  97. }
  98. $user = \app\common\model\User::getByMobile($mobile);
  99. if ($user) {
  100. if ($user->status != 1) {
  101. $this->error(__('Account is locked'));
  102. }
  103. //修改最新的sessionkey和openid。其他渠道注册,本渠道第一次来就赋值
  104. $user->mini_openid = $openid;
  105. $user->save();
  106. //如果已经有账号则直接登录
  107. $ret = $this->auth->direct($user->id);
  108. } else {
  109. // 用户信息不存在时使用
  110. $extend = [
  111. 'mini_openid' => $openid,
  112. ];
  113. $ret = $this->auth->register('', '', '', $mobile, $extend);
  114. }
  115. if ($ret) {
  116. Sms::flush($mobile, 'mobilelogin');
  117. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  118. } else {
  119. $this->error($this->auth->getError());
  120. }
  121. }
  122. /**
  123. * 微信小程序授权登录
  124. */
  125. public function wxMiniProgramLogin() {
  126. $openid = $this->request->request('openid');// openid值
  127. if (!$openid) {
  128. $this->error(__('Invalid parameters'));
  129. }
  130. // 用户登录逻辑 === 开始
  131. $user_sessionkey = Db::name('user_sessionkey')->where('openid',$openid)->find();
  132. if(empty($user_sessionkey)){
  133. $this->error('登录失败,请先使用手机号注册','',-1);
  134. }
  135. $userInfo = Db::name('user')->where(['mini_openid'=>$user_sessionkey['openid']])->find();
  136. // 判断用户是否已经存在
  137. if($userInfo) { // 登录
  138. if ($userInfo['status'] != 1) {
  139. $this->error(__('Account is locked'));
  140. }
  141. $res = $this->auth->direct($userInfo['id']);
  142. } else {
  143. //这里不在给注册,而是只能mobilelogin来注册
  144. $this->error('登录失败,请先使用手机号注册','',-1);
  145. }
  146. if($res) {
  147. $userInfo = $this->auth->getUserinfo();
  148. if(empty($userInfo['mobile'])){
  149. $this->success("登录成功!",$userInfo,-1);
  150. }
  151. $this->success("登录成功!",$userInfo);
  152. } else {
  153. $this->error($this->auth->getError());
  154. }
  155. }
  156. /**
  157. * 退出登录
  158. * @ApiMethod (POST)
  159. */
  160. public function logout()
  161. {
  162. if (!$this->request->isPost()) {
  163. $this->error(__('Invalid parameters'));
  164. }
  165. $this->auth->logout();
  166. $this->success(__('Logout successful'));
  167. }
  168. //用户详细资料
  169. public function getuserinfo(){
  170. $info = $this->auth->getUserinfo();
  171. $this->success(__('success'),$info);
  172. }
  173. /**
  174. * 修改会员个人信息
  175. *
  176. * @ApiMethod (POST)
  177. * @param string $avatar 头像地址
  178. * @param string $username 用户名
  179. * @param string $nickname 昵称
  180. * @param string $bio 个人简介
  181. */
  182. public function profile()
  183. {
  184. $field_array = ['avatar','nickname','contactname','address'];
  185. $data = [];
  186. foreach($field_array as $key => $field){
  187. //前端传不了post,改了
  188. /*if(!request()->has($field,'post')){
  189. continue;
  190. }*/
  191. if(!input('?'.$field)){
  192. continue;
  193. }
  194. $newone = input($field);
  195. if($field == 'avatar'){
  196. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  197. }
  198. $data[$field] = $newone;
  199. }
  200. if(empty($data)){
  201. $this->success();
  202. }
  203. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  204. if($update_rs === false){
  205. $this->error('修改资料失败');
  206. }
  207. $this->success();
  208. }
  209. /**
  210. * 修改手机号
  211. *
  212. * @ApiMethod (POST)
  213. * @param string $mobile 手机号
  214. * @param string $captcha 验证码
  215. */
  216. public function changemobile()
  217. {
  218. $user = $this->auth->getUser();
  219. $mobile = $this->request->post('mobile');
  220. $captcha = $this->request->post('captcha');
  221. if (!$mobile || !$captcha) {
  222. $this->error(__('Invalid parameters'));
  223. }
  224. if (!Validate::regex($mobile, "^1\d{10}$")) {
  225. $this->error(__('Mobile is incorrect'));
  226. }
  227. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  228. $this->error(__('Mobile already exists'));
  229. }
  230. $result = Sms::check($mobile, $captcha, 'changemobile');
  231. if (!$result) {
  232. $this->error(__('Captcha is incorrect'));
  233. }
  234. $user->mobile = $mobile;
  235. $user->save();
  236. Sms::flush($mobile, 'changemobile');
  237. $this->success();
  238. }
  239. }