User.php 8.6 KB

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