MemberService.php 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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. $scene = isset($params['scene'])? trim($params['scene']) : '';
  625. if(empty($email) && empty($mobile)){
  626. $this->error = 1064;
  627. return false;
  628. }
  629. // 用户验证
  630. $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])->select(['id','pay_password','parent_id','code', 'status'])->first();
  631. if (!$info) {
  632. $this->error = 2001;
  633. return false;
  634. }
  635. // 使用状态校验
  636. if ($info['status'] != 1) {
  637. $this->error = 2009;
  638. return false;
  639. }
  640. // 密码校验
  641. $data = ['update_time' => time()];
  642. // 验证交易密码
  643. if(isset($params['pay_password']) && $scene != 'trade'){
  644. $userPayPassword = isset($info['pay_password'])? $info['pay_password'] : '';
  645. if(empty($userPayPassword)){
  646. $this->error = 1040;
  647. return false;
  648. }
  649. if($userPayPassword != get_password($payPassword)){
  650. $this->error = 2038;
  651. return false;
  652. }
  653. }
  654. // 邀请人绑定
  655. $parent = isset($info['parent'])? $info['parent'] : [];
  656. $userParentCode = isset($parent['code'])? $parent['code'] : '';
  657. $userCode = isset($info['code'])? $info['code'] : '';
  658. $parentCode = isset($params['parent_code'])? trim($params['parent_code']) : '';
  659. if(isset($params['parent_code']) && $parentCode){
  660. //
  661. if($userParentCode){
  662. $this->error = 2451;
  663. return false;
  664. }
  665. if(($userCode == $parentCode)){
  666. $this->error = 2452;
  667. return false;
  668. }
  669. // 推荐人验证
  670. $inviteInfo = $this->model->where(['code' => $parentCode, 'mark' => 1])
  671. ->select(['id', 'parents', 'parent_id','trc_url'])
  672. ->first();
  673. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  674. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  675. if(!$parentId){
  676. $this->error = 2453;
  677. return false;
  678. }
  679. // 验证是否绑定用户为自己线下用户
  680. if(preg_match("/{$userId},/", $parents)){
  681. $this->error = 2454;
  682. return false;
  683. }
  684. // 推荐人
  685. $data['parent_id'] = $parentId;
  686. $data['parents'] = $parents;
  687. // 挂靠节点
  688. if($parentId>0){
  689. $pointId = $this->getPointParentId($parentId);
  690. if($pointId){
  691. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  692. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  693. $data['point_id'] = $pointId;
  694. $data['points'] = $points;
  695. }
  696. }
  697. }
  698. // 邮箱验证
  699. if (isset($params['email']) && $email) {
  700. // 修改邮箱
  701. $newEmail = isset($params['new_email'])? trim($params['new_email']) : '';
  702. if(isset($params['new_email']) && $newEmail){
  703. $data['email'] = $newEmail;
  704. $data['username'] = $newEmail;
  705. $checkInfo = $this->model->where(['username' => $newEmail, 'mark' => 1])->select(['id', 'status'])->first();
  706. if ($checkInfo && $checkInfo['id'] != $userId) {
  707. $this->error = 2005;
  708. return false;
  709. }
  710. }
  711. // 邮箱验证码
  712. $code = isset($params['code'])? trim($params['code']) : '';
  713. $checkEmail = $scene == 'modify_email'? $newEmail : $email;
  714. if (!EmailService::make()->check($checkEmail, $code, $scene)) {
  715. $this->error = EmailService::make()->getError();
  716. return false;
  717. }
  718. }
  719. // 手机号验证
  720. if (isset($params['mobile']) && $mobile) {
  721. // 修改手机号
  722. $newMobile = isset($params['new_mobile'])? trim($params['new_mobile']) : '';
  723. if(isset($params['new_mobile']) && $newMobile){
  724. $data['mobile'] = $newMobile;
  725. $data['username'] = $newMobile;
  726. $checkInfo = $this->model->where(['mobile' => $newMobile, 'mark' => 1])->select(['id', 'status'])->first();
  727. if ($checkInfo && $checkInfo['id'] != $userId) {
  728. $this->error = 2005;
  729. return false;
  730. }
  731. }
  732. // 手机验证码
  733. $code = isset($params['code'])? trim($params['code']) : '';
  734. $checkMobile = $scene == 'modify_mobile'? $newMobile : $mobile;
  735. if (!SmsService::make()->check($checkMobile, $code, $scene)) {
  736. $this->error = SmsService::make()->getError();
  737. return false;
  738. }
  739. }
  740. $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : '';
  741. $userTrcUrl = isset($info['trc_url'])? trim($info['trc_url']) : '';
  742. if(isset($params['trc_url']) && $trcUrl){
  743. if($trcUrl != $userTrcUrl){
  744. if($this->model->where(['trc_url'=> $trcUrl,'mark'=>1])->whereNotIn('id',[$userId])->value('id')){
  745. $this->error = 1048;
  746. return false;
  747. }
  748. if(!WalletService::make()->checkAddress($trcUrl)){
  749. $this->error = 1049;
  750. return false;
  751. }
  752. $data['trc_url'] = $trcUrl;
  753. }
  754. }
  755. // 修改数据
  756. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  757. if (isset($params['nickname']) && $nickname) {
  758. $data['nickname'] = $nickname;
  759. }
  760. $gender = isset($params['gender']) ? intval($params['gender']) : 3;
  761. if (isset($params['gender']) && $gender) {
  762. $data['gender'] = $gender;
  763. }
  764. $intro = isset($params['intro']) ? $params['intro'] : '';
  765. if (isset($params['intro']) && $intro) {
  766. $data['intro'] = $intro;
  767. }
  768. $birthday = isset($params['birthday_text']) ? $params['birthday_text'] : '';
  769. if (isset($params['birthday_text']) && $birthday) {
  770. $data['birthday'] = strtotime($birthday);
  771. }
  772. if (isset($params['pay_password']) && $payPassword) {
  773. $payPassword = get_password($payPassword);
  774. $data['pay_password'] = $payPassword;
  775. }
  776. if($this->model->where(['id' => $userId])->update($data)){
  777. // 更新绑定上级后,团队用户全部更新
  778. if(isset($data['parent_id']) && $data['parent_id']){
  779. $updateData = ['parents'=>DB::raw("concat(parents,'{$data['parent_id']},')"),'update_time'=>time()];
  780. $this->model->where('parents','like',"%{$userId},")->update($updateData);
  781. }
  782. // 更新团队节点关系
  783. if(isset($data['point_id']) && $data['point_id']){
  784. $updateData = ['points'=>DB::raw("concat(points,'{$data['point_id']},')"),'update_time'=>time()];
  785. $this->model->where('points','like',"%{$userId},")->update($updateData);
  786. }
  787. $this->error = 1008;
  788. return true;
  789. }
  790. $this->error = 1008;
  791. return true;
  792. }
  793. /**
  794. * 身份认证
  795. * @param $userId
  796. * @param $params
  797. * @return bool
  798. */
  799. public function auth($userId, $params)
  800. {
  801. // 用户验证
  802. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  803. if (!$info) {
  804. $this->error = 2001;
  805. return false;
  806. }
  807. // 使用状态校验
  808. if ($info['status'] != 1) {
  809. $this->error = 2009;
  810. return false;
  811. }
  812. // 密码校验
  813. $data = ['update_time' => time(),'idcard_check'=>1];
  814. if (isset($params['realname']) && $params['realname']) {
  815. $data['realname'] = trim($params['realname']);
  816. }
  817. $idcard = isset($params['idcard']) && $params['idcard'];
  818. if ($idcard) {
  819. $data['idcard'] = trim($params['idcard']);
  820. }
  821. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  822. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  823. }
  824. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  825. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  826. }
  827. $this->model->where(['id' => $userId])->update($data);
  828. // 发送消息
  829. $this->error = 2036;
  830. return true;
  831. }
  832. /**
  833. * 转账
  834. * @param $userId
  835. * @param $params
  836. * @return bool
  837. */
  838. public function transfer($userId, $params)
  839. {
  840. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  841. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  842. $money = isset($params['money'])? floatval($params['money']) : 0;
  843. if(empty($toUserId) || $money<=0){
  844. $this->error = 2420;
  845. return false;
  846. }
  847. $cacheKey = "caches:members:transfer:{$userId}";
  848. if(RedisService::get($cacheKey)){
  849. $this->error = 1034;
  850. return false;
  851. }
  852. // 验证账户
  853. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  854. ->select(['id','nickname','username','usdt','pay_password','balance','status'])
  855. ->first();
  856. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  857. $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  858. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  859. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  860. if(empty($userInfo) || $status != 1){
  861. $this->error = 2024;
  862. return false;
  863. }
  864. // 余额支付支付密码验证
  865. if(empty($userPayPassword)){
  866. $this->error = 1040;
  867. return false;
  868. }
  869. $payPassword = get_password($payPassword);
  870. if($payPassword != $userPayPassword){
  871. $this->error = 2038;
  872. return false;
  873. }
  874. // 验证账户
  875. $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
  876. ->select(['id','nickname','usdt','username','balance','status'])
  877. ->first();
  878. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  879. $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
  880. $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
  881. if(empty($accountInfo) || $status != 1){
  882. $this->error = 2421;
  883. return false;
  884. }
  885. if($money > $usdt){
  886. $this->error = 2422;
  887. return false;
  888. }
  889. // 用户余额转账到用户
  890. DB::beginTransaction();
  891. RedisService::set($cacheKey, $params, rand(2,5));
  892. $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
  893. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  894. DB::rollBack();
  895. $this->error = 2028;
  896. RedisService::clear($cacheKey);
  897. return false;
  898. }
  899. // 明细
  900. $orderNo = get_order_num('TR');
  901. $log = [
  902. 'user_id' => $userId,
  903. 'source_id' => $toUserId,
  904. 'source_order_no' => $orderNo,
  905. 'type' => 6,
  906. 'coin_type' => 1,
  907. 'user_type'=> 1,
  908. 'money' => -$money,
  909. 'actual_money' => -$money,
  910. 'balance' => $usdt,
  911. 'create_time' => time(),
  912. 'update_time' => time(),
  913. 'remark' => "USDT转账",
  914. 'status' => 1,
  915. 'mark' => 1,
  916. ];
  917. if(!AccountLogModel::insertGetId($log)){
  918. DB::rollBack();
  919. $this->error = 2423;
  920. RedisService::clear($cacheKey);
  921. return false;
  922. }
  923. // 到账
  924. $fee = ConfigService::make()->getConfigByCode('transfer_fee_rate',5);
  925. $fee = $fee>0 && $fee<100? moneyFormat($fee/100, 2) : 0;
  926. $feeUsdt = round($money * $fee,2);
  927. $realUsdt = moneyFormat($money - $feeUsdt,2);
  928. $updateData = ['usdt'=> DB::raw("usdt + {$realUsdt}"),'update_time'=> time()];
  929. if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
  930. DB::rollBack();
  931. $this->error = 2424;
  932. RedisService::clear($cacheKey);
  933. return false;
  934. }
  935. // 明细
  936. $log = [
  937. 'user_id' => $toUserId,
  938. 'source_id' => $userId,
  939. 'source_order_no' => $orderNo,
  940. 'type' => 6,
  941. 'coin_type' => 1,
  942. 'user_type'=> 1,
  943. 'money' => $realUsdt,
  944. 'actual_money' => $realUsdt,
  945. 'balance' => $accountUsdt,
  946. 'create_time' => time(),
  947. 'update_time' => time(),
  948. 'remark' => "USDT转账",
  949. 'status' => 1,
  950. 'mark' => 1,
  951. ];
  952. if(!AccountLogModel::insertGetId($log)){
  953. DB::rollBack();
  954. $this->error = 2029;
  955. RedisService::clear($cacheKey);
  956. return false;
  957. }
  958. // 平台明细
  959. $log = [
  960. 'user_id' => 0,
  961. 'source_id' => $userId,
  962. 'source_order_no' => $orderNo,
  963. 'type' => 6,
  964. 'coin_type' => 1,
  965. 'user_type'=> 4,
  966. 'money' => $feeUsdt,
  967. 'actual_money' => $feeUsdt,
  968. 'balance' => 0,
  969. 'create_time' => time(),
  970. 'update_time' => time(),
  971. 'remark' => "USDT转账",
  972. 'status' => 1,
  973. 'mark' => 1,
  974. ];
  975. if(!AccountLogModel::insertGetId($log)){
  976. DB::rollBack();
  977. $this->error = 2029;
  978. RedisService::clear($cacheKey);
  979. return false;
  980. }
  981. // 平台
  982. FinanceService::make()->saveLog(0, $feeUsdt,1);
  983. // 消息
  984. $dateTime = date('Y-m-d H:i:s');
  985. MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
  986. $dateTime = date('Y-m-d H:i:s');
  987. MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}的{$realUsdt}USDT转账",3);
  988. DB::commit();
  989. RedisService::clear($cacheKey);
  990. $this->error = 2425;
  991. return true;
  992. }
  993. /**
  994. * 余额充值
  995. * @param $userId
  996. * @param $params
  997. * @return array|false
  998. */
  999. public function recharge($userId, $params)
  1000. {
  1001. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1002. $usdt = isset($params['usdt'])? moneyFormat($params['usdt'], 2) : 0;
  1003. $coinType = isset($params['coin_type']) && $params['coin_type']? intval($params['coin_type']) : 2;
  1004. $payType = 10;
  1005. if($usdt<=0){
  1006. $this->error = 2031;
  1007. return false;
  1008. }
  1009. $cacheKey = "caches:member:recharge:lock_{$userId}";
  1010. if(RedisService::get($cacheKey)){
  1011. $this->error = 1034;
  1012. return false;
  1013. }
  1014. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1015. ->select(['id','nickname','usdt','pay_password','balance','status'])
  1016. ->first();
  1017. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1018. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1019. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1020. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1021. if(empty($userInfo) || $status != 1){
  1022. $this->error = 2024;
  1023. return false;
  1024. }
  1025. if($userPayPassword != get_password($payPassword)){
  1026. $this->error = 2038;
  1027. return false;
  1028. }
  1029. // 充值订单
  1030. $orderNo = get_order_num('DR');
  1031. $money = $usdt;
  1032. // 星豆
  1033. DB::beginTransaction();
  1034. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1035. if($coinType == 2){
  1036. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1037. $xdPrice = $xdPrice>0 && $xdPrice <=10000? $xdPrice : 100;
  1038. $money = round($xdPrice * $usdt,2);
  1039. if($usdt > $userUsdt){
  1040. $this->error = 2035;
  1041. RedisService::clear($cacheKey);
  1042. return false;
  1043. }
  1044. // 扣除usdt余额
  1045. $updateData = ['usdt'=>DB::raw("usdt - {$usdt}"),'balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  1046. if(!$this->model->where(['id'=> $userId])->update($updateData)){
  1047. RedisService::clear($cacheKey);
  1048. $this->error = 2036;
  1049. DB::rollBack();
  1050. return false;
  1051. }
  1052. }
  1053. $data = [
  1054. 'order_no'=> $orderNo,
  1055. 'user_id'=> $userId,
  1056. 'type'=> 1,
  1057. 'user_type'=> 1,
  1058. 'coin_type'=> 2,
  1059. 'money'=> $usdt,
  1060. 'actual_money'=> $money,
  1061. 'pay_type'=> $payType,
  1062. 'pay_status'=> 20,
  1063. 'pay_at'=> date('Y-m-d H:i:s'),
  1064. 'create_time'=> time(),
  1065. 'update_time'=> time(),
  1066. 'status'=> 2,
  1067. 'mark'=> 1,
  1068. ];
  1069. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1070. RedisService::clear($cacheKey);
  1071. $this->error = 2033;
  1072. DB::rollBack();
  1073. return false;
  1074. }
  1075. // 扣除USDT
  1076. $log = [
  1077. 'user_id'=> $userId,
  1078. 'source_order_no'=> $data['order_no'],
  1079. 'user_type'=> 1,
  1080. 'type'=> 5,
  1081. 'coin_type'=> 1,
  1082. 'money'=> -$usdt,
  1083. 'actual_money'=> -$usdt,
  1084. 'balance'=> $userUsdt,
  1085. 'date'=> date('Y-m-d'),
  1086. 'create_time'=> time(),
  1087. 'remark'=> '星豆余额充值扣除',
  1088. 'status'=>1,
  1089. 'mark'=>1
  1090. ];
  1091. if(!AccountLogModel::insertGetId($log)){
  1092. RedisService::clear($cacheKey);
  1093. $this->error = 2029;
  1094. DB::rollBack();
  1095. return false;
  1096. }
  1097. // 增加星豆余额
  1098. $log = [
  1099. 'user_id'=> $userId,
  1100. 'source_order_no'=> $data['order_no'],
  1101. 'user_type'=> 1,
  1102. 'type'=> 5,
  1103. 'coin_type'=> 2,
  1104. 'money'=> $usdt,
  1105. 'actual_money'=> $money,
  1106. 'balance'=> $balance,
  1107. 'date'=> date('Y-m-d'),
  1108. 'create_time'=> time(),
  1109. 'remark'=> '星豆余额充值',
  1110. 'status'=>1,
  1111. 'mark'=>1
  1112. ];
  1113. if(!AccountLogModel::insertGetId($log)){
  1114. RedisService::clear($cacheKey);
  1115. $this->error = 2029;
  1116. DB::rollBack();
  1117. return false;
  1118. }
  1119. // 消息
  1120. $dateTime = date('Y-m-d H:i:s');
  1121. MessageService::make()->pushMessage($userId,'星豆余额充值成功',"您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}星豆",3);
  1122. DB::commit();
  1123. // 充值星豆任务
  1124. TaskService::make()->updateTask($userId,9, 0);
  1125. $this->error = 2037;
  1126. RedisService::clear($cacheKey);
  1127. return ['order_id'=> $orderId,'balance'=> moneyFormat($balance + $money,2),'usdt'=> moneyFormat($userUsdt-$usdt, 2)];
  1128. }
  1129. /**
  1130. * 余额提现
  1131. * @param $userId
  1132. * @param $params
  1133. * @return array|false
  1134. */
  1135. public function withdraw($userId, $params)
  1136. {
  1137. $money = isset($params['money'])? floatval($params['money']) : 0;
  1138. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1139. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  1140. if($money<=0){
  1141. $this->error = 2401;
  1142. return false;
  1143. }
  1144. if(!in_array($userType,[1,2])){
  1145. $this->error = 2404;
  1146. return false;
  1147. }
  1148. $userInfo = $this->model->with(['merchant'])
  1149. ->where(['id'=> $userId,'mark'=>1])
  1150. ->select(['id','nickname','usdt','trc_url','pay_password','status'])
  1151. ->first();
  1152. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1153. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1154. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1155. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1156. $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
  1157. $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
  1158. $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
  1159. if(empty($userInfo) || $status != 1){
  1160. $this->error = 2024;
  1161. return false;
  1162. }
  1163. if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
  1164. $this->error = 2024;
  1165. return false;
  1166. }
  1167. // 提现金额验证
  1168. $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
  1169. if ($money > $accountUsdt) {
  1170. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  1171. return false;
  1172. }
  1173. // 提现账户
  1174. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  1175. if (empty($trcUrl)) {
  1176. $this->error = 2403;
  1177. return false;
  1178. }
  1179. if($userPayPassword != get_password($payPassword)){
  1180. $this->error = 2038;
  1181. return false;
  1182. }
  1183. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  1184. if(RedisService::get($cacheKey)){
  1185. $this->error = 1034;
  1186. return false;
  1187. }
  1188. DB::beginTransaction();
  1189. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1190. // 提现记录
  1191. $orderNo = get_order_num('DW');
  1192. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  1193. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  1194. $fee = round($money*$feeRate, 2);
  1195. $realUsdt = moneyFormat($money - $fee, 2);
  1196. $data = [
  1197. 'order_no'=> $orderNo,
  1198. 'user_id'=> $userType==1? $userId : $merchantId,
  1199. 'type'=> 2,
  1200. 'user_type'=> $userType,
  1201. 'coin_type'=> 1,
  1202. 'money'=> $money,
  1203. 'actual_money'=> $realUsdt,
  1204. 'fee'=> $fee,
  1205. 'trc_url'=> $trcUrl,
  1206. 'pay_type'=> 20,
  1207. 'date'=> date('Y-m-d'),
  1208. 'create_time'=> time(),
  1209. 'update_time'=> time(),
  1210. 'status'=> 1,
  1211. 'mark'=> 1,
  1212. ];
  1213. if(!$id = BalanceLogModel::insertGetId($data)){
  1214. DB::rollBack();
  1215. $this->error = 2405;
  1216. RedisService::clear($cacheKey);
  1217. return false;
  1218. }
  1219. // 商户扣款
  1220. if($userType == 2){
  1221. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1222. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1223. DB::rollBack();
  1224. $this->error = 2406;
  1225. RedisService::clear($cacheKey);
  1226. return false;
  1227. }
  1228. // 明细
  1229. $log = [
  1230. 'user_id' => $merchantId,
  1231. 'source_id' => $userId,
  1232. 'source_order_no' => $orderNo,
  1233. 'type' => 5,
  1234. 'coin_type' => 1,
  1235. 'user_type'=> 2,
  1236. 'money' => -$money,
  1237. 'actual_money' => -$money,
  1238. 'balance' => $merchantUsdt,
  1239. 'create_time' => time(),
  1240. 'update_time' => time(),
  1241. 'remark' => "商户账户余额提现",
  1242. 'status' => 1,
  1243. 'mark' => 1,
  1244. ];
  1245. if (!AccountLogModel::insertGetId($log)) {
  1246. $this->error = 2407;
  1247. DB::rollBack();
  1248. RedisService::clear($cacheKey);
  1249. return false;
  1250. }
  1251. }
  1252. // 用户扣款
  1253. else{
  1254. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1255. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1256. DB::rollBack();
  1257. $this->error = 2406;
  1258. RedisService::clear($cacheKey);
  1259. return false;
  1260. }
  1261. // 明细
  1262. $log = [
  1263. 'user_id' => $userId,
  1264. 'source_id' => 0,
  1265. 'source_order_no' => $orderNo,
  1266. 'type' => 5,
  1267. 'coin_type' => 1,
  1268. 'user_type'=> 1,
  1269. 'money' => -$money,
  1270. 'actual_money' => -$money,
  1271. 'balance' => $userUsdt,
  1272. 'create_time' => time(),
  1273. 'update_time' => time(),
  1274. 'remark' => "USDT余额提现",
  1275. 'status' => 1,
  1276. 'mark' => 1,
  1277. ];
  1278. if (!AccountLogModel::insertGetId($log)) {
  1279. $this->error = 2407;
  1280. DB::rollBack();
  1281. RedisService::clear($cacheKey);
  1282. return false;
  1283. }
  1284. }
  1285. DB::commit();
  1286. // 站内消息
  1287. $dateTime = date('Y-m-d H:i:s');
  1288. $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
  1289. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
  1290. MessageService::make()->pushMessage($userId, $title, $message);
  1291. // 提现自动审核,低于该金额自动审核
  1292. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  1293. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  1294. if ($money <= $autoCheckUsdt) {
  1295. // 打款处理
  1296. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  1297. $txID = isset($result['txId']) ? $result['txId'] : '';
  1298. $payAddress = isset($result['address']) ? $result['address'] : '';
  1299. if ($txID && $payAddress) {
  1300. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  1301. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  1302. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  1303. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  1304. MessageService::make()->pushMessage($userId, $title, $message);
  1305. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  1306. // 平台明细
  1307. $log = [
  1308. 'user_id' => 0,
  1309. 'source_id' => $userId,
  1310. 'source_order_no' => $orderNo,
  1311. 'type' => 5,
  1312. 'coin_type' => 1,
  1313. 'user_type'=> 4,
  1314. 'money' => $fee,
  1315. 'actual_money' => $fee,
  1316. 'balance' => 0,
  1317. 'create_time' => time(),
  1318. 'update_time' => time(),
  1319. 'hash' => $txID,
  1320. 'remark' => "USDT余额提现",
  1321. 'status' => 1,
  1322. 'mark' => 1,
  1323. ];
  1324. AccountLogModel::insertGetId($log);
  1325. // 平台流水
  1326. FinanceService::make()->saveLog(0, $fee, 1);
  1327. }
  1328. }
  1329. }
  1330. $this->error = $title;
  1331. RedisService::clear($cacheKey);
  1332. return [
  1333. 'id'=> $id,
  1334. 'money'=> $money,
  1335. 'user_type'=> $userType,
  1336. ];
  1337. }
  1338. /**
  1339. * 获取点对点上级节点
  1340. * @param $userId 推荐人ID
  1341. * @return int|mixed
  1342. */
  1343. public function getPointParentId($userId)
  1344. {
  1345. if($userId<=0){
  1346. return 0;
  1347. }
  1348. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1349. $userList = $userList? $userList->toArray() : [];
  1350. if(count($userList) < 3){
  1351. return $userId;
  1352. }
  1353. $users = [];
  1354. foreach($userList as $item){
  1355. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1356. $points = array_filter($points);
  1357. $users[$item['id']]['pointLen'] = count($points);
  1358. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1359. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1360. }
  1361. //var_dump($users);
  1362. $pointId = 0;
  1363. $tempPointLen = 0;
  1364. $tempChildren = 0;
  1365. foreach ($userList as $item) {
  1366. $id = isset($item['id'])? $item['id'] : 0;
  1367. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1368. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1369. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1370. if($pointLen<=$tempPointLen || $pointId == 0){
  1371. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1372. if($children <$tempChildren || $pointId == 0){
  1373. $pointId = $id;
  1374. $tempPointLen = $pointLen;
  1375. $tempChildren = $children;
  1376. }
  1377. }
  1378. }
  1379. return $pointId;
  1380. }
  1381. /**
  1382. * DAO升级购买
  1383. * @param $userId 用户ID
  1384. * @param $params 参数:level-等级,pay_password-交易密码
  1385. * @return bool
  1386. */
  1387. public function upgrade($userId, $params)
  1388. {
  1389. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1390. $level = isset($params['level'])? intval($params['level']) : 0;
  1391. if($level<=0 || empty($payPassword)){
  1392. $this->error = 2014;
  1393. return false;
  1394. }
  1395. $cacheKey = "caches:members:upgrade:buy_{$userId}_{$level}";
  1396. if(RedisService::get($cacheKey.'_lock')){
  1397. $this->error = 1034;
  1398. return false;
  1399. }
  1400. $levelInfo = MemberLevelModel::from('member_level as a')
  1401. ->where(['a.id'=> $level,'a.status'=>1,'a.mark'=>1])
  1402. ->select(['a.*'])
  1403. ->first();
  1404. $upgradeUsdt = isset($levelInfo['upgrade_usdt'])? floatval($levelInfo['upgrade_usdt']) : 0;
  1405. $upgradeProfit = isset($levelInfo['upgrade_profit'])? floatval($levelInfo['upgrade_profit']) : 0;
  1406. $boxNum = isset($levelInfo['box_num'])? intval($levelInfo['box_num']) : 0;
  1407. $levelName = isset($levelInfo['name'])? $levelInfo['name'] : '';
  1408. if(empty($levelInfo)){
  1409. $this->error = 2511;
  1410. return false;
  1411. }
  1412. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  1413. ->select(['id','nickname','usdt','wait_score','parents','points','parent_id','member_level','power_num','trc_url','pay_password','status'])
  1414. ->first();
  1415. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1416. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1417. $userTotalProfit = isset($userInfo['upgrade_profit_total'])? $userInfo['upgrade_profit_total'] : 0;
  1418. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1419. $userWaitScore = isset($userInfo['wait_score'])? $userInfo['wait_score'] : 0;
  1420. $userPowerNum = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  1421. $userLevel = isset($userInfo['member_level'])? intval($userInfo['member_level']) : 0;
  1422. if(empty($userInfo) || $status != 1){
  1423. $this->error = 2024;
  1424. return false;
  1425. }
  1426. if($userLevel>= $level){
  1427. $this->error = 2512;
  1428. return false;
  1429. }
  1430. if($level >= 30){
  1431. $this->error = 2513;
  1432. return false;
  1433. }
  1434. // 收益是否满足条件
  1435. if($upgradeProfit>0 && $userTotalProfit < $upgradeProfit){
  1436. $this->error = 2514;
  1437. return false;
  1438. }
  1439. if(AccountLogModel::where(['user_id'=> $userId,'source_id'=> $level,'type'=>2,'coin_type'=>1,'status'=>1,'mark'=>1])->value('id')){
  1440. $this->error = 2512;
  1441. return false;
  1442. }
  1443. // USDT余额是否足够
  1444. if($userUsdt < $upgradeUsdt){
  1445. $this->error = 2035;
  1446. return false;
  1447. }
  1448. // 交易密码
  1449. if(empty($userPayPassword)){
  1450. $this->error = 1040;
  1451. return false;
  1452. }
  1453. if($userPayPassword != get_password($payPassword)){
  1454. $this->error = 2038;
  1455. return false;
  1456. }
  1457. // 购买明细
  1458. RedisService::set($cacheKey, $params, rand(2,3));
  1459. $orderNo = get_order_num('UP');
  1460. DB::beginTransaction();
  1461. $log = [
  1462. 'user_id' => $userId,
  1463. 'source_id' => $level,
  1464. 'source_order_no' => $orderNo,
  1465. 'type' => 2,
  1466. 'coin_type' => 1,
  1467. 'user_type'=> 1,
  1468. 'money' => -$upgradeUsdt,
  1469. 'actual_money' => -$upgradeUsdt,
  1470. 'balance' => $userUsdt,
  1471. 'create_time' => time(),
  1472. 'update_time' => time(),
  1473. 'remark' => "购买{$levelName}升级",
  1474. 'status' => 1,
  1475. 'mark' => 1,
  1476. ];
  1477. if(!AccountLogModel::insertGetId($log)){
  1478. DB::rollBack();
  1479. $this->error = 2029;
  1480. RedisService::clear($cacheKey);
  1481. return false;
  1482. }
  1483. // 待返积分
  1484. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1485. $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
  1486. $waitScoreRate = ConfigService::make()->getConfigByCode('upgrade_award_score_rate', 200);
  1487. $waitScoreRate = $waitScoreRate>0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  1488. $waitScore = moneyFormat($upgradeUsdt * $xdPrice * $waitScoreRate/100, 2);
  1489. if($waitScore>0){
  1490. $log = [
  1491. 'user_id' => $userId,
  1492. 'source_id' => $level,
  1493. 'source_order_no' => $orderNo,
  1494. 'type' => 9,
  1495. 'coin_type' => 5,
  1496. 'user_type'=> 1,
  1497. 'money' => $waitScore,
  1498. 'actual_money' => $waitScore,
  1499. 'balance' => $userWaitScore,
  1500. 'create_time' => time(),
  1501. 'update_time' => time(),
  1502. 'remark' => "购买{$levelName}升级",
  1503. 'status' => 1,
  1504. 'mark' => 1,
  1505. ];
  1506. if(!AccountLogModel::insertGetId($log)){
  1507. DB::rollBack();
  1508. $this->error = 2029;
  1509. RedisService::clear($cacheKey);
  1510. return false;
  1511. }
  1512. }
  1513. // 算力明细
  1514. $powerNum = $upgradeUsdt;
  1515. $log = [
  1516. 'user_id' => $userId,
  1517. 'source_id' => $level,
  1518. 'source_order_no' => $orderNo,
  1519. 'type' => 9,
  1520. 'coin_type' => 3,
  1521. 'user_type'=> 1,
  1522. 'money' => $powerNum,
  1523. 'actual_money' => $powerNum,
  1524. 'balance' => $userPowerNum,
  1525. 'create_time' => time(),
  1526. 'update_time' => time(),
  1527. 'remark' => "购买{$levelName}升级",
  1528. 'status' => 1,
  1529. 'mark' => 1,
  1530. ];
  1531. if(!AccountLogModel::insertGetId($log)){
  1532. DB::rollBack();
  1533. $this->error = 2029;
  1534. RedisService::clear($cacheKey);
  1535. return false;
  1536. }
  1537. // 账户
  1538. $updateData = [
  1539. 'usdt'=> DB::raw("usdt - {$upgradeUsdt}"), // 扣除USDT
  1540. 'member_level'=> $level, // 升级等级
  1541. 'box_num'=>DB::raw("box_num + {$boxNum}"), // 奖励盲盒数量
  1542. 'wait_score'=>DB::raw("wait_score + {$waitScore}"), // 待返积分奖励
  1543. 'power_num'=>DB::raw("power_num + {$powerNum}"), // 算力奖励
  1544. 'update_time'=>time()
  1545. ];
  1546. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  1547. DB::rollBack();
  1548. $this->error = 2036;
  1549. RedisService::clear($cacheKey);
  1550. return false;
  1551. }
  1552. // 消息
  1553. $dateTime = date('Y-m-d H:i:s');
  1554. MessageService::make()->pushMessage($userId,"购买{$levelName}升级成功","您在{$dateTime}(UTC+8)成功支付{$upgradeUsdt}USDT购买【{$levelName}】升级成功。",3);
  1555. DB::commit();
  1556. // 算力统计
  1557. AccountLogService::make()->saveCount($userId, $powerNum, 1);
  1558. // 业绩统计
  1559. AccountLogService::make()->saveCount($userId, $upgradeUsdt, 2);
  1560. // 结算收益奖励
  1561. FinanceService::make()->settleBonus($userId, $userInfo, $upgradeUsdt, $orderNo, "购买{$levelName}");
  1562. $this->error = 2503;
  1563. RedisService::clear($cacheKey);
  1564. return true;
  1565. }
  1566. }