MemberService.php 30 KB

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