User.php 8.7 KB

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