MemberService.php 35 KB

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