MemberService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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_jd_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_jd_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. }
  481. RedisService::set($cacheKey, $info, rand(30, 60));
  482. }
  483. return $info;
  484. }
  485. /**
  486. * 收款账户
  487. * @param $userId
  488. * @return array|mixed
  489. */
  490. public function accountInfo($userId,$type=0)
  491. {
  492. $cacheKey = "caches:members:account:{$userId}_{$type}";
  493. $datas = RedisService::get($cacheKey);
  494. if ($datas) {
  495. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  496. }
  497. $datas[0] = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'status'=>1,'mark'=>1])
  498. ->select(['id','user_id','type','realname','account','account_remark','status'])
  499. ->first();
  500. $datas[0] = $datas[0]? $datas[0] : ['id'=>0,'type'=>1];
  501. $datas[1] = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'status'=>1,'mark'=>1])
  502. ->select(['id','user_id','type','realname','account','account_remark','status'])
  503. ->first();
  504. $datas[1] = $datas[1]? $datas[1] : ['id'=>0,'type'=>2];
  505. if($datas){
  506. RedisService::set($cacheKey, $datas, rand(5,10));
  507. }else{
  508. $datas = [['id'=>0,'type'=>1],['id'=>0,'type'=>2]];
  509. }
  510. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  511. }
  512. /**
  513. * 绑定收款账户
  514. * @param $userId
  515. * @return array|mixed
  516. */
  517. public function bindAccount($userId, $params)
  518. {
  519. if($params['type']==1){
  520. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  521. ->select(['id','user_id','type','realname','account','account_remark','status'])
  522. ->first();
  523. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  524. $data = [
  525. 'type'=> 1,
  526. 'user_id'=> $userId,
  527. 'realname'=>$params['realname'],
  528. 'account'=>$params['account'],
  529. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  530. 'status'=>1
  531. ];
  532. if($alipayId){
  533. $data['update_time']=time();
  534. MemberBankModel::where(['id'=>$alipayId])->update($data);
  535. }else {
  536. $data['create_time']=time();
  537. MemberBankModel::insertGetId($data);
  538. }
  539. } else if($params['type']==2){
  540. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  541. ->select(['id','user_id','type','realname','account','account_remark','status'])
  542. ->first();
  543. $bankId = isset($banks['id'])?$banks['id'] : 0;
  544. $data = [
  545. 'type'=>2,
  546. 'user_id'=> $userId,
  547. 'realname'=>$params['realname'],
  548. 'account'=>$params['account'],
  549. 'account_remark'=>$params['account_remark'],
  550. 'status'=>1
  551. ];
  552. if($bankId){
  553. $data['update_time']=time();
  554. MemberBankModel::where(['id'=>$bankId])->update($data);
  555. }else {
  556. $data['create_time']=time();
  557. MemberBankModel::insertGetId($data);
  558. }
  559. }else{
  560. $this->error = '账号类型错误';
  561. return false;
  562. }
  563. RedisService::keyDel("caches:members:account:{$userId}*");
  564. $this->error = '绑定收款账号成功';
  565. return true;
  566. }
  567. /**
  568. * 获取代理等级
  569. * @param $uid
  570. * @return array|int|mixed
  571. */
  572. public function getAgentLevel($uid)
  573. {
  574. $cacheKey = "caches:members:agentLevel:{$uid}";
  575. $data = RedisService::get($cacheKey);
  576. if ($data) {
  577. return $data;
  578. }
  579. $data = $this->model->from('member as a')
  580. ->leftJoin('agents as b', function ($join) {
  581. $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]);
  582. })
  583. ->where('b.id', '>', 0)
  584. ->where('a.parents', 'like', "%,{$uid},%")
  585. ->where(['a.status' => 1, 'a.mark' => 1])
  586. ->select(['a.id', 'a.parents'])
  587. ->orderBy('a.parents', 'desc')
  588. ->first();
  589. $data = $data ? $data->toArray() : [];
  590. $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : '';
  591. $parents = $parents ? explode(',', $parents) : [];
  592. $level = $parents ? count($parents) : 0;
  593. if($level){
  594. RedisService::set($cacheKey, $level, rand(5,10));
  595. }
  596. return $level;
  597. }
  598. /**
  599. * 团队人数
  600. * @param $uid
  601. * @return array|int|mixed
  602. */
  603. public function getTeamCount($uid)
  604. {
  605. $cacheKey = "caches:members:teamCount:{$uid}";
  606. $data = RedisService::get($cacheKey);
  607. if ($data) {
  608. return $data;
  609. }
  610. $data = $this->model->from('member as a')
  611. ->where('a.parents', 'like', "%,{$uid},%")
  612. ->where(['a.status' => 1, 'a.mark' => 1])
  613. ->count('id');
  614. if($data){
  615. RedisService::set($cacheKey, $data, rand(5,10));
  616. }
  617. return $data;
  618. }
  619. /**
  620. * 生成普通参数二维码
  621. * @param $str 参数
  622. * @param bool $refresh 是否重新生成
  623. * @return bool
  624. */
  625. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  626. {
  627. $basePath = base_path() . '/public';
  628. $qrFile = '/images/qrcode/';
  629. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  630. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  631. }
  632. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  633. $qrFile = $qrFile . "C_{$key}.png";
  634. $cacheKey = "caches:qrcodes:member_" . $key;
  635. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  636. return $qrFile;
  637. }
  638. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  639. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  640. return false;
  641. }
  642. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  643. return $qrFile;
  644. }
  645. /**
  646. * 修改头像
  647. * @param $userId
  648. * @param $avatar
  649. * @return mixed
  650. */
  651. public function saveAvatar($userId, $avatar)
  652. {
  653. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  654. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  655. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  656. @unlink(ATTACHMENT_PATH . $oldAvatar);
  657. }
  658. return true;
  659. }
  660. return false;
  661. }
  662. /**
  663. * 修改账号信息
  664. * @param $userId
  665. * @param $params
  666. * @return bool
  667. */
  668. public function modify($userId, $params)
  669. {
  670. $cacheLockKey = "caches:members:modify_{$userId}";
  671. if (RedisService::get($cacheLockKey)) {
  672. $this->error = 1034;
  673. return false;
  674. }
  675. // 用户验证
  676. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  677. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  678. ->select(['id', 'password', 'status'])
  679. ->first();
  680. $userPassword = isset($info['password']) ? $info['password'] : '';
  681. if (!$info || $info['status'] != 1) {
  682. $this->error = 1029;
  683. RedisService::clear($cacheLockKey);
  684. return false;
  685. }
  686. // 密码校验
  687. $data = ['update_time' => time()];
  688. // 修改数据
  689. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  690. if (isset($params['nickname']) && $nickname) {
  691. $data['nickname'] = $nickname;
  692. }
  693. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  694. if (isset($params['mobile']) && $mobile) {
  695. $data['mobile'] = $mobile;
  696. }
  697. $address = isset($params['address']) ? $params['address'] : '';
  698. if (isset($params['address']) && $address) {
  699. $data['address'] = $address;
  700. }
  701. $password = isset($params['password']) ? $params['password'] : '';
  702. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  703. if (isset($params['password']) && $password) {
  704. if ($userPassword != get_password($password)) {
  705. $this->error = 1038;
  706. RedisService::clear($cacheLockKey);
  707. return false;
  708. }
  709. if (empty($newPassword)) {
  710. $this->error = 1039;
  711. RedisService::clear($cacheLockKey);
  712. return false;
  713. }
  714. $data['password'] = get_password($newPassword);
  715. }
  716. // 头像
  717. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  718. if (isset($params['avatar']) && $avatar) {
  719. $data['avatar'] = get_image_path($avatar);
  720. }
  721. if (!$this->model->where(['id' => $userId])->update($data)) {
  722. $this->error = 1014;
  723. RedisService::clear($cacheLockKey);
  724. return false;
  725. }
  726. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  727. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  728. @unlink(ATTACHMENT_PATH . $oldAvatar);
  729. }
  730. $this->error = 1013;
  731. RedisService::clear($cacheLockKey);
  732. return true;
  733. }
  734. /**
  735. * 账号注销
  736. * @param $userId
  737. * @return bool
  738. */
  739. public function logOff($userId)
  740. {
  741. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  742. ->select(['id', 'password', 'status'])
  743. ->first();
  744. $status = isset($info['status']) ? $info['status'] : 0;
  745. if (empty($info)) {
  746. $this->error = 2044;
  747. return false;
  748. }
  749. if ($status != 1) {
  750. $this->error = 2044;
  751. return false;
  752. }
  753. if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  754. $this->error = 2045;
  755. return false;
  756. }
  757. if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  758. $this->error = 2046;
  759. return false;
  760. }
  761. if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) {
  762. $this->error = 2047;
  763. return false;
  764. }
  765. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  766. $this->error = 2049;
  767. return false;
  768. }
  769. $this->error = 2048;
  770. RedisService::clear("auths:info:" . $userId);
  771. return true;
  772. }
  773. }