User.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\common\Enum\UserEnum;
  4. use app\common\library\Sms;
  5. use think\Env;
  6. use app\api\controller\inspection\Base;
  7. use app\common\Enum\StatusEnum;
  8. use app\common\Service\InspectionService;
  9. use app\common\model\inspection\InspectionApplication;
  10. use app\common\Service\SupplierService;
  11. /**
  12. * 会员
  13. */
  14. class User extends Base
  15. {
  16. protected $noNeedLogin = ['mobilelogin'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 手机验证码登录
  23. *
  24. * @param string $mobile 手机号
  25. * @param string $captcha 验证码
  26. */
  27. public function mobilelogin()
  28. {
  29. $params = $this->request->param();
  30. $mobile = $params['mobile'] ?? '';
  31. $captcha = $params['captcha'] ?? '';
  32. // 验证器
  33. $validate = new \app\api\validate\User();
  34. if (!$validate->check($params, [], 'mobilelogin')) {
  35. $this->error($validate->getError());
  36. }
  37. // 这里需要处理 测试环境 env('app_debug') 为 true 时,不进行验证码验证 校验 验证码固定为env的配置 DEFAULT_SMSCODE: 123456
  38. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  39. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  40. $this->error(__('Captcha is incorrect'));
  41. }
  42. }
  43. $user = \app\common\model\User::getByMobile($mobile);
  44. if(!$user){
  45. $this->error(__('Account does not exist'));
  46. }
  47. if ($user) {
  48. if ($user->status != StatusEnum::ENABLED) {
  49. $this->error(__('Account is locked'));
  50. }
  51. $isInspection = InspectionService::getUserApplication($user->id);
  52. if (!$isInspection) {
  53. $this->error('您不是审核员');
  54. }
  55. // 验证是否 通过
  56. if ($isInspection->audit_status !== InspectionApplication::AUDIT_STATUS_PASSED) {
  57. $this->error('您的验货员申请未通过');
  58. }
  59. //如果已经有账号则直接登录
  60. $ret = $this->auth->direct($user->id);
  61. }
  62. if ($ret) {
  63. Sms::flush($mobile, 'mobilelogin');
  64. $user = $this->auth->getUserinfo();
  65. $user['avatar'] = cdnurl($user['avatar'], true);
  66. $data = ['token' => $this->auth->getToken(), 'user' => $user];
  67. $this->success(__('Logged in successful'), $data);
  68. } else {
  69. $this->error($this->auth->getError());
  70. }
  71. }
  72. /**
  73. * 个人中心
  74. */
  75. public function index()
  76. {
  77. $info = $this->auth->getUserInfo();
  78. $info['avatar'] = cdnurl($info['avatar'], true);
  79. $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
  80. $info['age'] = $this->auth->getUser()->age ?? 0;
  81. //查询验货员申请信息
  82. $inspectionApplication = InspectionService::getUserApplication($this->auth->id);
  83. $info['inspection_application'] = $inspectionApplication;
  84. $info['supplier'] = null;
  85. // 查询供应商信息
  86. $supplierId = $this->application->supplier_id;
  87. if($supplierId){
  88. // 查询供应商信息
  89. $supplier = SupplierService::getFactoryById($supplierId);
  90. $info['supplier'] = $supplier;
  91. }
  92. $this->success('', $info);
  93. }
  94. /**
  95. * 个人资料
  96. */
  97. public function profile()
  98. {
  99. $user = $this->auth->getUser();
  100. $params = $this->request->param();
  101. // 只处理实际传递的参数
  102. $updateData = [];
  103. // 处理用户名
  104. if (isset($params['username']) && $params['username'] !== '') {
  105. $username = $params['username'];
  106. // 检查用户名是否已存在
  107. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  108. if ($exists) {
  109. $this->error(__('Username already exists'));
  110. }
  111. $updateData['username'] = $username;
  112. }
  113. // 处理头像
  114. if (isset($params['avatar'])) {
  115. $avatar = $params['avatar'];
  116. // 替换有域名的头像
  117. $avatar = str_replace(cdnurl('', true), '', $avatar);
  118. $updateData['avatar'] = $avatar;
  119. }
  120. // 处理昵称
  121. if (isset($params['nickname'])) {
  122. $updateData['nickname'] = $params['nickname'];
  123. }
  124. // 处理个人简介
  125. if (isset($params['bio'])) {
  126. $updateData['bio'] = $params['bio'];
  127. }
  128. // 处理年龄
  129. if (isset($params['age'])) {
  130. $updateData['age'] = $params['age'];
  131. }
  132. // 处理性别
  133. if (isset($params['gender'])) {
  134. $updateData['gender'] = $params['gender'];
  135. }
  136. // 如果没有任何要更新的数据
  137. if (empty($updateData)) {
  138. $this->error('没有要更新的数据');
  139. }
  140. // 验证器 - 只验证传递的参数
  141. $validate = new \app\api\validate\inspection\User();
  142. $validateParams = array_merge($params, isset($updateData['avatar']) ? ['avatar' => $updateData['avatar']] : []);
  143. if (!$validate->check($validateParams, [], 'profile')) {
  144. $this->error($validate->getError());
  145. }
  146. // 批量更新用户信息
  147. foreach ($updateData as $field => $value) {
  148. $user->$field = $value;
  149. }
  150. $user->save();
  151. $this->success('修改成功!');
  152. }
  153. /**
  154. * 保存头像
  155. */
  156. public function avatar()
  157. {
  158. $user = $this->auth->getUser();
  159. $avatar = $this->request->post('avatar');
  160. if (!$avatar) {
  161. $this->error("头像不能为空");
  162. }
  163. $avatar = str_replace(cdnurl('', true), '', $avatar);
  164. $user->avatar = $avatar;
  165. $user->save();
  166. $this->success('修改成功!');
  167. }
  168. /**
  169. * 注销登录
  170. */
  171. public function logout()
  172. {
  173. $this->auth->logout();
  174. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  175. }
  176. /**
  177. * 换绑手机号
  178. */
  179. public function changeMobile()
  180. {
  181. $params = $this->request->param();
  182. $mobile = $params['mobile'] ?? '';
  183. $captcha = $params['captcha'] ?? '';
  184. // 验证器
  185. $validate = new \app\api\validate\inspection\User();
  186. if (!$validate->check($params, [], 'changeMobile')) {
  187. $this->error($validate->getError());
  188. }
  189. $user = $this->auth->getUser();
  190. if ($user->mobile == $mobile) {
  191. $this->error(__('手机号不能与当前手机号相同'));
  192. }
  193. // 换绑手机号
  194. $user = \app\common\model\User::getByMobile($mobile);
  195. if ($user) {
  196. $this->error(__('手机号已存在'));
  197. }
  198. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  199. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  200. if (!$ret) {
  201. $this->error(__('Captcha is incorrect'));
  202. }
  203. }
  204. Sms::flush($mobile, 'resetpwd');
  205. $this->auth->getUser()->save(['mobile' => $mobile]);
  206. $this->success(__('换绑手机号成功'));
  207. }
  208. /**
  209. * 修改密码
  210. */
  211. public function changePassword()
  212. {
  213. $params = $this->request->param();
  214. $oldpassword = $params['oldpassword'] ?? '';
  215. $newpassword = $params['newpassword'] ?? '';
  216. $confirmpassword = $params['confirmpassword'] ?? '';
  217. // 验证器
  218. $validate = new \app\api\validate\inspection\User();
  219. if (!$validate->check($params, [], 'changePassword')) {
  220. $this->error($validate->getError());
  221. }
  222. $user = $this->auth->getUser();
  223. // 验证原密码是否正确
  224. if ($user->password != $this->auth->getEncryptPassword($oldpassword, $user->salt)) {
  225. $this->error(__('原密码错误'));
  226. }
  227. // 检查新密码是否与原密码相同
  228. if ($oldpassword === $newpassword) {
  229. $this->error(__('新密码不能与原密码相同'));
  230. }
  231. // 使用 Auth 类的 changepwd 方法修改密码
  232. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  233. if ($ret) {
  234. $this->success(__('密码修改成功'));
  235. } else {
  236. $this->error($this->auth->getError());
  237. }
  238. }
  239. }