MemberService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\Common\MemberSettingService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use App\Services\UsdtWalletService;
  20. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  21. use Illuminate\Support\Facades\DB;
  22. use phpQrcode\QRcode;
  23. /**
  24. * 会员-服务类
  25. * Class MemberService
  26. * @package App\Services\Api
  27. */
  28. class MemberService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @since 2020/11/10
  35. * MemberService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new MemberModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 获取资料详情
  54. * @param $where
  55. * @param array $field
  56. */
  57. public function getInfo($where, array $field = [])
  58. {
  59. $field = $field ? $field : ['id', 'username', 'realname', 'nickname', 'openid', 'idcard','google_secret', 'trade_password', 'trc_address', 'erc_address','erc_hexaddress', 'source', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'safe_level', 'user_type', 'member_level', 'usdt_num', 'user_type', 'status', 'credit', 'avatar'];
  60. if (is_array($where)) {
  61. $info = $this->model->where($where)->select($field)->first();
  62. } else {
  63. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  64. }
  65. $info = $info ? $info->toArray() : [];
  66. if ($info) {
  67. $tradePrice = ConfigService::make()->getConfigByCode('usdt_sell_price');
  68. $tradePrice = $tradePrice? $tradePrice : 0;
  69. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  70. $info['idcard_front_img'] = $info['idcard_front_img'] ? get_image_url($info['idcard_front_img']) : '';
  71. $info['idcard_back_img'] = $info['idcard_back_img'] ? get_image_url($info['idcard_back_img']) : '';
  72. $info['usdt_num'] = isset($info['usdt_num']) ? moneyFormat($info['usdt_num'], 4) : '0.0000';
  73. $info['cny_num'] = $info['usdt_num'] && $tradePrice>0? moneyFormat($info['usdt_num']*$tradePrice, 6) : '0.0000';
  74. $info['is_collection'] = MemberPaymentService::make()->checkHasByUser($info['id']);
  75. $info['set_trade_password'] = $info['trade_password']? 1 : 0;
  76. unset($info['trade_password']);
  77. // 收款二维码
  78. $qrcode = $this->makeQrcode($info['trc_address']);
  79. $info['trc_qrcode'] = $qrcode? get_image_url($qrcode) : '';
  80. $ercQrcode = $this->makeQrcode($info['erc_hexaddress']);
  81. $info['erc_qrcode'] = $ercQrcode? get_image_url($ercQrcode) : '';
  82. }
  83. return $info;
  84. }
  85. /**
  86. * 用户注册
  87. * @param $params
  88. * @return bool
  89. */
  90. public function register($params)
  91. {
  92. // 检测账号是否存在
  93. if ($this->checkExists('username', $params['username'])) {
  94. $this->error = '2005';
  95. return false;
  96. }
  97. $username = isset($params['username']) ? trim($params['username']) : '';
  98. $password = isset($params['password']) ? trim($params['password']) : '123456';
  99. $avatar = isset($params['avatar']) ? trim($params['avatar']) : '';
  100. $data = [
  101. 'username' => $username,
  102. 'password' => get_password($password . md5($password.'otc')),
  103. 'mobile' => isPhone($username) ? $username : '',
  104. 'avatar' => $avatar ? $avatar : '',
  105. 'status' => 1,
  106. 'mark' => 1,
  107. 'create_time' => time(),
  108. ];
  109. // 谷歌验证码
  110. $google = GoogleAuthenticator::CreateSecret();
  111. $data['google_secret'] = isset($google['secret'])? $google['secret'] : '';
  112. // 生成trc2.0钱包地址
  113. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  114. if ($trcAddress) {
  115. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  116. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  117. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  118. } else {
  119. $this->error = 2201;
  120. return false;
  121. }
  122. // 生erc2.0钱包地址
  123. $ercAddress = UsdtWalletService::make()->getErcAddress();
  124. if ($trcAddress) {
  125. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  126. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  127. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  128. } else {
  129. $this->error = 2202;
  130. return false;
  131. }
  132. if ($id = $this->model->edit($data)) {
  133. MemberSettingService::make()->getInfo($id);
  134. return true;
  135. }
  136. $this->error = 2007;
  137. return false;
  138. }
  139. /**
  140. * 用户登录
  141. * @param $params
  142. * @return array|false
  143. */
  144. public function login($params)
  145. {
  146. $username = isset($params['username']) ? $params['username'] : '';
  147. $password = isset($params['password']) ? $params['password'] : '';
  148. if (empty($username) || empty($password)) {
  149. $this->error = 1013;
  150. return false;
  151. }
  152. // 用户验证
  153. $info = $this->model->getOne([['username', '=', $username]]);
  154. if (!$info) {
  155. $this->error = 2001;
  156. return false;
  157. }
  158. // 密码校验
  159. $password = get_password($password . md5($password.'otc'));
  160. if ($password != $info['password']) {
  161. $this->error = 2002;
  162. return false;
  163. }
  164. // 使用状态校验
  165. if ($info['status'] != 1) {
  166. $this->error = 2009;
  167. return false;
  168. }
  169. // 承兑商
  170. if ($info['user_type'] == 2) {
  171. $this->error = 1024;
  172. return false;
  173. }
  174. // 设置日志标题
  175. ActionLogModel::setTitle("会员登录APP");
  176. ActionLogModel::record($info);
  177. // JWT生成token
  178. $jwt = new Jwt('jwt_app');
  179. $token = $jwt->getToken($info['id']);
  180. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  181. // 登录
  182. $updateData = ['login_time'=>time(),'login_ip'=>get_client_ip()];
  183. if(empty($info['google_secret'])){
  184. $google = GoogleAuthenticator::CreateSecret();
  185. $updateData['google_secret'] = isset($google['secret'])? $google['secret'] : '';
  186. }
  187. $this->model->where(['id'=> $info['id']])->update($updateData);
  188. // 登录数据
  189. return [
  190. 'token' => $token,
  191. 'user_id' => $info['id'],
  192. 'user_type' => $info['user_type'],
  193. ];
  194. }
  195. /**
  196. * 身份认证
  197. * @param $userId
  198. * @param $params
  199. * @return false
  200. */
  201. public function auth($userId, $params)
  202. {
  203. $data = [
  204. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  205. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  206. ];
  207. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  208. $data['idcard_front_img'] = $params['idcard_front_img'];
  209. }
  210. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  211. $data['idcard_back_img'] = $params['idcard_back_img'];
  212. }
  213. $info = $this->getInfo($userId);
  214. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  215. if ($idcardCheck == 1) {
  216. $this->error = '2011';
  217. return false;
  218. }
  219. return $this->model->where(['id' => $userId])->update($data);
  220. }
  221. /**
  222. * 修改账号
  223. * @param $userId
  224. * @param $params
  225. * @return bool
  226. */
  227. public function modify($userId, $params)
  228. {
  229. $username = isset($params['username']) ? $params['username'] : '';
  230. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  231. $password = isset($params['password']) ? $params['password'] : '';
  232. if (empty($username) || empty($password)) {
  233. $this->error = 1013;
  234. return false;
  235. }
  236. // 用户验证
  237. $info = $this->model->getOne([['username', '=', $username]]);
  238. if (!$info || $info['id'] != $userId) {
  239. $this->error = 2001;
  240. return false;
  241. }
  242. // 使用状态校验
  243. if ($info['status'] != 1) {
  244. $this->error = 2009;
  245. return false;
  246. }
  247. // 密码校验
  248. $password = get_password($password . md5($password.'otc'));
  249. if ($password != $info['password']) {
  250. $this->error = 2002;
  251. return false;
  252. }
  253. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  254. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  255. $this->error = 2005;
  256. return false;
  257. }
  258. if(!$this->model->where(['id'=> $info['id']])->update(['username'=> $newUsername,'update_time'=> time()])){
  259. $this->error = 2021;
  260. return false;
  261. }
  262. $this->error = 2020;
  263. return true;
  264. }
  265. /**
  266. * 修改更新登录密码
  267. * @param $userId
  268. * @param $params
  269. * @return bool
  270. */
  271. public function updatePassword($userId, $params)
  272. {
  273. $username = isset($params['username']) ? $params['username'] : '';
  274. $password = isset($params['password']) ? $params['password'] : '';
  275. if (empty($username) || empty($password)) {
  276. $this->error = 1013;
  277. return false;
  278. }
  279. // 用户验证
  280. $info = $this->model->getOne([['username', '=', $username]]);
  281. if (!$info || $info['id'] != $userId) {
  282. $this->error = 2001;
  283. return false;
  284. }
  285. // 使用状态校验
  286. if ($info['status'] != 1) {
  287. $this->error = 2009;
  288. return false;
  289. }
  290. // 更新登录密码
  291. $password = get_password($password . md5($password.'otc'));
  292. if(!$this->model->where(['id'=> $info['id']])->update(['password'=> $password,'update_time'=> time()])){
  293. $this->error = 2023;
  294. return false;
  295. }
  296. $this->error = 2022;
  297. return true;
  298. }
  299. /**
  300. * 修改更新交易密码
  301. * @param $userId
  302. * @param $params
  303. * @return bool
  304. */
  305. public function updateTradePassword($userId, $params)
  306. {
  307. $username = isset($params['username']) ? $params['username'] : '';
  308. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  309. if (empty($username) || empty($tradePassword)) {
  310. $this->error = 1013;
  311. return false;
  312. }
  313. // 用户验证
  314. $info = $this->model->getOne([['username', '=', $username]]);
  315. if (!$info || $info['id'] != $userId) {
  316. $this->error = 2001;
  317. return false;
  318. }
  319. // 使用状态校验
  320. if ($info['status'] != 1) {
  321. $this->error = 2009;
  322. return false;
  323. }
  324. // 交易密码
  325. $password = get_password($tradePassword . md5($tradePassword.'otc'));
  326. if(!$this->model->where(['id'=> $info['id']])->update(['trade_password'=> $password,'update_time'=> time()])){
  327. $this->error = 2023;
  328. return false;
  329. }
  330. $this->error = 2022;
  331. return true;
  332. }
  333. /**
  334. * 获取钱包地址密钥参数
  335. * @param $address
  336. * @param string $type 链类型:trc-trc2.0,erc
  337. * @return mixed
  338. */
  339. public function getWallet($address, $type = 'trc')
  340. {
  341. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  342. }
  343. /**
  344. * 获取待处理用户数据
  345. * @param int $page
  346. * @param int $pageSize
  347. * @return array|mixed
  348. */
  349. public function getCatchMember($page = 1, $pageSize = 500)
  350. {
  351. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  352. $datas = RedisService::get($cacheKey);
  353. if ($datas) {
  354. return $datas;
  355. }
  356. $datas = $this->model->where(['mark' => 1])
  357. ->select(['id', 'trc_address', 'usdt_num'])
  358. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  359. $datas = $datas ? $datas->toArray() : [];
  360. $datas = isset($datas['data']) ? $datas['data'] : [];
  361. if ($datas) {
  362. RedisService::set($cacheKey, $datas, rand(120, 600));
  363. }
  364. return $datas;
  365. }
  366. /**
  367. * 匹配承兑商
  368. * @param $num 交易数量
  369. * @param int $tradeType 交易类型:1-购买,2-出售
  370. */
  371. public function getTradeMember($num, $tradeType = 1, $userId=0)
  372. {
  373. $data = $this->model->from('member as m')
  374. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  375. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  376. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  377. ->where('m.usdt_num', '>=', $num)
  378. ->where(function($query)use($userId){
  379. if($userId){
  380. $query->whereNotIn('m.id', [$userId]);
  381. }
  382. })
  383. ->where(function ($query) use ($tradeType) {
  384. $time = time();
  385. // 买单
  386. if ($tradeType == 1) {
  387. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  388. } // 卖单
  389. else {
  390. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  391. }
  392. })
  393. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  394. ->orderBy("credit",'desc')
  395. ->orderBy(DB::raw("rand()"))
  396. ->first();
  397. $data = $data? $data->toArray() : [];
  398. return $data;
  399. }
  400. /**
  401. * 匹配承兑商
  402. * @param $num 交易数量
  403. * @param int $tradeType 交易类型:1-购买,2-出售
  404. */
  405. public function getTradeMemberOptions($num, $tradeType = 1, $userId=0, $keyword='', $paseSize=50)
  406. {
  407. $cacheKey = "caches:tradeMenber:{$tradeType}_".md5($num.$userId.$keyword.$paseSize);
  408. if($datas = RedisService::get($cacheKey)){
  409. return $datas;
  410. }
  411. $datas = $this->model->from('member as m')
  412. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  413. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  414. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  415. ->where('m.usdt_num', '>=', $num)
  416. ->where(function($query) use($userId, $keyword){
  417. if($userId){
  418. $query->whereNotIn('m.id', [$userId]);
  419. }
  420. if($keyword){
  421. $query->where('m.username', 'like',"%{$keyword}%");
  422. }
  423. })
  424. ->where(function ($query) use ($tradeType) {
  425. $time = time();
  426. // 买单
  427. if ($tradeType == 1) {
  428. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  429. } // 卖单
  430. else {
  431. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  432. }
  433. })
  434. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  435. ->orderBy("credit",'desc')
  436. ->orderBy("m.safe_level",'desc')
  437. ->paginate($paseSize);
  438. $datas = $datas ? $datas->toArray() : [];
  439. $datas = isset($datas['data']) ? $datas['data'] : [];
  440. if ($datas) {
  441. RedisService::set($cacheKey, $datas, rand(10, 30));
  442. }
  443. return $datas;
  444. }
  445. /**
  446. * 生成普通参数二维码
  447. * @param $str 参数
  448. * @param bool $refresh 是否重新生成
  449. * @return bool
  450. */
  451. public function makeQrcode($str, $refresh = false, $size = 4, $margin=2, $level=2)
  452. {
  453. $qrFile = '/images/qrcode/';
  454. if (!is_dir('/uploads'.$qrFile)) {
  455. @mkdir('./uploads' . $qrFile, 0755, true);
  456. }
  457. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size.$margin.$level)) . '.png';
  458. $cacheKey = "caches:qrcodes:member_".md5($str);
  459. if(RedisService::get($cacheKey) && is_file('/uploads'.$qrFile) && !$refresh){
  460. //return $qrFile;
  461. }
  462. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  463. if(!file_exists('./uploads'.$qrFile)){
  464. return false;
  465. }
  466. RedisService::set($cacheKey, ['str'=> $str, 'qrcode'=> $qrFile,'date'=> date('Y-m-d H:i:s')], 7*24 * 3600);
  467. return $qrFile;
  468. }
  469. /**
  470. * 获取钱包归集用户列表
  471. * @param float $minUsdt
  472. * @param int $page
  473. * @param int $pageSize
  474. * @return array|mixed
  475. */
  476. public function getTriggerAddressList($minUsdt=0.2, $page=1, $pageSize = 200)
  477. {
  478. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  479. $datas = RedisService::get($cacheKey);
  480. if ($datas) {
  481. return $datas;
  482. }
  483. $datas = $this->model->where(['mark'=>1])
  484. ->where('trc_usdt','>=', $minUsdt)
  485. ->select(['id','trc_usdt','trc_address','trc_wif','trc_hexaddress','erc_address','erc_wif','erc_hexaddress'])
  486. ->orderBy('trc_usdt')
  487. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  488. $datas = $datas ? $datas->toArray() : [];
  489. $datas = isset($datas['data']) ? $datas['data'] : [];
  490. if ($datas) {
  491. RedisService::set($cacheKey, $datas, rand(10, 30));
  492. }
  493. return $datas;
  494. }
  495. }