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