User.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if (!$isInspection->supplier_id) {
  61. $this->error('您未绑定供应商');
  62. }
  63. //如果已经有账号则直接登录
  64. $ret = $this->auth->direct($user->id);
  65. }
  66. if ($ret) {
  67. Sms::flush($mobile, 'mobilelogin');
  68. $user = $this->auth->getUserinfo();
  69. $user['avatar'] = cdnurl($user['avatar'], true);
  70. $data = ['token' => $this->auth->getToken(), 'user' => $user];
  71. $this->success(__('Logged in successful'), $data);
  72. } else {
  73. $this->error($this->auth->getError());
  74. }
  75. }
  76. /**
  77. * 个人中心
  78. */
  79. public function index()
  80. {
  81. $info = $this->auth->getUserInfo();
  82. $info['avatar'] = cdnurl($info['avatar'], true);
  83. $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0);
  84. $info['age'] = $this->auth->getUser()->age ?? 0;
  85. //查询验货员申请信息
  86. $inspectionApplication = InspectionService::getUserApplication($this->auth->id);
  87. $info['inspection_application'] = $inspectionApplication;
  88. $info['supplier'] = null;
  89. // 查询供应商信息
  90. $supplierId = $this->application->supplier_id;
  91. if($supplierId){
  92. // 查询供应商信息
  93. $supplier = SupplierService::getFactoryById($supplierId);
  94. $info['supplier'] = $supplier;
  95. }
  96. $this->success('', $info);
  97. }
  98. /**
  99. * 个人资料
  100. */
  101. public function profile()
  102. {
  103. $user = $this->auth->getUser();
  104. $params = $this->request->param();
  105. // 只处理实际传递的参数
  106. $updateData = [];
  107. // 处理用户名
  108. if (isset($params['username']) && $params['username'] !== '') {
  109. $username = $params['username'];
  110. // 检查用户名是否已存在
  111. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  112. if ($exists) {
  113. $this->error(__('Username already exists'));
  114. }
  115. $updateData['username'] = $username;
  116. }
  117. // 处理头像
  118. if (isset($params['avatar'])) {
  119. $avatar = $params['avatar'];
  120. // 替换有域名的头像
  121. $avatar = str_replace(cdnurl('', true), '', $avatar);
  122. $updateData['avatar'] = $avatar;
  123. }
  124. // 处理昵称
  125. if (isset($params['nickname'])) {
  126. $updateData['nickname'] = $params['nickname'];
  127. }
  128. // 处理个人简介
  129. if (isset($params['bio'])) {
  130. $updateData['bio'] = $params['bio'];
  131. }
  132. // 处理年龄
  133. if (isset($params['age'])) {
  134. $updateData['age'] = $params['age'];
  135. }
  136. // 处理性别
  137. if (isset($params['gender'])) {
  138. $updateData['gender'] = $params['gender'];
  139. }
  140. // 如果没有任何要更新的数据
  141. if (empty($updateData)) {
  142. $this->error('没有要更新的数据');
  143. }
  144. // 验证器 - 只验证传递的参数
  145. $validate = new \app\api\validate\inspection\User();
  146. $validateParams = array_merge($params, isset($updateData['avatar']) ? ['avatar' => $updateData['avatar']] : []);
  147. if (!$validate->check($validateParams, [], 'profile')) {
  148. $this->error($validate->getError());
  149. }
  150. // 批量更新用户信息
  151. foreach ($updateData as $field => $value) {
  152. $user->$field = $value;
  153. }
  154. $user->save();
  155. $this->success('修改成功!');
  156. }
  157. /**
  158. * 保存头像
  159. */
  160. public function avatar()
  161. {
  162. $user = $this->auth->getUser();
  163. $avatar = $this->request->post('avatar');
  164. if (!$avatar) {
  165. $this->error("头像不能为空");
  166. }
  167. $avatar = str_replace(cdnurl('', true), '', $avatar);
  168. $user->avatar = $avatar;
  169. $user->save();
  170. $this->success('修改成功!');
  171. }
  172. /**
  173. * 注销登录
  174. */
  175. public function logout()
  176. {
  177. $this->auth->logout();
  178. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  179. }
  180. /**
  181. * 换绑手机号
  182. */
  183. public function changeMobile()
  184. {
  185. $params = $this->request->param();
  186. $mobile = $params['mobile'] ?? '';
  187. $captcha = $params['captcha'] ?? '';
  188. // 验证器
  189. $validate = new \app\api\validate\inspection\User();
  190. if (!$validate->check($params, [], 'changeMobile')) {
  191. $this->error($validate->getError());
  192. }
  193. $user = $this->auth->getUser();
  194. if ($user->mobile == $mobile) {
  195. $this->error(__('手机号不能与当前手机号相同'));
  196. }
  197. // 换绑手机号
  198. $user = \app\common\model\User::getByMobile($mobile);
  199. if ($user) {
  200. $this->error(__('手机号已存在'));
  201. }
  202. if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) {
  203. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  204. if (!$ret) {
  205. $this->error(__('Captcha is incorrect'));
  206. }
  207. }
  208. Sms::flush($mobile, 'resetpwd');
  209. $this->auth->getUser()->save(['mobile' => $mobile]);
  210. $this->success(__('换绑手机号成功'));
  211. }
  212. /**
  213. * 修改密码
  214. */
  215. public function changePassword()
  216. {
  217. $params = $this->request->param();
  218. $oldpassword = $params['oldpassword'] ?? '';
  219. $newpassword = $params['newpassword'] ?? '';
  220. $confirmpassword = $params['confirmpassword'] ?? '';
  221. // 验证器
  222. $validate = new \app\api\validate\inspection\User();
  223. if (!$validate->check($params, [], 'changePassword')) {
  224. $this->error($validate->getError());
  225. }
  226. $user = $this->auth->getUser();
  227. // 验证原密码是否正确
  228. if ($user->password != $this->auth->getEncryptPassword($oldpassword, $user->salt)) {
  229. $this->error(__('原密码错误'));
  230. }
  231. // 检查新密码是否与原密码相同
  232. if ($oldpassword === $newpassword) {
  233. $this->error(__('新密码不能与原密码相同'));
  234. }
  235. // 使用 Auth 类的 changepwd 方法修改密码
  236. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  237. if ($ret) {
  238. $this->success(__('密码修改成功'));
  239. } else {
  240. $this->error($this->auth->getError());
  241. }
  242. }
  243. }