MemberService.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\CartsModel;
  16. use App\Models\LiveModel;
  17. use App\Models\MemberBankModel;
  18. use App\Models\MemberLevelModel;
  19. use App\Models\MemberModel;
  20. use App\Models\MerchantModel;
  21. use App\Models\VideoModel;
  22. use App\Services\BaseService;
  23. use App\Services\CityService;
  24. use App\Services\ConfigService;
  25. use App\Services\EmailService;
  26. use App\Services\RedisService;
  27. use App\Services\SmsService;
  28. use App\Services\ToolService;
  29. use App\Services\UdunpayService;
  30. use App\Services\WalletService;
  31. use Illuminate\Support\Facades\DB;
  32. use phpQrcode\QRcode;
  33. /**
  34. * 会员管理-服务类
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. * Class MemberService
  38. * @package App\Services\Api
  39. */
  40. class MemberService extends BaseService
  41. {
  42. // 静态对象
  43. protected static $instance = null;
  44. /**
  45. * 构造函数
  46. * @author laravel开发员
  47. * @since 2020/11/11
  48. * MemberService constructor.
  49. */
  50. public function __construct()
  51. {
  52. $this->model = new MemberModel();
  53. }
  54. /**
  55. * 静态入口
  56. * @return MemberService|static|null
  57. */
  58. public static function make()
  59. {
  60. if (!self::$instance) {
  61. self::$instance = new static();
  62. }
  63. return self::$instance;
  64. }
  65. /**
  66. * 获取资料详情
  67. * @param $where
  68. * @param array $field
  69. */
  70. public function getInfo($where, $type = 'detail', array $field = [], $userId=0)
  71. {
  72. $defaultField = ['id', 'username','email', 'realname', 'mobile','gender','member_level','box_num', 'nickname', 'code','supper_point', 'parent_id','birthday','city_id','signature','power_num','upgrade_profit_total','trade_state','trc_url','recharge_trc_url', 'point_id', 'balance','trx','usdt','wait_score','intro', 'score','pay_password','realname','idcard_check', 'member_level', 'status', 'avatar'];
  73. $withs = ['merchant','acceptor','parent','point','level'];
  74. if ($type == 'team') {
  75. $defaultField = ['id', 'username','email','member_level', 'nickname','usdt', 'code', 'balance', 'score','point_id','power_num','parent_id', 'status', 'avatar'];
  76. }else if($type == 'share'){
  77. $withs = ['parent','level'];
  78. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url', 'score', 'status', 'avatar'];
  79. }else if($type == 'check'){
  80. $withs = ['parent','level'];
  81. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url','wait_score', 'score', 'status', 'avatar'];
  82. }else if ($type == 'info'){
  83. $withs = ['parent','level'];
  84. }
  85. $field = $field ? $field : $defaultField;
  86. if (is_array($where)) {
  87. $info = $this->model->with($withs)->where('mark',1)->where($where)->select($field)->first();
  88. } else {
  89. $info = $this->model->with($withs)->where('mark',1)->where(['id' => (int)$where])->select($field)->first();
  90. }
  91. $info = $info ? $info->toArray() : [];
  92. if ($info && !in_array($type, ['auth', 'check','login'])) {
  93. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  94. $info['balance'] = moneyFormat($info['balance'], 2);
  95. $info['usdt'] = isset($info['usdt'])?moneyFormat($info['usdt'], 2):'0.00';
  96. $info['score'] = moneyFormat($info['score'], 2);
  97. $info['merchant'] = isset($info['merchant']) && $info['merchant']? $info['merchant'] : ['id'=>0];
  98. $info['acceptor'] = isset($info['acceptor']) && $info['acceptor']? $info['acceptor'] : ['id'=>0];
  99. $info['level'] = isset($info['level']) && $info['level']? $info['level'] : ['id'=>0,'name'=>'普通用户'];
  100. $info['custom_phone'] = ConfigService::make()->getConfigByCode('custom_phone');
  101. $info['custom_telegram'] = ConfigService::make()->getConfigByCode('custom_telegram');
  102. $info['custom_email'] = ConfigService::make()->getConfigByCode('custom_email');
  103. if(isset($info['birthday'])){
  104. $info['birthday_text'] = $info['birthday']? date('Y-m-d',$info['birthday']) : '';
  105. }
  106. // 二维码
  107. if (in_array($type, ['detail'])) {
  108. $info['parent_code'] = '';
  109. $info['parent'] = $info['parent']? $info['parent'] : ['id'=>0,'nickname' => '无', 'code' => '无'];
  110. if($info['parent']['id']>0){
  111. $info['parent_code'] = isset($info['parent']['code'])? $info['parent']['code'] :'';
  112. }
  113. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  114. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  115. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  116. if($userId != $info['id']){
  117. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  118. }else{
  119. $info['is_follow'] = 1;
  120. }
  121. $info['app_version'] = ConfigService::make()->getConfigByCode('app_version');
  122. }
  123. else if ($type == 'info') {
  124. $liveOpenLevel = ConfigService::make()->getConfigByCode('live_open_level',0);
  125. $liveOpenLevel = $liveOpenLevel>0? $liveOpenLevel : 0;
  126. $info['live_open'] = $info['member_level']>=$liveOpenLevel? 1 : 0;
  127. }else if($type == 'buy'){
  128. $address = MemberAddressService::make()->getBindInfo($userId);
  129. $info['address'] = $address;
  130. $streetCode = isset($address['street_code'])? $address['street_code'] : '';
  131. $info['freight_address_id'] = $streetCode? $streetCode : (isset($address['district_code'])? $address['district_code'] : '');
  132. $info['address_id'] = isset($address['id'])? $address['id'] : 0;
  133. $info['cart_num'] = GoodsService::make()->getCartCount($info['id']);
  134. }else if($type == 'upgrade'){
  135. $level = isset($info['level'])? $info['level'] : [];
  136. $nextLevel = MemberLevelModel::where('id','>', $info['member_level'])->orderBy('id','asc')->first();
  137. $info['next_level'] = isset($nextLevel['id']) && $nextLevel['id']? $nextLevel['id'] : $info['member_level'];
  138. $info['next_level_profit'] = isset($nextLevel['upgrade_usdt_profit']) && $nextLevel['upgrade_usdt_profit']? $nextLevel['upgrade_usdt_profit'] : (isset($level['upgrade_usdt_profit'])? $level['upgrade_usdt_profit']:0);
  139. $info['next_level_usdt'] = isset($nextLevel['upgrade_usdt']) && $nextLevel['upgrade_usdt']? $nextLevel['upgrade_usdt'] : (isset($level['upgrade_usdt'])? $level['upgrade_usdt']:0);
  140. $info['next_level_name'] = isset($nextLevel['name'])? $nextLevel['name'] : (isset($level['name'])? $level['name']:'');
  141. }
  142. else if ($type == 'recharge') {
  143. $wallet = WalletService::make()->getWallet(1);
  144. $info['recharge_trc_url'] = isset($wallet['address'])? $wallet['address'] : '';
  145. $info['xd_price'] = ConfigService::make()->getConfigByCode('xd_price',100);
  146. if($info['recharge_trc_url']){
  147. $qrcode = $this->makeQrcode($info['recharge_trc_url']);
  148. $info['trc_qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  149. }else{
  150. $info['trc_qrcode'] = '';
  151. }
  152. }
  153. else if ($type == 'team' || $type == 'share') {
  154. $inviteUrl = get_web_url('/#/pages/register/index?code=' . $info['code']);
  155. $qrcode = $this->makeQrcode($inviteUrl);
  156. $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  157. $info['invite_url'] = $inviteUrl;
  158. $info['qrcode_base64'] = $qrcode?getImageContent($qrcode):'';
  159. $info['qrcode_header'] = get_image_url('/images/qrcode/qrcode-header.png');
  160. $info['qrcode_header_base64'] = getImageContent('/images/qrcode/qrcode-header.png');
  161. $info['qrcode_bg'] = get_image_url('/images/qrcode/qrcode-bg.png');
  162. $info['qrcode_bg_base64'] = getImageContent('/images/qrcode/qrcode-bg.png');
  163. $info['team_count1'] = $this->getUserCountByType($info['id'], 1, 0); // 直推人数
  164. $info['team_count2'] = $this->getUserCountByType($info['id'], 3, 0); // 滑落人数
  165. $info['team_count'] = $this->getUserCountByType($info['id'], 2, 0); // 团队人数
  166. }else {
  167. // 默认地址
  168. $info['addressData'] = MemberAddressService::make()->getBindInfo($info['id']);
  169. }
  170. }
  171. return $info;
  172. }
  173. /**
  174. * 获取主页信息
  175. * @param $where
  176. * @param array $field
  177. */
  178. public function getHomeInfo($id, $userId=0)
  179. {
  180. $field = ['id', 'username','email', 'realname', 'mobile','gender', 'nickname', 'code','supper_point', 'parent_id','province_id','city_id','signature','power_num','trade_state', 'point_id', 'balance','trx','usdt','wait_score', 'score','realname', 'member_level', 'status', 'avatar'];
  181. $info = $this->model->where('mark',1)->where(['id' => $id])->select($field)->first();
  182. $info = $info ? $info->toArray() : [];
  183. if ($info) {
  184. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  185. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  186. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  187. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  188. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  189. $info['is_fans'] = (int)MemberCollectService::make()->checkCollect($info['id'], $userId,1); // 是否我的粉丝
  190. $info['live_id'] = (int)LiveModel::where(['user_id'=> $info['id'],'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id'); // 是否正在直播
  191. }
  192. return $info;
  193. }
  194. /**
  195. * 获取用户缓存信息
  196. * @param $where
  197. * @param array $field
  198. * @param int $expired
  199. * @return array|mixed
  200. */
  201. public function getCacheInfo($where, $field = [], $expired = 0)
  202. {
  203. $cacheKey = "caches:member:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  204. $info = RedisService::get($cacheKey);
  205. if ($info) {
  206. return $info;
  207. }
  208. $defaultField = ['id', 'username', 'realname', 'mobile', 'nickname', 'code', 'parent_id', 'balance','usdt', 'score', 'member_level', 'status', 'avatar'];
  209. $field = $field ? $field : $defaultField;
  210. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  211. $info = $info ? $info->toArray() : [];
  212. if ($info) {
  213. if (isset($info['avatar'])) {
  214. $info['avatar'] = $info['avatar']? $info['avatar'] : '/images/member/logo.png';
  215. $info['avatar_preview'] = $info['avatar']? get_image_url($info['avatar']) : '';
  216. }
  217. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  218. }
  219. return $info;
  220. }
  221. /**
  222. * 生成普通参数二维码
  223. * @param $str 参数
  224. * @param bool $refresh 是否重新生成
  225. * @return bool
  226. */
  227. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  228. {
  229. $basePath = base_path() . '/public';
  230. $qrFile = '/images/qrcode/';
  231. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  232. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  233. }
  234. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  235. $qrFile = $qrFile . "C_{$key}.png";
  236. $cacheKey = "caches:qrcodes:member_" . $key;
  237. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  238. return $qrFile;
  239. }
  240. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  241. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  242. return false;
  243. }
  244. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  245. return $qrFile;
  246. }
  247. /**
  248. * 获取团队用户数量
  249. * @param $userId 当前用户ID
  250. * @param int $type 层级:1-直推,2-团队
  251. * @param int $isPoint 节点:1-滑落推荐,0-直接推荐
  252. * @return array|mixed
  253. */
  254. public function getUserCountByType($userId, $type = 1, $isPoint=0)
  255. {
  256. $cacheKey = "caches:teams:count_{$userId}_{$type}_{$isPoint}";
  257. $data = RedisService::get($cacheKey);
  258. if ($data) {
  259. return $data;
  260. }
  261. $where = ['status' => 1, 'mark' => 1];
  262. $data = $this->model->where($where)
  263. ->where(function ($query) use ($type, $isPoint, $userId) {
  264. if($isPoint){
  265. if ($type == 1) { // 滑落推荐
  266. $query->where('point_id', $userId);
  267. } else if($type == 2){ // 滑落团队人数
  268. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  269. }
  270. }else{
  271. if ($type == 1) { // 直推人数
  272. $query->where('parent_id', $userId);
  273. } else if($type == 2){ // 团队人数
  274. $query->whereRaw("FIND_IN_SET({$userId},parents)");
  275. } else if($type == 3){ // 滑落人数
  276. $query->where('parent_id', $userId)->where('point_id','!=', $userId);
  277. }
  278. }
  279. })->count('id');
  280. if ($data) {
  281. RedisService::set($cacheKey, $data, rand(3, 5));
  282. }
  283. return $data;
  284. }
  285. /**
  286. * 分销用户列表
  287. * @param $userId
  288. * @param $params
  289. * @param int $pageSize
  290. * @return array
  291. */
  292. public function getTeamList($userId, $params, $pageSize=15)
  293. {
  294. $page = request()->post('page',1);
  295. $cacheKey = "caches:teams:list_{$userId}_{$page}";
  296. $datas = RedisService::get($cacheKey);
  297. if ($datas) {
  298. return [
  299. 'pageSize' => $pageSize,
  300. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  301. 'list' => isset($datas['data']) ? $datas['data'] : []
  302. ];
  303. }
  304. $where = ['status' => 1, 'mark' => 1];
  305. $field = ['id','avatar', 'nickname', 'username', 'email', 'balance', 'parent_id','power_num','point_id','create_time', 'status'];
  306. $datas = $this->model->with(['level'])->where($where)
  307. ->where(function ($query) use ($userId) {
  308. $query->where('parent_id', $userId);
  309. })
  310. ->where(function($query) use($params){
  311. $kw = isset($params['kw'])? trim($params['kw']) : '';
  312. if($kw){
  313. $query->where('id','=', "%{$kw}%")->orWhere('email','like',"%{$kw}%")->orWhere('username','like',"%{$kw}%");
  314. }
  315. })
  316. ->select($field)
  317. ->orderBy('create_time', 'desc')
  318. ->orderBy('id', 'desc')
  319. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  320. $datas = $datas ? $datas->toArray() : [];
  321. if ($datas) {
  322. foreach ($datas['data'] as &$item) {
  323. $item['avatar'] = $item['avatar']? $item['avatar'] : '/images/member/logo.png';
  324. $item['avatar'] = get_image_url($item['avatar']);
  325. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'y-m-d H:i') : '';
  326. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  327. }
  328. unset($item);
  329. RedisService::set($cacheKey, $datas, rand(5, 10));
  330. }
  331. return [
  332. 'pageSize' => $pageSize,
  333. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  334. 'list' => isset($datas['data']) ? $datas['data'] : []
  335. ];
  336. }
  337. /**
  338. * 手机号是否已注册
  339. * @param $mobile
  340. * @return mixed
  341. */
  342. public function checkPhoneExists($mobile)
  343. {
  344. return parent::checkExists('mobile', $mobile); // TODO: Change the autogenerated stub
  345. }
  346. /**
  347. * 账号注册
  348. * @param $params
  349. * @return array|false
  350. */
  351. public function register($params)
  352. {
  353. // 授权
  354. $email = isset($params['email']) ? trim($params['email']) : '';
  355. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  356. $registerType = isset($params['type']) ? intval($params['type']) : 1;
  357. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  358. $code = isset($params['code']) ? trim($params['code']) : '';
  359. $scene = isset($params['scene']) ? $params['scene'] : '';
  360. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  361. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  362. if (empty($params) || $code <= 0 || !in_array($registerType,[1,2])) {
  363. $this->error = 2014;
  364. return false;
  365. }
  366. // 邮箱注册
  367. if($registerType == 1){
  368. if(empty($email)){
  369. $this->error = 2014;
  370. return false;
  371. }
  372. // 验证码
  373. if (!EmailService::make()->check($email, $code, $scene)) {
  374. $this->error = EmailService::make()->getError();
  375. return false;
  376. }
  377. // 验证是否注册
  378. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  379. $this->error = 2009;
  380. return false;
  381. }
  382. }else if($registerType == 2){
  383. if(empty($mobile)){
  384. $this->error = 2014;
  385. return false;
  386. }
  387. // 验证码
  388. if (!SmsService::make()->check($mobile, $code, $scene)) {
  389. $this->error = SmsService::make()->getError();
  390. return false;
  391. }
  392. // 验证是否注册
  393. if($this->model->where(['mobile'=> $mobile,'mark'=>1])->value('id')){
  394. $this->error = 2009;
  395. return false;
  396. }
  397. }
  398. // 推荐人
  399. $parentId = 0;
  400. $pointId = 0;
  401. $parents = '';
  402. $points = '';
  403. if ($inviteCode) {
  404. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  405. ->select(['id', 'parents', 'parent_id'])
  406. ->first();
  407. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  408. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  409. // 挂靠节点
  410. if($parentId>0){
  411. $pointId = $this->getPointParentId($parentId);
  412. if($pointId){
  413. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  414. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  415. }
  416. }
  417. }
  418. // 注册
  419. $id = $this->model->max('id') + 1;
  420. $system = isset($params['system'])? $params['system'] : [];
  421. $ip = get_client_ip();
  422. $ipData = ToolService::make()->getIpAddress($ip,'');
  423. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  424. $city = isset($ipData['city'])? $ipData['city'] : '';
  425. $emailName = explode('@', $email)[0];
  426. // 钱包
  427. $data = [
  428. 'parent_id' => $parentId,
  429. 'point_id' => $pointId,
  430. 'points' => $points,
  431. 'openid' => '',
  432. 'avatar' => '',
  433. 'nickname' => $nickname ? $nickname : 'XL'.substr($emailName?$emailName:substr($mobile,-8,8),-8,8),
  434. 'username' => $registerType==1? $email : $mobile,
  435. 'password' => get_password('xl123456'),
  436. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  437. 'email' => $email,
  438. 'mobile' => $mobile,
  439. 'recharge_trc_url' => '',
  440. 'parents' => $parents,
  441. 'province_id' => CityService::make()->getFieldByName($province),
  442. 'city_id' => CityService::make()->getFieldByName($city),
  443. 'city' => $city,
  444. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  445. 'device' => $appSources == 'android' ? 2 : 1,
  446. 'status' => 1,
  447. 'balance' => 0,
  448. 'score' => 0,
  449. 'mark' => 1,
  450. 'login_ip' => $ip,
  451. 'create_time' => time(),
  452. ];
  453. $userId = $this->model->insertGetId($data);
  454. $this->error = 2008;
  455. return [
  456. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  457. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  458. ];
  459. }
  460. /**
  461. * 邮箱登录
  462. * @param $params
  463. * @return array|false
  464. */
  465. public function loginByEmail($params)
  466. {
  467. // 授权
  468. $email = isset($params['email']) ? $params['email'] : '';
  469. $code = isset($params['code']) ? $params['code'] : '';
  470. $scene = isset($params['scene']) ? $params['scene'] : '';
  471. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  472. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  473. $this->error = 2014;
  474. return false;
  475. }
  476. // 验证码
  477. if (!EmailService::make()->check($email, $code, $scene)) {
  478. $this->error = EmailService::make()->getError();
  479. return false;
  480. }
  481. // 验证是否注册,没有则注册
  482. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  483. $data = $data ? $data->toArray() : [];
  484. $userId = isset($data['id']) ? $data['id'] : 0;
  485. $status = isset($data['status']) ? $data['status'] : 0;
  486. if(empty($data) || $userId<=0){
  487. $this->error = 2001;
  488. return false;
  489. }
  490. if ($status != 1) {
  491. $this->error = 2015;
  492. return false;
  493. }
  494. // 获取登录授权token
  495. $jwt = new Jwt('jwt_dys_app');
  496. $token = $jwt->getToken($userId);
  497. // 结果返回
  498. $result = [
  499. 'access_token' => $token,
  500. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  501. ];
  502. // 用户信息
  503. $ip = get_client_ip();
  504. $updateData = [
  505. 'login_time' => time(),
  506. 'login_ip' => $ip,
  507. ];
  508. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  509. $ipData = ToolService::make()->getIpAddress($ip,'');
  510. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  511. $city = isset($ipData['city'])? $ipData['city'] : '';
  512. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  513. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  514. }
  515. $this->model->where(['id'=> $userId])->update($updateData);
  516. $this->error = 2004;
  517. $data['token'] = $token;
  518. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  519. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  520. return $result;
  521. }
  522. /**
  523. * 手机号登录
  524. * @param $params
  525. * @return array|false
  526. */
  527. public function loginByMobile($params)
  528. {
  529. // 授权
  530. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  531. $code = isset($params['code']) ? $params['code'] : '';
  532. $scene = isset($params['scene']) ? $params['scene'] : '';
  533. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  534. if (empty($params) || empty($mobile) || empty($scene) || $code <= 0) {
  535. $this->error = 2014;
  536. return false;
  537. }
  538. // 验证码
  539. if (!SmsService::make()->check($mobile, $code, $scene)) {
  540. $this->error = SmsService::make()->getError();
  541. return false;
  542. }
  543. // 验证是否注册,没有则注册
  544. $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'username','point_id','mobile', 'parent_id', 'code', 'status'])->first();
  545. $data = $data ? $data->toArray() : [];
  546. $userId = isset($data['id']) ? $data['id'] : 0;
  547. $status = isset($data['status']) ? $data['status'] : 0;
  548. if(empty($data) || $userId<=0){
  549. $this->error = 2001;
  550. return false;
  551. }
  552. if ($status != 1) {
  553. $this->error = 2015;
  554. return false;
  555. }
  556. // 获取登录授权token
  557. $jwt = new Jwt('jwt_dys_app');
  558. $token = $jwt->getToken($userId);
  559. // 结果返回
  560. $result = [
  561. 'access_token' => $token,
  562. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  563. ];
  564. // 用户信息
  565. $ip = get_client_ip();
  566. $updateData = [
  567. 'login_time' => time(),
  568. 'login_ip' => $ip,
  569. ];
  570. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  571. $ipData = ToolService::make()->getIpAddress($ip,'');
  572. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  573. $city = isset($ipData['city'])? $ipData['city'] : '';
  574. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  575. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  576. }
  577. $this->model->where(['id'=> $userId])->update($updateData);
  578. $this->error = 2004;
  579. $data['token'] = $token;
  580. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  581. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  582. return $result;
  583. }
  584. /**
  585. * 修改头像/商家logo/技师头像
  586. * @param $userId
  587. * @param $avatar
  588. * @return mixed
  589. */
  590. public function saveAvatar($userId, $avatar)
  591. {
  592. $type = request()->post('type', 1);
  593. if($type == 1){
  594. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  595. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  596. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  597. @unlink(ATTACHMENT_PATH . $oldAvatar);
  598. }
  599. return true;
  600. }
  601. }else if($type == 2){
  602. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  603. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  604. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  605. @unlink(ATTACHMENT_PATH . $oldAvatar);
  606. }
  607. return true;
  608. }
  609. }
  610. return false;
  611. }
  612. /**
  613. * 修改账号
  614. * @param $userId
  615. * @param $params
  616. * @return bool
  617. */
  618. public function modify($userId, $params)
  619. {
  620. $email = isset($params['email']) ? $params['email'] : '';
  621. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  622. $password = isset($params['password']) ? $params['password'] : '';
  623. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  624. $type = isset($params['type'])? trim($params['type']) : '';
  625. $scene = isset($params['scene'])? trim($params['scene']) : '';
  626. if($type != 'profile' && empty($email) && empty($mobile)){
  627. $this->error = 1064;
  628. return false;
  629. }
  630. // 用户验证
  631. $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])->select(['id','pay_password','parent_id','code', 'status'])->first();
  632. if (!$info) {
  633. $this->error = 2001;
  634. return false;
  635. }
  636. // 使用状态校验
  637. if ($info['status'] != 1) {
  638. $this->error = 2009;
  639. return false;
  640. }
  641. // 密码校验
  642. $data = ['update_time' => time()];
  643. // 验证交易密码
  644. if(isset($params['pay_password']) && $scene != 'trade'){
  645. $userPayPassword = isset($info['pay_password'])? $info['pay_password'] : '';
  646. if(empty($userPayPassword)){
  647. $this->error = 1040;
  648. return false;
  649. }
  650. if($userPayPassword != get_password($payPassword)){
  651. $this->error = 2038;
  652. return false;
  653. }
  654. }
  655. // 邀请人绑定
  656. $parent = isset($info['parent'])? $info['parent'] : [];
  657. $userParentCode = isset($parent['code'])? $parent['code'] : '';
  658. $userCode = isset($info['code'])? $info['code'] : '';
  659. $parentCode = isset($params['parent_code'])? trim($params['parent_code']) : '';
  660. if(isset($params['parent_code']) && $parentCode){
  661. //
  662. if($userParentCode){
  663. $this->error = 2451;
  664. return false;
  665. }
  666. if(($userCode == $parentCode)){
  667. $this->error = 2452;
  668. return false;
  669. }
  670. // 推荐人验证
  671. $inviteInfo = $this->model->where(['code' => $parentCode, 'mark' => 1])
  672. ->select(['id', 'parents', 'parent_id','trc_url'])
  673. ->first();
  674. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  675. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  676. if(!$parentId){
  677. $this->error = 2453;
  678. return false;
  679. }
  680. // 验证是否绑定用户为自己线下用户
  681. if(preg_match("/{$userId},/", $parents)){
  682. $this->error = 2454;
  683. return false;
  684. }
  685. // 推荐人
  686. $data['parent_id'] = $parentId;
  687. $data['parents'] = $parents;
  688. // 挂靠节点
  689. if($parentId>0){
  690. $pointId = $this->getPointParentId($parentId);
  691. if($pointId){
  692. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  693. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  694. $data['point_id'] = $pointId;
  695. $data['points'] = $points;
  696. }
  697. }
  698. }
  699. // 邮箱验证
  700. if (isset($params['email']) && $email) {
  701. // 修改邮箱
  702. $newEmail = isset($params['new_email'])? trim($params['new_email']) : '';
  703. if(isset($params['new_email']) && $newEmail){
  704. $data['email'] = $newEmail;
  705. $data['username'] = $newEmail;
  706. $checkInfo = $this->model->where(['username' => $newEmail, 'mark' => 1])->select(['id', 'status'])->first();
  707. if ($checkInfo && $checkInfo['id'] != $userId) {
  708. $this->error = 2005;
  709. return false;
  710. }
  711. }
  712. // 邮箱验证码
  713. $code = isset($params['code'])? trim($params['code']) : '';
  714. $checkEmail = $scene == 'modify_email'? $newEmail : $email;
  715. if (!EmailService::make()->check($checkEmail, $code, $scene)) {
  716. $this->error = EmailService::make()->getError();
  717. return false;
  718. }
  719. }
  720. // 手机号验证
  721. if (isset($params['mobile']) && $mobile) {
  722. // 修改手机号
  723. $newMobile = isset($params['new_mobile'])? trim($params['new_mobile']) : '';
  724. if(isset($params['new_mobile']) && $newMobile){
  725. $data['mobile'] = $newMobile;
  726. $data['username'] = $newMobile;
  727. $checkInfo = $this->model->where(['mobile' => $newMobile, 'mark' => 1])->select(['id', 'status'])->first();
  728. if ($checkInfo && $checkInfo['id'] != $userId) {
  729. $this->error = 2005;
  730. return false;
  731. }
  732. }
  733. // 手机验证码
  734. $code = isset($params['code'])? trim($params['code']) : '';
  735. $checkMobile = $scene == 'modify_mobile'? $newMobile : $mobile;
  736. if (!SmsService::make()->check($checkMobile, $code, $scene)) {
  737. $this->error = SmsService::make()->getError();
  738. return false;
  739. }
  740. }
  741. $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : '';
  742. $userTrcUrl = isset($info['trc_url'])? trim($info['trc_url']) : '';
  743. if(isset($params['trc_url']) && $trcUrl){
  744. if($trcUrl != $userTrcUrl){
  745. if($this->model->where(['trc_url'=> $trcUrl,'mark'=>1])->whereNotIn('id',[$userId])->value('id')){
  746. $this->error = 1048;
  747. return false;
  748. }
  749. if(!WalletService::make()->checkAddress($trcUrl)){
  750. $this->error = 1049;
  751. return false;
  752. }
  753. $data['trc_url'] = $trcUrl;
  754. }
  755. }
  756. // 修改数据
  757. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  758. if (isset($params['nickname']) && $nickname) {
  759. $data['nickname'] = $nickname;
  760. }
  761. $gender = isset($params['gender']) ? intval($params['gender']) : 3;
  762. if (isset($params['gender']) && $gender) {
  763. $data['gender'] = $gender;
  764. }
  765. $intro = isset($params['intro']) ? $params['intro'] : '';
  766. if (isset($params['intro']) && $intro) {
  767. $data['intro'] = $intro;
  768. }
  769. $birthday = isset($params['birthday_text']) ? $params['birthday_text'] : '';
  770. if (isset($params['birthday_text']) && $birthday) {
  771. $data['birthday'] = strtotime($birthday);
  772. }
  773. if (isset($params['pay_password']) && $payPassword) {
  774. $payPassword = get_password($payPassword);
  775. $data['pay_password'] = $payPassword;
  776. }
  777. if($this->model->where(['id' => $userId])->update($data)){
  778. // 更新绑定上级后,团队用户全部更新
  779. if(isset($data['parent_id']) && $data['parent_id']){
  780. $updateData = ['parents'=>DB::raw("concat(parents,'{$data['parent_id']},')"),'update_time'=>time()];
  781. $this->model->where('parents','like',"%{$userId},")->update($updateData);
  782. }
  783. // 更新团队节点关系
  784. if(isset($data['point_id']) && $data['point_id']){
  785. $updateData = ['points'=>DB::raw("concat(points,'{$data['point_id']},')"),'update_time'=>time()];
  786. $this->model->where('points','like',"%{$userId},")->update($updateData);
  787. }
  788. $this->error = 1008;
  789. return true;
  790. }
  791. $this->error = 1008;
  792. return true;
  793. }
  794. /**
  795. * 身份认证
  796. * @param $userId
  797. * @param $params
  798. * @return bool
  799. */
  800. public function auth($userId, $params)
  801. {
  802. // 用户验证
  803. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  804. if (!$info) {
  805. $this->error = 2001;
  806. return false;
  807. }
  808. // 使用状态校验
  809. if ($info['status'] != 1) {
  810. $this->error = 2009;
  811. return false;
  812. }
  813. // 密码校验
  814. $data = ['update_time' => time(),'idcard_check'=>1];
  815. if (isset($params['realname']) && $params['realname']) {
  816. $data['realname'] = trim($params['realname']);
  817. }
  818. $idcard = isset($params['idcard']) && $params['idcard'];
  819. if ($idcard) {
  820. $data['idcard'] = trim($params['idcard']);
  821. }
  822. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  823. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  824. }
  825. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  826. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  827. }
  828. $this->model->where(['id' => $userId])->update($data);
  829. // 发送消息
  830. $this->error = 2036;
  831. return true;
  832. }
  833. /**
  834. * 转账
  835. * @param $userId
  836. * @param $params
  837. * @return bool
  838. */
  839. public function transfer($userId, $params)
  840. {
  841. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  842. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  843. $money = isset($params['money'])? floatval($params['money']) : 0;
  844. if(empty($toUserId) || $money<=0){
  845. $this->error = 2420;
  846. return false;
  847. }
  848. $cacheKey = "caches:members:transfer:{$userId}";
  849. if(RedisService::get($cacheKey)){
  850. $this->error = 1034;
  851. return false;
  852. }
  853. // 验证账户
  854. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  855. ->select(['id','nickname','username','usdt','pay_password','balance','status'])
  856. ->first();
  857. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  858. $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  859. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  860. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  861. if(empty($userInfo) || $status != 1){
  862. $this->error = 2024;
  863. return false;
  864. }
  865. // 余额支付支付密码验证
  866. if(empty($userPayPassword)){
  867. $this->error = 1040;
  868. return false;
  869. }
  870. $payPassword = get_password($payPassword);
  871. if($payPassword != $userPayPassword){
  872. $this->error = 2038;
  873. return false;
  874. }
  875. // 验证账户
  876. $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
  877. ->select(['id','nickname','usdt','username','balance','status'])
  878. ->first();
  879. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  880. $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
  881. $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
  882. if(empty($accountInfo) || $status != 1){
  883. $this->error = 2421;
  884. return false;
  885. }
  886. if($money > $usdt){
  887. $this->error = 2422;
  888. return false;
  889. }
  890. // 用户余额转账到用户
  891. DB::beginTransaction();
  892. RedisService::set($cacheKey, $params, rand(2,5));
  893. $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
  894. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  895. DB::rollBack();
  896. $this->error = 2028;
  897. RedisService::clear($cacheKey);
  898. return false;
  899. }
  900. // 明细
  901. $orderNo = get_order_num('TR');
  902. $log = [
  903. 'user_id' => $userId,
  904. 'source_id' => $toUserId,
  905. 'source_order_no' => $orderNo,
  906. 'type' => 6,
  907. 'coin_type' => 1,
  908. 'user_type'=> 1,
  909. 'money' => -$money,
  910. 'actual_money' => -$money,
  911. 'balance' => $usdt,
  912. 'create_time' => time(),
  913. 'update_time' => time(),
  914. 'remark' => "USDT转账",
  915. 'status' => 1,
  916. 'mark' => 1,
  917. ];
  918. if(!AccountLogModel::insertGetId($log)){
  919. DB::rollBack();
  920. $this->error = 2423;
  921. RedisService::clear($cacheKey);
  922. return false;
  923. }
  924. // 到账
  925. $fee = ConfigService::make()->getConfigByCode('transfer_fee_rate',5);
  926. $fee = $fee>0 && $fee<100? moneyFormat($fee/100, 2) : 0;
  927. $feeUsdt = round($money * $fee,2);
  928. $realUsdt = moneyFormat($money - $feeUsdt,2);
  929. $updateData = ['usdt'=> DB::raw("usdt + {$realUsdt}"),'update_time'=> time()];
  930. if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
  931. DB::rollBack();
  932. $this->error = 2424;
  933. RedisService::clear($cacheKey);
  934. return false;
  935. }
  936. // 明细
  937. $log = [
  938. 'user_id' => $toUserId,
  939. 'source_id' => $userId,
  940. 'source_order_no' => $orderNo,
  941. 'type' => 6,
  942. 'coin_type' => 1,
  943. 'user_type'=> 1,
  944. 'money' => $realUsdt,
  945. 'actual_money' => $realUsdt,
  946. 'balance' => $accountUsdt,
  947. 'create_time' => time(),
  948. 'update_time' => time(),
  949. 'remark' => "USDT转账",
  950. 'status' => 1,
  951. 'mark' => 1,
  952. ];
  953. if(!AccountLogModel::insertGetId($log)){
  954. DB::rollBack();
  955. $this->error = 2029;
  956. RedisService::clear($cacheKey);
  957. return false;
  958. }
  959. // 平台明细
  960. $log = [
  961. 'user_id' => 0,
  962. 'source_id' => $userId,
  963. 'source_order_no' => $orderNo,
  964. 'type' => 6,
  965. 'coin_type' => 1,
  966. 'user_type'=> 4,
  967. 'money' => $feeUsdt,
  968. 'actual_money' => $feeUsdt,
  969. 'balance' => 0,
  970. 'create_time' => time(),
  971. 'update_time' => time(),
  972. 'remark' => "USDT转账",
  973. 'status' => 1,
  974. 'mark' => 1,
  975. ];
  976. if(!AccountLogModel::insertGetId($log)){
  977. DB::rollBack();
  978. $this->error = 2029;
  979. RedisService::clear($cacheKey);
  980. return false;
  981. }
  982. // 平台
  983. FinanceService::make()->saveLog(0, $feeUsdt,1);
  984. // 消息
  985. $dateTime = date('Y-m-d H:i:s');
  986. MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
  987. $dateTime = date('Y-m-d H:i:s');
  988. MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}的{$realUsdt}USDT转账",3);
  989. DB::commit();
  990. RedisService::clear($cacheKey);
  991. $this->error = 2425;
  992. return true;
  993. }
  994. /**
  995. * 余额充值
  996. * @param $userId
  997. * @param $params
  998. * @return array|false
  999. */
  1000. public function recharge($userId, $params)
  1001. {
  1002. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1003. $usdt = isset($params['usdt'])? moneyFormat($params['usdt'], 2) : 0;
  1004. $coinType = isset($params['coin_type']) && $params['coin_type']? intval($params['coin_type']) : 2;
  1005. $payType = 10;
  1006. if($usdt<=0){
  1007. $this->error = 2031;
  1008. return false;
  1009. }
  1010. $cacheKey = "caches:member:recharge:lock_{$userId}";
  1011. if(RedisService::get($cacheKey)){
  1012. $this->error = 1034;
  1013. return false;
  1014. }
  1015. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1016. ->select(['id','nickname','usdt','pay_password','balance','status'])
  1017. ->first();
  1018. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1019. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1020. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1021. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1022. if(empty($userInfo) || $status != 1){
  1023. $this->error = 2024;
  1024. return false;
  1025. }
  1026. if($userPayPassword != get_password($payPassword)){
  1027. $this->error = 2038;
  1028. return false;
  1029. }
  1030. // 充值订单
  1031. $orderNo = get_order_num('DR');
  1032. $money = $usdt;
  1033. // 星豆
  1034. DB::beginTransaction();
  1035. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1036. if($coinType == 2){
  1037. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1038. $xdPrice = $xdPrice>0 && $xdPrice <=10000? $xdPrice : 100;
  1039. $money = round($xdPrice * $usdt,2);
  1040. if($usdt > $userUsdt){
  1041. $this->error = 2035;
  1042. RedisService::clear($cacheKey);
  1043. return false;
  1044. }
  1045. // 扣除usdt余额
  1046. $updateData = ['usdt'=>DB::raw("usdt - {$usdt}"),'balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  1047. if(!$this->model->where(['id'=> $userId])->update($updateData)){
  1048. RedisService::clear($cacheKey);
  1049. $this->error = 2036;
  1050. DB::rollBack();
  1051. return false;
  1052. }
  1053. }
  1054. $data = [
  1055. 'order_no'=> $orderNo,
  1056. 'user_id'=> $userId,
  1057. 'type'=> 1,
  1058. 'user_type'=> 1,
  1059. 'coin_type'=> 2,
  1060. 'money'=> $usdt,
  1061. 'actual_money'=> $money,
  1062. 'pay_type'=> $payType,
  1063. 'pay_status'=> 20,
  1064. 'pay_at'=> date('Y-m-d H:i:s'),
  1065. 'create_time'=> time(),
  1066. 'update_time'=> time(),
  1067. 'status'=> 2,
  1068. 'mark'=> 1,
  1069. ];
  1070. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1071. RedisService::clear($cacheKey);
  1072. $this->error = 2033;
  1073. DB::rollBack();
  1074. return false;
  1075. }
  1076. // 扣除USDT
  1077. $log = [
  1078. 'user_id'=> $userId,
  1079. 'source_order_no'=> $data['order_no'],
  1080. 'user_type'=> 1,
  1081. 'type'=> 5,
  1082. 'coin_type'=> 1,
  1083. 'money'=> -$usdt,
  1084. 'actual_money'=> -$usdt,
  1085. 'balance'=> $userUsdt,
  1086. 'date'=> date('Y-m-d'),
  1087. 'create_time'=> time(),
  1088. 'remark'=> '星豆余额充值扣除',
  1089. 'status'=>1,
  1090. 'mark'=>1
  1091. ];
  1092. if(!AccountLogModel::insertGetId($log)){
  1093. RedisService::clear($cacheKey);
  1094. $this->error = 2029;
  1095. DB::rollBack();
  1096. return false;
  1097. }
  1098. // 增加星豆余额
  1099. $log = [
  1100. 'user_id'=> $userId,
  1101. 'source_order_no'=> $data['order_no'],
  1102. 'user_type'=> 1,
  1103. 'type'=> 5,
  1104. 'coin_type'=> 2,
  1105. 'money'=> $usdt,
  1106. 'actual_money'=> $money,
  1107. 'balance'=> $balance,
  1108. 'date'=> date('Y-m-d'),
  1109. 'create_time'=> time(),
  1110. 'remark'=> '星豆余额充值',
  1111. 'status'=>1,
  1112. 'mark'=>1
  1113. ];
  1114. if(!AccountLogModel::insertGetId($log)){
  1115. RedisService::clear($cacheKey);
  1116. $this->error = 2029;
  1117. DB::rollBack();
  1118. return false;
  1119. }
  1120. // 消息
  1121. $dateTime = date('Y-m-d H:i:s');
  1122. MessageService::make()->pushMessage($userId,'星豆余额充值成功',"您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}星豆",3);
  1123. DB::commit();
  1124. // 充值星豆任务
  1125. TaskService::make()->updateTask($userId,9, 0);
  1126. $this->error = 2037;
  1127. RedisService::clear($cacheKey);
  1128. return ['order_id'=> $orderId,'balance'=> moneyFormat($balance + $money,2),'usdt'=> moneyFormat($userUsdt-$usdt, 2)];
  1129. }
  1130. /**
  1131. * 余额提现
  1132. * @param $userId
  1133. * @param $params
  1134. * @return array|false
  1135. */
  1136. public function withdraw($userId, $params)
  1137. {
  1138. $money = isset($params['money'])? floatval($params['money']) : 0;
  1139. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1140. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  1141. if($money<=0){
  1142. $this->error = 2401;
  1143. return false;
  1144. }
  1145. if(!in_array($userType,[1,2])){
  1146. $this->error = 2404;
  1147. return false;
  1148. }
  1149. $userInfo = $this->model->with(['merchant'])
  1150. ->where(['id'=> $userId,'mark'=>1])
  1151. ->select(['id','nickname','usdt','trc_url','pay_password','status'])
  1152. ->first();
  1153. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1154. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1155. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1156. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1157. $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
  1158. $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
  1159. $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
  1160. if(empty($userInfo) || $status != 1){
  1161. $this->error = 2024;
  1162. return false;
  1163. }
  1164. if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
  1165. $this->error = 2024;
  1166. return false;
  1167. }
  1168. // 提现金额验证
  1169. $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
  1170. if ($money > $accountUsdt) {
  1171. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  1172. return false;
  1173. }
  1174. // 提现账户
  1175. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  1176. if (empty($trcUrl)) {
  1177. $this->error = 2403;
  1178. return false;
  1179. }
  1180. if($userPayPassword != get_password($payPassword)){
  1181. $this->error = 2038;
  1182. return false;
  1183. }
  1184. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  1185. if(RedisService::get($cacheKey)){
  1186. $this->error = 1034;
  1187. return false;
  1188. }
  1189. DB::beginTransaction();
  1190. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1191. // 提现记录
  1192. $orderNo = get_order_num('DW');
  1193. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  1194. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  1195. $fee = round($money*$feeRate, 2);
  1196. $realUsdt = moneyFormat($money - $fee, 2);
  1197. $data = [
  1198. 'order_no'=> $orderNo,
  1199. 'user_id'=> $userType==1? $userId : $merchantId,
  1200. 'type'=> 2,
  1201. 'user_type'=> $userType,
  1202. 'coin_type'=> 1,
  1203. 'money'=> $money,
  1204. 'actual_money'=> $realUsdt,
  1205. 'fee'=> $fee,
  1206. 'trc_url'=> $trcUrl,
  1207. 'pay_type'=> 20,
  1208. 'date'=> date('Y-m-d'),
  1209. 'create_time'=> time(),
  1210. 'update_time'=> time(),
  1211. 'status'=> 1,
  1212. 'mark'=> 1,
  1213. ];
  1214. if(!$id = BalanceLogModel::insertGetId($data)){
  1215. DB::rollBack();
  1216. $this->error = 2405;
  1217. RedisService::clear($cacheKey);
  1218. return false;
  1219. }
  1220. // 商户扣款
  1221. if($userType == 2){
  1222. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1223. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1224. DB::rollBack();
  1225. $this->error = 2406;
  1226. RedisService::clear($cacheKey);
  1227. return false;
  1228. }
  1229. // 明细
  1230. $log = [
  1231. 'user_id' => $merchantId,
  1232. 'source_id' => $userId,
  1233. 'source_order_no' => $orderNo,
  1234. 'type' => 5,
  1235. 'coin_type' => 1,
  1236. 'user_type'=> 2,
  1237. 'money' => -$money,
  1238. 'actual_money' => -$money,
  1239. 'balance' => $merchantUsdt,
  1240. 'create_time' => time(),
  1241. 'update_time' => time(),
  1242. 'remark' => "商户账户余额提现",
  1243. 'status' => 1,
  1244. 'mark' => 1,
  1245. ];
  1246. if (!AccountLogModel::insertGetId($log)) {
  1247. $this->error = 2407;
  1248. DB::rollBack();
  1249. RedisService::clear($cacheKey);
  1250. return false;
  1251. }
  1252. }
  1253. // 用户扣款
  1254. else{
  1255. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1256. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1257. DB::rollBack();
  1258. $this->error = 2406;
  1259. RedisService::clear($cacheKey);
  1260. return false;
  1261. }
  1262. // 明细
  1263. $log = [
  1264. 'user_id' => $userId,
  1265. 'source_id' => 0,
  1266. 'source_order_no' => $orderNo,
  1267. 'type' => 5,
  1268. 'coin_type' => 1,
  1269. 'user_type'=> 1,
  1270. 'money' => -$money,
  1271. 'actual_money' => -$money,
  1272. 'balance' => $userUsdt,
  1273. 'create_time' => time(),
  1274. 'update_time' => time(),
  1275. 'remark' => "USDT余额提现",
  1276. 'status' => 1,
  1277. 'mark' => 1,
  1278. ];
  1279. if (!AccountLogModel::insertGetId($log)) {
  1280. $this->error = 2407;
  1281. DB::rollBack();
  1282. RedisService::clear($cacheKey);
  1283. return false;
  1284. }
  1285. }
  1286. DB::commit();
  1287. // 站内消息
  1288. $dateTime = date('Y-m-d H:i:s');
  1289. $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
  1290. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
  1291. MessageService::make()->pushMessage($userId, $title, $message);
  1292. // 提现自动审核,低于该金额自动审核
  1293. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  1294. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  1295. if ($money <= $autoCheckUsdt) {
  1296. // 打款处理
  1297. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  1298. $txID = isset($result['txId']) ? $result['txId'] : '';
  1299. $payAddress = isset($result['address']) ? $result['address'] : '';
  1300. if ($txID && $payAddress) {
  1301. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  1302. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  1303. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  1304. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  1305. MessageService::make()->pushMessage($userId, $title, $message);
  1306. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  1307. // 平台明细
  1308. $log = [
  1309. 'user_id' => 0,
  1310. 'source_id' => $userId,
  1311. 'source_order_no' => $orderNo,
  1312. 'type' => 5,
  1313. 'coin_type' => 1,
  1314. 'user_type'=> 4,
  1315. 'money' => $fee,
  1316. 'actual_money' => $fee,
  1317. 'balance' => 0,
  1318. 'create_time' => time(),
  1319. 'update_time' => time(),
  1320. 'hash' => $txID,
  1321. 'remark' => "USDT余额提现",
  1322. 'status' => 1,
  1323. 'mark' => 1,
  1324. ];
  1325. AccountLogModel::insertGetId($log);
  1326. // 平台流水
  1327. FinanceService::make()->saveLog(0, $fee, 1);
  1328. }
  1329. }
  1330. }
  1331. $this->error = $title;
  1332. RedisService::clear($cacheKey);
  1333. return [
  1334. 'id'=> $id,
  1335. 'money'=> $money,
  1336. 'user_type'=> $userType,
  1337. ];
  1338. }
  1339. /**
  1340. * 获取点对点上级节点
  1341. * @param $userId 推荐人ID
  1342. * @return int|mixed
  1343. */
  1344. public function getPointParentId($userId)
  1345. {
  1346. if($userId<=0){
  1347. return 0;
  1348. }
  1349. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1350. $userList = $userList? $userList->toArray() : [];
  1351. if(count($userList) < 3){
  1352. return $userId;
  1353. }
  1354. $users = [];
  1355. foreach($userList as $item){
  1356. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1357. $points = array_filter($points);
  1358. $users[$item['id']]['pointLen'] = count($points);
  1359. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1360. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1361. }
  1362. //var_dump($users);
  1363. $pointId = 0;
  1364. $tempPointLen = 0;
  1365. $tempChildren = 0;
  1366. foreach ($userList as $item) {
  1367. $id = isset($item['id'])? $item['id'] : 0;
  1368. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1369. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1370. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1371. if($pointLen<=$tempPointLen || $pointId == 0){
  1372. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1373. if($children <$tempChildren || $pointId == 0){
  1374. $pointId = $id;
  1375. $tempPointLen = $pointLen;
  1376. $tempChildren = $children;
  1377. }
  1378. }
  1379. }
  1380. return $pointId;
  1381. }
  1382. /**
  1383. * DAO升级购买
  1384. * @param $userId 用户ID
  1385. * @param $params 参数:level-等级,pay_password-交易密码
  1386. * @return bool
  1387. */
  1388. public function upgrade($userId, $params)
  1389. {
  1390. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1391. $level = isset($params['level'])? intval($params['level']) : 0;
  1392. if($level<=0 || empty($payPassword)){
  1393. $this->error = 2014;
  1394. return false;
  1395. }
  1396. $cacheKey = "caches:members:upgrade:buy_{$userId}_{$level}";
  1397. if(RedisService::get($cacheKey.'_lock')){
  1398. $this->error = 1034;
  1399. return false;
  1400. }
  1401. $levelInfo = MemberLevelModel::from('member_level as a')
  1402. ->where(['a.id'=> $level,'a.status'=>1,'a.mark'=>1])
  1403. ->select(['a.*'])
  1404. ->first();
  1405. $upgradeUsdt = isset($levelInfo['upgrade_usdt'])? floatval($levelInfo['upgrade_usdt']) : 0;
  1406. $upgradeProfit = isset($levelInfo['upgrade_profit'])? floatval($levelInfo['upgrade_profit']) : 0;
  1407. $boxNum = isset($levelInfo['box_num'])? intval($levelInfo['box_num']) : 0;
  1408. $levelName = isset($levelInfo['name'])? $levelInfo['name'] : '';
  1409. if(empty($levelInfo)){
  1410. $this->error = 2511;
  1411. return false;
  1412. }
  1413. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  1414. ->select(['id','nickname','usdt','wait_score','parents','points','parent_id','member_level','power_num','trc_url','pay_password','status'])
  1415. ->first();
  1416. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1417. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1418. $userTotalProfit = isset($userInfo['upgrade_profit_total'])? $userInfo['upgrade_profit_total'] : 0;
  1419. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1420. $userWaitScore = isset($userInfo['wait_score'])? $userInfo['wait_score'] : 0;
  1421. $userPowerNum = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  1422. $userLevel = isset($userInfo['member_level'])? intval($userInfo['member_level']) : 0;
  1423. if(empty($userInfo) || $status != 1){
  1424. $this->error = 2024;
  1425. return false;
  1426. }
  1427. if($userLevel>= $level){
  1428. $this->error = 2512;
  1429. return false;
  1430. }
  1431. if($level >= 30){
  1432. $this->error = 2513;
  1433. return false;
  1434. }
  1435. // 收益是否满足条件
  1436. if($upgradeProfit>0 && $userTotalProfit < $upgradeProfit){
  1437. $this->error = 2514;
  1438. return false;
  1439. }
  1440. if(AccountLogModel::where(['user_id'=> $userId,'source_id'=> $level,'type'=>2,'coin_type'=>1,'status'=>1,'mark'=>1])->value('id')){
  1441. $this->error = 2512;
  1442. return false;
  1443. }
  1444. // USDT余额是否足够
  1445. if($userUsdt < $upgradeUsdt){
  1446. $this->error = 2035;
  1447. return false;
  1448. }
  1449. // 交易密码
  1450. if(empty($userPayPassword)){
  1451. $this->error = 1040;
  1452. return false;
  1453. }
  1454. if($userPayPassword != get_password($payPassword)){
  1455. $this->error = 2038;
  1456. return false;
  1457. }
  1458. // 购买明细
  1459. RedisService::set($cacheKey, $params, rand(2,3));
  1460. $orderNo = get_order_num('UP');
  1461. DB::beginTransaction();
  1462. $log = [
  1463. 'user_id' => $userId,
  1464. 'source_id' => $level,
  1465. 'source_order_no' => $orderNo,
  1466. 'type' => 2,
  1467. 'coin_type' => 1,
  1468. 'user_type'=> 1,
  1469. 'money' => -$upgradeUsdt,
  1470. 'actual_money' => -$upgradeUsdt,
  1471. 'balance' => $userUsdt,
  1472. 'create_time' => time(),
  1473. 'update_time' => time(),
  1474. 'remark' => "购买{$levelName}升级",
  1475. 'status' => 1,
  1476. 'mark' => 1,
  1477. ];
  1478. if(!AccountLogModel::insertGetId($log)){
  1479. DB::rollBack();
  1480. $this->error = 2029;
  1481. RedisService::clear($cacheKey);
  1482. return false;
  1483. }
  1484. // 待返积分
  1485. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1486. $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
  1487. $waitScoreRate = ConfigService::make()->getConfigByCode('upgrade_award_score_rate', 200);
  1488. $waitScoreRate = $waitScoreRate>0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  1489. $waitScore = moneyFormat($upgradeUsdt * $xdPrice * $waitScoreRate/100, 2);
  1490. if($waitScore>0){
  1491. $log = [
  1492. 'user_id' => $userId,
  1493. 'source_id' => $level,
  1494. 'source_order_no' => $orderNo,
  1495. 'type' => 9,
  1496. 'coin_type' => 5,
  1497. 'user_type'=> 1,
  1498. 'money' => $waitScore,
  1499. 'actual_money' => $waitScore,
  1500. 'balance' => $userWaitScore,
  1501. 'create_time' => time(),
  1502. 'update_time' => time(),
  1503. 'remark' => "购买{$levelName}升级",
  1504. 'status' => 1,
  1505. 'mark' => 1,
  1506. ];
  1507. if(!AccountLogModel::insertGetId($log)){
  1508. DB::rollBack();
  1509. $this->error = 2029;
  1510. RedisService::clear($cacheKey);
  1511. return false;
  1512. }
  1513. }
  1514. // 算力明细
  1515. $powerNum = $upgradeUsdt;
  1516. $log = [
  1517. 'user_id' => $userId,
  1518. 'source_id' => $level,
  1519. 'source_order_no' => $orderNo,
  1520. 'type' => 9,
  1521. 'coin_type' => 3,
  1522. 'user_type'=> 1,
  1523. 'money' => $powerNum,
  1524. 'actual_money' => $powerNum,
  1525. 'balance' => $userPowerNum,
  1526. 'create_time' => time(),
  1527. 'update_time' => time(),
  1528. 'remark' => "购买{$levelName}升级",
  1529. 'status' => 1,
  1530. 'mark' => 1,
  1531. ];
  1532. if(!AccountLogModel::insertGetId($log)){
  1533. DB::rollBack();
  1534. $this->error = 2029;
  1535. RedisService::clear($cacheKey);
  1536. return false;
  1537. }
  1538. // 账户
  1539. $updateData = [
  1540. 'usdt'=> DB::raw("usdt - {$upgradeUsdt}"), // 扣除USDT
  1541. 'member_level'=> $level, // 升级等级
  1542. 'box_num'=>DB::raw("box_num + {$boxNum}"), // 奖励盲盒数量
  1543. 'wait_score'=>DB::raw("wait_score + {$waitScore}"), // 待返积分奖励
  1544. 'power_num'=>DB::raw("power_num + {$powerNum}"), // 算力奖励
  1545. 'update_time'=>time()
  1546. ];
  1547. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  1548. DB::rollBack();
  1549. $this->error = 2036;
  1550. RedisService::clear($cacheKey);
  1551. return false;
  1552. }
  1553. // 消息
  1554. $dateTime = date('Y-m-d H:i:s');
  1555. MessageService::make()->pushMessage($userId,"购买{$levelName}升级成功","您在{$dateTime}(UTC+8)成功支付{$upgradeUsdt}USDT购买【{$levelName}】升级成功。",3);
  1556. DB::commit();
  1557. // 算力统计
  1558. AccountLogService::make()->saveCount($userId, $powerNum, 1);
  1559. // 业绩统计
  1560. AccountLogService::make()->saveCount($userId, $upgradeUsdt, 2);
  1561. // 结算收益奖励
  1562. FinanceService::make()->settleBonus($userId, $userInfo, $upgradeUsdt, $orderNo, "购买{$levelName}");
  1563. $this->error = 2503;
  1564. RedisService::clear($cacheKey);
  1565. return true;
  1566. }
  1567. }