MemberService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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(180, 300));
  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. return $result;
  256. }
  257. /**
  258. * 重置密码
  259. * @param $params
  260. * @return array|false
  261. */
  262. public function forget($params)
  263. {
  264. // 账号登录
  265. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  266. $password = isset($params['password']) ? trim($params['password']) : '';
  267. if (empty($params) || empty($mobile) || empty($password)) {
  268. $this->error = 1041;
  269. return false;
  270. }
  271. // 验证码验证
  272. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  273. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  274. $this->error = SmsService::make()->getError();
  275. return false;
  276. }
  277. // 验证是否注册
  278. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  279. $this->error = 1038;
  280. return false;
  281. }
  282. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  283. $this->error = 2030;
  284. return false;
  285. }
  286. // 操作日志
  287. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  288. ActionLogModel::record();
  289. $this->error = 2031;
  290. return true;
  291. }
  292. /**
  293. * 账号注册
  294. * @param $params
  295. * @return array|false
  296. */
  297. public function register($params)
  298. {
  299. // 账号登录
  300. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  301. $password = isset($params['password']) ? trim($params['password']) : '';
  302. $nickname = isset($params['nockname']) ? trim($params['nockname']) : '';
  303. if (empty($params) || empty($mobile) || empty($password)) {
  304. $this->error = 1041;
  305. return false;
  306. }
  307. // 验证码验证
  308. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  309. if (!SmsService::make()->check($mobile, $smsCode, 'register')) {
  310. $this->error = SmsService::make()->getError();
  311. return false;
  312. }
  313. // 验证是否注册
  314. if ($this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  315. $this->error = 2002;
  316. return false;
  317. }
  318. // 驾驶证
  319. $drivingLicense = isset($params['driving_license']) && $params['driving_license'] ? get_image_path($params['driving_license']) : '';
  320. if (empty($drivingLicense)) {
  321. $this->error = 2007;
  322. return false;
  323. }
  324. // 行驶证
  325. $driversLicense = isset($params['drivers_license']) && $params['drivers_license'] ? get_image_path($params['drivers_license']) : '';
  326. if (empty($driversLicense)) {
  327. $this->error = 2008;
  328. return false;
  329. }
  330. $id = $this->model->max('id') + 1;
  331. $system = isset($params['system']) ? $params['system'] : [];
  332. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  333. $appSources = isset($system['app_sources']) ? $system['app_sources'] : '';
  334. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  335. $data = [
  336. 'mobile' => $mobile,
  337. 'user_type' => 2,
  338. 'avatar' => '',
  339. 'nickname' => $nickname ? $nickname : 'DU' . rand(10, 99) . substr($mobile, -6, 6),
  340. 'realname' => isset($params['realname']) ? trim($params['realname']) : '',
  341. 'password' => get_password($password),
  342. 'car_number' => isset($params['car_number']) ? trim($params['car_number']) : '',
  343. 'car_type' => isset($params['car_type']) ? trim($params['car_type']) : '',
  344. 'driving_license' => $drivingLicense,
  345. 'drivers_license' => $driversLicense,
  346. 'code' => strtoupper(get_random_code(9, 'D', "{$id}")),
  347. 'app_uuid' => $uuid,
  348. 'device' => $appSources == 'android' ? 2 : 1,
  349. 'create_time' => time(),
  350. 'status' => 1,
  351. 'picker_status' => 1,
  352. 'confirm_remark' => '',
  353. 'confirm_status' => 2,
  354. 'balance' => 0,
  355. 'deposit' => 0,
  356. 'mark' => 1,
  357. 'login_ip' => get_client_ip(),
  358. ];
  359. if (!$userId = $this->model->insertGetId($data)) {
  360. $this->error = 2003;
  361. return false;
  362. }
  363. // 获取登录授权token
  364. $jwt = new Jwt('jwt_jd_app');
  365. $token = $jwt->getToken($userId);
  366. // 结果返回
  367. $result = [
  368. 'access_token' => $token,
  369. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  370. ];
  371. // 注册成功
  372. $this->error = 2004;
  373. $data['token'] = $token;
  374. unset($data['password']);
  375. unset($data['mobile']);
  376. RedisService::keyDel("caches:members:count*");
  377. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  378. return $result;
  379. }
  380. /**
  381. * 设置资料
  382. * @param $userId
  383. * @param $params
  384. * @return bool
  385. */
  386. public function setEntry($userId, $params)
  387. {
  388. $cacheLockKey = "caches:members:profile_{$userId}";
  389. if (RedisService::get($cacheLockKey)) {
  390. $this->error = 1034;
  391. return false;
  392. }
  393. // 用户验证
  394. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  395. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  396. ->select(['id', 'password', 'status'])
  397. ->first();
  398. if (!$info || $info['status'] != 1) {
  399. $this->error = 1043;
  400. RedisService::clear($cacheLockKey);
  401. return false;
  402. }
  403. // 获取头像
  404. $avatar = '';
  405. if (isset($params['avatar']) && $params['avatar']) {
  406. $avatar = save_base64_image($params['avatar'], 'avatar');
  407. }
  408. //
  409. $data = [
  410. 'avatar' => $avatar,
  411. 'nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  412. 'update_time' => time()
  413. ];
  414. if (isset($params['province']) && $params['city']) {
  415. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  416. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  417. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  418. }
  419. if (!$this->model->where(['id' => $userId])->update($data)) {
  420. $this->error = 1020;
  421. RedisService::clear($cacheLockKey);
  422. return false;
  423. }
  424. $this->error = 1019;
  425. RedisService::clear($cacheLockKey);
  426. return true;
  427. }
  428. /**
  429. * 获取资料详情
  430. * @param $where
  431. * @param array $field
  432. */
  433. public function getInfo($where, array $field = [], $refresh = true)
  434. {
  435. if (empty($where)) {
  436. return false;
  437. }
  438. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  439. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  440. $info = RedisService::get($cacheKey);
  441. if ($info && !$refresh) {
  442. return $info;
  443. }
  444. $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'balance', 'code', 'openid', 'status', 'avatar'];
  445. $field = $field ? $field : $defaultField;
  446. if (is_array($where)) {
  447. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where($where)->select($field)->first();
  448. } else {
  449. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  450. }
  451. $info = $info ? $info->toArray() : [];
  452. if ($info) {
  453. if (isset($info['avatar'])) {
  454. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  455. }
  456. if (isset($info['mobile'])) {
  457. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  458. }
  459. if (isset($info['create_time'])) {
  460. $info['create_at'] = datetime(strtotime($info['create_time']));
  461. }
  462. $info['store'] = isset($info['store']) ? $info['store'] : [];
  463. $info['agent'] = isset($info['agent']) ? $info['agent'] : [];
  464. $info['agent_level'] = 0;
  465. $info['team_count'] = 0;
  466. $params = request()->all();
  467. $type = isset($params['type'])?$params['type']:'';
  468. if ($type == 'agent' && $info['agent']) {
  469. $info['agent_level'] = $this->getAgentLevel($info['id']);
  470. $info['agent_level'] = $info['agent_level']>=2?2 : 1;
  471. $info['team_count'] = $this->getTeamCount($info['id']);
  472. }
  473. if($type == 'agent'){
  474. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/login/login',"rid={$info['id']}");
  475. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  476. }
  477. RedisService::set($cacheKey, $info, rand(30, 60));
  478. }
  479. return $info;
  480. }
  481. /**
  482. * 收款账户
  483. * @param $userId
  484. * @return array|mixed
  485. */
  486. public function accountInfo($userId,$type=0)
  487. {
  488. $cacheKey = "caches:members:account:{$userId}_{$type}";
  489. $datas = RedisService::get($cacheKey);
  490. if ($datas) {
  491. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  492. }
  493. $datas[0] = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'status'=>1,'mark'=>1])
  494. ->select(['id','user_id','type','realname','account','account_remark','status'])
  495. ->first();
  496. $datas[0] = $datas[0]? $datas[0] : ['id'=>0,'type'=>1];
  497. $datas[1] = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'status'=>1,'mark'=>1])
  498. ->select(['id','user_id','type','realname','account','account_remark','status'])
  499. ->first();
  500. $datas[1] = $datas[1]? $datas[1] : ['id'=>0,'type'=>2];
  501. if($datas){
  502. RedisService::set($cacheKey, $datas, rand(5,10));
  503. }else{
  504. $datas = [['id'=>0,'type'=>1],['id'=>0,'type'=>2]];
  505. }
  506. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  507. }
  508. /**
  509. * 绑定收款账户
  510. * @param $userId
  511. * @return array|mixed
  512. */
  513. public function bindAccount($userId, $params)
  514. {
  515. if($params['type']==1){
  516. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  517. ->select(['id','user_id','type','realname','account','account_remark','status'])
  518. ->first();
  519. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  520. $data = [
  521. 'type'=> 1,
  522. 'user_id'=> $userId,
  523. 'realname'=>$params['realname'],
  524. 'account'=>$params['account'],
  525. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  526. 'status'=>1
  527. ];
  528. if($alipayId){
  529. $data['update_time']=time();
  530. MemberBankModel::where(['id'=>$alipayId])->update($data);
  531. }else {
  532. $data['create_time']=time();
  533. MemberBankModel::insertGetId($data);
  534. }
  535. } else if($params['type']==2){
  536. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  537. ->select(['id','user_id','type','realname','account','account_remark','status'])
  538. ->first();
  539. $bankId = isset($banks['id'])?$banks['id'] : 0;
  540. $data = [
  541. 'type'=>2,
  542. 'user_id'=> $userId,
  543. 'realname'=>$params['realname'],
  544. 'account'=>$params['account'],
  545. 'account_remark'=>$params['account_remark'],
  546. 'status'=>1
  547. ];
  548. if($bankId){
  549. $data['update_time']=time();
  550. MemberBankModel::where(['id'=>$bankId])->update($data);
  551. }else {
  552. $data['create_time']=time();
  553. MemberBankModel::insertGetId($data);
  554. }
  555. }else{
  556. $this->error = '账号类型错误';
  557. return false;
  558. }
  559. RedisService::keyDel("caches:members:account:{$userId}*");
  560. $this->error = '绑定收款账号成功';
  561. return true;
  562. }
  563. /**
  564. * 获取代理等级
  565. * @param $uid
  566. * @return array|int|mixed
  567. */
  568. public function getAgentLevel($uid)
  569. {
  570. $cacheKey = "caches:members:agentLevel:{$uid}";
  571. $data = RedisService::get($cacheKey);
  572. if ($data) {
  573. return $data;
  574. }
  575. $data = $this->model->from('member as a')
  576. ->leftJoin('agents as b', function ($join) {
  577. $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]);
  578. })
  579. ->where('b.id', '>', 0)
  580. ->where('a.parents', 'like', "%,{$uid},%")
  581. ->where(['a.status' => 1, 'a.mark' => 1])
  582. ->select(['a.id', 'a.parents'])
  583. ->orderBy('a.parents', 'desc')
  584. ->first();
  585. $data = $data ? $data->toArray() : [];
  586. $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : '';
  587. $parents = $parents ? explode(',', $parents) : [];
  588. $level = $parents ? count($parents) : 0;
  589. if($level){
  590. RedisService::set($cacheKey, $level, rand(5,10));
  591. }
  592. return $level;
  593. }
  594. /**
  595. * 团队人数
  596. * @param $uid
  597. * @return array|int|mixed
  598. */
  599. public function getTeamCount($uid)
  600. {
  601. $cacheKey = "caches:members:teamCount:{$uid}";
  602. $data = RedisService::get($cacheKey);
  603. if ($data) {
  604. return $data;
  605. }
  606. $data = $this->model->from('member as a')
  607. ->where('a.parents', 'like', "%,{$uid},%")
  608. ->where(['a.status' => 1, 'a.mark' => 1])
  609. ->count('id');
  610. if($data){
  611. RedisService::set($cacheKey, $data, rand(5,10));
  612. }
  613. return $data;
  614. }
  615. /**
  616. * 生成普通参数二维码
  617. * @param $str 参数
  618. * @param bool $refresh 是否重新生成
  619. * @return bool
  620. */
  621. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  622. {
  623. $basePath = base_path() . '/public';
  624. $qrFile = '/images/qrcode/';
  625. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  626. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  627. }
  628. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  629. $qrFile = $qrFile . "C_{$key}.png";
  630. $cacheKey = "caches:qrcodes:member_" . $key;
  631. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  632. return $qrFile;
  633. }
  634. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  635. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  636. return false;
  637. }
  638. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  639. return $qrFile;
  640. }
  641. /**
  642. * 修改头像
  643. * @param $userId
  644. * @param $avatar
  645. * @return mixed
  646. */
  647. public function saveAvatar($userId, $avatar)
  648. {
  649. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  650. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  651. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  652. @unlink(ATTACHMENT_PATH . $oldAvatar);
  653. }
  654. return true;
  655. }
  656. return false;
  657. }
  658. /**
  659. * 修改账号信息
  660. * @param $userId
  661. * @param $params
  662. * @return bool
  663. */
  664. public function modify($userId, $params)
  665. {
  666. $cacheLockKey = "caches:members:modify_{$userId}";
  667. if (RedisService::get($cacheLockKey)) {
  668. $this->error = 1034;
  669. return false;
  670. }
  671. // 用户验证
  672. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  673. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  674. ->select(['id', 'password', 'status'])
  675. ->first();
  676. $userPassword = isset($info['password']) ? $info['password'] : '';
  677. if (!$info || $info['status'] != 1) {
  678. $this->error = 1029;
  679. RedisService::clear($cacheLockKey);
  680. return false;
  681. }
  682. // 密码校验
  683. $data = ['update_time' => time()];
  684. // 修改数据
  685. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  686. if (isset($params['nickname']) && $nickname) {
  687. $data['nickname'] = $nickname;
  688. }
  689. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  690. if (isset($params['mobile']) && $mobile) {
  691. $data['mobile'] = $mobile;
  692. }
  693. $address = isset($params['address']) ? $params['address'] : '';
  694. if (isset($params['address']) && $address) {
  695. $data['address'] = $address;
  696. }
  697. $password = isset($params['password']) ? $params['password'] : '';
  698. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  699. if (isset($params['password']) && $password) {
  700. if ($userPassword != get_password($password)) {
  701. $this->error = 1038;
  702. RedisService::clear($cacheLockKey);
  703. return false;
  704. }
  705. if (empty($newPassword)) {
  706. $this->error = 1039;
  707. RedisService::clear($cacheLockKey);
  708. return false;
  709. }
  710. $data['password'] = get_password($newPassword);
  711. }
  712. // 头像
  713. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  714. if (isset($params['avatar']) && $avatar) {
  715. $data['avatar'] = get_image_path($avatar);
  716. }
  717. if (!$this->model->where(['id' => $userId])->update($data)) {
  718. $this->error = 1014;
  719. RedisService::clear($cacheLockKey);
  720. return false;
  721. }
  722. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  723. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  724. @unlink(ATTACHMENT_PATH . $oldAvatar);
  725. }
  726. $this->error = 1013;
  727. RedisService::clear($cacheLockKey);
  728. return true;
  729. }
  730. /**
  731. * 账号注销
  732. * @param $userId
  733. * @return bool
  734. */
  735. public function logOff($userId)
  736. {
  737. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  738. ->select(['id', 'password', 'status'])
  739. ->first();
  740. $status = isset($info['status']) ? $info['status'] : 0;
  741. if (empty($info)) {
  742. $this->error = 2044;
  743. return false;
  744. }
  745. if ($status != 1) {
  746. $this->error = 2044;
  747. return false;
  748. }
  749. if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  750. $this->error = 2045;
  751. return false;
  752. }
  753. if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  754. $this->error = 2046;
  755. return false;
  756. }
  757. if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) {
  758. $this->error = 2047;
  759. return false;
  760. }
  761. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  762. $this->error = 2049;
  763. return false;
  764. }
  765. $this->error = 2048;
  766. RedisService::clear("auths:info:" . $userId);
  767. return true;
  768. }
  769. }