User.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. if ($invite_no) {
  1076. $invite_info = Db::name('user')->where(['invite_no' => $invite_no])->find();
  1077. if (!$invite_info) {
  1078. $this->error('邀请码不存在');
  1079. }
  1080. }
  1081. if (!$nickname || !$avatar) {
  1082. $this->error('参数缺失');
  1083. }
  1084. if (iconv_strlen($nickname, 'utf-8') > 30 || iconv_strlen($avatar, 'utf-8') > 255) {
  1085. $this->error('参数错误');
  1086. }
  1087. $ip = request()->ip();
  1088. $time = time();
  1089. $data = [
  1090. 'nickname' => $nickname,
  1091. 'salt' => Random::alnum(),
  1092. 'mobile' => $mobile,
  1093. 'avatar' => $avatar,
  1094. 'joinip' => $ip,
  1095. 'jointime' => $time,
  1096. 'logintime' => $time,
  1097. 'loginip' => $ip,
  1098. 'prevtime' => $time,
  1099. 'createtime' => $time,
  1100. 'gender' => $gender,
  1101. 'birthday' => $birthday,
  1102. 'openid' => $openid,
  1103. 'invite_no' => $this->myinvite(),
  1104. 'pre_user_id' => $invite_info['id'],
  1105. 'invite_time' => $time,
  1106. ];
  1107. //开启事务
  1108. Db::startTrans();
  1109. $rs = Db::name('user')->insertGetId($data);
  1110. if (!$rs) {
  1111. Db::rollback();
  1112. $this->error('注册失败');
  1113. }
  1114. //生成uid
  1115. $username = $this->myuid($rs);
  1116. $rt = Db::name('user')->where(['id' => $rs])->setField('username', $username);
  1117. if (!$rt) {
  1118. Db::rollback();
  1119. $this->error('注册失败');
  1120. }
  1121. //给用户发放注册优惠券
  1122. $register_coupon = Db::name('coupon')->where(['purpose' => 1, 'status' => 1])->order('weigh desc, id desc')->find();
  1123. if ($register_coupon) {
  1124. $register_coupon_data = [
  1125. 'user_id' => $rs,
  1126. 'coupon_id' => $register_coupon['id'],
  1127. 'title' => $register_coupon['title'],
  1128. 'desc' => $register_coupon['desc'],
  1129. 'type' => $register_coupon['type'],
  1130. 'money' => $register_coupon['money'],
  1131. 'minmoney' => $register_coupon['minmoney'],
  1132. 'purpose' => $register_coupon['purpose'],
  1133. 'starttime' => time(),
  1134. 'endtime' => time() + $register_coupon['effectiveday'] * 86400,
  1135. 'createtime' => time()
  1136. ];
  1137. $register_coupon_rs = Db::name('user_coupon')->insertGetId($register_coupon_data);
  1138. if (!$register_coupon_rs) {
  1139. Db::rollback();
  1140. $this->error('注册失败');
  1141. }
  1142. }
  1143. //查询是否需要给上级发放优惠券
  1144. if ($invite_info) {
  1145. //查询上一次获得推广优惠券时间
  1146. $last_coupon = Db::name('user_coupon')->where(['user_id' => $invite_info['id'], 'purpose' => 2])->order('id', 'desc')->find();
  1147. $last_coupon_time = $last_coupon ? $last_coupon['createtime'] : 0;
  1148. //获取后台配置推广获得优惠券指定人数
  1149. $invitepersonnum = (int)config('site.invitepersonnum');
  1150. //查询最新推广任务
  1151. $invite_count = Db::name('user')->where(['pre_user_id' => $invite_info['id'], 'createtime' => ['gt', $last_coupon_time]])->count();
  1152. if ($invite_count >= $invitepersonnum && $invitepersonnum > 0) {
  1153. //每邀请10人发放一次优惠券
  1154. //查询推广优惠券
  1155. $invite_coupon = Db::name('coupon')->where(['purpose' => 2, 'status' => 1])->order('weigh desc, id desc')->find();
  1156. if ($invite_coupon) {
  1157. $invite_coupon_data = [
  1158. 'user_id' => $invite_info['id'],
  1159. 'coupon_id' => $invite_coupon['id'],
  1160. 'title' => $invite_coupon['title'],
  1161. 'desc' => $invite_coupon['desc'],
  1162. 'type' => $invite_coupon['type'],
  1163. 'money' => $invite_coupon['money'],
  1164. 'minmoney' => $invite_coupon['minmoney'],
  1165. 'purpose' => $invite_coupon['purpose'],
  1166. 'starttime' => time(),
  1167. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  1168. 'createtime' => time()
  1169. ];
  1170. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  1171. if (!$invite_coupon_rs) {
  1172. Db::rollback();
  1173. $this->error('注册失败');
  1174. }
  1175. }
  1176. }
  1177. }
  1178. Db::commit();
  1179. $ret = $this->auth->direct($rs);
  1180. if ($ret) {
  1181. $data = ['userinfo' => $this->auth->getUserinfo()];
  1182. $this->success(__('Sign up successful'), $data);
  1183. } else {
  1184. $this->error($this->auth->getError());
  1185. }
  1186. }
  1187. //生成邀请码
  1188. public function myinvite() {
  1189. $invite = Random::alnum(7);
  1190. $count = Db::name('user')->where(['invite_no' => $invite])->count('id');
  1191. if ($count) {
  1192. $this->myinvite();
  1193. }
  1194. return $invite;
  1195. }
  1196. //生成uid
  1197. public function myuid($id = 0) {
  1198. if (strlen($id) < 8) {
  1199. $username_len = 8 - strlen($id);
  1200. $username = $id . Random::numeric($username_len);
  1201. } else {
  1202. $username = $id;
  1203. }
  1204. $count = Db::name('user')->where(['username' => $username])->count('id');
  1205. if ($count) {
  1206. $this->myuid($id);
  1207. }
  1208. return $username;
  1209. }
  1210. }