MemberService.php 34 KB

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