User.php 8.1 KB

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