User.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use think\Db;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'registercheck', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. if (!Config::get('fastadmin.usercenter')) {
  21. $this->error(__('User center already closed'));
  22. }
  23. }
  24. /**
  25. * 会员中心
  26. */
  27. public function index()
  28. {
  29. $this->success('', ['welcome' => $this->auth->nickname]);
  30. }
  31. /**
  32. * 会员登录
  33. *
  34. * @ApiMethod (POST)
  35. * @param string $account 账号
  36. * @param string $password 密码
  37. */
  38. public function login()
  39. {
  40. $account = $this->request->post('account');
  41. $password = $this->request->post('password');
  42. if (!$account || !$password) {
  43. $this->error(__('Invalid parameters'));
  44. }
  45. $ret = $this->auth->login($account, $password);
  46. if ($ret) {
  47. $data = ['userinfo' => $this->auth->getUserinfo()];
  48. $this->success(__('Logged in successful'), $data);
  49. } else {
  50. $this->error($this->auth->getError());
  51. }
  52. }
  53. /**
  54. * 手机验证码登录
  55. *
  56. * @ApiMethod (POST)
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. */
  60. public function mobilelogin()
  61. {
  62. $mobile = $this->request->post('mobile');
  63. $captcha = $this->request->post('captcha');
  64. if (!$mobile || !$captcha) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::regex($mobile, "^1\d{10}$")) {
  68. $this->error(__('Mobile is incorrect'));
  69. }
  70. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user) {
  75. if ($user->status != 'normal') {
  76. $this->error(__('Account is locked'));
  77. }
  78. //如果已经有账号则直接登录
  79. $ret = $this->auth->direct($user->id);
  80. } else {
  81. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  82. }
  83. if ($ret) {
  84. Sms::flush($mobile, 'mobilelogin');
  85. $data = ['userinfo' => $this->auth->getUserinfo()];
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  90. }
  91. /**
  92. * 注册会员
  93. *
  94. * @ApiMethod (POST)
  95. * @param string $username 用户名
  96. * @param string $password 密码
  97. * @param string $email 邮箱
  98. * @param string $mobile 手机号
  99. * @param string $code 验证码
  100. */
  101. public function register()
  102. {
  103. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  104. $code = $this->request->post('code', '', 'trim'); //验证码
  105. $password = $this->request->post('password', '' , 'trim'); //密码
  106. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  107. $nickname = $this->request->post('nickname', '', 'trim'); //姓名
  108. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  109. $province = $this->request->post('province', '', 'trim'); //省
  110. $city = $this->request->post('city', '', 'trim'); //市
  111. $area = $this->request->post('area', '', 'trim'); //区
  112. $address = $this->request->post('address', '', 'trim'); //详细地址
  113. $recommender = $this->request->post('recommender', '', 'trim'); //推荐人姓名
  114. $recommender_mobile = $this->request->post('recommender_mobile', '', 'trim'); //推荐人手机号
  115. $zimage = $this->request->post('zimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证正面照
  116. $fimage = $this->request->post('fimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证反面照
  117. if (!Validate::regex($mobile, "^1\d{10}$")) {
  118. $this->error(__('Mobile is incorrect'));
  119. }
  120. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  121. if ($count) {
  122. $this->error('手机号已被注册');
  123. }
  124. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  125. $this->error(__('Captcha is incorrect'));
  126. }
  127. $ret = Sms::check($mobile, $code, 'register');
  128. if (!$ret) {
  129. $this->error(__('Captcha is incorrect'));
  130. }
  131. if (!$password) {
  132. $this->error('请输入新密码');
  133. }
  134. //验证新密码
  135. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  136. $this->error(__('Password must be 6 to 30 characters'));
  137. }
  138. if (!$repassword) {
  139. $this->error('请再次输入密码');
  140. }
  141. if ($repassword != $password) {
  142. $this->error('两次密码不一致');
  143. }
  144. if (!$nickname) {
  145. $this->error('请输入姓名');
  146. }
  147. if (iconv_strlen($nickname, 'utf-8') > 30) {
  148. $this->error('姓名最多30位');
  149. }
  150. if (!$idcard) {
  151. $this->error('请输入身份证号');
  152. }
  153. if (iconv_strlen($idcard, 'utf-8') != 18) {
  154. $this->error('身份证号错误');
  155. }
  156. if (!$province || !$city || !$area) {
  157. $this->error('请选择养殖场地址');
  158. }
  159. if (!$address) {
  160. $this->error('请输入详细地址');
  161. }
  162. if (iconv_strlen($address, 'utf-8') > 255) {
  163. $this->error('详细地址最多255位');
  164. }
  165. if (!$recommender) {
  166. $this->error('请输入推荐人姓名');
  167. }
  168. if (iconv_strlen($recommender, 'utf-8') > 30) {
  169. $this->error('推荐人姓名最多30位');
  170. }
  171. if (!$recommender_mobile || !is_mobile($recommender_mobile)) {
  172. $this->error('请输入正确推荐人手机号');
  173. }
  174. if (!$zimage || !$fimage) {
  175. $this->error('请上传身份证相关照片');
  176. }
  177. $ip = request()->ip();
  178. $time = time();
  179. $data = [
  180. 'nickname' => $nickname,
  181. 'province' => $province,
  182. 'city' => $city,
  183. 'area' => $area,
  184. 'address' => $address,
  185. 'createtime' => $time
  186. ];
  187. $params = array_merge($data, [
  188. 'mobile' => $mobile,
  189. 'password' => $password,
  190. 'avatar' => '/assets/img/avatar.png',
  191. 'salt' => Random::alnum(),
  192. 'jointime' => $time,
  193. 'joinip' => $ip,
  194. 'logintime' => $time,
  195. 'loginip' => $ip,
  196. 'prevtime' => $time,
  197. 'is_auth' => 1
  198. ]);
  199. $params['password'] = md5(md5($password) . $params['salt']);
  200. //开启事务
  201. Db::startTrans();
  202. $rs = Db::name('user')->insertGetId($params);
  203. if (!$rs) {
  204. Db::rollback();
  205. $this->error('注册失败');
  206. }
  207. $data['user_id'] = $rs;
  208. $data['idcard'] = $idcard;
  209. $data['zimage'] = $zimage;
  210. $data['fimage'] = $fimage;
  211. $data['recommender'] = $recommender;
  212. $data['recommender_mobile'] = $recommender_mobile;
  213. $rt = Db::name('user_auth')->insertGetId($data);
  214. if (!$rt) {
  215. Db::rollback();
  216. $this->error('注册失败');
  217. }
  218. Db::commit();
  219. $ret = $this->auth->login($mobile, $password);
  220. if ($ret) {
  221. $data = ['userinfo' => $this->auth->getUserinfo()];
  222. $this->success(__('Sign up successful'), $data);
  223. } else {
  224. $this->error($this->auth->getError());
  225. }
  226. }
  227. //注册第一步验证
  228. public function registercheck()
  229. {
  230. $mobile = $this->request->post('mobile'); //手机号
  231. $code = $this->request->post('code'); //验证码
  232. $password = $this->request->post('password'); //密码
  233. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  234. if (!Validate::regex($mobile, "^1\d{10}$")) {
  235. $this->error(__('Mobile is incorrect'));
  236. }
  237. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  238. if (!$count) {
  239. $this->error('手机号已被注册');
  240. }
  241. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  242. $this->error(__('Captcha is incorrect'));
  243. }
  244. $ret = Sms::check($mobile, $code, 'register');
  245. if (!$ret) {
  246. $this->error(__('Captcha is incorrect'));
  247. }
  248. if (!$password) {
  249. $this->error('请输入新密码');
  250. }
  251. //验证新密码
  252. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  253. $this->error(__('Password must be 6 to 30 characters'));
  254. }
  255. if (!$repassword) {
  256. $this->error('请再次输入密码');
  257. }
  258. if ($repassword != $password) {
  259. $this->error('两次密码不一致');
  260. }
  261. $this->success('验证通过');
  262. }
  263. /**
  264. * 退出登录
  265. * @ApiMethod (POST)
  266. */
  267. public function logout()
  268. {
  269. if (!$this->request->isPost()) {
  270. $this->error(__('Invalid parameters'));
  271. }
  272. $this->auth->logout();
  273. $this->success(__('Logout successful'));
  274. }
  275. //查询会员信息
  276. public function getinfo()
  277. {
  278. $data['nickname'] = $this->auth->nickname; //姓名
  279. $data['avatar'] = one_domain_image($this->auth->avatar); //头像
  280. $data['mobile'] = $this->auth->mobile; //手机号
  281. $data['province'] = $this->auth->province; //省
  282. $data['city'] = $this->auth->city; //市
  283. $data['area'] = $this->auth->area; //区
  284. $data['address'] = $this->auth->address; //详细地址
  285. $data['is_auth'] = $this->auth->is_auth; //认证状态:0=未申请,1=待审核,2=已通过,3=拒绝
  286. $data['auth_info'] = [];
  287. $auth_info = Db::name('user_auth')->field('nickname, idcard, zimage, fimage, province, city, area, address, recommender, recommender_mobile')
  288. ->where(['user_id' => $this->auth->id])->find();
  289. $data['auth_info'] = info_domain_image($auth_info, ['zimage', 'fimage']);
  290. $this->success('会员信息', $data);
  291. }
  292. /**
  293. * 修改会员个人信息
  294. *
  295. * @ApiMethod (POST)
  296. * @param string $avatar 头像地址
  297. * @param string $username 用户名
  298. * @param string $nickname 昵称
  299. * @param string $bio 个人简介
  300. */
  301. public function profile()
  302. {
  303. if ($this->auth->is_auth == 1) {
  304. $this->error('认证正在审核,不能修改');
  305. }
  306. if ($this->auth->is_auth == 2) {
  307. $this->error('认证已通过审核,不能修改');
  308. }
  309. $nickname = $this->request->post('nickname', '', 'trim'); //姓名
  310. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  311. $province = $this->request->post('province', '', 'trim'); //省
  312. $city = $this->request->post('city', '', 'trim'); //市
  313. $area = $this->request->post('area', '', 'trim'); //区
  314. $address = $this->request->post('address', '', 'trim'); //详细地址
  315. $recommender = $this->request->post('recommender', '', 'trim'); //推荐人姓名
  316. $recommender_mobile = $this->request->post('recommender_mobile', '', 'trim'); //推荐人手机号
  317. $zimage = $this->request->post('zimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证正面照
  318. $fimage = $this->request->post('fimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证反面照
  319. if (!$nickname) {
  320. $this->error('请输入姓名');
  321. }
  322. if (iconv_strlen($nickname, 'utf-8') > 30) {
  323. $this->error('姓名最多30位');
  324. }
  325. if (!$idcard) {
  326. $this->error('请输入身份证号');
  327. }
  328. if (iconv_strlen($idcard, 'utf-8') != 18) {
  329. $this->error('身份证号错误');
  330. }
  331. if (!$province || !$city || !$area) {
  332. $this->error('请选择养殖场地址');
  333. }
  334. if (!$address) {
  335. $this->error('请输入详细地址');
  336. }
  337. if (iconv_strlen($address, 'utf-8') > 255) {
  338. $this->error('详细地址最多255位');
  339. }
  340. if (!$recommender) {
  341. $this->error('请输入推荐人姓名');
  342. }
  343. if (iconv_strlen($recommender, 'utf-8') > 30) {
  344. $this->error('推荐人姓名最多30位');
  345. }
  346. if (!$recommender_mobile || !is_mobile($recommender_mobile)) {
  347. $this->error('请输入正确推荐人手机号');
  348. }
  349. if (!$zimage || !$fimage) {
  350. $this->error('请上传身份证相关照片');
  351. }
  352. // $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  353. // if ($username) {
  354. // $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  355. // if ($exists) {
  356. // $this->error(__('Username already exists'));
  357. // }
  358. // $user->username = $username;
  359. // }
  360. // if ($nickname) {
  361. // $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  362. // if ($exists) {
  363. // $this->error(__('Nickname already exists'));
  364. // }
  365. // $user->nickname = $nickname;
  366. // }
  367. // $user->avatar = $avatar;
  368. $data = [
  369. 'nickname' => $nickname,
  370. 'province' => $province,
  371. 'city' => $city,
  372. 'area' => $area,
  373. 'address' => $address,
  374. 'updatetime' => time()
  375. ];
  376. //开启事务
  377. Db::startTrans();
  378. //修改用户表
  379. $data['is_auth'] = 1;
  380. $rt = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  381. if ($rt === false) {
  382. Db::rollback();
  383. $this->error('修改失败');
  384. }
  385. //修改认证表
  386. unset($data['is_auth']);
  387. $data['idcard'] = $idcard;
  388. $data['zimage'] = $zimage;
  389. $data['fimage'] = $fimage;
  390. $data['recommender'] = $recommender;
  391. $data['recommender_mobile'] = $recommender_mobile;
  392. $data['status'] = 0;
  393. $rs = Db::name('user_auth')->where(['user_id' => $this->auth->id])->setField($data);
  394. if (!$rs) {
  395. Db::rollback();
  396. $this->error('修改失败');
  397. }
  398. Db::commit();
  399. $this->success('修改成功');
  400. }
  401. //修改头像
  402. public function changeavatar()
  403. {
  404. $avatar = input('avatar', '', 'trim'); //头像
  405. if (!$avatar) {
  406. $this->error('请上传头像');
  407. }
  408. if (iconv_strlen($avatar, 'utf-8') > 255) {
  409. $this->error('头像长度超出限制');
  410. }
  411. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('avatar', $avatar);
  412. if (!$rs) {
  413. $this->error('修改失败');
  414. }
  415. $this->success('修改成功');
  416. }
  417. /**
  418. * 修改邮箱
  419. *
  420. * @ApiMethod (POST)
  421. * @param string $email 邮箱
  422. * @param string $captcha 验证码
  423. */
  424. public function changeemail()
  425. {
  426. $user = $this->auth->getUser();
  427. $email = $this->request->post('email');
  428. $captcha = $this->request->post('captcha');
  429. if (!$email || !$captcha) {
  430. $this->error(__('Invalid parameters'));
  431. }
  432. if (!Validate::is($email, "email")) {
  433. $this->error(__('Email is incorrect'));
  434. }
  435. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  436. $this->error(__('Email already exists'));
  437. }
  438. $result = Ems::check($email, $captcha, 'changeemail');
  439. if (!$result) {
  440. $this->error(__('Captcha is incorrect'));
  441. }
  442. $verification = $user->verification;
  443. $verification->email = 1;
  444. $user->verification = $verification;
  445. $user->email = $email;
  446. $user->save();
  447. Ems::flush($email, 'changeemail');
  448. $this->success();
  449. }
  450. /**
  451. * 修改手机号
  452. *
  453. * @ApiMethod (POST)
  454. * @param string $mobile 手机号
  455. * @param string $captcha 验证码
  456. */
  457. public function changemobile()
  458. {
  459. $user = $this->auth->getUser();
  460. $mobile = $this->request->post('mobile');
  461. $captcha = $this->request->post('captcha');
  462. if (!$mobile || !$captcha) {
  463. $this->error(__('Invalid parameters'));
  464. }
  465. if (!Validate::regex($mobile, "^1\d{10}$")) {
  466. $this->error(__('Mobile is incorrect'));
  467. }
  468. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  469. $this->error(__('Mobile already exists'));
  470. }
  471. $result = Sms::check($mobile, $captcha, 'changemobile');
  472. if (!$result) {
  473. $this->error(__('Captcha is incorrect'));
  474. }
  475. $verification = $user->verification;
  476. $verification->mobile = 1;
  477. $user->verification = $verification;
  478. $user->mobile = $mobile;
  479. $user->save();
  480. Sms::flush($mobile, 'changemobile');
  481. $this->success();
  482. }
  483. /**
  484. * 第三方登录
  485. *
  486. * @ApiMethod (POST)
  487. * @param string $platform 平台名称
  488. * @param string $code Code码
  489. */
  490. public function third()
  491. {
  492. $url = url('user/index');
  493. $platform = $this->request->post("platform");
  494. $code = $this->request->post("code");
  495. $config = get_addon_config('third');
  496. if (!$config || !isset($config[$platform])) {
  497. $this->error(__('Invalid parameters'));
  498. }
  499. $app = new \addons\third\library\Application($config);
  500. //通过code换access_token和绑定会员
  501. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  502. if ($result) {
  503. $loginret = \addons\third\library\Service::connect($platform, $result);
  504. if ($loginret) {
  505. $data = [
  506. 'userinfo' => $this->auth->getUserinfo(),
  507. 'thirdinfo' => $result
  508. ];
  509. $this->success(__('Logged in successful'), $data);
  510. }
  511. }
  512. $this->error(__('Operation failed'), $url);
  513. }
  514. /**
  515. * 重置密码
  516. *
  517. * @ApiMethod (POST)
  518. * @param string $mobile 手机号
  519. * @param string $newpassword 新密码
  520. * @param string $captcha 验证码
  521. */
  522. public function resetpwd()
  523. {
  524. $type = 'mobile';//$this->request->post("type");
  525. $mobile = $this->request->post("mobile");
  526. $email = $this->request->post("email");
  527. $newpassword = $this->request->post("newpassword");
  528. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  529. $captcha = $this->request->post("captcha");
  530. if (!$newpassword || !$captcha || !$repassword) {
  531. $this->error(__('Invalid parameters'));
  532. }
  533. //验证Token
  534. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  535. $this->error(__('Password must be 6 to 30 characters'));
  536. }
  537. if ($repassword != $newpassword) {
  538. $this->error('两次密码不一致');
  539. }
  540. if ($type == 'mobile') {
  541. if (!Validate::regex($mobile, "^1\d{10}$")) {
  542. $this->error(__('Mobile is incorrect'));
  543. }
  544. $user = \app\common\model\User::getByMobile($mobile);
  545. if (!$user) {
  546. $this->error(__('User not found'));
  547. }
  548. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  549. if (!$ret) {
  550. $this->error(__('Captcha is incorrect'));
  551. }
  552. Sms::flush($mobile, 'resetpwd');
  553. } else {
  554. if (!Validate::is($email, "email")) {
  555. $this->error(__('Email is incorrect'));
  556. }
  557. $user = \app\common\model\User::getByEmail($email);
  558. if (!$user) {
  559. $this->error(__('User not found'));
  560. }
  561. $ret = Ems::check($email, $captcha, 'resetpwd');
  562. if (!$ret) {
  563. $this->error(__('Captcha is incorrect'));
  564. }
  565. Ems::flush($email, 'resetpwd');
  566. }
  567. //模拟一次登录
  568. $this->auth->direct($user->id);
  569. $ret = $this->auth->changepwd($newpassword, '', true);
  570. if ($ret) {
  571. $this->success(__('Reset password successful'));
  572. } else {
  573. $this->error($this->auth->getError());
  574. }
  575. }
  576. /**
  577. * 修改密码
  578. *
  579. * @ApiMethod (POST)
  580. * @param string $captcha 验证码
  581. * @param string $newpassword 新密码
  582. * @param string $repassword 确认密码
  583. */
  584. public function editpwd()
  585. {
  586. $captcha = $this->request->post("captcha", '', 'trim'); //验证码
  587. $newpassword = $this->request->post("newpassword", '' , 'trim'); //新密码
  588. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  589. if (iconv_strlen($captcha, 'utf-8') != config('alisms.length')) {
  590. $this->error(__('Captcha is incorrect'));
  591. }
  592. if (!$newpassword) {
  593. $this->error('请输入新密码');
  594. }
  595. //验证新密码
  596. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  597. $this->error(__('Password must be 6 to 30 characters'));
  598. }
  599. if (!$repassword) {
  600. $this->error('请再次输入密码');
  601. }
  602. if ($repassword != $newpassword) {
  603. $this->error('两次密码不一致');
  604. }
  605. // $user = \app\common\model\User::getById($this->auth->id);
  606. // if (!$user) {
  607. // $this->error(__('User not found'));
  608. // }
  609. $ret = Sms::check($this->auth->mobile, $captcha, 'changepwd');
  610. if (!$ret) {
  611. $this->error(__('Captcha is incorrect'));
  612. }
  613. //模拟一次登录
  614. $this->auth->direct($this->auth->id);
  615. $ret = $this->auth->changepwd($newpassword, '', true);
  616. if ($ret) {
  617. Sms::flush($this->auth->mobile, 'changepwd');
  618. $this->success(__('Reset password successful'));
  619. } else {
  620. $this->error($this->auth->getError());
  621. }
  622. }
  623. //查询经营信息
  624. public function get_businessinfo()
  625. {
  626. $data = Db::name('user_business_info')->field('manage, mobile, province, city, area, distribution_channel')
  627. ->where(['user_id' => $this->auth->id])->find();
  628. $this->success('经营信息', $data);
  629. }
  630. //修改经营信息
  631. public function editbusinessinfo()
  632. {
  633. $manage = $this->request->post('manage', '', 'trim'); //采购负责人
  634. $mobile = $this->request->post('mobile', '', 'trim'); //联系方式
  635. $province = $this->request->post('province', '', 'trim'); //省
  636. $city = $this->request->post('city', '', 'trim'); //市
  637. $area = $this->request->post('area', '', 'trim'); //区
  638. $distribution_channel = $this->request->post('distribution_channel', '', 'trim'); //销售渠道
  639. $data = [];
  640. if ($manage) {
  641. if (iconv_strlen($manage, 'utf-8') > 50) {
  642. $this->error('采购负责人最多50字');
  643. }
  644. $data['manage'] = $manage;
  645. }
  646. if ($mobile) {
  647. if (!Validate::regex($mobile, "^1\d{10}$")) {
  648. $this->error(__('Mobile is incorrect'));
  649. }
  650. $data['mobile'] = $mobile;
  651. }
  652. if ($province && $city && $area) {
  653. $data['province'] = $province;
  654. $data['city'] = $city;
  655. $data['area'] = $area;
  656. }
  657. if ($distribution_channel) {
  658. if (iconv_strlen($distribution_channel, 'utf-8') > 255) {
  659. $this->error('销售渠道说明最多50字');
  660. }
  661. $data['distribution_channel'] = $distribution_channel;
  662. }
  663. if (!$data) {
  664. $this->error('暂无修改内容');
  665. }
  666. $count = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->count();
  667. if ($count) {
  668. $data['updatetime'] = time();
  669. $rs = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->setField($data);
  670. } else {
  671. $data['user_id'] = $this->auth->id;
  672. $data['createtime'] = time();
  673. $rs = Db::name('user_business_info')->insertGetId($data);
  674. }
  675. if (!$rs) {
  676. $this->error('修改失败');
  677. }
  678. $this->success('修改成功');
  679. }
  680. //查询猪车信息
  681. public function getpigcar()
  682. {
  683. $data = Db::name('user_pig_car')->where(['user_id' => $this->auth->id])->select();
  684. $data = list_domain_image($data, ['images']);
  685. $this->success('查询猪车信息', $data);
  686. }
  687. /**
  688. * 添加猪车信息
  689. *
  690. *$data = [
  691. [
  692. 'carnumber' => '123',
  693. 'carframenumber' => '344',
  694. 'images' => '123.jpg,234.jpg,345.jpg'
  695. ],
  696. [
  697. 'carnumber' => '123',
  698. 'carframenumber' => '344',
  699. 'images' => '123.jpg,234.jpg,345.jpg'
  700. ],
  701. [
  702. 'carnumber' => '123',
  703. 'carframenumber' => '344',
  704. 'images' => '123.jpg,234.jpg,345.jpg'
  705. ]
  706. ];
  707. *
  708. */
  709. public function addpigcar()
  710. {
  711. $pig_car = input('pig_car', '', 'trim'); //猪车信息json串: carnumber车牌号 carframenumber车架号 images车辆照片
  712. if (!$pig_car) {
  713. $this->error('请添加猪车信息');
  714. }
  715. $pig_car = json_decode($pig_car, true);
  716. if (!$pig_car) {
  717. $this->error('请添加猪车信息');
  718. }
  719. if (count($pig_car) > 9) {
  720. $this->error('一次最多添加9条记录');
  721. }
  722. $_data = [];
  723. foreach ($pig_car as &$v) {
  724. $data = [];
  725. if (!$v['carnumber']) {
  726. $this->error('请输入车牌号');
  727. break;
  728. }
  729. if (iconv_strlen($v['carnumber'], 'utf-8') > 50) {
  730. $this->error('车牌号最多50字');
  731. break;
  732. }
  733. if (!$v['carframenumber']) {
  734. $this->error('请输入车架号');
  735. break;
  736. }
  737. if (iconv_strlen($v['carframenumber'], 'utf-8') > 50) {
  738. $this->error('车架号最多50字');
  739. break;
  740. }
  741. if ($v['images']) {
  742. $image_arr = explode(',', $v['images']);
  743. if (count($image_arr) > 9) {
  744. $this->error('每辆车照片最多9张');
  745. break;
  746. }
  747. $data['images'] = $v['images'];
  748. }
  749. $data['user_id'] = $this->auth->id;
  750. $data['carnumber'] = $v['carnumber'];
  751. $data['carframenumber'] = $v['carframenumber'];
  752. $data['createtime'] = time();
  753. array_push($_data, $data);
  754. }
  755. $rs = Db::name('user_pig_car')->insertAll($_data);
  756. if (!$rs) {
  757. $this->error('添加失败');
  758. }
  759. $this->success('添加成功');
  760. }
  761. //删除猪车信息
  762. public function delpigcar()
  763. {
  764. $id = input('id', 0, 'intval'); //猪车id
  765. if (!$id) {
  766. $this->error('请选择要删除的猪车');
  767. }
  768. $info = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  769. if (!$info) {
  770. $this->error('这不是您的车');
  771. }
  772. $rs = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->delete();
  773. if (!$rs) {
  774. $this->error('删除失败');
  775. }
  776. $this->success('删除成功');
  777. }
  778. //平台介绍
  779. public function getabout()
  780. {
  781. $info = Db::name('platform_info')->field('title, content')->where(['type' => 1])->find();
  782. $this->success('平台介绍', $info);
  783. }
  784. //常见问题
  785. public function problem()
  786. {
  787. $list = Db::name('problem')->field('id, title, content')
  788. ->page($this->page, config('paginate.list_rows'))->order('weigh', 'desc')->select();
  789. $this->success('常见问题', $list);
  790. }
  791. //投诉举报类型
  792. public function complainttype()
  793. {
  794. $list = Db::name('complaint_type')->field('id, name')->where(['pid' => 0])->order('weigh', 'desc')->select();
  795. $list = $this->getchildtype($list);
  796. $this->success('常见问题', $list);
  797. }
  798. //无限级投诉举报类型
  799. public function getchildtype($list = [])
  800. {
  801. $complaint_type = Db::name('complaint_type');
  802. foreach ($list as &$v) {
  803. $child = $complaint_type->field('id, name')->where(['pid' => $v['id']])->order('weigh', 'desc')->select();
  804. if ($child) {
  805. $child = $this->getchildtype($child);
  806. }
  807. $v['child'] = $child;
  808. }
  809. return $list;
  810. }
  811. //投诉举报
  812. public function complaint()
  813. {
  814. $complaint_type = input('complaint_type', '', 'trim'); //投诉类型
  815. $order_sn = input('order_sn', '', 'trim'); //订单号
  816. $be_complaint_user = input('be_complaint_user', '', 'trim'); //被投诉人/单位
  817. $complaint_user = input('complaint_user', '', 'trim'); //投诉人
  818. $mobile = input('mobile', '', 'trim'); //联系电话
  819. $code = input('code', '', 'trim'); //验证码
  820. $content = input('content', '', 'trim'); //情况说明
  821. $images = input('images', '', 'trim'); //附件材料
  822. $data = [];
  823. if (!$complaint_type) {
  824. $this->error('请选择投诉类型');
  825. }
  826. if ($order_sn) {
  827. if (iconv_strlen($order_sn, 'utf-8') > 255) {
  828. $this->error('订单号最多255位');
  829. }
  830. //查询订单
  831. $count = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'order_sn' => $order_sn, 'status' => 1])->count();
  832. if (!$count) {
  833. $this->error('请输入正确中标订单号');
  834. }
  835. }
  836. if (!$be_complaint_user) {
  837. $this->error('请输入被投诉人/单位');
  838. }
  839. if (iconv_strlen($be_complaint_user, 'utf-8') > 50) {
  840. $this->error('被投诉人/单位最多50字');
  841. }
  842. if (!$complaint_user) {
  843. $this->error('请输入投诉人');
  844. }
  845. if (iconv_strlen($complaint_user, 'utf-8') > 50) {
  846. $this->error('投诉人最多50字');
  847. }
  848. if (!is_mobile($mobile)) {
  849. $this->error('请输入正确联系电话');
  850. }
  851. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  852. $this->error(__('Captcha is incorrect'));
  853. }
  854. $ret = Sms::check($mobile, $code, 'complaint');
  855. if (!$ret) {
  856. $this->error(__('Captcha is incorrect'));
  857. }
  858. if (!$content) {
  859. $this->error('请输入情况说明');
  860. }
  861. if (iconv_strlen($content, 'utf-8') > 3000) {
  862. $this->error('情况说明最多3000字');
  863. }
  864. if ($images) {
  865. $image_arr = explode(',', $images);
  866. if (count($image_arr) > 9) {
  867. $this->error('附件照片最多9张');
  868. }
  869. $data['images'] = $images;
  870. }
  871. $data['user_id'] = $this->auth->id;
  872. $data['complaint_type'] = $complaint_type;
  873. $data['order_sn'] = $order_sn;
  874. $data['be_complaint_user'] = $be_complaint_user;
  875. $data['complaint_user'] = $complaint_user;
  876. $data['mobile'] = $mobile;
  877. $data['content'] = $content;
  878. $data['images'] = $images;
  879. $data['createtime'] = time();
  880. $rs = Db::name('complaint')->insertGetId($data);
  881. if (!$rs) {
  882. $this->error('投诉失败');
  883. }
  884. Sms::flush($this->auth->mobile, 'complaint');
  885. $this->success('投诉成功');
  886. }
  887. //评价列表
  888. public function getmarkcomment()
  889. {
  890. $list = Db::name('mark_bid_comment')->where(['user_id' => $this->auth->id])
  891. ->page($this->page, config('paginate.list_rows'))->order('createtime', 'desc')->select();
  892. $list = list_domain_image($list, ['images']);
  893. $mark = Db::name('mark');
  894. $mark_bid = Db::name('mark_bid');
  895. foreach ($list as &$v) {
  896. $v['mark'] = $mark->find($v['mark_id']);
  897. $v['mark'] = info_domain_image($v['mark'], ['image', 'video', 'defective_images', 'road_images', 'pig_out_images']);
  898. $v['order_sn'] = $mark_bid->where(['id' => $v['mark_bid_id']])->value('order_sn');
  899. }
  900. $this->success('评论列表', $list);
  901. }
  902. //评价
  903. public function markcomment()
  904. {
  905. $mark_id = input('mark_id', 0, 'intval'); //招标id
  906. $content = input('content', '', 'trim'); //情况说明
  907. $images = input('images', '', 'trim'); //附件材料
  908. $data = [];
  909. if (!$mark_id) {
  910. $this->error('请选择要评价的数据');
  911. }
  912. $info = Db::name('mark')->find($mark_id);
  913. if (!$info) {
  914. $this->error('招标信息不存在');
  915. }
  916. $bid_id = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'mark_id' => $mark_id, 'status' => 1])->value('id');
  917. if (!$bid_id) {
  918. $this->error('您未中标,暂不能评价');
  919. }
  920. if (!$content) {
  921. $this->error('请输入评价内容');
  922. }
  923. if (iconv_strlen($content, 'utf-8') > 3000) {
  924. $this->error('评价内容最多3000字');
  925. }
  926. if ($images) {
  927. $image_arr = explode(',', $images);
  928. if (count($image_arr) > 9) {
  929. $this->error('照片最多9张');
  930. }
  931. $data['images'] = $images;
  932. }
  933. $data['user_id'] = $this->auth->id;
  934. $data['mark_id'] = $mark_id;
  935. $data['mark_bid_id'] = $bid_id;
  936. $data['content'] = $content;
  937. $data['images'] = $images;
  938. $data['createtime'] = time();
  939. $rs = Db::name('mark_bid_comment')->insertGetId($data);
  940. if (!$rs) {
  941. $this->error('评价失败');
  942. }
  943. $this->success('评价成功');
  944. }
  945. //个人账单列表
  946. public function getcompanymoney()
  947. {
  948. $list = Db::name('company')->field('id, name')->page($this->page, config('paginate.list_rows'))->select();
  949. $company_money_log = Db::name('company_money_log');
  950. foreach ($list as &$v) {
  951. $log = $company_money_log->where(['company_id' => $v['id'], 'user_id' => $this->auth->id])->order('id', 'desc')->find();
  952. if ($log) {
  953. $v['money'] = $log['after'];
  954. $v['can_money'] = $log['after'];
  955. } else {
  956. $v['money'] = '0';
  957. $v['can_money'] = '0';
  958. }
  959. }
  960. $this->success('个人账单列表', $list);
  961. }
  962. //个人账单明细
  963. public function getcompanymoneydetail()
  964. {
  965. $company_id = input('company_id', 0, 'intval'); //公司id
  966. $start_time = input('start_time', 0, 'strtotime'); //开始时间
  967. $end_time = input('end_time', 0, 'strtotime'); //结束时间
  968. if (!$company_id) {
  969. $this->error('参数缺失');
  970. }
  971. $where['company_id'] = $company_id;
  972. $where['user_id'] = $this->auth->id;
  973. if ($start_time && $end_time) {
  974. $where['createtime'] = ['between', [$start_time, $end_time]];
  975. }
  976. $list = Db::name('company_money_log')->field('id, money, memo, createtime')
  977. ->where($where)->page($this->page, config('paginate.list_rows'))->order('id', 'desc')->select();
  978. foreach ($list as &$v) {
  979. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  980. }
  981. $data['income'] = Db::name('company_money_log')->where($where)->where(['money' => ['gt', 0]])->sum('money');
  982. $data['expend'] = Db::name('company_money_log')->where($where)->where(['money' => ['lt', 0]])->sum('money');
  983. $data['list'] = $list;
  984. $this->success('个人账单明细', $data);
  985. }
  986. }