MemberService.php 33 KB

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