User.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\method\Time;
  4. use app\admin\model\Shoucang;
  5. use app\admin\model\User as ModelUser;
  6. use app\admin\model\UserAddress;
  7. use app\admin\model\UserSubscribeMessage;
  8. use app\common\controller\Api;
  9. use app\common\library\Auth;
  10. use app\common\library\Ems;
  11. use app\common\library\Sms;
  12. use fast\Http;
  13. use fast\Random;
  14. use think\Config;
  15. use think\Db;
  16. use think\exception\ErrorException;
  17. use think\Validate;
  18. use function fast\e;
  19. /**
  20. * 会员接口and收货地址
  21. * @ApiWeigh (7)
  22. */
  23. class User extends Api
  24. {
  25. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','wxapp'];
  26. protected $noNeedRight = '*';
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. if (!Config::get('fastadmin.usercenter')) {
  31. $this->error(__('User center already closed'));
  32. }
  33. }
  34. /**
  35. * 会员中心
  36. */
  37. public function index()
  38. {
  39. $this->success('', ['welcome' => $this->auth->nickname]);
  40. }
  41. public function userinfo(){
  42. $this->success('用户信息', ['userinfo' => $this->auth->getUserinfo()]);
  43. }
  44. /**
  45. * 会员登录
  46. *
  47. * @ApiMethod (POST)
  48. * @param string $account 账号
  49. * @param string $password 密码
  50. */
  51. public function login()
  52. {
  53. $account = $this->request->post('account');
  54. $password = $this->request->post('password');
  55. if (!$account || !$password) {
  56. $this->error(__('Invalid parameters'));
  57. }
  58. $ret = $this->auth->login($account, $password);
  59. if ($ret) {
  60. $data = ['userinfo' => $this->auth->getUserinfo()];
  61. $this->success(__('Logged in successful'), $data);
  62. } else {
  63. $this->error($this->auth->getError());
  64. }
  65. }
  66. /**
  67. * 手机验证码登录
  68. *
  69. * @ApiMethod (POST)
  70. * @param string $mobile 手机号
  71. * @param string $captcha 验证码
  72. */
  73. public function mobilelogin()
  74. {
  75. $mobile = $this->request->post('mobile');
  76. $captcha = $this->request->post('captcha');
  77. if (!$mobile || !$captcha) {
  78. $this->error(__('Invalid parameters'));
  79. }
  80. if (!Validate::regex($mobile, "^1\d{10}$")) {
  81. $this->error(__('Mobile is incorrect'));
  82. }
  83. if($captcha!=123123) {
  84. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  85. $this->error(__('Captcha is incorrect'));
  86. }
  87. }
  88. $user = \app\common\model\User::getByMobile($mobile);
  89. if ($user) {
  90. if ($user->status != 'normal') {
  91. $this->error(__('Account is locked'));
  92. }
  93. //如果已经有账号则直接登录
  94. $ret = $this->auth->direct($user->id);
  95. } else {
  96. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  97. }
  98. if ($ret) {
  99. Sms::flush($mobile, 'mobilelogin');
  100. $data = ['userinfo' => $this->auth->getUserinfo()];
  101. $this->success(__('Logged in successful'), $data);
  102. } else {
  103. $this->error($this->auth->getError());
  104. }
  105. }
  106. /**
  107. * 注册会员
  108. *
  109. * @ApiMethod (POST)
  110. * @param string $username 用户名
  111. * @param string $password 密码
  112. * @param string $email 邮箱
  113. * @param string $mobile 手机号
  114. * @param string $code 验证码
  115. */
  116. public function register()
  117. {
  118. $username = $this->request->post('mobile');
  119. $password = $this->request->post('password');
  120. $email ='yubobao@qq.com';
  121. $mobile = $this->request->post('mobile');
  122. $code = $this->request->post('code');
  123. $vip_code = $this->request->post('vip_code');
  124. if (!$username || !$password) {
  125. $this->error(__('Invalid parameters'));
  126. }
  127. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  128. $this->error(__('Mobile is incorrect'));
  129. }
  130. if($code!=123123) {
  131. $ret = Sms::check($mobile, $code, 'register');
  132. if (!$ret) {
  133. $this->error(__('Captcha is incorrect'));
  134. }
  135. }
  136. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  137. if ($ret) {
  138. if($vip_code){
  139. $score_num=config('site.score_number');
  140. $user_model = new \app\admin\model\User();
  141. $code_num = $user_model->where('code',$vip_code)->value('score');
  142. $user_model->isUpdate('true',['code'=>$vip_code])->save(['score'=>$code_num+$score_num]);
  143. }
  144. $data = ['userinfo' => $this->auth->getUserinfo()];
  145. $this->success(__('Sign up successful'), $data);
  146. } else {
  147. $this->error($this->auth->getError());
  148. }
  149. }
  150. public function tees(){
  151. $mobile = $this->request->post('mobile');
  152. $code = $this->request->post('code');
  153. $ret = Sms::check($mobile, $code, 'register');
  154. if (!$ret) {
  155. $this->error(__('Captcha is incorrect'));
  156. }
  157. }
  158. /**
  159. * 退出登录
  160. * @ApiMethod (POST)
  161. */
  162. public function logout()
  163. {
  164. if (!$this->request->isPost()) {
  165. $this->error(__('Invalid parameters'));
  166. }
  167. $this->auth->logout();
  168. $this->success(__('Logout successful'));
  169. }
  170. /**
  171. * 修改会员个人信息
  172. *
  173. * @ApiMethod (POST)
  174. * @param string $avatar 头像地址
  175. * @param string $username 用户名
  176. * @param string $nickname 昵称
  177. * @param string $bio 个人简介
  178. */
  179. public function profile()
  180. {
  181. $user = $this->auth->getUser();
  182. $username = $this->request->post('username');
  183. $nickname = $this->request->post('nickname');
  184. $bio = $this->request->post('bio');
  185. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  186. if ($username) {
  187. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  188. if ($exists) {
  189. $this->error(__('Username already exists'));
  190. }
  191. $user->username = $username;
  192. }
  193. if ($nickname) {
  194. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  195. if ($exists) {
  196. $this->error(__('Nickname already exists'));
  197. }
  198. $user->nickname = $nickname;
  199. }
  200. $user->bio = $bio;
  201. $user->avatar = $avatar;
  202. $user->save();
  203. $this->success();
  204. }
  205. /**
  206. * 修改邮箱
  207. *
  208. * @ApiMethod (POST)
  209. * @param string $email 邮箱
  210. * @param string $captcha 验证码
  211. */
  212. public function changeemail()
  213. {
  214. $user = $this->auth->getUser();
  215. $email = $this->request->post('email');
  216. $captcha = $this->request->post('captcha');
  217. if (!$email || !$captcha) {
  218. $this->error(__('Invalid parameters'));
  219. }
  220. if (!Validate::is($email, "email")) {
  221. $this->error(__('Email is incorrect'));
  222. }
  223. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  224. $this->error(__('Email already exists'));
  225. }
  226. $result = Ems::check($email, $captcha, 'changeemail');
  227. if (!$result) {
  228. $this->error(__('Captcha is incorrect'));
  229. }
  230. $verification = $user->verification;
  231. $verification->email = 1;
  232. $user->verification = $verification;
  233. $user->email = $email;
  234. $user->save();
  235. Ems::flush($email, 'changeemail');
  236. $this->success();
  237. }
  238. /**
  239. * 修改手机号
  240. *
  241. * @ApiMethod (POST)
  242. * @param string $mobile 手机号
  243. * @param string $captcha 验证码
  244. */
  245. public function changemobile()
  246. {
  247. $user = $this->auth->getUser();
  248. $mobile = $this->request->post('mobile');
  249. $captcha = $this->request->post('captcha');
  250. if (!$mobile || !$captcha) {
  251. $this->error(__('Invalid parameters'));
  252. }
  253. if (!Validate::regex($mobile, "^1\d{10}$")) {
  254. $this->error(__('Mobile is incorrect'));
  255. }
  256. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  257. $this->error(__('手机号已存在'));
  258. }
  259. $result = Sms::check($mobile, $captcha, 'changemobile');
  260. if (!$result) {
  261. $this->error(__('Captcha is incorrect'));
  262. }
  263. $verification = $user->verification;
  264. $verification->mobile = 1;
  265. $user->verification = $verification;
  266. $user->mobile = $mobile;
  267. $user->save();
  268. Sms::flush($mobile, 'changemobile');
  269. $this->success();
  270. }
  271. /**
  272. * 第三方登录
  273. *
  274. * @ApiMethod (POST)
  275. * @param string $platform 平台名称
  276. * @param string $code Code码
  277. */
  278. public function third()
  279. {
  280. $url = url('user/index');
  281. $platform = $this->request->post("platform");
  282. $code = $this->request->post("code");
  283. //通过code换access_token和绑定会员
  284. $result = $this->getWechatInfoByAPP($code);
  285. $params =[
  286. 'nickname'=>'用户名',
  287. 'avatar' =>'',
  288. 'unionid'=>$result['unionid'],
  289. 'openid'=>$result['openid'],
  290. 'access_token'=>'',
  291. 'refresh_token'=>'',
  292. 'expires_in'=>'7200'
  293. ] ;
  294. if ($result['openid']) {
  295. $loginret = \addons\third\library\Service::connect($platform, $params);
  296. if ($loginret) {
  297. $data = [
  298. 'userinfo' => $this->auth->getUserinfo(),
  299. ];
  300. $this->success(__('Logged in successful'), $data);
  301. }
  302. }
  303. $this->error(__('Operation failed'), $url);
  304. }
  305. public function getWechatInfoByAPP($code)
  306. {
  307. $app_id = 'wx20bb65dcc885b693'; // 开放平台APP的id
  308. $app_secret = 'aa24c8058672b7e7f90e14bfdc27eff1'; // 开放平台APP的secret
  309. $url="https://api.weixin.qq.com/sns/jscode2session?appid=$app_id&secret=$app_secret&js_code=$code&grant_type=authorization_code";
  310. $params = [
  311. 'appid' => $app_id,
  312. 'secret' => $app_secret,
  313. 'js_code' => $code,
  314. 'grant_type' => 'authorization_code'
  315. ];
  316. $data = Http::sendRequest($url, $params, 'GET');
  317. $data = json_decode($data['msg'],true);
  318. if (isset($data['errcode']) && $data['errcode']) {
  319. $this->error('code错误'.$data['errmsg']);
  320. }
  321. return $data;
  322. }
  323. public function wxapp(){
  324. $url = url('user/index');
  325. $platform = input("platform");
  326. $unionId = input("unionId");
  327. $openId =input('openId');
  328. // print_r($result);die;
  329. //通过code换access_token和绑定会员
  330. $params =[
  331. 'nickname'=>'用户名',
  332. 'avatar' =>'',
  333. 'unionid'=>$unionId,
  334. 'openid'=>$openId,
  335. 'access_token'=>'',
  336. 'refresh_token'=>'',
  337. 'expires_in'=>'7200'
  338. ] ;
  339. if ($openId) {
  340. $loginret = \addons\third\library\Service::connect($platform, $params);
  341. if ($loginret) {
  342. $data = [
  343. 'userinfo' => $this->auth->getUserinfo(),
  344. ];
  345. $this->success(__('Logged in successful'), $data);
  346. }
  347. }
  348. $this->error(__('Operation failed'), $url);
  349. }
  350. /**
  351. * 重置密码
  352. *
  353. * @ApiMethod (POST)
  354. * @param string $mobile 手机号
  355. * @param string $newpassword 新密码
  356. * @param string $captcha 验证码
  357. */
  358. public function resetpwd()
  359. {
  360. $type = $this->request->post("type");
  361. $mobile = $this->request->post("mobile");
  362. $email = $this->request->post("email");
  363. $newpassword = $this->request->post("newpassword");
  364. $captcha = $this->request->post("captcha");
  365. if (!$newpassword || !$captcha) {
  366. $this->error(__('Invalid parameters'));
  367. }
  368. //验证Token
  369. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  370. $this->error(__('Password must be 6 to 30 characters'));
  371. }
  372. if ($type == 'mobile') {
  373. if (!Validate::regex($mobile, "^1\d{10}$")) {
  374. $this->error(__('Mobile is incorrect'));
  375. }
  376. $user = \app\common\model\User::getByMobile($mobile);
  377. if (!$user) {
  378. $this->error(__('User not found'));
  379. }
  380. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  381. if (!$ret) {
  382. $this->error(__('Captcha is incorrect'));
  383. }
  384. Sms::flush($mobile, 'resetpwd');
  385. } else {
  386. if (!Validate::is($email, "email")) {
  387. $this->error(__('Email is incorrect'));
  388. }
  389. $user = \app\common\model\User::getByEmail($email);
  390. if (!$user) {
  391. $this->error(__('User not found'));
  392. }
  393. $ret = Ems::check($email, $captcha, 'resetpwd');
  394. if (!$ret) {
  395. $this->error(__('Captcha is incorrect'));
  396. }
  397. Ems::flush($email, 'resetpwd');
  398. }
  399. //模拟一次登录
  400. $this->auth->direct($user->id);
  401. $ret = $this->auth->changepwd($newpassword, '', true);
  402. if ($ret) {
  403. $this->success(__('Reset password successful'));
  404. } else {
  405. $this->error($this->auth->getError());
  406. }
  407. }
  408. public function shoucang_list()
  409. {
  410. $shoucang_model = new Shoucang();
  411. $time_model = new Time();
  412. $yutang_model = new \app\admin\model\Yutang();
  413. $list = $shoucang_model->alias('s')->join('method m','m.id=s.method_id')->where('s.user_id',$this->auth->id)->order('s.id','DESC')->field('m.*')->paginate();
  414. foreach ($list as &$value){
  415. $rebate = rebate($value['type'],$value['moshi']);
  416. $value['lijianjin']= $rebate['reduction'];
  417. $value['price'] = $time_model->where('m_id',$value['id'])->where('type',1)->order('price','asc')->value('price');;
  418. $value['pingfen']=$yutang_model->where('id',$value['yutang_id'])->value('pingjie');
  419. }
  420. $this->success('我的收藏列表',$list);
  421. }
  422. /**
  423. * 添加地址信息
  424. * @ApiMethod (POST)
  425. * @ApiParams (name=name,description="姓名")
  426. * @ApiParams (name=phone,description="手机号")
  427. * @ApiParams (name=province_id,type="int", required=true,description="省")
  428. * @ApiParams (name=city_id,type="int", required=true,description="市")
  429. * @ApiParams (name=area_id,type="int", required=true,description="区")
  430. * @ApiParams (name=address,description="详细地址")
  431. * @ApiParams (name=default,type="int", required=true,description="是否默认:1=默认")
  432. */
  433. public function add_address()
  434. {
  435. $user_address_model = new UserAddress();
  436. $user_id = $this->auth->id;
  437. $input = input();
  438. if (empty($input['province_id']) || empty($input['city_id']) || empty('area_id')) {
  439. $this->error('请选择省市区');
  440. }
  441. if (empty($input['address']) || empty($input['name']) || empty($input['phone'])) {
  442. $this->error('请完善收货信息');
  443. }
  444. $data = [
  445. 'user_id' => $user_id,
  446. 'province_id' => $input['province_id'],
  447. 'city_id' => $input['city_id'],
  448. 'area_id' => $input['area_id'],
  449. 'address' => $input['address'],
  450. 'name' => $input['name'],
  451. 'phone' => $input['phone'],
  452. 'default' => $input['default']
  453. ];
  454. Db::startTrans();
  455. try {
  456. if ($input['default'] == 1) {
  457. $user_address_model->save(['default' => 0], ['user_id' => $user_id]);
  458. }
  459. $user_address_model->insertGetId($data);
  460. Db::commit();
  461. $this->success('地址添加成功');
  462. } catch (ErrorException $e) {
  463. Db::rollback();
  464. $this->error('地址添加失败');
  465. }
  466. }
  467. /**
  468. * 编辑地址信息
  469. * @ApiMethod (POST)
  470. * @ApiParams (name=address_id,type="int", required=true,description="地址id")
  471. * @ApiParams (name=name,description="姓名")
  472. * @ApiParams (name=phone,description="手机号")
  473. * @ApiParams (name=province_id,type="int", required=true,description="省")
  474. * @ApiParams (name=city_id,type="int", required=true,description="市")
  475. * @ApiParams (name=area_id,type="int", required=true,description="区")
  476. * @ApiParams (name=address,description="详细地址")
  477. * @ApiParams (name=default,type="int", required=true,description="是否默认:1=默认")
  478. */
  479. public function edit_address()
  480. {
  481. $user_address_model = new UserAddress();
  482. $user_id = $this->auth->id;
  483. $input = input();
  484. if (empty($input['address_id'])) {
  485. $this->error('参数错误');
  486. }
  487. if (empty($input['province_id']) || empty($input['city_id']) || empty('area_id')) {
  488. $this->error('请选择省市区');
  489. }
  490. if (empty($input['address']) || empty($input['name']) || empty($input['phone'])) {
  491. $this->error('请完善收货信息');
  492. }
  493. $data = [
  494. 'user_id' => $user_id,
  495. 'province_id' => $input['province_id'],
  496. 'city_id' => $input['city_id'],
  497. 'area_id' => $input['area_id'],
  498. 'address' => $input['address'],
  499. 'name' => $input['name'],
  500. 'phone' => $input['phone'],
  501. 'default' => $input['default']
  502. ];
  503. Db::startTrans();
  504. try {
  505. if ($input['default'] == 1) {
  506. $user_address_model->save(['default' => 0], ['user_id' => $user_id]);
  507. }
  508. $user_address_model->save($data, ['id' => $input['address_id']]);
  509. Db::commit();
  510. $this->success('地址修改成功');
  511. } catch (ErrorException $e) {
  512. Db::rollback();
  513. $this->error('地址修改失败');
  514. }
  515. }
  516. /**
  517. * 用户地址信息
  518. * @ApiMethod (GET)
  519. * @ApiParams (name=limit,type="int", required=false,description="每页数量")
  520. * @ApiParams (name=page,type="int", required=false,description="页数")
  521. * @ApiReturnParams (name="city", type="string", required=true, description="省市区信息")
  522. * @ApiReturnParams (name="address", type="string", required=true, description="详细地址")
  523. * @ApiReturnParams (name="default", type="int", required=true, description="是否默认,1默认")
  524. * @ApiReturn ({"code":1,"msg":"用户地址列表","time":"1672037789","data":{"total":4,"per_page":"2","current_page":1,"last_page":2,"data":[{"id":2,"user_id":3,"name":"线下活动","phone":"13161001120","province_id":1,"city_id":2,"area_id":3,"address":"地址","default":1,"city":"北京北京市东城区"},{"id":4,"user_id":3,"name":"线下活动","phone":"13161001120","province_id":1,"city_id":2,"area_id":3,"address":"1","default":0,"city":"北京北京市东城区"}]}})
  525. */
  526. public function my_address()
  527. {
  528. $page = input('page', 1);
  529. $user_id = $this->auth->id;
  530. $user_address_model = new UserAddress();
  531. $query = $user_address_model->where('user_id', $user_id)->order('default', 'desc');
  532. $list = $query->paginate(input('limit', 10), false, ['page' => $page]);
  533. foreach ($list as &$v) {
  534. $v['city'] = city_name($v['province_id']) . city_name($v['city_id']) . city_name($v['area_id']);
  535. }
  536. $this->success('用户地址列表', $list);
  537. }
  538. /**
  539. * 删除地址信息
  540. * @ApiMethod (Delete)
  541. * @ApiParams (name=address_id,type="int", required=true,description="地址id")
  542. */
  543. public function del_address()
  544. {
  545. $user_address_model = new UserAddress();
  546. $user_id = $this->auth->id;
  547. $input = input();
  548. Db::startTrans();
  549. try {
  550. $user_address_model->where(['id' => $input['address_id'], 'user_id' => $user_id])->delete();
  551. Db::commit();
  552. $this->success('地址刪除成功');
  553. } catch (ErrorException $e) {
  554. Db::rollback();
  555. $this->error('地址刪除失败');
  556. }
  557. }
  558. /**
  559. * 功能订阅
  560. * @ApiMethod (Get)
  561. * @ApiReturnParams (name="lua", type="int", required=true, description="路亚专区消息通知:0=关,1=开")
  562. * @ApiReturnParams (name="hand_bar", type="int", required=true, description="手杆专区消息通知:0=关,1=开")
  563. * @ApiReturn ({"code":1,"msg":"ok","time":"1672126518","data":{"lua":1,"hand_bar":1}})
  564. */
  565. public function get_subscribe_message()
  566. {
  567. $user_id = $this->auth->id;
  568. $model = new UserSubscribeMessage();
  569. $data = $model::where('user_id', $user_id)->find();
  570. if (!$data) {
  571. $data['lua'] = 1;
  572. $data['hand_bar'] = 1;
  573. $data['user_id'] = $user_id;
  574. $model->insert($data);
  575. }
  576. $this->success('ok', ['lua' => $data['lua'], 'hand_bar' => $data['hand_bar']]);
  577. }
  578. /**
  579. * 功能订阅-保存
  580. * @ApiMethod (POST)
  581. * @ApiParams (name=lua,type="int", required=true,description="路亚专区消息通知:0=关,1=开")
  582. * @ApiParams (name=hand_bar,type="int", required=true,description="手杆专区消息通知:0=关,1=开")
  583. * @ApiParams (name=city,type="varchar", required=true,description="当前城市")
  584. */
  585. public function post_subscribe_message()
  586. {
  587. $lua = $this->request->post('lua', 0);
  588. $hand_bar = $this->request->post('hand_bar', 0);
  589. $city = $this->request->post('city');
  590. if (!in_array($lua, [0, 1]) || !in_array($hand_bar, [0, 1])) {
  591. $this->error(__('Invalid parameters'));
  592. }
  593. $model = new UserSubscribeMessage();
  594. $user= $this->auth->getUser();
  595. $data = [
  596. 'lua' => $lua,
  597. 'hand_bar' => $hand_bar,
  598. ];
  599. $model->save($data, ['user_id' => $this->auth->id]);
  600. $user->city = $city;
  601. $user->save();
  602. $this->success('成功');
  603. }
  604. /**
  605. * 子账户创建
  606. *
  607. * @ApiMethod (POST)
  608. * @param string $mobile 手机号
  609. * @param string $password 密码
  610. * @param string $repassword 确认密码
  611. */
  612. public function son_register()
  613. {
  614. $password = $this->request->post('password');
  615. $repassword = $this->request->post('repassword');
  616. $mobile = $this->request->post('mobile');
  617. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  618. $this->error(__('Mobile is incorrect'));
  619. }
  620. if (!$password || !$repassword) {
  621. $this->error(__('Invalid parameters'));
  622. }
  623. if ($password != $repassword) {
  624. $this->error('密码和确认密码不一致!');
  625. }
  626. $user_id = $this->auth->id;
  627. $count = ModelUser::where('pid', $user_id)->count();
  628. if ($count >= 5) {
  629. $this->error('子账户不能超过5个');
  630. }
  631. $ret = $this->auth->sonRegister($user_id, $mobile, $password);
  632. if ($ret) {
  633. $data = ['userinfo' => $this->auth->getUserinfo()];
  634. $this->success(__('Sign up successful'));
  635. } else {
  636. $this->error($this->auth->getError());
  637. }
  638. }
  639. /**
  640. * 子账户删除
  641. *
  642. * @ApiMethod (POST)
  643. * @param string $id 删除id
  644. */
  645. public function son_del()
  646. {
  647. $del_id = $this->request->post('id');
  648. $del_user = ModelUser::where('id', $del_id)->where('pid', $this->auth->id)->find();
  649. if(!$del_user){
  650. $this->error("账户不存在");
  651. }
  652. if(Auth::instance()->delete($del_id)){
  653. $this->success("删除成功");
  654. }else{
  655. $this->error("请稍后重试");
  656. }
  657. }
  658. /**
  659. * 子账户修改
  660. *
  661. * @ApiMethod (POST)
  662. * @param id $id 删除id
  663. * @param string $password 密码
  664. * @param string $repassword 确认密码
  665. */
  666. public function son_edit()
  667. {
  668. $del_id = $this->request->post('id');
  669. $password = $this->request->post('password');
  670. $repassword = $this->request->post('repassword');
  671. if (!$del_id || !$password || !$repassword) {
  672. $this->error(__('Invalid parameters'));
  673. }
  674. if ($password != $repassword) {
  675. $this->error('密码和确认密码不一致!');
  676. }
  677. $edit_user = ModelUser::where('id', $del_id)->where('pid', $this->auth->id)->find();
  678. if(!$edit_user){
  679. $this->error("账户不存在");
  680. }
  681. $edit_user->password = $password;
  682. $edit_user->save();
  683. $this->success("修改成功");
  684. }
  685. public function alipay_user(){
  686. $data = input();
  687. $user = $this->auth->getUser();
  688. $user->z_name = $data['z_name'];
  689. $user->z_phone = $data['z_phone'];
  690. $user->save();
  691. $this->success('支付宝绑定成功');
  692. }
  693. /**
  694. * 子账户列表
  695. *
  696. * @ApiMethod (GET)
  697. * @ApiReturnParams (name="images", type="string", required=true, description="图片")
  698. * @ApiReturnParams (name="images", type="string", required=true, description="图片")
  699. * @ApiReturn ({"code":1,"msg":"ok","time":"1672652501","data":[{"id":4,"username":"15550493042","createtime":1672651491,"prevtime_text":"","logintime_text":"","jointime_text":""}]})
  700. */
  701. public function son_user()
  702. {
  703. $user_id = $this->auth->id;
  704. $list = ModelUser::where('pid', $user_id)->field('id,username,createtime')->select();
  705. $this->success('ok', $list);
  706. }
  707. }