User.php 42 KB

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