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. var_dump(888);
  123. // 生erc2.0钱包地址
  124. $ercAddress = UsdtWalletService::make()->getErcAddress();
  125. if ($trcAddress) {
  126. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  127. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  128. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  129. } else {
  130. $this->error = 2202;
  131. return false;
  132. }
  133. if ($id = $this->model->edit($data)) {
  134. MemberSettingService::make()->getInfo($id);
  135. return true;
  136. }
  137. $this->error = 2007;
  138. return false;
  139. }
  140. /**
  141. * 用户登录
  142. * @param $params
  143. * @return array|false
  144. */
  145. public function login($params)
  146. {
  147. $username = isset($params['username']) ? $params['username'] : '';
  148. $password = isset($params['password']) ? $params['password'] : '';
  149. if (empty($username) || empty($password)) {
  150. $this->error = 1013;
  151. return false;
  152. }
  153. // 用户验证
  154. $info = $this->model->getOne([['username', '=', $username]]);
  155. if (!$info) {
  156. $this->error = 2001;
  157. return false;
  158. }
  159. // 密码校验
  160. $password = get_password($password . md5($password.'otc'));
  161. if ($password != $info['password']) {
  162. $this->error = 2002;
  163. return false;
  164. }
  165. // 使用状态校验
  166. if ($info['status'] != 1) {
  167. $this->error = 2009;
  168. return false;
  169. }
  170. // 承兑商
  171. if ($info['user_type'] == 2) {
  172. $this->error = 1024;
  173. return false;
  174. }
  175. // 设置日志标题
  176. ActionLogModel::setTitle("会员登录APP");
  177. ActionLogModel::record($info);
  178. // JWT生成token
  179. $jwt = new Jwt('jwt_app');
  180. $token = $jwt->getToken($info['id']);
  181. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  182. // 登录
  183. $updateData = ['login_time'=>time(),'login_ip'=>get_client_ip()];
  184. if(empty($info['google_secret'])){
  185. $google = GoogleAuthenticator::CreateSecret();
  186. $updateData['google_secret'] = isset($google['secret'])? $google['secret'] : '';
  187. }
  188. $this->model->where(['id'=> $info['id']])->update($updateData);
  189. // 登录数据
  190. return [
  191. 'token' => $token,
  192. 'user_id' => $info['id'],
  193. 'user_type' => $info['user_type'],
  194. ];
  195. }
  196. /**
  197. * 身份认证
  198. * @param $userId
  199. * @param $params
  200. * @return false
  201. */
  202. public function auth($userId, $params)
  203. {
  204. $data = [
  205. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  206. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  207. ];
  208. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  209. $data['idcard_front_img'] = $params['idcard_front_img'];
  210. }
  211. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  212. $data['idcard_back_img'] = $params['idcard_back_img'];
  213. }
  214. $info = $this->getInfo($userId);
  215. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  216. if ($idcardCheck == 1) {
  217. $this->error = '2011';
  218. return false;
  219. }
  220. return $this->model->where(['id' => $userId])->update($data);
  221. }
  222. /**
  223. * 修改账号
  224. * @param $userId
  225. * @param $params
  226. * @return bool
  227. */
  228. public function modify($userId, $params)
  229. {
  230. $username = isset($params['username']) ? $params['username'] : '';
  231. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  232. $password = isset($params['password']) ? $params['password'] : '';
  233. if (empty($username) || empty($password)) {
  234. $this->error = 1013;
  235. return false;
  236. }
  237. // 用户验证
  238. $info = $this->model->getOne([['username', '=', $username]]);
  239. if (!$info || $info['id'] != $userId) {
  240. $this->error = 2001;
  241. return false;
  242. }
  243. // 使用状态校验
  244. if ($info['status'] != 1) {
  245. $this->error = 2009;
  246. return false;
  247. }
  248. // 密码校验
  249. $password = get_password($password . md5($password.'otc'));
  250. if ($password != $info['password']) {
  251. $this->error = 2002;
  252. return false;
  253. }
  254. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  255. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  256. $this->error = 2005;
  257. return false;
  258. }
  259. if(!$this->model->where(['id'=> $info['id']])->update(['username'=> $newUsername,'update_time'=> time()])){
  260. $this->error = 2021;
  261. return false;
  262. }
  263. $this->error = 2020;
  264. return true;
  265. }
  266. /**
  267. * 修改更新登录密码
  268. * @param $userId
  269. * @param $params
  270. * @return bool
  271. */
  272. public function updatePassword($userId, $params)
  273. {
  274. $username = isset($params['username']) ? $params['username'] : '';
  275. $password = isset($params['password']) ? $params['password'] : '';
  276. if (empty($username) || empty($password)) {
  277. $this->error = 1013;
  278. return false;
  279. }
  280. // 用户验证
  281. $info = $this->model->getOne([['username', '=', $username]]);
  282. if (!$info || $info['id'] != $userId) {
  283. $this->error = 2001;
  284. return false;
  285. }
  286. // 使用状态校验
  287. if ($info['status'] != 1) {
  288. $this->error = 2009;
  289. return false;
  290. }
  291. // 更新登录密码
  292. $password = get_password($password . md5($password.'otc'));
  293. if(!$this->model->where(['id'=> $info['id']])->update(['password'=> $password,'update_time'=> time()])){
  294. $this->error = 2023;
  295. return false;
  296. }
  297. $this->error = 2022;
  298. return true;
  299. }
  300. /**
  301. * 修改更新交易密码
  302. * @param $userId
  303. * @param $params
  304. * @return bool
  305. */
  306. public function updateTradePassword($userId, $params)
  307. {
  308. $username = isset($params['username']) ? $params['username'] : '';
  309. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  310. if (empty($username) || empty($tradePassword)) {
  311. $this->error = 1013;
  312. return false;
  313. }
  314. // 用户验证
  315. $info = $this->model->getOne([['username', '=', $username]]);
  316. if (!$info || $info['id'] != $userId) {
  317. $this->error = 2001;
  318. return false;
  319. }
  320. // 使用状态校验
  321. if ($info['status'] != 1) {
  322. $this->error = 2009;
  323. return false;
  324. }
  325. // 交易密码
  326. $password = get_password($tradePassword . md5($tradePassword.'otc'));
  327. if(!$this->model->where(['id'=> $info['id']])->update(['trade_password'=> $password,'update_time'=> time()])){
  328. $this->error = 2023;
  329. return false;
  330. }
  331. $this->error = 2022;
  332. return true;
  333. }
  334. /**
  335. * 获取钱包地址密钥参数
  336. * @param $address
  337. * @param string $type 链类型:trc-trc2.0,erc
  338. * @return mixed
  339. */
  340. public function getWallet($address, $type = 'trc')
  341. {
  342. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  343. }
  344. /**
  345. * 获取待处理用户数据
  346. * @param int $page
  347. * @param int $pageSize
  348. * @return array|mixed
  349. */
  350. public function getCatchMember($page = 1, $pageSize = 500)
  351. {
  352. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  353. $datas = RedisService::get($cacheKey);
  354. if ($datas) {
  355. return $datas;
  356. }
  357. $datas = $this->model->where(['mark' => 1])
  358. ->select(['id', 'trc_address', 'usdt_num'])
  359. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  360. $datas = $datas ? $datas->toArray() : [];
  361. $datas = isset($datas['data']) ? $datas['data'] : [];
  362. if ($datas) {
  363. RedisService::set($cacheKey, $datas, rand(120, 600));
  364. }
  365. return $datas;
  366. }
  367. /**
  368. * 匹配承兑商
  369. * @param $num 交易数量
  370. * @param int $tradeType 交易类型:1-购买,2-出售
  371. */
  372. public function getTradeMember($num, $tradeType = 1, $userId=0)
  373. {
  374. $data = $this->model->from('member as m')
  375. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  376. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  377. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  378. ->where('m.usdt_num', '>=', $num)
  379. ->where(function($query)use($userId){
  380. if($userId){
  381. $query->whereNotIn('m.id', [$userId]);
  382. }
  383. })
  384. ->where(function ($query) use ($tradeType) {
  385. $time = time();
  386. // 买单
  387. if ($tradeType == 1) {
  388. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  389. } // 卖单
  390. else {
  391. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  392. }
  393. })
  394. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  395. ->orderBy("credit",'desc')
  396. ->orderBy(DB::raw("rand()"))
  397. ->first();
  398. $data = $data? $data->toArray() : [];
  399. return $data;
  400. }
  401. /**
  402. * 匹配承兑商
  403. * @param $num 交易数量
  404. * @param int $tradeType 交易类型:1-购买,2-出售
  405. */
  406. public function getTradeMemberOptions($num, $tradeType = 1, $userId=0, $keyword='', $paseSize=50)
  407. {
  408. $cacheKey = "caches:tradeMenber:{$tradeType}_".md5($num.$userId.$keyword.$paseSize);
  409. if($datas = RedisService::get($cacheKey)){
  410. return $datas;
  411. }
  412. $datas = $this->model->from('member as m')
  413. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  414. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  415. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  416. ->where('m.usdt_num', '>=', $num)
  417. ->where(function($query) use($userId, $keyword){
  418. if($userId){
  419. $query->whereNotIn('m.id', [$userId]);
  420. }
  421. if($keyword){
  422. $query->where('m.username', 'like',"%{$keyword}%");
  423. }
  424. })
  425. ->where(function ($query) use ($tradeType) {
  426. $time = time();
  427. // 买单
  428. if ($tradeType == 1) {
  429. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  430. } // 卖单
  431. else {
  432. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  433. }
  434. })
  435. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  436. ->orderBy("credit",'desc')
  437. ->orderBy("m.safe_level",'desc')
  438. ->paginate($paseSize);
  439. $datas = $datas ? $datas->toArray() : [];
  440. $datas = isset($datas['data']) ? $datas['data'] : [];
  441. if ($datas) {
  442. RedisService::set($cacheKey, $datas, rand(10, 30));
  443. }
  444. return $datas;
  445. }
  446. /**
  447. * 生成普通参数二维码
  448. * @param $str 参数
  449. * @param bool $refresh 是否重新生成
  450. * @return bool
  451. */
  452. public function makeQrcode($str, $refresh = false, $size = 4, $margin=2, $level=2)
  453. {
  454. $qrFile = '/images/qrcode/';
  455. if (!is_dir('/uploads'.$qrFile)) {
  456. @mkdir('./uploads' . $qrFile, 0755, true);
  457. }
  458. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size.$margin.$level)) . '.png';
  459. $cacheKey = "caches:qrcodes:member_".md5($str);
  460. if(RedisService::get($cacheKey) && is_file('/uploads'.$qrFile) && !$refresh){
  461. //return $qrFile;
  462. }
  463. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  464. if(!file_exists('./uploads'.$qrFile)){
  465. return false;
  466. }
  467. RedisService::set($cacheKey, ['str'=> $str, 'qrcode'=> $qrFile,'date'=> date('Y-m-d H:i:s')], 7*24 * 3600);
  468. return $qrFile;
  469. }
  470. /**
  471. * 获取钱包归集用户列表
  472. * @param float $minUsdt
  473. * @param int $page
  474. * @param int $pageSize
  475. * @return array|mixed
  476. */
  477. public function getTriggerAddressList($minUsdt=0.2, $page=1, $pageSize = 200)
  478. {
  479. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  480. $datas = RedisService::get($cacheKey);
  481. if ($datas) {
  482. return $datas;
  483. }
  484. $datas = $this->model->where(['mark'=>1])
  485. ->where('trc_usdt','>=', $minUsdt)
  486. ->select(['id','trc_usdt','trc_address','trc_wif','trc_hexaddress','erc_address','erc_wif','erc_hexaddress'])
  487. ->orderBy('trc_usdt')
  488. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  489. $datas = $datas ? $datas->toArray() : [];
  490. $datas = isset($datas['data']) ? $datas['data'] : [];
  491. if ($datas) {
  492. RedisService::set($cacheKey, $datas, rand(10, 30));
  493. }
  494. return $datas;
  495. }
  496. }