User.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. public function accountlogin(){
  78. $mobile = $this->request->post('mobile');
  79. $password = $this->request->post('password');
  80. if (!$mobile || !$password) {
  81. $this->error(__('Invalid parameters'));
  82. }
  83. $ret = $this->auth->login($mobile, $password);
  84. if ($ret) {
  85. $data = ['userinfo' => $this->auth->getUserinfo()];
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  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. public function getUserinfo($type = 1){
  105. $info = $this->auth->getUserinfo();
  106. if($type == 'return'){
  107. return $info;
  108. }
  109. $this->success(__('success'),$info);
  110. }
  111. //用户申请资料
  112. public function getUserapplyinfo(){
  113. $field = [
  114. 'company_name',
  115. 'company_code',
  116. 'company_registerdate',
  117. 'company_address',
  118. 'company_image',
  119. 'truename',
  120. 'idcard',
  121. 'idcard_images',
  122. 'bank_name',
  123. 'bank_branchname',
  124. 'bank_account',
  125. 'bank_card',
  126. ];
  127. $info = Db::name('company')->field($field)->where('id',$this->auth->id)->find();
  128. $info = info_domain_image($info,['company_image','idcard_images']);
  129. $this->success(1,$info);
  130. }
  131. /**
  132. * 修改会员个人信息
  133. *
  134. * @ApiMethod (POST)
  135. * @param string $avatar 头像地址
  136. * @param string $username 用户名
  137. * @param string $nickname 昵称
  138. * @param string $bio 个人简介
  139. */
  140. public function profile()
  141. {
  142. //检查
  143. $check = Db::name('company')->where('id',$this->auth->id)->find();
  144. if($check['status'] == 1){
  145. $this->success('资料审核通过后需联系客服修改');
  146. }
  147. $field = [
  148. 'company_name',
  149. 'company_code',
  150. 'company_registerdate',
  151. 'company_address',
  152. 'company_image',
  153. 'truename',
  154. 'idcard',
  155. 'idcard_images',
  156. 'bank_name',
  157. 'bank_branchname',
  158. 'bank_account',
  159. 'bank_card',
  160. ];
  161. $data = request_post_hub($field);
  162. $data['status'] = 0;
  163. $update_rs = Db::name('company')->where('id',$this->auth->id)->update($data);
  164. $this->success('资料更新完成');
  165. }
  166. }