MemberService.php 30 KB

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