User.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use fast\Random;
  6. use think\Config;
  7. use think\Validate;
  8. use think\Db;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Apic
  13. {
  14. protected $noNeedLogin = ['mobilelogin'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 手机验证码登录
  22. *
  23. * @ApiMethod (POST)
  24. * @param string $mobile 手机号
  25. * @param string $captcha 验证码
  26. */
  27. public function mobilelogin()
  28. {
  29. $mobile = $this->request->post('mobile');
  30. $captcha = $this->request->post('captcha');
  31. if (!$mobile || !$captcha) {
  32. $this->error(__('Invalid parameters'));
  33. }
  34. if (!Validate::regex($mobile, "^1\d{10}$")) {
  35. $this->error('请填写正确的手机号');
  36. }
  37. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  38. $this->error(__('Captcha is incorrect'));
  39. }
  40. //登录与注册
  41. $ret = false;
  42. $user = \app\common\model\Company::getByMobile($mobile);
  43. if ($user) {
  44. /*if ($user->status == 0) {
  45. $this->error(__('Account is locked'));
  46. }
  47. if ($user->status == 2) {
  48. $this->error('该用户已注销');
  49. }*/
  50. //如果已经有账号则直接登录
  51. $ret = $this->auth->direct($user->id);
  52. } else {
  53. //找员工
  54. $userstaff = Db::name('company_staff')->where('mobile',$mobile)->find();
  55. if($userstaff)
  56. {
  57. $user = \app\common\model\Company::get($userstaff['company_id']);
  58. if($user)
  59. {
  60. $ret = $this->auth->direct($user->id);
  61. }
  62. }
  63. if($ret === false){
  64. // 用户信息不存在时使用
  65. $extend = [];
  66. $ret = $this->auth->register_mobile($mobile, Random::alnum(), '', $mobile, $extend);
  67. }
  68. }
  69. if ($ret) {
  70. Sms::flush($mobile, 'mobilelogin');
  71. $data = ['userinfo' => $this->getUserinfo('return')];
  72. $this->success('登录成功', $data);
  73. } else {
  74. $this->error($this->auth->getError());
  75. }
  76. }
  77. /**
  78. * 退出登录
  79. * @ApiMethod (POST)
  80. */
  81. public function logout()
  82. {
  83. if (!$this->request->isPost()) {
  84. $this->error(__('Invalid parameters'));
  85. }
  86. $this->auth->logout();
  87. $this->success(__('Logout successful'));
  88. }
  89. //用户详细资料
  90. public function getUserinfo($type = 1){
  91. $info = $this->auth->getUserinfo();
  92. if($type == 'return'){
  93. return $info;
  94. }
  95. $this->success(__('success'),$info);
  96. }
  97. //用户申请资料
  98. public function getUserapplyinfo(){
  99. $field = [
  100. 'company_name',
  101. 'company_code',
  102. 'company_registerdate',
  103. 'company_address',
  104. 'company_image',
  105. 'truename',
  106. 'idcard',
  107. 'idcard_images',
  108. 'bank_name',
  109. 'bank_branchname',
  110. 'bank_account',
  111. 'bank_card',
  112. ];
  113. $info = Db::name('company')->field($field)->where('id',$this->auth->id)->find();
  114. $info = info_domain_image($info,['company_image','idcard_images']);
  115. $this->success(1,$info);
  116. }
  117. /**
  118. * 修改会员个人信息
  119. *
  120. * @ApiMethod (POST)
  121. * @param string $avatar 头像地址
  122. * @param string $username 用户名
  123. * @param string $nickname 昵称
  124. * @param string $bio 个人简介
  125. */
  126. public function profile()
  127. {
  128. //检查
  129. $check = Db::name('company')->where('id',$this->auth->id)->find();
  130. if($check['status'] == 1){
  131. $this->success('资料审核通过后需联系客服修改');
  132. }
  133. $field = [
  134. 'company_name',
  135. 'company_code',
  136. 'company_registerdate',
  137. 'company_address',
  138. 'company_image',
  139. 'truename',
  140. 'idcard',
  141. 'idcard_images',
  142. 'bank_name',
  143. 'bank_branchname',
  144. 'bank_account',
  145. 'bank_card',
  146. ];
  147. $data = request_post_hub($field);
  148. $data['status'] = 0;
  149. $update_rs = Db::name('company')->where('id',$this->auth->id)->update($data);
  150. $this->success('资料更新完成');
  151. }
  152. }