User.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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. //检查今日是否登录赠送过成长值
  286. $this->checklogingrowth($this->auth->id);
  287. //检查会员等级
  288. $this->checkviplevel($this->auth->id);
  289. $user = Db::name('user')->find($this->auth->id);
  290. $data['nickname'] = $user['nickname']; //姓名
  291. $data['username'] = $user['username']; //UID
  292. $data['avatar'] = $user['avatar']; //头像
  293. $data['mobile'] = $user['mobile']; //手机号
  294. $data['money'] = $user['money']; //余额
  295. $data['realname'] = $user['realname']; //真实姓名
  296. $data['gender'] = $user['gender']; //性别:1=男,2=女
  297. $data['birthday'] = date('Y-m-d', $user['birthday']); //生日
  298. $data['idcard'] = $user['idcard']; //身份证号
  299. $data['passport'] = $user['passport']; //护照号
  300. $data['emergencycontact'] = $user['emergencycontact']; //紧急联系人
  301. $data['contactmobile'] = $user['contactmobile']; //紧急联系方式
  302. $data['outdoorduration'] = $user['outdoorduration']; //户外时长
  303. $data['maxlevel'] = $user['maxlevel']; //实际有效会员ID
  304. $data['viplevel'] = Db::name('vip')->where(['id' => $user['maxlevel']])->value('level'); //会员等级
  305. $data['active_count'] = Db::name('active_order')->where(['user_id' => $this->auth->id, 'status' => 2])->count('id'); //完成活动数量
  306. $this->success('会员信息', $data);
  307. }
  308. /**
  309. * 修改会员个人信息
  310. *
  311. * @ApiMethod (POST)
  312. * @param string $avatar 头像地址
  313. * @param string $username 用户名
  314. * @param string $nickname 昵称
  315. * @param string $bio 个人简介
  316. */
  317. public function profile()
  318. {
  319. if ($this->auth->is_auth == 1) {
  320. $this->error('认证正在审核,不能修改');
  321. }
  322. if ($this->auth->is_auth == 2) {
  323. $this->error('认证已通过审核,不能修改');
  324. }
  325. $nickname = $this->request->post('nickname', '', 'trim'); //姓名
  326. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  327. $province = $this->request->post('province', '', 'trim'); //省
  328. $city = $this->request->post('city', '', 'trim'); //市
  329. $area = $this->request->post('area', '', 'trim'); //区
  330. $address = $this->request->post('address', '', 'trim'); //详细地址
  331. $recommender = $this->request->post('recommender', '', 'trim'); //推荐人姓名
  332. $recommender_mobile = $this->request->post('recommender_mobile', '', 'trim'); //推荐人手机号
  333. $zimage = $this->request->post('zimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证正面照
  334. $fimage = $this->request->post('fimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证反面照
  335. if (!$nickname) {
  336. $this->error('请输入姓名');
  337. }
  338. if (iconv_strlen($nickname, 'utf-8') > 30) {
  339. $this->error('姓名最多30位');
  340. }
  341. if (!$idcard) {
  342. $this->error('请输入身份证号');
  343. }
  344. if (iconv_strlen($idcard, 'utf-8') != 18) {
  345. $this->error('身份证号错误');
  346. }
  347. if (!$province || !$city || !$area) {
  348. $this->error('请选择养殖场地址');
  349. }
  350. if (!$address) {
  351. $this->error('请输入详细地址');
  352. }
  353. if (iconv_strlen($address, 'utf-8') > 255) {
  354. $this->error('详细地址最多255位');
  355. }
  356. if (!$recommender) {
  357. $this->error('请输入推荐人姓名');
  358. }
  359. if (iconv_strlen($recommender, 'utf-8') > 30) {
  360. $this->error('推荐人姓名最多30位');
  361. }
  362. if (!$recommender_mobile || !is_mobile($recommender_mobile)) {
  363. $this->error('请输入正确推荐人手机号');
  364. }
  365. if (!$zimage || !$fimage) {
  366. $this->error('请上传身份证相关照片');
  367. }
  368. // $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  369. // if ($username) {
  370. // $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  371. // if ($exists) {
  372. // $this->error(__('Username already exists'));
  373. // }
  374. // $user->username = $username;
  375. // }
  376. // if ($nickname) {
  377. // $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  378. // if ($exists) {
  379. // $this->error(__('Nickname already exists'));
  380. // }
  381. // $user->nickname = $nickname;
  382. // }
  383. // $user->avatar = $avatar;
  384. $data = [
  385. 'nickname' => $nickname,
  386. 'province' => $province,
  387. 'city' => $city,
  388. 'area' => $area,
  389. 'address' => $address,
  390. 'updatetime' => time()
  391. ];
  392. //开启事务
  393. Db::startTrans();
  394. //修改用户表
  395. $data['is_auth'] = 1;
  396. $rt = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  397. if ($rt === false) {
  398. Db::rollback();
  399. $this->error('修改失败');
  400. }
  401. //修改认证表
  402. unset($data['is_auth']);
  403. $data['idcard'] = $idcard;
  404. $data['zimage'] = $zimage;
  405. $data['fimage'] = $fimage;
  406. $data['recommender'] = $recommender;
  407. $data['recommender_mobile'] = $recommender_mobile;
  408. $data['status'] = 0;
  409. $rs = Db::name('user_auth')->where(['user_id' => $this->auth->id])->setField($data);
  410. if (!$rs) {
  411. Db::rollback();
  412. $this->error('修改失败');
  413. }
  414. Db::commit();
  415. $this->success('修改成功');
  416. }
  417. //修改头像
  418. public function changeavatar()
  419. {
  420. $avatar = input('avatar', '', 'trim'); //头像
  421. if (!$avatar) {
  422. $this->error('请上传头像');
  423. }
  424. if (iconv_strlen($avatar, 'utf-8') > 255) {
  425. $this->error('头像长度超出限制');
  426. }
  427. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('avatar', $avatar);
  428. if (!$rs) {
  429. $this->error('修改失败');
  430. }
  431. $this->success('修改成功');
  432. }
  433. /**
  434. * 修改邮箱
  435. *
  436. * @ApiMethod (POST)
  437. * @param string $email 邮箱
  438. * @param string $captcha 验证码
  439. */
  440. public function changeemail()
  441. {
  442. $user = $this->auth->getUser();
  443. $email = $this->request->post('email');
  444. $captcha = $this->request->post('captcha');
  445. if (!$email || !$captcha) {
  446. $this->error(__('Invalid parameters'));
  447. }
  448. if (!Validate::is($email, "email")) {
  449. $this->error(__('Email is incorrect'));
  450. }
  451. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  452. $this->error(__('Email already exists'));
  453. }
  454. $result = Ems::check($email, $captcha, 'changeemail');
  455. if (!$result) {
  456. $this->error(__('Captcha is incorrect'));
  457. }
  458. $verification = $user->verification;
  459. $verification->email = 1;
  460. $user->verification = $verification;
  461. $user->email = $email;
  462. $user->save();
  463. Ems::flush($email, 'changeemail');
  464. $this->success();
  465. }
  466. /**
  467. * 修改手机号
  468. *
  469. * @ApiMethod (POST)
  470. * @param string $mobile 手机号
  471. * @param string $captcha 验证码
  472. */
  473. public function changemobile()
  474. {
  475. $user = $this->auth->getUser();
  476. $mobile = $this->request->post('mobile');
  477. $captcha = $this->request->post('captcha');
  478. if (!$mobile || !$captcha) {
  479. $this->error(__('Invalid parameters'));
  480. }
  481. if (!Validate::regex($mobile, "^1\d{10}$")) {
  482. $this->error(__('Mobile is incorrect'));
  483. }
  484. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  485. $this->error(__('Mobile already exists'));
  486. }
  487. $result = Sms::check($mobile, $captcha, 'changemobile');
  488. if (!$result) {
  489. $this->error(__('Captcha is incorrect'));
  490. }
  491. $verification = $user->verification;
  492. $verification->mobile = 1;
  493. $user->verification = $verification;
  494. $user->mobile = $mobile;
  495. $user->save();
  496. Sms::flush($mobile, 'changemobile');
  497. $this->success();
  498. }
  499. /**
  500. * 第三方登录
  501. *
  502. * @ApiMethod (POST)
  503. * @param string $platform 平台名称
  504. * @param string $code Code码
  505. */
  506. public function third()
  507. {
  508. $url = url('user/index');
  509. $platform = $this->request->post("platform");
  510. $code = $this->request->post("code");
  511. $config = get_addon_config('third');
  512. if (!$config || !isset($config[$platform])) {
  513. $this->error(__('Invalid parameters'));
  514. }
  515. $app = new \addons\third\library\Application($config);
  516. //通过code换access_token和绑定会员
  517. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  518. if ($result) {
  519. $loginret = \addons\third\library\Service::connect($platform, $result);
  520. if ($loginret) {
  521. $data = [
  522. 'userinfo' => $this->auth->getUserinfo(),
  523. 'thirdinfo' => $result
  524. ];
  525. $this->success(__('Logged in successful'), $data);
  526. }
  527. }
  528. $this->error(__('Operation failed'), $url);
  529. }
  530. /**
  531. * 重置密码
  532. *
  533. * @ApiMethod (POST)
  534. * @param string $mobile 手机号
  535. * @param string $newpassword 新密码
  536. * @param string $captcha 验证码
  537. */
  538. public function resetpwd()
  539. {
  540. $type = 'mobile';//$this->request->post("type");
  541. $mobile = $this->request->post("mobile");
  542. $email = $this->request->post("email");
  543. $newpassword = $this->request->post("newpassword");
  544. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  545. $captcha = $this->request->post("captcha");
  546. if (!$newpassword || !$captcha || !$repassword) {
  547. $this->error(__('Invalid parameters'));
  548. }
  549. //验证Token
  550. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  551. $this->error(__('Password must be 6 to 30 characters'));
  552. }
  553. if ($repassword != $newpassword) {
  554. $this->error('两次密码不一致');
  555. }
  556. if ($type == 'mobile') {
  557. if (!Validate::regex($mobile, "^1\d{10}$")) {
  558. $this->error(__('Mobile is incorrect'));
  559. }
  560. $user = \app\common\model\User::getByMobile($mobile);
  561. if (!$user) {
  562. $this->error(__('User not found'));
  563. }
  564. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  565. if (!$ret) {
  566. $this->error(__('Captcha is incorrect'));
  567. }
  568. Sms::flush($mobile, 'resetpwd');
  569. } else {
  570. if (!Validate::is($email, "email")) {
  571. $this->error(__('Email is incorrect'));
  572. }
  573. $user = \app\common\model\User::getByEmail($email);
  574. if (!$user) {
  575. $this->error(__('User not found'));
  576. }
  577. $ret = Ems::check($email, $captcha, 'resetpwd');
  578. if (!$ret) {
  579. $this->error(__('Captcha is incorrect'));
  580. }
  581. Ems::flush($email, 'resetpwd');
  582. }
  583. //模拟一次登录
  584. $this->auth->direct($user->id);
  585. $ret = $this->auth->changepwd($newpassword, '', true);
  586. if ($ret) {
  587. $this->success(__('Reset password successful'));
  588. } else {
  589. $this->error($this->auth->getError());
  590. }
  591. }
  592. /**
  593. * 修改密码
  594. *
  595. * @ApiMethod (POST)
  596. * @param string $captcha 验证码
  597. * @param string $newpassword 新密码
  598. * @param string $repassword 确认密码
  599. */
  600. public function editpwd()
  601. {
  602. $captcha = $this->request->post("captcha", '', 'trim'); //验证码
  603. $newpassword = $this->request->post("newpassword", '' , 'trim'); //新密码
  604. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  605. if (iconv_strlen($captcha, 'utf-8') != config('alisms.length')) {
  606. $this->error(__('Captcha is incorrect'));
  607. }
  608. if (!$newpassword) {
  609. $this->error('请输入新密码');
  610. }
  611. //验证新密码
  612. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  613. $this->error(__('Password must be 6 to 30 characters'));
  614. }
  615. if (!$repassword) {
  616. $this->error('请再次输入密码');
  617. }
  618. if ($repassword != $newpassword) {
  619. $this->error('两次密码不一致');
  620. }
  621. // $user = \app\common\model\User::getById($this->auth->id);
  622. // if (!$user) {
  623. // $this->error(__('User not found'));
  624. // }
  625. $ret = Sms::check($this->auth->mobile, $captcha, 'changepwd');
  626. if (!$ret) {
  627. $this->error(__('Captcha is incorrect'));
  628. }
  629. //模拟一次登录
  630. $this->auth->direct($this->auth->id);
  631. $ret = $this->auth->changepwd($newpassword, '', true);
  632. if ($ret) {
  633. Sms::flush($this->auth->mobile, 'changepwd');
  634. $this->success(__('Reset password successful'));
  635. } else {
  636. $this->error($this->auth->getError());
  637. }
  638. }
  639. //查询经营信息
  640. public function get_businessinfo()
  641. {
  642. $data = Db::name('user_business_info')->field('manage, mobile, province, city, area, distribution_channel')
  643. ->where(['user_id' => $this->auth->id])->find();
  644. $this->success('经营信息', $data);
  645. }
  646. //修改经营信息
  647. public function editbusinessinfo()
  648. {
  649. $manage = $this->request->post('manage', '', 'trim'); //采购负责人
  650. $mobile = $this->request->post('mobile', '', 'trim'); //联系方式
  651. $province = $this->request->post('province', '', 'trim'); //省
  652. $city = $this->request->post('city', '', 'trim'); //市
  653. $area = $this->request->post('area', '', 'trim'); //区
  654. $distribution_channel = $this->request->post('distribution_channel', '', 'trim'); //销售渠道
  655. $data = [];
  656. if ($manage) {
  657. if (iconv_strlen($manage, 'utf-8') > 50) {
  658. $this->error('采购负责人最多50字');
  659. }
  660. $data['manage'] = $manage;
  661. }
  662. if ($mobile) {
  663. if (!Validate::regex($mobile, "^1\d{10}$")) {
  664. $this->error(__('Mobile is incorrect'));
  665. }
  666. $data['mobile'] = $mobile;
  667. }
  668. if ($province && $city && $area) {
  669. $data['province'] = $province;
  670. $data['city'] = $city;
  671. $data['area'] = $area;
  672. }
  673. if ($distribution_channel) {
  674. if (iconv_strlen($distribution_channel, 'utf-8') > 255) {
  675. $this->error('销售渠道说明最多50字');
  676. }
  677. $data['distribution_channel'] = $distribution_channel;
  678. }
  679. if (!$data) {
  680. $this->error('暂无修改内容');
  681. }
  682. $count = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->count();
  683. if ($count) {
  684. $data['updatetime'] = time();
  685. $rs = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->setField($data);
  686. } else {
  687. $data['user_id'] = $this->auth->id;
  688. $data['createtime'] = time();
  689. $rs = Db::name('user_business_info')->insertGetId($data);
  690. }
  691. if (!$rs) {
  692. $this->error('修改失败');
  693. }
  694. $this->success('修改成功');
  695. }
  696. //查询猪车信息
  697. public function getpigcar()
  698. {
  699. $data = Db::name('user_pig_car')->where(['user_id' => $this->auth->id])->select();
  700. $data = list_domain_image($data, ['images']);
  701. $this->success('查询猪车信息', $data);
  702. }
  703. /**
  704. * 添加猪车信息
  705. *
  706. *$data = [
  707. [
  708. 'carnumber' => '123',
  709. 'carframenumber' => '344',
  710. 'images' => '123.jpg,234.jpg,345.jpg'
  711. ],
  712. [
  713. 'carnumber' => '123',
  714. 'carframenumber' => '344',
  715. 'images' => '123.jpg,234.jpg,345.jpg'
  716. ],
  717. [
  718. 'carnumber' => '123',
  719. 'carframenumber' => '344',
  720. 'images' => '123.jpg,234.jpg,345.jpg'
  721. ]
  722. ];
  723. *
  724. */
  725. public function addpigcar()
  726. {
  727. $pig_car = input('pig_car', '', 'trim'); //猪车信息json串: carnumber车牌号 carframenumber车架号 images车辆照片
  728. if (!$pig_car) {
  729. $this->error('请添加猪车信息');
  730. }
  731. $pig_car = json_decode($pig_car, true);
  732. if (!$pig_car) {
  733. $this->error('请添加猪车信息');
  734. }
  735. if (count($pig_car) > 9) {
  736. $this->error('一次最多添加9条记录');
  737. }
  738. $_data = [];
  739. foreach ($pig_car as &$v) {
  740. $data = [];
  741. if (!$v['carnumber']) {
  742. $this->error('请输入车牌号');
  743. break;
  744. }
  745. if (iconv_strlen($v['carnumber'], 'utf-8') > 50) {
  746. $this->error('车牌号最多50字');
  747. break;
  748. }
  749. if (!$v['carframenumber']) {
  750. $this->error('请输入车架号');
  751. break;
  752. }
  753. if (iconv_strlen($v['carframenumber'], 'utf-8') > 50) {
  754. $this->error('车架号最多50字');
  755. break;
  756. }
  757. if ($v['images']) {
  758. $image_arr = explode(',', $v['images']);
  759. if (count($image_arr) > 9) {
  760. $this->error('每辆车照片最多9张');
  761. break;
  762. }
  763. $data['images'] = $v['images'];
  764. }
  765. $data['user_id'] = $this->auth->id;
  766. $data['carnumber'] = $v['carnumber'];
  767. $data['carframenumber'] = $v['carframenumber'];
  768. $data['createtime'] = time();
  769. array_push($_data, $data);
  770. }
  771. $rs = Db::name('user_pig_car')->insertAll($_data);
  772. if (!$rs) {
  773. $this->error('添加失败');
  774. }
  775. $this->success('添加成功');
  776. }
  777. //删除猪车信息
  778. public function delpigcar()
  779. {
  780. $id = input('id', 0, 'intval'); //猪车id
  781. if (!$id) {
  782. $this->error('请选择要删除的猪车');
  783. }
  784. $info = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  785. if (!$info) {
  786. $this->error('这不是您的车');
  787. }
  788. $rs = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->delete();
  789. if (!$rs) {
  790. $this->error('删除失败');
  791. }
  792. $this->success('删除成功');
  793. }
  794. //平台介绍
  795. public function getabout()
  796. {
  797. $info = Db::name('platform_info')->field('title, content')->where(['type' => 1])->find();
  798. $this->success('平台介绍', $info);
  799. }
  800. //常见问题
  801. public function problem()
  802. {
  803. $list = Db::name('problem')->field('id, title, content')
  804. ->page($this->page, config('paginate.list_rows'))->order('weigh', 'desc')->select();
  805. $this->success('常见问题', $list);
  806. }
  807. //投诉举报类型
  808. public function complainttype()
  809. {
  810. $list = Db::name('complaint_type')->field('id, name')->where(['pid' => 0])->order('weigh', 'desc')->select();
  811. $list = $this->getchildtype($list);
  812. $this->success('常见问题', $list);
  813. }
  814. //无限级投诉举报类型
  815. public function getchildtype($list = [])
  816. {
  817. $complaint_type = Db::name('complaint_type');
  818. foreach ($list as &$v) {
  819. $child = $complaint_type->field('id, name')->where(['pid' => $v['id']])->order('weigh', 'desc')->select();
  820. if ($child) {
  821. $child = $this->getchildtype($child);
  822. }
  823. $v['child'] = $child;
  824. }
  825. return $list;
  826. }
  827. //投诉举报
  828. public function complaint()
  829. {
  830. $complaint_type = input('complaint_type', '', 'trim'); //投诉类型
  831. $order_sn = input('order_sn', '', 'trim'); //订单号
  832. $be_complaint_user = input('be_complaint_user', '', 'trim'); //被投诉人/单位
  833. $complaint_user = input('complaint_user', '', 'trim'); //投诉人
  834. $mobile = input('mobile', '', 'trim'); //联系电话
  835. $code = input('code', '', 'trim'); //验证码
  836. $content = input('content', '', 'trim'); //情况说明
  837. $images = input('images', '', 'trim'); //附件材料
  838. $data = [];
  839. if (!$complaint_type) {
  840. $this->error('请选择投诉类型');
  841. }
  842. if ($order_sn) {
  843. if (iconv_strlen($order_sn, 'utf-8') > 255) {
  844. $this->error('订单号最多255位');
  845. }
  846. //查询订单
  847. $count = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'order_sn' => $order_sn, 'status' => 1])->count();
  848. if (!$count) {
  849. $this->error('请输入正确中标订单号');
  850. }
  851. }
  852. if (!$be_complaint_user) {
  853. $this->error('请输入被投诉人/单位');
  854. }
  855. if (iconv_strlen($be_complaint_user, 'utf-8') > 50) {
  856. $this->error('被投诉人/单位最多50字');
  857. }
  858. if (!$complaint_user) {
  859. $this->error('请输入投诉人');
  860. }
  861. if (iconv_strlen($complaint_user, 'utf-8') > 50) {
  862. $this->error('投诉人最多50字');
  863. }
  864. if (!is_mobile($mobile)) {
  865. $this->error('请输入正确联系电话');
  866. }
  867. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  868. $this->error(__('Captcha is incorrect'));
  869. }
  870. $ret = Sms::check($mobile, $code, 'complaint');
  871. if (!$ret) {
  872. $this->error(__('Captcha is incorrect'));
  873. }
  874. if (!$content) {
  875. $this->error('请输入情况说明');
  876. }
  877. if (iconv_strlen($content, 'utf-8') > 3000) {
  878. $this->error('情况说明最多3000字');
  879. }
  880. if ($images) {
  881. $image_arr = explode(',', $images);
  882. if (count($image_arr) > 9) {
  883. $this->error('附件照片最多9张');
  884. }
  885. $data['images'] = $images;
  886. }
  887. $data['user_id'] = $this->auth->id;
  888. $data['complaint_type'] = $complaint_type;
  889. $data['order_sn'] = $order_sn;
  890. $data['be_complaint_user'] = $be_complaint_user;
  891. $data['complaint_user'] = $complaint_user;
  892. $data['mobile'] = $mobile;
  893. $data['content'] = $content;
  894. $data['images'] = $images;
  895. $data['createtime'] = time();
  896. $rs = Db::name('complaint')->insertGetId($data);
  897. if (!$rs) {
  898. $this->error('投诉失败');
  899. }
  900. Sms::flush($this->auth->mobile, 'complaint');
  901. $this->success('投诉成功');
  902. }
  903. //评价列表
  904. public function getmarkcomment()
  905. {
  906. $list = Db::name('mark_bid_comment')->where(['user_id' => $this->auth->id])
  907. ->page($this->page, config('paginate.list_rows'))->order('createtime', 'desc')->select();
  908. $list = list_domain_image($list, ['images']);
  909. $mark = Db::name('mark');
  910. $mark_bid = Db::name('mark_bid');
  911. foreach ($list as &$v) {
  912. $v['mark'] = $mark->find($v['mark_id']);
  913. $v['mark'] = info_domain_image($v['mark'], ['image', 'video', 'defective_images', 'road_images', 'pig_out_images']);
  914. $v['order_sn'] = $mark_bid->where(['id' => $v['mark_bid_id']])->value('order_sn');
  915. }
  916. $this->success('评论列表', $list);
  917. }
  918. //评价
  919. public function markcomment()
  920. {
  921. $mark_id = input('mark_id', 0, 'intval'); //招标id
  922. $content = input('content', '', 'trim'); //情况说明
  923. $images = input('images', '', 'trim'); //附件材料
  924. $data = [];
  925. if (!$mark_id) {
  926. $this->error('请选择要评价的数据');
  927. }
  928. $info = Db::name('mark')->find($mark_id);
  929. if (!$info) {
  930. $this->error('招标信息不存在');
  931. }
  932. $bid_id = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'mark_id' => $mark_id, 'status' => 1])->value('id');
  933. if (!$bid_id) {
  934. $this->error('您未中标,暂不能评价');
  935. }
  936. if (!$content) {
  937. $this->error('请输入评价内容');
  938. }
  939. if (iconv_strlen($content, 'utf-8') > 3000) {
  940. $this->error('评价内容最多3000字');
  941. }
  942. if ($images) {
  943. $image_arr = explode(',', $images);
  944. if (count($image_arr) > 9) {
  945. $this->error('照片最多9张');
  946. }
  947. $data['images'] = $images;
  948. }
  949. $data['user_id'] = $this->auth->id;
  950. $data['mark_id'] = $mark_id;
  951. $data['mark_bid_id'] = $bid_id;
  952. $data['content'] = $content;
  953. $data['images'] = $images;
  954. $data['createtime'] = time();
  955. $rs = Db::name('mark_bid_comment')->insertGetId($data);
  956. if (!$rs) {
  957. $this->error('评价失败');
  958. }
  959. $this->success('评价成功');
  960. }
  961. //个人账单列表
  962. public function getcompanymoney()
  963. {
  964. $list = Db::name('company')->field('id, name')->page($this->page, config('paginate.list_rows'))->select();
  965. $company_money_log = Db::name('company_money_log');
  966. foreach ($list as &$v) {
  967. $log = $company_money_log->where(['company_id' => $v['id'], 'user_id' => $this->auth->id])->order('id', 'desc')->find();
  968. if ($log) {
  969. $v['money'] = $log['after'];
  970. $v['can_money'] = $log['after'];
  971. } else {
  972. $v['money'] = '0';
  973. $v['can_money'] = '0';
  974. }
  975. }
  976. $this->success('个人账单列表', $list);
  977. }
  978. //个人账单明细
  979. public function getcompanymoneydetail()
  980. {
  981. $company_id = input('company_id', 0, 'intval'); //公司id
  982. $start_time = input('start_time', 0, 'strtotime'); //开始时间
  983. $end_time = input('end_time', 0, 'strtotime'); //结束时间
  984. if (!$company_id) {
  985. $this->error('参数缺失');
  986. }
  987. $where['company_id'] = $company_id;
  988. $where['user_id'] = $this->auth->id;
  989. if ($start_time && $end_time) {
  990. $where['createtime'] = ['between', [$start_time, $end_time]];
  991. }
  992. $list = Db::name('company_money_log')->field('id, money, memo, createtime')
  993. ->where($where)->page($this->page, config('paginate.list_rows'))->order('id', 'desc')->select();
  994. foreach ($list as &$v) {
  995. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  996. }
  997. $data['income'] = Db::name('company_money_log')->where($where)->where(['money' => ['gt', 0]])->sum('money');
  998. $data['expend'] = Db::name('company_money_log')->where($where)->where(['money' => ['lt', 0]])->sum('money');
  999. $data['list'] = $list;
  1000. $this->success('个人账单明细', $data);
  1001. }
  1002. //关于我们/免责协议/用户协议/隐私政策
  1003. public function getagreement()
  1004. {
  1005. $type = input('type', 0, 'intval');
  1006. if (!in_array($type, [1, 2, 3, 4])) {
  1007. $this->error('参数错误');
  1008. }
  1009. $info = Db::name('platform_info')->field('title, content')->where(['type' => $type])->find();
  1010. $this->success('协议', $info);
  1011. }
  1012. //获取openid
  1013. public function getopenid() {
  1014. //code
  1015. $code = $this->request->param('code', '', 'trim');// code值
  1016. if (!$code) {
  1017. $this->error(__('Invalid parameters'));
  1018. }
  1019. $config = config('wxchatpay');
  1020. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&js_code='.$code.'&grant_type=authorization_code';
  1021. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  1022. $openidInfo = json_decode($openidInfo,true);
  1023. if(!isset($openidInfo['openid'])) {
  1024. $this->error('用户openid获取失败', $openidInfo);
  1025. }
  1026. $this->success('success', $openidInfo);
  1027. }
  1028. //微信登录
  1029. public function wxlogin() {
  1030. $openid = input('openid', '', 'trim');
  1031. if (!$openid) {
  1032. $this->error('参数缺失');
  1033. }
  1034. $user = \app\common\model\User::getByOpenid($openid);
  1035. if (!$user) {
  1036. $this->success('用户尚未注册', ['code' => 5]);
  1037. }
  1038. if ($user['status'] != 1) {
  1039. $this->error(__('Account is locked'));
  1040. }
  1041. $ret = $this->auth->direct($user->id);
  1042. if ($ret) {
  1043. $data = ['userinfo' => $this->auth->getUserinfo()];
  1044. $this->success(__('Logged in successful'), $data);
  1045. } else {
  1046. $this->error($this->auth->getError());
  1047. }
  1048. }
  1049. //注册
  1050. public function register()
  1051. {
  1052. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  1053. $code = $this->request->post('code', '', 'trim'); //验证码
  1054. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  1055. $gender = $this->request->post('gender', 0, 'intval'); //性别:1=男,2=女
  1056. $invite_no = $this->request->post('invite_no', '', 'trim'); //邀请码
  1057. $openid = $this->request->post('openid', '', 'trim'); //微信openid
  1058. $nickname = $this->request->post('nickname', '', 'trim'); //微信昵称
  1059. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //微信头像
  1060. $openidcount = Db::name('user')->where(['openid' => $openid])->count('id');
  1061. if ($openidcount) {
  1062. $this->error('微信已经注册,请直接登录');
  1063. }
  1064. if (!Validate::regex($mobile, "^1\d{10}$")) {
  1065. $this->error(__('Mobile is incorrect'));
  1066. }
  1067. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  1068. if ($count) {
  1069. $this->error('手机号已被注册');
  1070. }
  1071. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  1072. $this->error(__('Captcha is incorrect'));
  1073. }
  1074. $ret = Sms::check($mobile, $code, 'register');
  1075. if (!$ret) {
  1076. $this->error(__('Captcha is incorrect'));
  1077. }
  1078. if (!$birthday || $birthday >= time()) {
  1079. $this->error('请选择正确生日');
  1080. }
  1081. if (!in_array($gender, [1, 2])) {
  1082. $this->error('请选择性别');
  1083. }
  1084. if ($invite_no) {
  1085. $invite_info = Db::name('user')->where(['invite_no' => $invite_no])->find();
  1086. if (!$invite_info) {
  1087. $this->error('邀请码不存在');
  1088. }
  1089. }
  1090. if (!$nickname || !$avatar) {
  1091. $this->error('参数缺失');
  1092. }
  1093. if (iconv_strlen($nickname, 'utf-8') > 30 || iconv_strlen($avatar, 'utf-8') > 255) {
  1094. $this->error('参数错误');
  1095. }
  1096. $ip = request()->ip();
  1097. $time = time();
  1098. $data = [
  1099. 'nickname' => $nickname,
  1100. 'salt' => Random::alnum(),
  1101. 'mobile' => $mobile,
  1102. 'avatar' => $avatar,
  1103. 'joinip' => $ip,
  1104. 'jointime' => $time,
  1105. 'logintime' => $time,
  1106. 'loginip' => $ip,
  1107. 'prevtime' => $time,
  1108. 'createtime' => $time,
  1109. 'gender' => $gender,
  1110. 'birthday' => $birthday,
  1111. 'openid' => $openid,
  1112. 'invite_no' => $this->myinvite(),
  1113. 'pre_user_id' => isset($invite_info) ? $invite_info['id'] : 0,
  1114. 'invite_time' => $time,
  1115. ];
  1116. //开启事务
  1117. Db::startTrans();
  1118. $rs = Db::name('user')->insertGetId($data);
  1119. if (!$rs) {
  1120. Db::rollback();
  1121. $this->error('注册失败');
  1122. }
  1123. //生成uid
  1124. $username = $this->myuid($rs);
  1125. $rt = Db::name('user')->where(['id' => $rs])->setField('username', $username);
  1126. if (!$rt) {
  1127. Db::rollback();
  1128. $this->error('注册失败');
  1129. }
  1130. //给用户发放注册优惠券
  1131. $register_coupon = Db::name('coupon')->where(['purpose' => 1, 'status' => 1])->order('weigh desc, id desc')->find();
  1132. if ($register_coupon) {
  1133. $register_coupon_data = [
  1134. 'user_id' => $rs,
  1135. 'coupon_id' => $register_coupon['id'],
  1136. 'title' => $register_coupon['title'],
  1137. 'desc' => $register_coupon['desc'],
  1138. 'type' => $register_coupon['type'],
  1139. 'money' => $register_coupon['money'],
  1140. 'minmoney' => $register_coupon['minmoney'],
  1141. 'purpose' => $register_coupon['purpose'],
  1142. 'starttime' => time(),
  1143. 'endtime' => time() + $register_coupon['effectiveday'] * 86400,
  1144. 'createtime' => time()
  1145. ];
  1146. $register_coupon_rs = Db::name('user_coupon')->insertGetId($register_coupon_data);
  1147. if (!$register_coupon_rs) {
  1148. Db::rollback();
  1149. $this->error('注册失败');
  1150. }
  1151. }
  1152. //查询是否需要给上级发放优惠券
  1153. if (isset($invite_info)) {
  1154. //查询上一次获得推广优惠券时间
  1155. $last_coupon = Db::name('user_coupon')->where(['user_id' => $invite_info['id'], 'purpose' => 2])->order('id', 'desc')->find();
  1156. $last_coupon_time = $last_coupon ? $last_coupon['createtime'] : 0;
  1157. //获取后台配置推广获得优惠券指定人数
  1158. $invitepersonnum = (int)config('site.invitepersonnum');
  1159. //查询最新推广任务
  1160. $invite_count = Db::name('user')->where(['pre_user_id' => $invite_info['id'], 'createtime' => ['gt', $last_coupon_time]])->count();
  1161. if ($invite_count >= $invitepersonnum && $invitepersonnum > 0) {
  1162. //每邀请10人发放一次优惠券
  1163. //查询推广优惠券
  1164. $invite_coupon = Db::name('coupon')->where(['purpose' => 2, 'status' => 1])->order('weigh desc, id desc')->find();
  1165. if ($invite_coupon) {
  1166. $invite_coupon_data = [
  1167. 'user_id' => $invite_info['id'],
  1168. 'coupon_id' => $invite_coupon['id'],
  1169. 'title' => $invite_coupon['title'],
  1170. 'desc' => $invite_coupon['desc'],
  1171. 'type' => $invite_coupon['type'],
  1172. 'money' => $invite_coupon['money'],
  1173. 'minmoney' => $invite_coupon['minmoney'],
  1174. 'purpose' => $invite_coupon['purpose'],
  1175. 'starttime' => time(),
  1176. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  1177. 'createtime' => time()
  1178. ];
  1179. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  1180. if (!$invite_coupon_rs) {
  1181. Db::rollback();
  1182. $this->error('注册失败');
  1183. }
  1184. }
  1185. }
  1186. }
  1187. Db::commit();
  1188. $ret = $this->auth->direct($rs);
  1189. if ($ret) {
  1190. $data = ['userinfo' => $this->auth->getUserinfo()];
  1191. $this->success(__('Sign up successful'), $data);
  1192. } else {
  1193. $this->error($this->auth->getError());
  1194. }
  1195. }
  1196. //生成邀请码
  1197. public function myinvite() {
  1198. $invite = Random::alnum(7);
  1199. $count = Db::name('user')->where(['invite_no' => $invite])->count('id');
  1200. if ($count) {
  1201. $this->myinvite();
  1202. }
  1203. return $invite;
  1204. }
  1205. //生成uid
  1206. public function myuid($id = 0) {
  1207. if (strlen($id) < 8) {
  1208. $username_len = 8 - strlen($id);
  1209. $username = $id . Random::numeric($username_len);
  1210. } else {
  1211. $username = $id;
  1212. }
  1213. $count = Db::name('user')->where(['username' => $username])->count('id');
  1214. if ($count) {
  1215. $this->myuid($id);
  1216. }
  1217. return $username;
  1218. }
  1219. //消息列表
  1220. public function sysmsg() {
  1221. $list = Db::name('sys_msg')->field('id, title, content, createtime')->where(['user_id' => $this->auth->id])
  1222. ->page($this->page, $this->pagenum)->order('id desc')->select();
  1223. foreach ($list as &$v) {
  1224. $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
  1225. }
  1226. $this->success('消息列表', $list);
  1227. }
  1228. }