UserCenter.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\GoodsOrder;
  4. use app\common\model\UserCollect;
  5. use app\common\model\UserCouponList;
  6. use app\common\model\UserLevel;
  7. use app\common\model\UserTrack;
  8. use app\common\validate\UserVali;
  9. use think\Db;
  10. use app\common\model\UserBank;
  11. use app\common\model\User;
  12. /**
  13. * @title 会员个人中心
  14. * @controller UserCenter
  15. * @group base
  16. */
  17. class UserCenter extends Base
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. parent::checkLogin();
  23. }
  24. /**
  25. * @title 获取个人信息(不含统计)
  26. * @desc 个人信息
  27. * @author qc
  28. * @url /api/User_center/getPureUserInfo
  29. * @method GET
  30. * @tag 个人信息
  31. * @header name:Authorization require:1 desc:Token
  32. * @return name:phone type:int default:-- desc:手机号
  33. * @return name:name type:string default:-- desc:昵称
  34. * @return name:encryption_password type:string default:-- desc:加密密码
  35. * @return name:headimg type:string default:-- desc:头像地址
  36. * @return name:sex type:int default:1 desc:性别(1男2女)
  37. * @return name:true_name type:string default:-- desc:真实姓名
  38. * @return name:invite_code type:string default:-- desc:邀请码
  39. * @return name:birthday type:string default:-- desc:出生日期
  40. * @return name:province_name type:string default:-- desc:省
  41. * @return name:city_name type:string default:-- desc:市
  42. * @return name:counties_name type:string default:-- desc:县区
  43. * @return name:bind_wechat type:string default:-- desc:绑定微信号
  44. * @return name:true_name type:string default:-- desc:真实姓名
  45. * @return name:id_card type:string default:-- desc:身份证号
  46. * @return name:is_auth type:int default:-- desc:是否认证(0否1是)
  47. * @return name:auth_at type:string default:-- desc:认证时间
  48. * @return name:card_front type:string default:-- desc:身份证正面
  49. * @return name:card_back type:string default:-- desc:身份证背面
  50. * @return name:level_name type:string default:-- desc:等级名称
  51. * @return name:wallet type:array default:-- desc:钱包
  52. * @return name:wallet.integral type:int default:-- desc:积分
  53. * @return name:wallet.growth type:int default:-- desc:经验值(成长值)
  54. * @return name:wallet.money type:float default:-- desc:佣金
  55. * @return name:order_stat type:array default:-- desc:订单统计
  56. *
  57. */
  58. public function getPureUserInfo()
  59. {
  60. $field = 'id,phone,encryption_password,name,level_id,pay_password,headimg,openid,true_name,sex,invite_code,birthday,province_name,city_name,counties_name,bind_wechat,id_card,card_front,card_back,auth_at,is_agency,partner_lev';
  61. $user_info = User::with('wallet')->field($field)->where('id',$this->user_id)->find()->toArray();
  62. if(empty($user_info)) $this->error('用户信息不正确');
  63. $user_info['level_name'] = UserLevel::where('id',$user_info['level_id'])->value('name');
  64. $this->success('获取成功',['detail'=>$user_info,]);
  65. }
  66. /**
  67. * @title 获取个人信息
  68. * @desc 个人信息
  69. * @author qc
  70. * @url /api/User_center/getUserInfo
  71. * @method GET
  72. * @tag 个人信息
  73. * @header name:Authorization require:1 desc:Token
  74. * @return name:phone type:int default:-- desc:手机号
  75. * @return name:name type:string default:-- desc:昵称
  76. * @return name:encryption_password type:string default:-- desc:加密密码
  77. * @return name:headimg type:string default:-- desc:头像地址
  78. * @return name:sex type:int default:1 desc:性别(1男2女)
  79. * @return name:true_name type:string default:-- desc:真实姓名
  80. * @return name:invite_code type:string default:-- desc:邀请码
  81. * @return name:birthday type:string default:-- desc:出生日期
  82. * @return name:province_name type:string default:-- desc:省
  83. * @return name:city_name type:string default:-- desc:市
  84. * @return name:counties_name type:string default:-- desc:县区
  85. * @return name:bind_wechat type:string default:-- desc:绑定微信号
  86. * @return name:true_name type:string default:-- desc:真实姓名
  87. * @return name:id_card type:string default:-- desc:身份证号
  88. * @return name:is_auth type:int default:-- desc:是否认证(0否1是)
  89. * @return name:auth_at type:string default:-- desc:认证时间
  90. * @return name:card_front type:string default:-- desc:身份证正面
  91. * @return name:card_back type:string default:-- desc:身份证背面
  92. * @return name:level_name type:string default:-- desc:等级名称
  93. * @return name:partner_lev type:int default:-- desc:分销商等级id(0不是分销商)
  94. * @return name:is_agency type:int default:-- desc:是否是区域代理(0不是)
  95. * @return name:fans type:int default:-- desc:粉丝数量
  96. * @return name:collect type:int default:-- desc:收藏量
  97. * @return name:coupon_num type:int default:-- desc:券数量
  98. * @return name:track_num type:int default:-- desc:足迹条数
  99. * @return name:wallet type:array default:-- desc:钱包
  100. * @return name:wallet.integral type:int default:-- desc:积分
  101. * @return name:wallet.growth type:int default:-- desc:经验值(成长值)
  102. * @return name:wallet.money type:float default:-- desc:佣金
  103. * @return name:order_stat type:array default:-- desc:订单统计
  104. * @return name:order_stat.no_pay type:int default:-- desc:未支付订单
  105. * @return name:order_stat.no_deliver type:int default:-- desc:未发货
  106. * @return name:order_stat.no_receive type:int default:-- desc:未收货
  107. * @return name:order_stat.no_comment type:int default:-- desc:未评论
  108. *
  109. */
  110. public function getUserInfo()
  111. {
  112. $field = 'id,phone,encryption_password,name,level_id,pay_password,headimg,openid,true_name,sex,invite_code,birthday,province_name,city_name,counties_name,bind_wechat,id_card,card_front,card_back,auth_at,is_agency,partner_lev';
  113. $user_info = User::with('wallet')->field($field)->where('id',$this->user_id)->find()->toArray();
  114. $user_info['level_name'] = UserLevel::where('id',$user_info['level_id'])->value('name');
  115. $user_info['fans'] = User::where('pid',$user_info['id'])->count();
  116. $user_info['collect'] = UserCollect::where('user_id',$user_info['id'])->count();
  117. $user_info['coupon_num'] = UserCouponList::where(['user_id'=>$user_info['id'],'status'=>0])->count();
  118. $user_info['track_num'] = UserTrack::where(['user_id'=>$user_info['id']])->count();
  119. $order_stat = [];
  120. $order_stat['no_pay'] = GoodsOrder::where(['user_id'=>$this->user_id,'status'=>0])->count();
  121. $order_stat['no_deliver'] = GoodsOrder::where(['user_id'=>$this->user_id,'status'=>1])->count();
  122. $order_stat['no_receive'] = GoodsOrder::where(['user_id'=>$this->user_id,'status'=>2])->count();
  123. $order_stat['no_comment '] = GoodsOrder::where(['user_id'=>$this->user_id,'status'=>3])->count();
  124. if(empty($user_info)) $this->error('用户信息不正确');
  125. $this->success('获取成功',['detail'=>$user_info,'order_stat'=>$order_stat]);
  126. }
  127. /**
  128. * @title 编辑个人信息
  129. * @desc 编辑个人信息
  130. * @author qc
  131. * @url /api/User_center/updateUserInfo
  132. * @method POST
  133. * @tag 编辑信息
  134. * @header name:Authorization require:1 desc:Token
  135. * @param name:name type:string default:-- desc:昵称
  136. * @param name:headimg type:string default:-- desc:头像地址
  137. * @param name:sex type:int default:1 desc:性别(1男2女)
  138. * @param name:birthday type:string default:1 desc:出生日期
  139. * @param name:address type:string default:1 desc:地址(山东省-临沂市-兰山区)
  140. */
  141. public function updateUserInfo()
  142. {
  143. $headimg = input('post.headimg');
  144. $name = trim(input('post.name',''));
  145. $sex = trim(input('post.sex',1));
  146. $birthday = input('post.birthday');
  147. $address = input('post.address');
  148. $update_data= [];
  149. $update_data['name'] = $name;
  150. $update_data['headimg'] = $headimg;
  151. $update_data['sex'] = $sex;
  152. $update_data['birthday'] = $birthday;
  153. if($address){
  154. $add_arr = explode('-',$address);
  155. $update_data['province_name'] = $add_arr[0];
  156. $update_data['city_name'] = $add_arr[1];
  157. $update_data['counties_name'] = $add_arr[2];
  158. }
  159. if($name){
  160. $check_member = Db::name('store_member')->where('name',$name)->where('id','<>',$this->user_id)->count();
  161. if($check_member) $this->error('该用户名已被占用');
  162. }
  163. Db::name('store_member')->where('id',$this->user_id)->update($update_data);
  164. $field = 'phone,name,headimg';
  165. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  166. $this->success('编辑成功',$user_info);
  167. }
  168. /**
  169. * @title 绑定手机号
  170. * @desc 绑定手机号
  171. * @author qc
  172. * @url /api/User_center/modifyPhone
  173. * @method POST
  174. * @header name:Authorization require:1 desc:Token
  175. * @param name:phone type:int require:1 default:-- desc:手机号
  176. * @param name:code type:int require:1 default:-- desc:手机号验证码
  177. * @return name:phone type:int default:-- desc:手机号
  178. * @return name:name type:string default:-- desc:昵称
  179. * @return name:headimg type:string default:-- desc:头像地址
  180. * @return name:true_name type:string default:-- desc:真实姓名
  181. * @return name:id_card type:string default:-- desc:身份证号
  182. * @return name:is_auth type:int default:-- desc:是否认证(0否1是)
  183. */
  184. public function modifyPhone()
  185. {
  186. $uid = $this->user_id;
  187. $phone = input('post.phone');
  188. $code = input('post.code');
  189. if(empty($phone) || empty($code) ) $this->error('参数错误');
  190. $field = 'id,phone,name,headimg,status,openid,true_name,id_card,is_auth';
  191. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  192. if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
  193. $check_code = $this->checkPhoneCode($phone,$code);
  194. if(!$check_code) $this->error('验证码错误');
  195. //判断手机号是否已注册
  196. $member_id = Db::name('store_member')
  197. ->where('phone','=',$phone)->where('is_deleted','=',0)->where('id','<>',$this->user_id)->value('id');
  198. if($member_id) $this->error('手机号已注册过');
  199. Db::name('store_member')->where('id',$uid)->update(['phone'=>$phone]);
  200. $user_info['phone'] = $phone;
  201. $this->updatePhoneCode($check_code);
  202. $this->success('绑定成功',$user_info);
  203. }
  204. /**
  205. * @title 验证已绑定手机号是否正确
  206. * @desc 验证已绑定手机号是否正确
  207. * @author qc
  208. * @url /api/User_center/checkBindPhone
  209. * @method POST
  210. * @header name:Authorization require:1 desc:Token
  211. * @param name:phone type:int require:1 default:-- desc:手机号
  212. * @param name:code type:int require:1 default:-- desc:手机号验证码
  213. */
  214. public function checkBindPhone()
  215. {
  216. $phone = input('post.phone');
  217. $code = input('post.code');
  218. if(empty($phone) || empty($code) ) $this->error('参数错误');
  219. $field = 'id,phone,name,headimg,status,openid,true_name,id_card,is_auth';
  220. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  221. if($user_info['phone'] != $phone) $this->error('该手机号与当前账户不一致!');
  222. $check_code = $this->checkPhoneCode($phone,$code);
  223. if(!$check_code) $this->error('验证码错误');
  224. $this->updatePhoneCode($check_code);
  225. $this->success('验证通过');
  226. }
  227. /**
  228. * @title 更换或设置登录密码
  229. * @desc 更换或设置登录密码
  230. * @author qc
  231. * @url /api/User_center/modifyPassword
  232. * @method POST
  233. * @header name:Authorization require:1 desc:Token
  234. * @param name:phone type:int require:1 default:-- desc:手机号
  235. * @param name:code type:int require:1 default:-- desc:手机号验证码
  236. * @param name:password type:string default:-- desc:密码
  237. */
  238. public function modifyPassword()
  239. {
  240. $uid = $this->user_id;
  241. $phone = input('post.phone');
  242. $code = input('post.code');
  243. $password = input('post.password');
  244. if(empty($phone) || empty($code) || empty($password) ) $this->error('参数错误');
  245. $field = 'id,phone';
  246. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  247. if(!$user_info['phone']) $this->error('请先绑定手机号');
  248. if($user_info['phone'] != $phone) $this->error('与绑定手机号不一致');
  249. $check_code = $this->checkPhoneCode($phone,$code);
  250. if(!$check_code) $this->error('验证码错误');
  251. $validate = new UserVali();
  252. if (!$validate->scene('reset_password')->check(['phone'=>$phone,'password'=>$password])) {
  253. $this->error($validate->getError());
  254. }
  255. $encryption_password = encrypt_password($password);
  256. User::where('id',$uid)->update(['password'=>$password,'encryption_password'=>$encryption_password]);
  257. $this->updatePhoneCode($check_code);
  258. $this->success('更换成功');
  259. }
  260. /**
  261. * @title 更换或设置提现密码
  262. * @desc 更换或设置提现密码
  263. * @author qc
  264. * @url /api/User_center/setPayPassword
  265. * @method POST
  266. * @header name:Authorization require:1 desc:Token
  267. * @param name:phone type:int require:1 default:-- desc:手机号
  268. * @param name:code type:int require:1 default:-- desc:手机号验证码
  269. * @param name:pay_password type:string default:-- desc:密码
  270. */
  271. public function setPayPassword()
  272. {
  273. $uid = $this->user_id;
  274. $phone = input('post.phone');
  275. $code = input('post.code');
  276. $pay_password = input('post.pay_password');
  277. if(empty($phone) || empty($code) || empty($pay_password)) $this->error('参数错误');
  278. $field = 'id,phone';
  279. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  280. if(!$user_info['phone']) $this->error('请先绑定手机号');
  281. if($user_info['phone'] != $phone) $this->error('与绑定手机号不一致');
  282. $check_code = $this->checkPhoneCode($phone,$code);
  283. if(!$check_code) $this->error('验证码错误');;
  284. User::where('id',$uid)->update(['pay_password'=>encrypt_password($pay_password)]);
  285. $this->updatePhoneCode($check_code);
  286. $this->success('更换成功');
  287. }
  288. /**
  289. * @title 实名认证
  290. * @desc 实名认证
  291. * @author qc
  292. * @method POST
  293. * @tag 实名认证
  294. * @url /api/User_center/userCertification
  295. * @header name:Authorization require:1 desc:Token
  296. * @param name:true_name type:string require:1 default:-- desc:真实姓名
  297. * @param name:id_card type:string require:1 default:-- desc:身份证号
  298. * @param name:card_front type:string require:1 default:-- desc:身份证正面
  299. * @param name:card_back type:string require:1 default:-- desc:身份证反面
  300. */
  301. public function userCertification()
  302. {
  303. $true_name = input('post.true_name');
  304. $id_card = input('post.id_card');
  305. $card_front = input('post.card_front');
  306. $card_back = input('post.card_back');
  307. $is_auth =User::where('id',$this->user_id)->value('is_auth');
  308. if($is_auth) $this->error('已认证');
  309. $get_age = get_age($id_card);
  310. if($get_age < 18)$this->error('未满18周岁!');
  311. $res = user_certification($id_card,$true_name);
  312. if(!$res) $this->error('认证失败');
  313. User::where('id',$this->user_id)->update(['is_auth'=>1,'true_name'=>$true_name,'card_front'=>$card_front,'card_back'=>$card_back,'id_card'=>$id_card,'auth_at'=>date('Y-m-d H:i:s')]);
  314. $this->success('认证成功');
  315. }
  316. /**
  317. * @title 注销会员
  318. * @desc 注销会员
  319. * @author qc
  320. * @method POST
  321. * @url /api/User_center/cancelUserInfo
  322. * @header name:Authorization require:1 desc:Token
  323. */
  324. public function cancelUserInfo()
  325. {
  326. Db::name('store_member')->where('id',$this->user_id)->update(['is_deleted'=>1]);
  327. $this->success('注销成功');
  328. }
  329. /**
  330. * @title 获取我绑定的支付宝账户
  331. * @desc 获取我绑定的支付宝账户
  332. * @author qc
  333. * @method GET
  334. * @url /api/User_center/getMyAliAccount
  335. * @return name:birthday type:string default:-- desc:出生日期
  336. * @return name:real_name type:string default:-- desc:姓名
  337. * @return name:card_no type:string default:-- desc:账号
  338. */
  339. public function getMyAliAccount()
  340. {
  341. $ali_account = UserBank::field('id,real_name,card_no')->where(['user_id'=>$this->user_id,'type'=>1,'is_deleted'=>0])->find();
  342. $ali_account ? $this->success('ok',['account'=>$ali_account->toArray()]) : $this->error('还没有绑定支付宝账户');
  343. }
  344. /**
  345. * @title 更改或绑定支付宝账户
  346. * @desc 更改或绑定支付宝账户
  347. * @author qc
  348. * @method POST
  349. * @url /api/User_center/bindAliAccount
  350. * @param name:phone type:string require:1 default:-- desc:手机号
  351. * @param name:code type:string require:1 default:-- desc:验证码
  352. * @param name:real_name type:string require:1 default:-- desc:真实姓名
  353. * @param name:card_no type:string require:1 default:-- desc:账号
  354. */
  355. public function bindAliAccount()
  356. {
  357. $phone = input('post.phone');
  358. $code = input('post.code');
  359. $real_name = input('post.real_name');
  360. $card_no = input('post.card_no');
  361. $check_code = $this->checkPhoneCode($phone,$code);
  362. if(!$check_code) $this->error('验证码错误');
  363. $ali_account = UserBank::where(['user_id'=>$this->user_id,'type'=>1,'is_deleted'=>0])->find();
  364. if(!$ali_account){
  365. $account_data =[
  366. 'user_id' => $this->user_id,
  367. 'real_name' => $real_name,
  368. 'bank_name' => '支付宝账号',
  369. 'card_no' => $card_no,
  370. 'create_time' => date('Y-m-d H:i:s'),
  371. ];
  372. UserBank::create($account_data);
  373. }else{
  374. $account_data =[
  375. 'user_id' => $this->user_id,
  376. 'real_name' => $real_name,
  377. 'bank_name' => '支付宝账号',
  378. 'card_no' => $card_no,
  379. 'update_time' => date('Y-m-d H:i:s'),
  380. ];
  381. UserBank::update($account_data,['id'=>$ali_account->id]);
  382. }
  383. $this->success('绑定成功');
  384. }
  385. /**
  386. * @title 更改或绑定银行卡
  387. * @desc 更改或绑定银行卡
  388. * @author qc
  389. * @method POST
  390. * @url /api/User_center/bindBankAccount
  391. * @param name:account_id type:string require:1 default:-- desc:记录id(修改时必传)
  392. * @param name:phone type:string require:1 default:-- desc:手机号
  393. * @param name:code type:string require:1 default:-- desc:验证码
  394. * @param name:real_name type:string require:1 default:-- desc:真实姓名
  395. * @param name:card_no type:string require:1 default:-- desc:账号
  396. * @param name:bank_name type:string require:1 default:-- desc:所属银行
  397. */
  398. public function bindBankAccount()
  399. {
  400. $phone = input('post.phone');
  401. $code = input('post.code');
  402. $real_name = input('post.real_name');
  403. $card_no = input('post.card_no');
  404. $bank_name = input('post.bank_name');
  405. $account_id = input('post.account_id');
  406. $check_code = $this->checkPhoneCode($phone,$code);
  407. if(!$check_code) $this->error('验证码错误');
  408. if(!$account_id){
  409. $account_data =[
  410. 'user_id' => $this->user_id,
  411. 'type' => 2,
  412. 'real_name' => $real_name,
  413. 'bank_name' => $bank_name,
  414. 'card_no' => $card_no,
  415. 'create_time' => date('Y-m-d H:i:s'),
  416. ];
  417. UserBank::create($account_data);
  418. }else{
  419. $account_data =[
  420. 'user_id' => $this->user_id,
  421. 'real_name' => $real_name,
  422. 'bank_name' => $bank_name,
  423. 'card_no' => $card_no,
  424. 'update_time' => date('Y-m-d H:i:s'),
  425. ];
  426. UserBank::update($account_data,['id'=>$account_id]);
  427. }
  428. $this->success('绑定成功');
  429. }
  430. /**
  431. * @title 获取绑定银行卡详情
  432. * @desc 获取绑定银行卡详情
  433. * @author qc
  434. * @method GET
  435. * @url /api/User_center/getBandAccountDetail
  436. * @param name:account_id type:string require:1 default:-- desc:id
  437. * @return name:real_name type:string require:1 default:-- desc:真实姓名
  438. * @return name:card_no type:string require:1 default:-- desc:账号
  439. * @return name:bank_name type:string require:1 default:-- desc:所属银行
  440. */
  441. public function getBandAccountDetail()
  442. {
  443. $account_id = input('get.account_id');
  444. $account_info = UserBank::field('id,real_name,card_no,bank_name')->where(['user_id'=>$this->user_id,'id'=>$account_id,'is_deleted'=>0])->find();
  445. $account_info ? $this->success('ok',['account'=>$account_info->toArray()]) : $this->error('没找到记录');
  446. }
  447. /**
  448. * @title 获取绑定银行卡列表
  449. * @desc 获取绑定银行卡列表
  450. * @author qc
  451. * @method GET
  452. * @url /api/User_center/getBankAccountList
  453. * @return name:real_name type:string require:1 default:-- desc:真实姓名
  454. * @return name:card_no type:string require:1 default:-- desc:账号
  455. * @return name:bank_name type:string require:1 default:-- desc:所属银行
  456. */
  457. public function getBankAccountList()
  458. {
  459. $list = UserBank::field('id,real_name,card_no,bank_name')->where(['user_id'=>$this->user_id,'type'=>2,'is_deleted'=>0])->order('id desc')->select();
  460. $this->success('ok',['list'=>$list ? $list->toArray() : []]);
  461. }
  462. }