User.php 50 KB

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