MemberService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberBankModel;
  16. use App\Models\MemberModel;
  17. use App\Models\OrderModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\MpService;
  21. use App\Services\RedisService;
  22. use App\Services\SmsService;
  23. use Illuminate\Support\Facades\DB;
  24. use phpQrcode\QRcode;
  25. /**
  26. * 会员管理-服务类
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * Class MemberService
  30. * @package App\Services\Api
  31. */
  32. class MemberService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * MemberService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new MemberModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return MemberService|static|null
  49. */
  50. public static function make()
  51. {
  52. if (!self::$instance) {
  53. self::$instance = new static();
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * 账号登录
  59. * @param $params
  60. * @return array|false
  61. */
  62. public function login($params)
  63. {
  64. // 账号登录
  65. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  66. $password = isset($params['password']) ? $params['password'] : '';
  67. if (empty($params) || empty($mobile) || empty($password)) {
  68. $this->error = 1041;
  69. return false;
  70. }
  71. // 验证是否注册,没有则注册
  72. $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'mobile', 'user_type', 'password', 'nickname', 'code', 'status'])->first();
  73. $data = $data ? $data->toArray() : [];
  74. $userId = isset($data['id']) ? $data['id'] : 0;
  75. $status = isset($data['status']) ? $data['status'] : 0;
  76. $userPassword = isset($data['password']) ? $data['password'] : '';
  77. if (empty($data) || $userId <= 0) {
  78. $this->error = 2014;
  79. return false;
  80. }
  81. if ($status == 3) {
  82. $this->error = 2050;
  83. return false;
  84. }
  85. if ($status != 1) {
  86. $this->error = 2015;
  87. return false;
  88. }
  89. // 验证登录密码
  90. if (empty($userPassword) || $userPassword != get_password($password)) {
  91. $this->error = 2017;
  92. return false;
  93. }
  94. // 更新
  95. if (!RedisService::get("caches:members:login_{$userId}")) {
  96. $system = isset($params['system']) ? $params['system'] : [];
  97. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  98. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  99. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  100. $version = isset($system['app_version']) ? $system['app_version'] : '';
  101. $updateData = [
  102. 'update_time' => time(),
  103. 'login_ip' => get_client_ip(),
  104. 'login_time' => time(),
  105. 'device_code' => $uuid,
  106. 'login_count' => DB::raw("login_count+1"),
  107. 'app_version' => $version,
  108. 'device' => $appSources == 'ios' ? 1 : 2,
  109. ];
  110. $this->model->where(['id' => $userId])->update($updateData);
  111. RedisService::set("caches:members:login_{$userId}", $updateData, rand(300, 600));
  112. }
  113. // 获取登录授权token
  114. $jwt = new Jwt('jwt_jd_app');
  115. $token = $jwt->getToken($userId);
  116. // 结果返回
  117. $result = [
  118. 'access_token' => $token,
  119. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  120. ];
  121. // 用户缓存信息
  122. $this->error = 2019;
  123. $data['token'] = $token;
  124. unset($data['password']);
  125. unset($data['mobile']);
  126. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  127. return $result;
  128. }
  129. /**
  130. * 授权登录
  131. * @param $code
  132. * @param array $params
  133. * @return array|false
  134. */
  135. public function mpAuth($code, $params = [])
  136. {
  137. // 账号登录
  138. if (empty($code)) {
  139. $this->error = 1041;
  140. return false;
  141. }
  142. $system = isset($params['system']) ? $params['system'] : [];
  143. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  144. $platform = isset($system['platform']) && $system['platform']?$system['platform'] : 'mp';
  145. // 获取授权信息
  146. $userInfo = MpService::make()->getUserInfo($code, $platform);
  147. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  148. $unionid = isset($userInfo['unionid']) ? $userInfo['unionid'] : '';
  149. $sessionKey = isset($userInfo['session_key']) ? $userInfo['session_key'] : '';
  150. $accessToken = isset($userInfo['access_token']) ? $userInfo['access_token'] : '';
  151. if (empty($userInfo)) {
  152. $this->error = MpService::make()->getError();
  153. return false;
  154. }
  155. if (empty($openid)) {
  156. $this->error = 1042;
  157. return false;
  158. }
  159. // 小程序兼容解码获取unionId
  160. if(empty($unionid) && $platform == 'mp' && $sessionKey){
  161. $loginInfo = isset($params['loginInfo'])?$params['loginInfo']:[];
  162. $encryptedData = isset($loginInfo['encryptedData'])?$loginInfo['encryptedData']:'';
  163. $iv = isset($loginInfo['iv'])?$loginInfo['iv']:'';
  164. $loginInfo = MpService::make()->decryptData($encryptedData,$iv,$sessionKey);
  165. $unionid = isset($loginInfo['unionid'])? $loginInfo['unionid'] : '';
  166. }
  167. // 公众号获取用户信息
  168. $nickname = '';
  169. $avatar = '';
  170. if($platform == 'wechat' && $accessToken){
  171. $loginInfo = MpService::make()->getWechatUserInfo($accessToken, $openid);
  172. $nickname = isset($loginInfo['nickname'])? $loginInfo['nickname'] : '';
  173. $avatar = isset($loginInfo['headimgurl'])? $loginInfo['headimgurl'] : '';
  174. }
  175. // 验证是否注册,没有则注册
  176. $where = ['openid' => $openid];
  177. // 关联账号
  178. if($unionid){
  179. $where['unionid'] = $unionid;
  180. unset($where['openid']);
  181. }
  182. $data = $this->model->where($where)
  183. ->select(['id', 'openid','unionid', 'mobile', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  184. ->first();
  185. $data = $data ? $data->toArray() : [];
  186. $userId = isset($data['id']) ? $data['id'] : 0;
  187. $userAvatar = isset($data['avatar']) ? $data['avatar'] : '';
  188. $nickName = isset($data['nickname']) ? $data['nickname'] : '';
  189. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  190. $unionid = isset($data['unionid']) ? $data['unionid'] : '';
  191. $status = isset($data['status']) ? $data['status'] : 0;
  192. $mark = isset($data['mark']) ? $data['mark'] : 0;
  193. if ($data && $userId && $status != 1 && $mark == 1) {
  194. $this->error = 2011;
  195. return false;
  196. }
  197. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  198. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  199. $version = isset($system['app_version']) ? $system['app_version'] : '';
  200. if (empty($data)) {
  201. $userId = $this->model->max('id') + 1;
  202. // 推荐人
  203. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  204. $parents = '';
  205. if ($rid) {
  206. $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1])
  207. ->select(['id', 'parent_id', 'parents', 'status'])
  208. ->first();
  209. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : '';
  210. if ($inviteInfo) {
  211. $parents = $parents ? $parents . $rid . ',' : ",{$rid},";
  212. }
  213. }
  214. // 下载远程头像
  215. if($platform == 'wechat' && $avatar){
  216. $avatar = save_remote_image($avatar);
  217. }
  218. $data = [
  219. 'nickname' => $nickname? $nickname : '用户' . $userId,
  220. 'openid' => $openid,
  221. 'mobile' => '',
  222. 'avatar' => $avatar,
  223. 'parent_id' => $rid,
  224. 'parents' => $parents,
  225. 'code' => get_random_code(9, 'S', $userId),
  226. 'password' => get_password('a123456'),
  227. 'login_ip' => get_client_ip(),
  228. 'create_time' => time(),
  229. 'login_time' => time(),
  230. 'login_count' => DB::raw("login_count+1"),
  231. 'app_version' => $version,
  232. 'app_uuid' => $uuid,
  233. 'device' => $appSources == 'ios' ? 1 : 2,
  234. ];
  235. if (!$userId = $this->model->insertGetId($data)) {
  236. $this->error = 2012;
  237. return false;
  238. }
  239. } // 更新登录信息
  240. else if ($mark == 0 || !RedisService::get("caches:members:login_{$userId}")) {
  241. $updateData = [
  242. 'login_ip' => get_client_ip(),
  243. 'create_time' => time(),
  244. 'login_time' => time(),
  245. 'app_uuid' => $uuid,
  246. 'login_count' => DB::raw("login_count+1"),
  247. 'app_version' => $version,
  248. 'device' => $appSources == 'ios' ? 1 : 2,
  249. 'mark' => 1,
  250. ];
  251. if ($openid) {
  252. $updateData['openid'] = $openid;
  253. }
  254. if(empty($userAvatar) && $avatar){
  255. // 下载远程头像
  256. if($platform == 'wechat'){
  257. $avatar = save_remote_image($avatar);
  258. }
  259. $updateData['avatar'] = $avatar;
  260. }
  261. if(empty($nickName) && $nickname){
  262. $updateData['nickname'] = $nickname;
  263. }
  264. if ($mark == 0) {
  265. $data['nickname'] = $nickname;
  266. $data['avatar'] = $avatar;
  267. $data['mobile'] = '';
  268. $data['openid'] = $openid;
  269. $data['unionid'] = $unionid;
  270. $nickName = '';
  271. $avatar = '';
  272. $mobile = '';
  273. }
  274. $this->model->where(['id' => $userId])->update($updateData);
  275. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  276. }
  277. // 获取登录授权token
  278. $jwt = new Jwt('jwt_lgx_app');
  279. $token = $jwt->getToken($userId);
  280. // 结果返回
  281. $result = [
  282. 'access_token' => $token,
  283. 'info' => ['uid' => $userId,'unionid'=>$unionid, 'openid' => $openid, 'has_info' => $avatar && $nickName ? 1 : 0, 'mobile' => $mobile],
  284. ];
  285. // 用户缓存信息
  286. $this->error = 2013;
  287. $data['token'] = $token;
  288. unset($data['mobile']);
  289. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  290. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  291. RedisService::clear("caches:storeId:id_{$userId}");
  292. RedisService::clear("caches:storeId:uuid_{$uuid}");
  293. RedisService::clear("caches:storeId:ip_".get_client_ip());
  294. return $result;
  295. }
  296. /**
  297. * 完善资料
  298. * @param $params
  299. * @return array|false
  300. */
  301. public function setProfile($params)
  302. {
  303. $id = isset($params['id'])? $params['id'] : 0;
  304. $code = isset($params['code'])? $params['code'] : '';
  305. $avatar = isset($params['avatar'])? $params['avatar'] : '';
  306. $nickname = isset($params['nickname'])? $params['nickname'] : '';
  307. $phone = isset($params['mobile'])? $params['mobile'] : '';
  308. if($id<=0 || empty($code)){
  309. $this->error = '授权参数错误,请刷新重试';
  310. return false;
  311. }
  312. if(empty($avatar) || empty($nickname)){
  313. $this->error = '请先获取用户授权信息';
  314. return false;
  315. }
  316. $userInfo = $this->model->where(['id'=>$id,'mark'=>1])
  317. ->select(['id as uid', 'nickname', 'openid','avatar'])
  318. ->first();
  319. if(empty($userInfo)){
  320. $this->error = '授权登录失败,请刷新重试';
  321. return false;
  322. }
  323. // 获取手机号信息
  324. if($code){
  325. $phoneData = MpService::make()->getPhoneNumber($code);
  326. $phoneData = isset($phoneData['phone_info']) ? $phoneData['phone_info'] : [];
  327. $phone = isset($phoneData['phoneNumber']) ? $phoneData['phoneNumber'] : '';
  328. if (empty($phone)) {
  329. $this->error = MpService::make()->getError();
  330. return false;
  331. }
  332. }
  333. $avatar = save_base64_image($avatar, 'avatar');
  334. if(!$this->model->where(['id'=>$id])->update(['mobile'=>$phone,'nickname'=>$nickname,'avatar'=>$avatar,'update_time'=>time()])){
  335. $this->error = '获取授权信息失败';
  336. return false;
  337. }
  338. $this->error = '登录成功';
  339. // 获取登录授权token
  340. $jwt = new Jwt('jwt_lgx_app');
  341. $token = $jwt->getToken($id);
  342. return [
  343. 'access_token'=> $token,
  344. 'info'=> $userInfo
  345. ];
  346. }
  347. /**
  348. * 重置密码
  349. * @param $params
  350. * @return array|false
  351. */
  352. public function forget($params)
  353. {
  354. // 账号登录
  355. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  356. $password = isset($params['password']) ? trim($params['password']) : '';
  357. if (empty($params) || empty($mobile) || empty($password)) {
  358. $this->error = 1041;
  359. return false;
  360. }
  361. // 验证码验证
  362. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  363. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  364. $this->error = SmsService::make()->getError();
  365. return false;
  366. }
  367. // 验证是否注册
  368. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  369. $this->error = 1038;
  370. return false;
  371. }
  372. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  373. $this->error = 2030;
  374. return false;
  375. }
  376. // 操作日志
  377. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  378. ActionLogModel::record();
  379. $this->error = 2031;
  380. return true;
  381. }
  382. /**
  383. * 账号注册
  384. * @param $params
  385. * @return array|false
  386. */
  387. public function register($params)
  388. {
  389. // 账号登录
  390. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  391. $password = isset($params['password']) ? trim($params['password']) : '';
  392. $nickname = isset($params['nockname']) ? trim($params['nockname']) : '';
  393. if (empty($params) || empty($mobile) || empty($password)) {
  394. $this->error = 1041;
  395. return false;
  396. }
  397. // 验证码验证
  398. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  399. if (!SmsService::make()->check($mobile, $smsCode, 'register')) {
  400. $this->error = SmsService::make()->getError();
  401. return false;
  402. }
  403. // 验证是否注册
  404. if ($this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  405. $this->error = 2002;
  406. return false;
  407. }
  408. // 驾驶证
  409. $drivingLicense = isset($params['driving_license']) && $params['driving_license'] ? get_image_path($params['driving_license']) : '';
  410. if (empty($drivingLicense)) {
  411. $this->error = 2007;
  412. return false;
  413. }
  414. // 行驶证
  415. $driversLicense = isset($params['drivers_license']) && $params['drivers_license'] ? get_image_path($params['drivers_license']) : '';
  416. if (empty($driversLicense)) {
  417. $this->error = 2008;
  418. return false;
  419. }
  420. $id = $this->model->max('id') + 1;
  421. $system = isset($params['system']) ? $params['system'] : [];
  422. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  423. $appSources = isset($system['app_sources']) ? $system['app_sources'] : '';
  424. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  425. $data = [
  426. 'mobile' => $mobile,
  427. 'user_type' => 2,
  428. 'avatar' => '',
  429. 'nickname' => $nickname ? $nickname : 'DU' . rand(10, 99) . substr($mobile, -6, 6),
  430. 'realname' => isset($params['realname']) ? trim($params['realname']) : '',
  431. 'password' => get_password($password),
  432. 'car_number' => isset($params['car_number']) ? trim($params['car_number']) : '',
  433. 'car_type' => isset($params['car_type']) ? trim($params['car_type']) : '',
  434. 'driving_license' => $drivingLicense,
  435. 'drivers_license' => $driversLicense,
  436. 'code' => strtoupper(get_random_code(9, 'D', "{$id}")),
  437. 'app_uuid' => $uuid,
  438. 'device' => $appSources == 'android' ? 2 : 1,
  439. 'create_time' => time(),
  440. 'status' => 1,
  441. 'picker_status' => 1,
  442. 'confirm_remark' => '',
  443. 'confirm_status' => 2,
  444. 'balance' => 0,
  445. 'deposit' => 0,
  446. 'mark' => 1,
  447. 'login_ip' => get_client_ip(),
  448. ];
  449. if (!$userId = $this->model->insertGetId($data)) {
  450. $this->error = 2003;
  451. return false;
  452. }
  453. // 获取登录授权token
  454. $jwt = new Jwt('jwt_jd_app');
  455. $token = $jwt->getToken($userId);
  456. // 结果返回
  457. $result = [
  458. 'access_token' => $token,
  459. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  460. ];
  461. // 注册成功
  462. $this->error = 2004;
  463. $data['token'] = $token;
  464. unset($data['password']);
  465. unset($data['mobile']);
  466. RedisService::keyDel("caches:members:count*");
  467. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  468. return $result;
  469. }
  470. /**
  471. * 设置资料
  472. * @param $userId
  473. * @param $params
  474. * @return bool
  475. */
  476. public function setEntry($userId, $params)
  477. {
  478. $cacheLockKey = "caches:members:profile_{$userId}";
  479. if (RedisService::get($cacheLockKey)) {
  480. $this->error = 1034;
  481. return false;
  482. }
  483. // 用户验证
  484. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  485. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  486. ->select(['id', 'password', 'status'])
  487. ->first();
  488. if (!$info || $info['status'] != 1) {
  489. $this->error = 1043;
  490. RedisService::clear($cacheLockKey);
  491. return false;
  492. }
  493. // 获取头像
  494. $avatar = '';
  495. if (isset($params['avatar']) && $params['avatar']) {
  496. $avatar = save_base64_image($params['avatar'], 'avatar');
  497. }
  498. //
  499. $data = [
  500. 'avatar' => $avatar,
  501. 'nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  502. 'update_time' => time()
  503. ];
  504. if (isset($params['province']) && $params['city']) {
  505. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  506. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  507. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  508. }
  509. if (!$this->model->where(['id' => $userId])->update($data)) {
  510. $this->error = 1020;
  511. RedisService::clear($cacheLockKey);
  512. return false;
  513. }
  514. $this->error = 1019;
  515. RedisService::clear($cacheLockKey);
  516. return true;
  517. }
  518. /**
  519. * 获取资料详情
  520. * @param $where
  521. * @param array $field
  522. */
  523. public function getInfo($where, array $field = [], $refresh = true)
  524. {
  525. if (empty($where)) {
  526. return false;
  527. }
  528. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  529. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  530. $info = RedisService::get($cacheKey);
  531. if ($info && !$refresh) {
  532. return $info;
  533. }
  534. $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'balance', 'code', 'openid', 'status', 'avatar'];
  535. $field = $field ? $field : $defaultField;
  536. if (is_array($where)) {
  537. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where($where)->select($field)->first();
  538. } else {
  539. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  540. }
  541. $info = $info ? $info->toArray() : [];
  542. if ($info) {
  543. if (isset($info['avatar'])) {
  544. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  545. }
  546. if (isset($info['mobile'])) {
  547. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  548. }
  549. if (isset($info['create_time'])) {
  550. $info['create_at'] = datetime(strtotime($info['create_time']));
  551. }
  552. $info['store'] = isset($info['store']) ? $info['store'] : [];
  553. $info['agent'] = isset($info['agent']) ? $info['agent'] : [];
  554. $info['agent_level'] = 0;
  555. $info['team_count'] = 0;
  556. $params = request()->all();
  557. $type = isset($params['type'])?$params['type']:'';
  558. if ($type == 'agent' && $info['agent']) {
  559. $info['agent_level'] = $this->getAgentLevel($info['id']);
  560. $info['agent_level'] = $info['agent_level']>=2?2 : 1;
  561. $info['team_count'] = $this->getTeamCount($info['id']);
  562. }
  563. if($type == 'agent'){
  564. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/login/login',"rid={$info['id']}");
  565. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  566. }else if($type == 'center'){
  567. $info['order1'] = OrderService::make()->getCountByStatus($info['id'], 1);
  568. $info['cart_count'] = CartService::make()->getCount($info['id']);
  569. $info['social_open'] = ConfigService::make()->getConfigByCode('social_open',0);
  570. }
  571. RedisService::set($cacheKey, $info, rand(30, 60));
  572. }
  573. return $info;
  574. }
  575. /**
  576. * 收款账户
  577. * @param $userId
  578. * @return array|mixed
  579. */
  580. public function accountInfo($userId,$type=0)
  581. {
  582. $cacheKey = "caches:members:account:{$userId}_{$type}";
  583. $datas = RedisService::get($cacheKey);
  584. if ($datas) {
  585. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  586. }
  587. $datas[0] = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'status'=>1,'mark'=>1])
  588. ->select(['id','user_id','type','realname','account','account_remark','status'])
  589. ->first();
  590. $datas[0] = $datas[0]? $datas[0] : ['id'=>0,'type'=>1];
  591. $datas[1] = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'status'=>1,'mark'=>1])
  592. ->select(['id','user_id','type','realname','account','account_remark','status'])
  593. ->first();
  594. $datas[1] = $datas[1]? $datas[1] : ['id'=>0,'type'=>2];
  595. if($datas){
  596. RedisService::set($cacheKey, $datas, rand(5,10));
  597. }else{
  598. $datas = [['id'=>0,'type'=>1],['id'=>0,'type'=>2]];
  599. }
  600. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  601. }
  602. /**
  603. * 绑定收款账户
  604. * @param $userId
  605. * @return array|mixed
  606. */
  607. public function bindAccount($userId, $params)
  608. {
  609. if($params['type']==1){
  610. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  611. ->select(['id','user_id','type','realname','account','account_remark','status'])
  612. ->first();
  613. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  614. $data = [
  615. 'type'=> 1,
  616. 'user_id'=> $userId,
  617. 'realname'=>$params['realname'],
  618. 'account'=>$params['account'],
  619. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  620. 'status'=>1
  621. ];
  622. if($alipayId){
  623. $data['update_time']=time();
  624. MemberBankModel::where(['id'=>$alipayId])->update($data);
  625. }else {
  626. $data['create_time']=time();
  627. MemberBankModel::insertGetId($data);
  628. }
  629. } else if($params['type']==2){
  630. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  631. ->select(['id','user_id','type','realname','account','account_remark','status'])
  632. ->first();
  633. $bankId = isset($banks['id'])?$banks['id'] : 0;
  634. $data = [
  635. 'type'=>2,
  636. 'user_id'=> $userId,
  637. 'realname'=>$params['realname'],
  638. 'account'=>$params['account'],
  639. 'account_remark'=>$params['account_remark'],
  640. 'status'=>1
  641. ];
  642. if($bankId){
  643. $data['update_time']=time();
  644. MemberBankModel::where(['id'=>$bankId])->update($data);
  645. }else {
  646. $data['create_time']=time();
  647. MemberBankModel::insertGetId($data);
  648. }
  649. }else{
  650. $this->error = '账号类型错误';
  651. return false;
  652. }
  653. RedisService::keyDel("caches:members:account:{$userId}*");
  654. $this->error = '绑定收款账号成功';
  655. return true;
  656. }
  657. /**
  658. * 获取代理等级
  659. * @param $uid
  660. * @return array|int|mixed
  661. */
  662. public function getAgentLevel($uid)
  663. {
  664. $cacheKey = "caches:members:agentLevel:{$uid}";
  665. $data = RedisService::get($cacheKey);
  666. if ($data) {
  667. return $data;
  668. }
  669. $data = $this->model->from('member as a')
  670. ->leftJoin('agents as b', function ($join) {
  671. $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]);
  672. })
  673. ->where('b.id', '>', 0)
  674. ->where('a.parents', 'like', "%,{$uid},%")
  675. ->where(['a.status' => 1, 'a.mark' => 1])
  676. ->select(['a.id', 'a.parents'])
  677. ->orderBy('a.parents', 'desc')
  678. ->first();
  679. $data = $data ? $data->toArray() : [];
  680. $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : '';
  681. $parents = $parents ? explode(',', $parents) : [];
  682. $level = $parents ? count($parents) : 0;
  683. if($level){
  684. RedisService::set($cacheKey, $level, rand(5,10));
  685. }
  686. return $level;
  687. }
  688. /**
  689. * 团队人数
  690. * @param $uid
  691. * @return array|int|mixed
  692. */
  693. public function getTeamCount($uid)
  694. {
  695. $cacheKey = "caches:members:teamCount:{$uid}";
  696. $data = RedisService::get($cacheKey);
  697. if ($data) {
  698. return $data;
  699. }
  700. $data = $this->model->from('member as a')
  701. ->where('a.parents', 'like', "%,{$uid},%")
  702. ->where(['a.status' => 1, 'a.mark' => 1])
  703. ->count('id');
  704. if($data){
  705. RedisService::set($cacheKey, $data, rand(5,10));
  706. }
  707. return $data;
  708. }
  709. /**
  710. * 生成普通参数二维码
  711. * @param $str 参数
  712. * @param bool $refresh 是否重新生成
  713. * @return bool
  714. */
  715. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  716. {
  717. $basePath = base_path() . '/public';
  718. $qrFile = '/images/qrcode/';
  719. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  720. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  721. }
  722. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  723. $qrFile = $qrFile . "C_{$key}.png";
  724. $cacheKey = "caches:qrcodes:member_" . $key;
  725. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  726. return $qrFile;
  727. }
  728. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  729. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  730. return false;
  731. }
  732. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  733. return $qrFile;
  734. }
  735. /**
  736. * 修改头像
  737. * @param $userId
  738. * @param $avatar
  739. * @return mixed
  740. */
  741. public function saveAvatar($userId, $avatar)
  742. {
  743. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  744. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  745. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  746. @unlink(ATTACHMENT_PATH . $oldAvatar);
  747. }
  748. return true;
  749. }
  750. return false;
  751. }
  752. /**
  753. * 修改账号信息
  754. * @param $userId
  755. * @param $params
  756. * @return bool
  757. */
  758. public function modify($userId, $params)
  759. {
  760. $cacheLockKey = "caches:members:modify_{$userId}";
  761. if (RedisService::get($cacheLockKey)) {
  762. $this->error = 1034;
  763. return false;
  764. }
  765. // 用户验证
  766. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  767. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  768. ->select(['id', 'password', 'status'])
  769. ->first();
  770. $userPassword = isset($info['password']) ? $info['password'] : '';
  771. if (!$info || $info['status'] != 1) {
  772. $this->error = 1029;
  773. RedisService::clear($cacheLockKey);
  774. return false;
  775. }
  776. // 密码校验
  777. $data = ['update_time' => time()];
  778. // 修改数据
  779. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  780. if (isset($params['nickname']) && $nickname) {
  781. $data['nickname'] = $nickname;
  782. }
  783. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  784. if (isset($params['mobile']) && $mobile) {
  785. $data['mobile'] = $mobile;
  786. }
  787. $address = isset($params['address']) ? $params['address'] : '';
  788. if (isset($params['address']) && $address) {
  789. $data['address'] = $address;
  790. }
  791. $password = isset($params['password']) ? $params['password'] : '';
  792. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  793. if (isset($params['password']) && $password) {
  794. if ($userPassword != get_password($password)) {
  795. $this->error = 1038;
  796. RedisService::clear($cacheLockKey);
  797. return false;
  798. }
  799. if (empty($newPassword)) {
  800. $this->error = 1039;
  801. RedisService::clear($cacheLockKey);
  802. return false;
  803. }
  804. $data['password'] = get_password($newPassword);
  805. }
  806. // 头像
  807. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  808. if (isset($params['avatar']) && $avatar) {
  809. $data['avatar'] = get_image_path($avatar);
  810. }
  811. if (!$this->model->where(['id' => $userId])->update($data)) {
  812. $this->error = 1014;
  813. RedisService::clear($cacheLockKey);
  814. return false;
  815. }
  816. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  817. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  818. @unlink(ATTACHMENT_PATH . $oldAvatar);
  819. }
  820. $this->error = 1013;
  821. RedisService::clear($cacheLockKey);
  822. return true;
  823. }
  824. /**
  825. * 账号注销
  826. * @param $userId
  827. * @return bool
  828. */
  829. public function logOff($userId)
  830. {
  831. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  832. ->select(['id', 'password', 'status'])
  833. ->first();
  834. $status = isset($info['status']) ? $info['status'] : 0;
  835. if (empty($info)) {
  836. $this->error = 2044;
  837. return false;
  838. }
  839. if ($status != 1) {
  840. $this->error = 2044;
  841. return false;
  842. }
  843. if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  844. $this->error = 2045;
  845. return false;
  846. }
  847. if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  848. $this->error = 2046;
  849. return false;
  850. }
  851. if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) {
  852. $this->error = 2047;
  853. return false;
  854. }
  855. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  856. $this->error = 2049;
  857. return false;
  858. }
  859. $this->error = 2048;
  860. RedisService::clear("auths:info:" . $userId);
  861. return true;
  862. }
  863. }