User.php 47 KB

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