123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace addons\qingdongams\controller;
- use addons\qingdongams\model\Contacts;
- use app\admin\model\Admin;
- use app\common\library\Sms;
- use app\common\library\Token;
- use fast\Random;
- use think\Validate;
- /**
- * 用户中心
- */
- class WebPerson extends WebIndexApi
- {
- protected $layout = 'default';
- protected $noNeedLogin = ['login', 'register', 'third','logintest'];
- protected $noNeedRight = ['*'];
- /**
- * @var \addons\qingdongams\model\Person
- */
- protected $model;
- public function __construct()
- {
- parent::__construct();
- $this->model = new \addons\qingdongams\model\Person();
- }
- /**
- * 获取密码加密后的字符串
- * @param string $password 密码
- * @param string $salt 密码盐
- * @return string
- */
- public function getEncryptPassword($password, $salt = '') {
- return md5(md5($password) . $salt);
- }
- /**
- * 会员登录
- */
- public function login()
- {
- if ($this->request->isPost()) {
- $account = $this->request->post('account');
- $code=$this->request->post('code');
- $password=$this->request->post('password');
- $rule = [
- 'account' => 'require|length:3,50',
- 'password' => 'require|length:3,50',
- ];
- $msg = [
- 'account.require' => '账户不能为空',
- 'account.length' => '账户必须3-50个字符',
- 'password.require' => '密码不能为空',
- ];
- $data = [
- 'account' => $account,
- 'password' => $password,
- ];
- $validate = new Validate($rule, $msg);
- $result = $validate->check($data);
- if (!$result) {
- $this->error(__($validate->getError()));
- }
- // if (!Sms::check($account, $code, 'mobilelogin')) {
- // $this->error(__('验证码不正确'));
- // }
- $info = $this->model->where(['account' => $account])->find();
- if(!$info){
- $this->error('账号不存在');
- return false;
- }
- if ($info->password != $this->getEncryptPassword($password, $info->salt)) {
- $this->error('密码错误');
- return false;
- }
- if (!$info) {
- $data = [
- 'nickname' => '',
- 'password' => '',
- 'email' => '',
- 'account' => $account];
- $data['salt'] = Random::alnum();
- $data['password'] = md5(md5($data['password']) . $data['salt']);
- if ($customerId = Contacts::where(['mobile' => $data['account']])->value('customer_id')) {
- $data['customer_id'] = $customerId;
- } else {
- $this->error('账号不存在');
- }
- $this->model->allowField(true)->save($data);
- $info = $this->model->where(['account' => $account])->find();
- }
- $token = md5('person'.$info['id'] . rand(1000, 9999));
- if (Token::set($token,$info['id'], $this->keeptime)) {
- $this->success(__('登录成功'), ['token' => $token]);
- }
- $this->error('登录失败');
- }
- }
- /**
- * 退出登录
- */
- public function logout()
- {
- $token = input('token');
- //删除Token
- Token::delete($token);
- $this->success(__('Logout successful'));
- }
- // 企业信息
- public function companyInfo(){
- $info = Admin::get(1);
- $info->avatar = cdnurl($info->avatar,true);
- $this->success('',$info);
- }
- //直接体验
- public function logintest()
- {
- $info = $this->model->where([])->order('id asc')->find();
- if (!$info) {
- $contacts = Contacts::where([])->order('id asc')->find();
- $data = [
- 'nickname' => '',
- 'password' => '',
- 'email' => '',
- 'account' => $contacts['mobile']];
- $data['salt'] = Random::alnum();
- $data['password'] = md5(md5($data['password']) . $data['salt']);
- $data['customer_id'] = $contacts['customer_id'];
- $this->model->allowField(true)->save($data);
- $info = $this->model->where(['account' => $contacts['mobile']])->find();
- }
- $token = md5('person'.$info['id'] . rand(1000, 9999));
- if (Token::set($token,$info['id'], $this->keeptime)) {
- $this->success(__('登录成功'), ['token' => $token]);
- }
- $this->error('登录失败');
- }
- }
|