User.php 48 KB

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