/** * @api {put} /user/password 修改密码 * @apiDescription 修改密码 * @apiGroup user * @apiPermission JWT * @apiVersion 0.1.0 * @apiParam {String} old_password 旧密码 * @apiParam {String} password 新密码 * @apiParam {String} password_confirmation 确认新密码 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 204 No Content * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "password": [ * "两次输入的密码不一致", * "新旧密码不能相同" * ], * "password_confirmation": [ * "两次输入的密码不一致" * ], * "old_password": [ * "密码错误" * ] * } */ public function editPassword() { $customer = $this->user(); $validator = \Validator::make($this->request->all(), ['old_password' => 'required', 'password' => 'required|confirmed', 'password_confirmation' => 'required|same:password'], ['password.confirmed' => '两次输入的密码不一致', 'password_confirmation.same' => '两次输入的密码不一致']); $auth = \Auth::once(['mobile' => $customer->mobile, 'password' => $this->request->get('old_password')]); if (!$auth) { $validator->after(function ($validator) { $validator->errors()->add('old_password', '密码错误'); }); } if ($validator->fails()) { if (count($validator->messages()->get('password')) > 0) { return return_rest('0', '', $validator->messages()->get('password')[0]); } if (count($validator->messages()->get('password_confirmation')) > 0) { return return_rest('0', '', $validator->messages()->get('password_confirmation')[0]); } if (count($validator->messages()->get('old_password')) > 0) { return return_rest('0', '', $validator->messages()->get('old_password')[0]); } } //变更环信密码 $easemob_reset_password = Easemob::reset_password($customer->user_name, $this->request->get('password')); if (!$easemob_reset_password) { return return_rest('0', '', '环信密码修改失败'); } $customer->password = bcrypt($this->request->get('password')); if ($customer->save()) { return return_rest('1', '', '密码修改成功'); } }
/** * 昵称 type */ public function signup() { $token = $this->request->get('smsToken'); $validator = \Validator::make($this->request->all(), ['user_name' => 'required|between:4,12|unique:customers|Regex:/^[a-z0-9]{4,12}$/', 'mobile' => "required|confirm_mobile_not_change:{$token}", 'password' => 'required', 'verifyCode' => "required|verify_code:{$token}|confirm_mobile_rule:mobile_required,{$token}"], ['verifyCode.required' => '请输入短信验证码', 'verify_code' => '验证码错误', 'confirm_mobile_not_change' => '当前手机号码与发送号码不符', 'confirm_mobile_rule' => '验证码验证错误', 'user_name.unique' => '用户名已注册', 'user_name.regex' => '用户名必须为小写字母或数字', 'user_name.between' => '用户名必须为4-12位']); $messages = $validator->messages(); if ($messages->has('mobile')) { $mobiles_rule = $messages->get('mobile'); foreach ($mobiles_rule as $mobile_rule) { if ($mobile_rule == '当前手机号码与发送号码不符') { return return_rest('0', '', '当前手机号码与发送号码不符'); } } } if ($messages->has('verifyCode')) { $verifyCodes = $messages->get('verifyCode'); foreach ($verifyCodes as $verifyCode) { if ($verifyCode == '请输入短信验证码') { return return_rest('0', '', '请输入短信验证码'); } if ($verifyCode == '验证码错误') { return return_rest('0', '', '验证码错误'); } if ($verifyCode == '验证码验证错误') { return return_rest('0', '', '验证码验证错误'); } } } if ($messages->has('password')) { return return_rest('0', '', '请输入密码'); } if ($messages->has('user_name')) { if ($mobile_rule == '用户名已注册') { return return_rest('0', '', '用户名已注册'); } if ($mobile_rule == '用户名必须为小写字母或数字') { return return_rest('0', '', '用户名必须为小写字母或数字'); } if ($mobile_rule == '用户名必须为4-12位') { return return_rest('0', '', '用户名必须为4-12位'); } } //增加环信注册 失败返回false $easemob = Easemob::user_register($this->request->get('user_name'), $this->request->get('password')); //TODO if (isset($easemob['mobile'])) { return return_rest('0', '', '该用户已注册环信'); } //设置用户相关信息 $mobile = $this->request->get('mobile'); $password = $this->request->get('password'); //TODO 用户类型 设置默认为3游客 1为创业者2为投资人 $type = $this->request->has('type') ? $this->request->get('type') : 3; //TODO 其他信息 $customer = new Customer(); $customer->user_name = $this->request->get('user_name'); $customer->mobile = $mobile; $customer->password = bcrypt($password); $customer->type = $type; $customer->avatar = 'http://image.haihespace.com/default/avatar/avatar.jpg'; if ($customer->save()) { // 用户注册事件 $token = \JWTAuth::fromUser($customer); //为用户生成头像 // $img = Image::make('uploads/avatars/avatar.jpg'); // $img->save('uploads/avatars/'.$mobile.'.jpg'); return return_rest('1', compact('token', 'customer')); } $this->errorBadRequest(return_rest('0', '', '操作失败')); }