MemberService.php 34 KB

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