123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace app\api\controller\inspection;
- use app\common\Enum\UserEnum;
- use app\common\library\Sms;
- use think\Env;
- use app\api\controller\inspection\Base;
- use app\common\Enum\StatusEnum;
- use app\common\Service\InspectionService;
- use app\common\model\inspection\InspectionApplication;
- use app\common\Service\SupplierService;
- /**
- * 会员
- */
- class User extends Base
- {
- protected $noNeedLogin = ['mobilelogin'];
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 手机验证码登录
- *
- * @param string $mobile 手机号
- * @param string $captcha 验证码
- */
- public function mobilelogin()
- {
- $params = $this->request->param();
- $mobile = $params['mobile'] ?? '';
- $captcha = $params['captcha'] ?? '';
- // 验证器
- $validate = new \app\api\validate\User();
- if (!$validate->check($params, [], 'mobilelogin')) {
- $this->error($validate->getError());
- }
- // 这里需要处理 测试环境 env('app_debug') 为 true 时,不进行验证码验证 校验 验证码固定为env的配置 DEFAULT_SMSCODE: 123456
- if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
- if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
- $this->error(__('Captcha is incorrect'));
- }
- }
- $inspection = InspectionApplication::getByPhone($mobile);
- if(!$inspection){
- $this->error(__('验货员不存在'));
- }
- if ($inspection) {
- if ($inspection->status != StatusEnum::ENABLED) {
- $this->error(__('验货员账号已锁定'));
- }
- // $isInspection = InspectionService::getUserApplication($user->id);
- // if (!$isInspection) {
- // $this->error('您不是审核员');
- // }
- // 验证是否 通过
- if ($inspection->audit_status != InspectionApplication::AUDIT_STATUS_PASSED) {
- $this->error('您的验货员申请未通过');
- }
- // 验证是否 绑定供应商
- if (!$inspection->supplier_id) {
- $this->error('您未绑定供应商');
- }
- //如果已经有账号则直接登录
- $ret = $this->auth->direct($inspection->id);
- }
- if ($ret) {
- Sms::flush($mobile, 'mobilelogin');
- $user = $this->auth->getInspectorInfo();
- $user['avatar'] = cdnurl($user['avatar'], true);
- $data = ['token' => $this->auth->getToken(), 'user' => $user];
- $this->success(__('Logged in successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
- /**
- * 个人中心
- */
- public function index()
- {
- $info = $this->application;
- //查询验货员申请信息
- $inspectionApplication = InspectionService::getApplicationById($this->application->id);
- $info['inspection_application'] = $inspectionApplication;
- $info['supplier'] = null;
- // 查询供应商信息
- $supplierId = $this->application->supplier_id;
- if($supplierId){
- // 查询供应商信息
- $supplier = SupplierService::getFactoryById($supplierId);
- $info['supplier'] = $supplier;
- }
- $info['avatar'] = cdnurl($info['avatar'], true);
- $this->success('', $info);
- }
- /**
- * 个人资料
- */
- public function profile()
- {
- $application = $this->auth->getApplication();
- $params = $this->request->param();
-
- // 只处理实际传递的参数
- $updateData = [];
-
- // 处理用户名
- if (isset($params['name']) && $params['name'] !== '') {
- $name = $params['name'];
- // 检查用户名是否已存在
- $exists = InspectionApplication::where('name', $name)->where('id', '<>', $application->id)->find();
- if ($exists) {
- $this->error(__('该名称已经存在'));
- }
- $updateData['name'] = $name;
- }
-
- // 处理头像
- if (isset($params['avatar'])) {
- $avatar = $params['avatar'];
- // 替换有域名的头像
- $avatar = str_replace(cdnurl('', true), '', $avatar);
- $updateData['avatar'] = $avatar;
- }
-
- // 处理昵称
- if (isset($params['nickname'])) {
- $updateData['nickname'] = $params['nickname'];
- }
-
- // 处理个人简介
- if (isset($params['bio'])) {
- $updateData['bio'] = $params['bio'];
- }
-
- // 处理年龄
- if (isset($params['age'])) {
- $updateData['age'] = $params['age'];
- }
-
- // 处理性别
- if (isset($params['gender'])) {
- $updateData['gender'] = $params['gender'];
- }
-
- // 如果没有任何要更新的数据
- if (empty($updateData)) {
- $this->error('没有要更新的数据');
- }
-
- // 验证器 - 只验证传递的参数
- $validate = new \app\api\validate\inspection\User();
- $validateParams = array_merge($params, isset($updateData['avatar']) ? ['avatar' => $updateData['avatar']] : []);
- if (!$validate->check($validateParams, [], 'profile')) {
- $this->error($validate->getError());
- }
-
- // 批量更新用户信息
- foreach ($updateData as $field => $value) {
- $application->$field = $value;
- }
- $application->save();
- $this->success('修改成功!');
- }
- /**
- * 保存头像
- */
- public function avatar()
- {
- $user = $this->auth->getUser();
- $avatar = $this->request->post('avatar');
- if (!$avatar) {
- $this->error("头像不能为空");
- }
- $avatar = str_replace(cdnurl('', true), '', $avatar);
- $user->avatar = $avatar;
- $user->save();
- $this->success('修改成功!');
- }
- /**
- * 注销登录
- */
- public function logout()
- {
- $this->auth->logout();
- $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
- }
- /**
- * 换绑手机号
- */
- public function changeMobile()
- {
- $params = $this->request->param();
- $mobile = $params['mobile'] ?? '';
- $captcha = $params['captcha'] ?? '';
- // 验证器
- $validate = new \app\api\validate\inspection\User();
- if (!$validate->check($params, [], 'changeMobile')) {
- $this->error($validate->getError());
- }
- $user = $this->auth->getUser();
- if ($user->mobile == $mobile) {
- $this->error(__('手机号不能与当前手机号相同'));
- }
- // 换绑手机号
- $user = InspectionApplication::getByPhone($mobile);
- if ($user) {
- $this->error(__('手机号已存在'));
- }
- if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
- $ret = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- }
- Sms::flush($mobile, 'resetpwd');
- $this->auth->getApplication()->save(['phone' => $mobile]);
- $this->success(__('换绑手机号成功'));
- }
- /**
- * 修改密码
- */
- public function changePassword()
- {
- $params = $this->request->param();
- $oldpassword = $params['oldpassword'] ?? '';
- $newpassword = $params['newpassword'] ?? '';
- $confirmpassword = $params['confirmpassword'] ?? '';
-
- // 验证器
- $validate = new \app\api\validate\inspection\User();
- if (!$validate->check($params, [], 'changePassword')) {
- $this->error($validate->getError());
- }
-
- $user = $this->auth->getApplication();
-
- // 验证原密码是否正确
- if ($user->password != $this->auth->getEncryptPassword($oldpassword, $user->salt)) {
- $this->error(__('原密码错误'));
- }
-
- // 检查新密码是否与原密码相同
- if ($oldpassword === $newpassword) {
- $this->error(__('新密码不能与原密码相同'));
- }
-
- // 使用 Auth 类的 changepwd 方法修改密码
- $ret = $this->auth->changepwd($newpassword, $oldpassword);
- if ($ret) {
- $this->success(__('密码修改成功'));
- } else {
- $this->error($this->auth->getError());
- }
- }
- }
|