MemberService.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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\LiveModel;
  16. use App\Models\MemberBankModel;
  17. use App\Models\MemberModel;
  18. use App\Models\MerchantModel;
  19. use App\Models\VideoModel;
  20. use App\Services\BaseService;
  21. use App\Services\CityService;
  22. use App\Services\ConfigService;
  23. use App\Services\EmailService;
  24. use App\Services\RedisService;
  25. use App\Services\SmsService;
  26. use App\Services\ToolService;
  27. use App\Services\UdunpayService;
  28. use Illuminate\Support\Facades\DB;
  29. use phpQrcode\QRcode;
  30. /**
  31. * 会员管理-服务类
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. * Class MemberService
  35. * @package App\Services\Api
  36. */
  37. class MemberService extends BaseService
  38. {
  39. // 静态对象
  40. protected static $instance = null;
  41. /**
  42. * 构造函数
  43. * @author laravel开发员
  44. * @since 2020/11/11
  45. * MemberService constructor.
  46. */
  47. public function __construct()
  48. {
  49. $this->model = new MemberModel();
  50. }
  51. /**
  52. * 静态入口
  53. * @return MemberService|static|null
  54. */
  55. public static function make()
  56. {
  57. if (!self::$instance) {
  58. self::$instance = new static();
  59. }
  60. return self::$instance;
  61. }
  62. /**
  63. * 获取资料详情
  64. * @param $where
  65. * @param array $field
  66. */
  67. public function getInfo($where, $type = 'detail', array $field = [])
  68. {
  69. $defaultField = ['id', 'username','email', 'realname', 'mobile','gender', 'nickname', 'code','supper_point', 'parent_id','province_id','city_id','signature','power_num','trade_state','trc_url','recharge_trc_url', 'point_id', 'balance','trx','usdt','wait_score', 'score','pay_password','realname','idcard_check', 'member_level', 'status', 'avatar'];
  70. $withs = ['merchant','acceptor','parent','point','level'];
  71. if ($type == 'team') {
  72. $defaultField = ['id', 'username','email', 'nickname', 'code', 'balance', 'score','point_id','power_num','parent_id', 'status', 'avatar'];
  73. }else if($type == 'share'){
  74. $withs = ['parent','level'];
  75. $defaultField = ['id', 'username', 'nickname', 'code', 'balance','usdt','trx','trc_url', 'score', 'status', 'avatar'];
  76. }else if($type == 'check'){
  77. $withs = ['parent','level'];
  78. $defaultField = ['id', 'username', 'nickname', 'code', 'balance','usdt','trx','trc_url','wait_score', 'score', 'status', 'avatar'];
  79. }else if ($type == 'info'){
  80. $withs = ['parent','level'];
  81. }
  82. $field = $field ? $field : $defaultField;
  83. if (is_array($where)) {
  84. $info = $this->model->with($withs)->where('mark',1)->where($where)->select($field)->first();
  85. } else {
  86. $info = $this->model->with($withs)->where('mark',1)->where(['id' => (int)$where])->select($field)->first();
  87. }
  88. $info = $info ? $info->toArray() : [];
  89. if ($info && !in_array($type, ['auth', 'check','login'])) {
  90. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  91. // 二维码
  92. if (in_array($type, ['detail'])) {
  93. if (isset($info['wxpay_qrcode'])) {
  94. $info['wxpay_qrcode'] = $info['wxpay_qrcode'] ? get_image_url($info['wxpay_qrcode']) : '';
  95. }
  96. if (isset($info['alipay_qrcode'])) {
  97. $info['alipay_qrcode'] = $info['alipay_qrcode'] ? get_image_url($info['alipay_qrcode']) : '';
  98. }
  99. $info['parent'] = $info['parent']? $info['parent'] : ['id'=>0,'nickname' => '无', 'code' => '无'];
  100. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  101. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  102. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  103. }
  104. else if ($type == 'info') {
  105. $liveOpenLevel = ConfigService::make()->getConfigByCode('live_open_level',0);
  106. $liveOpenLevel = $liveOpenLevel>0? $liveOpenLevel : 0;
  107. $info['live_open'] = $info['member_level']>=$liveOpenLevel? 1 : 0;
  108. }
  109. else if ($type == 'team' || $type == 'share') {
  110. $inviteUrl = get_web_url('/#/pages/register/index?code=' . $info['code']);
  111. $qrcode = $this->makeQrcode($inviteUrl);
  112. $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  113. $info['invite_url'] = $inviteUrl;
  114. $info['qrcode_base64'] = $qrcode?getImageContent($qrcode):'';
  115. $info['team_count1'] = $this->getUserCountByType($info['id'], 1, 0); // 直推人数
  116. $info['team_count2'] = $this->getUserCountByType($info['id'], 3, 0); // 滑落人数
  117. $info['team_count'] = $this->getUserCountByType($info['id'], 1, 1); // 节点推荐人数
  118. }else {
  119. // 默认地址
  120. $info['addressData'] = MemberAddressService::make()->getBindInfo($info['id']);
  121. }
  122. }
  123. return $info;
  124. }
  125. /**
  126. * 获取主页信息
  127. * @param $where
  128. * @param array $field
  129. */
  130. public function getHomeInfo($id, $userId=0)
  131. {
  132. $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'];
  133. $info = $this->model->where('mark',1)->where(['id' => $id])->select($field)->first();
  134. $info = $info ? $info->toArray() : [];
  135. if ($info) {
  136. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  137. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  138. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  139. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  140. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  141. $info['is_fans'] = (int)MemberCollectService::make()->checkCollect($info['id'], $userId,1); // 是否我的粉丝
  142. $info['live_id'] = (int)LiveModel::where(['user_id'=> $info['id'],'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id'); // 是否正在直播
  143. }
  144. return $info;
  145. }
  146. /**
  147. * 获取用户缓存信息
  148. * @param $where
  149. * @param array $field
  150. * @param int $expired
  151. * @return array|mixed
  152. */
  153. public function getCacheInfo($where, $field = [], $expired = 0)
  154. {
  155. $cacheKey = "caches:member:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  156. $info = RedisService::get($cacheKey);
  157. if ($info) {
  158. return $info;
  159. }
  160. $defaultField = ['id', 'username', 'realname', 'mobile', 'nickname', 'code', 'parent_id', 'openid', 'balance', 'score', 'member_level', 'status', 'avatar'];
  161. $field = $field ? $field : $defaultField;
  162. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  163. $info = $info ? $info->toArray() : [];
  164. if ($info) {
  165. if (isset($info['avatar'])) {
  166. $info['avatar_preview'] = get_image_url($info['avatar']);
  167. }
  168. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  169. }
  170. return $info;
  171. }
  172. /**
  173. * 生成普通参数二维码
  174. * @param $str 参数
  175. * @param bool $refresh 是否重新生成
  176. * @return bool
  177. */
  178. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  179. {
  180. $basePath = base_path() . '/public';
  181. $qrFile = '/images/qrcode/';
  182. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  183. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  184. }
  185. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  186. $qrFile = $qrFile . "C_{$key}.png";
  187. $cacheKey = "caches:qrcodes:member_" . $key;
  188. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  189. return $qrFile;
  190. }
  191. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  192. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  193. return false;
  194. }
  195. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  196. return $qrFile;
  197. }
  198. /**
  199. * 获取团队用户数量
  200. * @param $userId 当前用户ID
  201. * @param int $type 层级:1-直推,2-团队
  202. * @param int $isPoint 节点:1-滑落推荐,0-直接推荐
  203. * @return array|mixed
  204. */
  205. public function getUserCountByType($userId, $type = 1, $isPoint=0)
  206. {
  207. $cacheKey = "caches:teams:count_{$userId}_{$type}_{$isPoint}";
  208. $data = RedisService::get($cacheKey);
  209. if ($data) {
  210. return $data;
  211. }
  212. $where = ['status' => 1, 'mark' => 1];
  213. $data = $this->model->where($where)
  214. ->where(function ($query) use ($type, $isPoint, $userId) {
  215. if($isPoint){
  216. if ($type == 1) { // 滑落推荐
  217. $query->where('point_id', $userId);
  218. } else if($type == 2){ // 滑落团队人数
  219. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  220. }
  221. }else{
  222. if ($type == 1) { // 直推人数
  223. $query->where('parent_id', $userId);
  224. } else if($type == 2){ // 团队人数
  225. $query->whereRaw('FIND_IN_SET(?,parents)', $userId);
  226. } else if($type == 3){ // 滑落人数
  227. $query->where('parent_id', $userId)->where('point_id','!=', $userId);
  228. }
  229. }
  230. })->count('id');
  231. if ($data) {
  232. RedisService::set($cacheKey, $data, rand(3, 5));
  233. }
  234. return $data;
  235. }
  236. /**
  237. * 分销用户列表
  238. * @param $userId
  239. * @param $params
  240. * @param int $pageSize
  241. * @return array
  242. */
  243. public function getTeamList($userId, $params, $pageSize=15)
  244. {
  245. $page = request()->post('page',1);
  246. $cacheKey = "caches:teams:list_{$userId}_{$page}";
  247. $datas = RedisService::get($cacheKey);
  248. if ($datas) {
  249. return [
  250. 'pageSize' => $pageSize,
  251. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  252. 'list' => isset($datas['data']) ? $datas['data'] : []
  253. ];
  254. }
  255. $where = ['status' => 1, 'mark' => 1];
  256. $field = ['id','avatar', 'nickname', 'username', 'email', 'balance', 'parent_id','create_time', 'status'];
  257. $datas = $this->model->where($where)
  258. ->where(function ($query) use ($userId) {
  259. $query->where('parent_id', $userId);
  260. })
  261. ->where(function($query) use($params){
  262. $kw = isset($params['kw'])? trim($params['kw']) : '';
  263. if($kw){
  264. $query->where('id','=', "%{$kw}%")->orWhere('email','like',"%{$kw}%")->orWhere('username','like',"%{$kw}%");
  265. }
  266. })
  267. ->select($field)
  268. ->orderBy('create_time', 'desc')
  269. ->orderBy('id', 'desc')
  270. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  271. $datas = $datas ? $datas->toArray() : [];
  272. if ($datas) {
  273. foreach ($datas['data'] as &$item) {
  274. $item['avatar'] = $item['avatar']? $item['avatar'] : '/images/member/logo.png';
  275. $item['avatar'] = get_image_url($item['avatar']);
  276. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'y-m-d H:i') : '';
  277. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  278. }
  279. unset($item);
  280. RedisService::set($cacheKey, $datas, rand(5, 10));
  281. }
  282. return [
  283. 'pageSize' => $pageSize,
  284. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  285. 'list' => isset($datas['data']) ? $datas['data'] : []
  286. ];
  287. }
  288. /**
  289. * 手机号是否已注册
  290. * @param $mobile
  291. * @return mixed
  292. */
  293. public function checkPhoneExists($mobile)
  294. {
  295. return parent::checkExists('mobile', $mobile); // TODO: Change the autogenerated stub
  296. }
  297. /**
  298. * 账号注册
  299. * @param $params
  300. * @return array|false
  301. */
  302. public function register($params)
  303. {
  304. // 授权
  305. $email = isset($params['email']) ? trim($params['email']) : '';
  306. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  307. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  308. $code = isset($params['code']) ? trim($params['code']) : '';
  309. $scene = isset($params['scene']) ? $params['scene'] : '';
  310. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  311. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  312. if (empty($params) || empty($email) || $code <= 0) {
  313. $this->error = 2014;
  314. return false;
  315. }
  316. // 验证码
  317. if (!EmailService::make()->check($email, $code, $scene)) {
  318. $this->error = EmailService::make()->getError();
  319. return false;
  320. }
  321. // 验证是否注册
  322. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  323. $this->error = 2009;
  324. return false;
  325. }
  326. // 推荐人
  327. $parentId = 0;
  328. $pointId = 0;
  329. $parents = '';
  330. $points = '';
  331. if ($inviteCode) {
  332. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  333. ->select(['id', 'parents', 'parent_id'])
  334. ->first();
  335. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  336. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  337. // 挂靠节点
  338. if($parentId>0){
  339. $pointId = $this->getPointParentId($parentId);
  340. if($pointId){
  341. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  342. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  343. }
  344. }
  345. }
  346. // 注册
  347. $id = $this->model->max('id') + 1;
  348. $system = isset($params['system'])? $params['system'] : [];
  349. $ip = get_client_ip();
  350. $ipData = ToolService::make()->getIpAddress($ip,'');
  351. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  352. $city = isset($ipData['city'])? $ipData['city'] : '';
  353. $emailName = explode('@', $email)[0];
  354. // 钱包
  355. $data = [
  356. 'parent_id' => $parentId,
  357. 'point_id' => $pointId,
  358. 'points' => $points,
  359. 'openid' => '',
  360. 'avatar' => '',
  361. 'nickname' => $nickname ? $nickname : '用户'.rand(100,999).$emailName,
  362. 'username' => $email,
  363. 'password' => get_password('xl123456'),
  364. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  365. 'email' => $email,
  366. 'mobile' => $mobile,
  367. 'recharge_trc_url' => UdunpayService::make()->createAddress(195),
  368. 'parents' => $parents,
  369. 'province_id' => CityService::make()->getFieldByName($province),
  370. 'city_id' => CityService::make()->getFieldByName($city),
  371. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  372. 'device' => $appSources == 'android' ? 2 : 1,
  373. 'status' => 1,
  374. 'balance' => 0,
  375. 'score' => 0,
  376. 'mark' => 1,
  377. 'login_ip' => $ip,
  378. 'create_time' => time(),
  379. ];
  380. $userId = $this->model->insertGetId($data);
  381. $this->error = 2008;
  382. return [
  383. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  384. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  385. ];
  386. }
  387. /**
  388. * 邮箱登录
  389. * @param $params
  390. * @return array|false
  391. */
  392. public function loginByEmail($params)
  393. {
  394. // 授权
  395. $email = isset($params['email']) ? $params['email'] : '';
  396. $code = isset($params['code']) ? $params['code'] : '';
  397. $scene = isset($params['scene']) ? $params['scene'] : '';
  398. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  399. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  400. $this->error = 2014;
  401. return false;
  402. }
  403. // 验证码
  404. if (!EmailService::make()->check($email, $code, $scene)) {
  405. $this->error = EmailService::make()->getError();
  406. return false;
  407. }
  408. // 验证是否注册,没有则注册
  409. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  410. $data = $data ? $data->toArray() : [];
  411. $userId = isset($data['id']) ? $data['id'] : 0;
  412. $status = isset($data['status']) ? $data['status'] : 0;
  413. if(empty($data) || $userId<=0){
  414. $this->error = 2001;
  415. return false;
  416. }
  417. if ($status != 1) {
  418. $this->error = 2015;
  419. return false;
  420. }
  421. // 获取登录授权token
  422. $jwt = new Jwt('jwt_dys_app');
  423. $token = $jwt->getToken($userId);
  424. // 结果返回
  425. $result = [
  426. 'access_token' => $token,
  427. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  428. ];
  429. // 用户信息
  430. $ip = get_client_ip();
  431. $updateData = [
  432. 'login_time' => time(),
  433. 'login_ip' => $ip,
  434. ];
  435. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  436. $ipData = ToolService::make()->getIpAddress($ip,'');
  437. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  438. $city = isset($ipData['city'])? $ipData['city'] : '';
  439. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  440. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  441. }
  442. $this->model->where(['id'=> $userId])->update($updateData);
  443. $this->error = 2004;
  444. $data['token'] = $token;
  445. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  446. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  447. return $result;
  448. }
  449. /**
  450. * 手机号登录
  451. * @param $params
  452. * @return array|false
  453. */
  454. public function loginByMobile($params)
  455. {
  456. // 授权
  457. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  458. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  459. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  460. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  461. $openid = isset($params['openid']) ? $params['openid'] : '';
  462. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  463. $regType = isset($params['type']) ? $params['type'] : 'app';
  464. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  465. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  466. if (empty($params) || empty($mobile) || $code <= 0) {
  467. $this->error = 2014;
  468. return false;
  469. }
  470. // 验证码
  471. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  472. $this->error = SmsService::make()->getError();
  473. return false;
  474. }
  475. // 推荐人
  476. $parentId = 0;
  477. $parents = '';
  478. if ($inviteCode) {
  479. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  480. ->select(['id', 'parents', 'parent_id'])
  481. ->first();
  482. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  483. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  484. }
  485. // 验证是否注册,没有则注册
  486. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  487. $data = $data ? $data->toArray() : [];
  488. $userId = isset($data['id']) ? $data['id'] : 0;
  489. $status = isset($data['status']) ? $data['status'] : 0;
  490. if ($userId && $status != 1) {
  491. $this->error = 2015;
  492. return false;
  493. }
  494. // 是否是商家或技师,有订单提醒
  495. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  496. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  497. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  498. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  499. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  500. if($merchantStatus==2){
  501. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  502. if($uid>0){
  503. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  504. }
  505. }else if($mechanicStatus == 2){
  506. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  507. if($uid>0){
  508. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  509. }
  510. }
  511. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  512. if (!$userId) {
  513. $id = $this->model->max('id') + 1;
  514. $data = [
  515. 'parent_id' => $parentId,
  516. 'openid' => $openid,
  517. 'avatar' => $avatar,
  518. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  519. 'username' => get_username($id),
  520. 'password' => get_password('123456'),
  521. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  522. 'mobile' => $mobile,
  523. 'parents' => $parents,
  524. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  525. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  526. 'device' => $appSources == 'android' ? 2 : 1,
  527. 'status' => 1,
  528. 'balance' => 0,
  529. 'score' => 0,
  530. 'mark' => 1,
  531. 'login_time' => time(),
  532. 'login_ip' => get_client_ip(),
  533. 'create_time' => time(),
  534. ];
  535. $userId = $this->model->insertGetId($data);
  536. } elseif ($regType != 'h5') {
  537. // 登录更新
  538. $updateData = [
  539. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  540. 'login_count' => DB::raw("login_count+1"),
  541. 'login_time' => time(),
  542. 'login_ip' => get_client_ip(),
  543. 'update_time' => time(),
  544. ];
  545. if ($pushCid) {
  546. $updateData['push_cid'] = $pushCid;
  547. }
  548. $this->model->where(['id' => $userId])->update($updateData);
  549. }
  550. // H5 邀请注册,无需登录
  551. if ($regType == 'h5') {
  552. $this->error = $status ? 2023 : 2022;
  553. return [
  554. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  555. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  556. ];
  557. }
  558. // 获取登录授权token
  559. $jwt = new Jwt('jwt_dys_app');
  560. $token = $jwt->getToken($userId);
  561. // 结果返回
  562. $result = [
  563. 'access_token' => $token,
  564. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  565. ];
  566. // 用户信息
  567. $this->error = 2004;
  568. $data['token'] = $token;
  569. unset($data['password']);
  570. // 实现初始化账单明细表
  571. AccountLogModel::where('id','<=',1)->value('id');
  572. RedisService::clear( "caches:map:{$userId}");
  573. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  574. return $result;
  575. }
  576. /**
  577. * 修改头像/商家logo/技师头像
  578. * @param $userId
  579. * @param $avatar
  580. * @return mixed
  581. */
  582. public function saveAvatar($userId, $avatar)
  583. {
  584. $type = request()->post('type', 1);
  585. if($type == 1){
  586. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  587. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  588. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  589. @unlink(ATTACHMENT_PATH . $oldAvatar);
  590. }
  591. return true;
  592. }
  593. }else if($type == 2){
  594. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  595. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $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 == 3){
  602. $oldAvatar = MechanicModel::where(['user_id' => $userId])->value('avatar');
  603. if (MechanicModel::where(['user_id' => $userId])->update(['avatar' => $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. $username = isset($params['username']) ? $params['username'] : '';
  621. $password = isset($params['password']) ? $params['password'] : '';
  622. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  623. // 用户验证
  624. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id', 'status'])->first();
  625. if (!$info) {
  626. $this->error = 2001;
  627. return false;
  628. }
  629. // 使用状态校验
  630. if ($info['status'] != 1) {
  631. $this->error = 2009;
  632. return false;
  633. }
  634. // 密码校验
  635. $data = ['update_time' => time()];
  636. if (isset($params['password']) && $password) {
  637. $password = get_password($password);
  638. $data['password'] = $password;
  639. if ($password != $info['password']) {
  640. $this->error = 2002;
  641. return false;
  642. }
  643. }
  644. // 用户账号验证
  645. if (isset($params['username']) && $username) {
  646. $data['username'] = $username;
  647. $checkInfo = $this->model->where(['username' => $username, 'mark' => 1])->select(['id', 'status'])->first();
  648. if ($checkInfo && $checkInfo['id'] != $userId) {
  649. $this->error = 2005;
  650. return false;
  651. }
  652. }
  653. // 手机号验证
  654. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  655. if (isset($params['mobile']) && $mobile) {
  656. $data['mobile'] = $mobile;
  657. $checkInfo = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'status'])->first();
  658. if ($checkInfo && $checkInfo['id'] != $userId) {
  659. $this->error = 2009;
  660. return false;
  661. }
  662. // 短信验证码
  663. $code = isset($params['code']) ? $params['code'] : '';
  664. if (empty($code)) {
  665. $this->error = 2013;
  666. return false;
  667. }
  668. if (!SmsService::make()->check($mobile, $code, 'modify')) {
  669. $this->error = SmsService::make()->getError();
  670. return false;
  671. }
  672. }
  673. // 修改数据
  674. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  675. if (isset($params['nickname']) && $nickname) {
  676. $data['nickname'] = $nickname;
  677. }
  678. $sex = isset($params['sex']) ? $params['sex'] : 0;
  679. if (isset($params['sex']) && $sex) {
  680. $data['sex'] = $sex;
  681. }
  682. $intro = isset($params['intro']) ? $params['intro'] : '';
  683. if (isset($params['intro']) && $intro) {
  684. $data['intro'] = $intro;
  685. }
  686. if (isset($params['pay_password']) && $payPassword) {
  687. $payPassword = get_password($payPassword);
  688. $data['pay_password'] = $payPassword;
  689. }
  690. $this->model->where(['id' => $userId])->update($data);
  691. $this->error = 1008;
  692. return true;
  693. }
  694. /**
  695. * 身份认证
  696. * @param $userId
  697. * @param $params
  698. * @return bool
  699. */
  700. public function auth($userId, $params)
  701. {
  702. // 用户验证
  703. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  704. if (!$info) {
  705. $this->error = 2001;
  706. return false;
  707. }
  708. // 使用状态校验
  709. if ($info['status'] != 1) {
  710. $this->error = 2009;
  711. return false;
  712. }
  713. // 密码校验
  714. $data = ['update_time' => time(),'idcard_check'=>1];
  715. if (isset($params['realname']) && $params['realname']) {
  716. $data['realname'] = trim($params['realname']);
  717. }
  718. $idcard = isset($params['idcard']) && $params['idcard'];
  719. if ($idcard) {
  720. $data['idcard'] = trim($params['idcard']);
  721. }
  722. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  723. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  724. }
  725. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  726. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  727. }
  728. $this->model->where(['id' => $userId])->update($data);
  729. // 发送消息
  730. $params = [
  731. 'title'=> "身份认证提交成功",
  732. 'body' => "您的身份认证信息已提交成功,请耐心等候审核",
  733. 'type' => 1, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  734. 'content' => [
  735. 'account' => ['name' => '账号', 'text' => $info['username']],
  736. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  737. 'status' => ['name' => '状态', 'text' => '待审核'],
  738. ],
  739. 'click_type' => 'payload',
  740. 'url' => '/pages/my/profile/auth',
  741. ];
  742. PushService::make()->pushMessageByUser($userId, $params, 0);
  743. $this->error = 2036;
  744. return true;
  745. }
  746. /**
  747. * 转账
  748. * @param $userId
  749. * @param $params
  750. * @return bool
  751. */
  752. public function transfer($userId, $params)
  753. {
  754. $account = isset($params['account'])? trim($params['account']) : '';
  755. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  756. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  757. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  758. $money = isset($params['money'])? floatval($params['money']) : 0;
  759. if(empty($account) || $money<=0){
  760. $this->error = 2501;
  761. return false;
  762. }
  763. $cachekey = "caches:members:transfer:{$userId}";
  764. if(RedisService::get($cachekey)){
  765. $this->error = 1034;
  766. return false;
  767. }
  768. // 验证账户
  769. $userInfo = $this->model->with(['merchant'])
  770. ->where(['id'=> $userId,'mark'=>1])
  771. ->select(['id','nickname','username','pay_password','balance','status'])
  772. ->first();
  773. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  774. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  775. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  776. $username = isset($userInfo['username'])? $userInfo['username'] : '';
  777. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  778. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  779. $merchName = isset($merchant['name'])? $merchant['name'] : '';
  780. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  781. if(empty($userInfo) || $status != 1){
  782. $this->error = 2024;
  783. return false;
  784. }
  785. // 余额支付支付密码验证
  786. if(empty($userPayPassword)){
  787. $this->error ='1039';
  788. return false;
  789. }
  790. $payPassword = get_password($payPassword);
  791. if($payPassword != $userPayPassword){
  792. $this->error ='1038';
  793. return false;
  794. }
  795. // 验证账户
  796. $accountInfo = $this->model->where(['username'=> $account,'mark'=>1])
  797. ->select(['id','nickname','username','balance','status'])
  798. ->first();
  799. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  800. $accountBalance = isset($accountInfo['balance'])? $accountInfo['balance'] : 0;
  801. if(empty($accountInfo) || $status != 1){
  802. $this->error = 2025;
  803. return false;
  804. }
  805. if($accountInfo['id'] == $userId && $type == 1){
  806. $this->error = 2645;
  807. return false;
  808. }
  809. // 用户余额转账到用户
  810. DB::beginTransaction();
  811. if($type == 1){
  812. if($money>$balance){
  813. $this->error = 2026;
  814. return false;
  815. }
  816. // 扣除账户余额
  817. RedisService::set($cachekey, $params, rand(2,5));
  818. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  819. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  820. DB::rollBack();
  821. $this->error = 2028;
  822. RedisService::clear($cachekey);
  823. return false;
  824. }
  825. }
  826. // 商户余额转账到用户
  827. else if($type == 2){
  828. if($money>$merchBalance){
  829. $this->error = 2027;
  830. return false;
  831. }
  832. //
  833. if($merchId<=0){
  834. $this->error = 2018;
  835. return false;
  836. }
  837. // 扣除商户余额
  838. RedisService::set($cachekey, $params, rand(2,5));
  839. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  840. if(!MerchantModel::where(['id'=> $merchId,'mark'=>1])->update($updateData)){
  841. DB::rollBack();
  842. $this->error = 2028;
  843. RedisService::clear($cachekey);
  844. return false;
  845. }
  846. }
  847. // 到账
  848. $updateData = ['balance'=> DB::raw("balance + {$money}"),'update_time'=> time()];
  849. if(!$this->model->where(['id'=> $accountInfo['id'],'mark'=>1])->update($updateData)){
  850. DB::rollBack();
  851. $this->error = 2028;
  852. RedisService::clear($cachekey);
  853. return false;
  854. }
  855. // 明细
  856. $log = [
  857. 'user_id' => $userId,
  858. 'merch_id' => $merchId,
  859. 'source_uid' => $accountInfo['id'],
  860. 'source_order_no' => '',
  861. 'type' => 6,
  862. 'coin_type' => 4,
  863. 'user_type'=> $type==2? 2 : ($userType!=2? $userType : 1),
  864. 'money' => -$money,
  865. 'balance' => $type==2? $merchBalance : $balance,
  866. 'create_time' => time(),
  867. 'update_time' => time(),
  868. 'remark' => $type==2? "商户余额转账给【{$account}】": "账户余额转账给【{$account}】",
  869. 'status' => 1,
  870. 'mark' => 1,
  871. ];
  872. if(!AccountLogModel::insertGetId($log)){
  873. DB::rollBack();
  874. $this->error = 2029;
  875. RedisService::clear($cachekey);
  876. return false;
  877. }
  878. // 到账明细
  879. $log = [
  880. 'user_id' => $accountInfo['id'],
  881. 'merch_id' => $merchId,
  882. 'source_uid' => $userId,
  883. 'source_order_no' => '',
  884. 'type' => 6,
  885. 'coin_type' => 4,
  886. 'user_type'=> 1,
  887. 'money' => $money,
  888. 'balance' => $accountBalance,
  889. 'create_time' => time(),
  890. 'update_time' => time(),
  891. 'remark' => $type==2? "来自商家【{$merchName}】的转账": "来自".($userType==3?'技师':($userType==4?'代理':'用户'))."【{$username}】的转账",
  892. 'status' => 1,
  893. 'mark' => 1,
  894. ];
  895. if(!AccountLogModel::insertGetId($log)){
  896. DB::rollBack();
  897. $this->error = 2029;
  898. RedisService::clear($cachekey);
  899. return false;
  900. }
  901. // 发送消息
  902. $params = [
  903. 'title'=> "转账成功通知",
  904. 'body' => $type == 2? "收到一笔来自商家的转账":"收到一笔来自用户的转账",
  905. 'type' => 2, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  906. 'content' => [
  907. 'account' => ['name' => $type==2?'来自商家':'来自用户', 'text' => $type==2? $merchName : $userInfo['username']],
  908. 'pay_time' => ['name' => '到账时间', 'text' => date('Y-m-d H:i:s')],
  909. 'money' => ['name' => '到账金额', 'text' => $money],
  910. 'status' => ['name' => '状态', 'text' => '已到账'],
  911. ],
  912. 'click_type' => 'payload',
  913. 'url' => '/pages/account/index',
  914. ];
  915. if(!PushService::make()->pushMessageByUser($accountInfo['id'], $params, 0)){
  916. DB::rollBack();
  917. $this->error = PushService::make()->getError();
  918. RedisService::clear($cachekey);
  919. return false;
  920. }
  921. DB::commit();
  922. $this->error = 2030;
  923. return true;
  924. }
  925. /**
  926. * 余额充值
  927. * @param $userId
  928. * @param $params
  929. * @return array|false
  930. */
  931. public function recharge($userId, $params)
  932. {
  933. $money = isset($params['money'])? $params['money'] : 0;
  934. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  935. if($money<=0){
  936. $this->error = 2031;
  937. return false;
  938. }
  939. if(!in_array($payType, [10,20])){
  940. $this->error = 2032;
  941. return false;
  942. }
  943. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  944. ->select(['id','nickname','balance','status'])
  945. ->first();
  946. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  947. if(empty($userInfo) || $status != 1){
  948. $this->error = 2024;
  949. return false;
  950. }
  951. // 充值订单
  952. $orderNo = get_order_num('DR');
  953. $data = [
  954. 'order_no'=> $orderNo,
  955. 'user_id'=> $userId,
  956. 'type'=> 1,
  957. 'user_type'=> 1,
  958. 'money'=> $money,
  959. 'gift_money'=> 0,
  960. 'pay_type'=> $payType,
  961. 'create_time'=> time(),
  962. 'update_time'=> time(),
  963. 'status'=> 1,
  964. 'mark'=> 1,
  965. ];
  966. if(!$orderId = BalanceLogModel::insertGetId($data)){
  967. $this->error = 2033;
  968. return false;
  969. }
  970. // 支付方式
  971. $order = [
  972. 'order_no'=> $orderNo,
  973. 'type'=> 0,
  974. 'pay_type'=> $payType,
  975. 'pay_money'=> $money,
  976. 'body'=> '充值订单支付',
  977. ];
  978. switch($payType){
  979. case 20: // 支付宝
  980. $payment = PaymentService::make()->aliPay($userInfo, $order,'recharge');
  981. if(empty($payment)){
  982. DB::rollBack();
  983. $this->error = PaymentService::make()->getError();
  984. return false;
  985. }
  986. break;
  987. case 10: // 微信支付
  988. $payment = PaymentService::make()->wechatPay($userInfo, $order,'recharge');
  989. if(empty($payment)){
  990. DB::rollBack();
  991. $this->error = PaymentService::make()->getError();
  992. return false;
  993. }
  994. break;
  995. default:
  996. $this->error = 1030;
  997. return false;
  998. }
  999. $this->error = 2034;
  1000. return [
  1001. 'id'=> $orderId,
  1002. 'payment'=> $payment,
  1003. 'total'=> $money,
  1004. 'order_no'=> $orderNo,
  1005. 'pay_type'=> $payType,
  1006. ];
  1007. }
  1008. /**
  1009. * 余额提现
  1010. * @param $userId
  1011. * @param $params
  1012. * @return array|false
  1013. */
  1014. public function withdraw($userId, $params)
  1015. {
  1016. $money = isset($params['money'])? floatval($params['money']) : 0;
  1017. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1018. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  1019. $bankId = isset($params['bank_id']) && $params['bank_id']? intval($params['bank_id']) : 0;
  1020. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  1021. if($money<=0){
  1022. $this->error = 2031;
  1023. return false;
  1024. }
  1025. if(!in_array($payType, [10,20,50])){
  1026. $this->error = 2425;
  1027. return false;
  1028. }
  1029. $userInfo = $this->model->with(['merchant'])
  1030. ->where(['id'=> $userId,'mark'=>1])
  1031. ->select(['id','nickname','balance','pay_password','wxpay_qrcode','alipay_qrcode','status'])
  1032. ->first();
  1033. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1034. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1035. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1036. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1037. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  1038. $merchPayPassword = isset($merchant['pay_password'])? $merchant['pay_password'] : '';
  1039. if(empty($userInfo) || $status != 1){
  1040. $this->error = 2024;
  1041. return false;
  1042. }
  1043. // 验证提现类型金额
  1044. $qrcode = '';
  1045. $account = '';
  1046. if($type == 2){
  1047. // 支付密码验证
  1048. if(empty($merchPayPassword)){
  1049. $this->error ='1042';
  1050. return false;
  1051. }
  1052. $payPassword = get_password($payPassword);
  1053. if($payPassword != $merchPayPassword){
  1054. $this->error ='1043';
  1055. return false;
  1056. }
  1057. if(empty($merchant)){
  1058. $this->error = 2646;
  1059. return false;
  1060. }
  1061. if($money>$merchBalance){
  1062. $this->error = 2647;
  1063. return false;
  1064. }
  1065. $wxpayQrcode = isset($merchant['wxpay_qrcode'])? $merchant['wxpay_qrcode'] : '';
  1066. $alipayQrcode = isset($merchant['alipay_qrcode'])? $merchant['alipay_qrcode'] : '';
  1067. if($payType == 10){
  1068. $qrcode = $wxpayQrcode;
  1069. if(empty($wxpayQrcode)){
  1070. $this->error = 2649;
  1071. return false;
  1072. }
  1073. }
  1074. if($payType == 20){
  1075. $qrcode = $alipayQrcode;
  1076. if(empty($alipayQrcode)) {
  1077. $this->error = 2649;
  1078. return false;
  1079. }
  1080. }
  1081. }else{
  1082. // 支付密码验证
  1083. if(empty($userPayPassword)){
  1084. $this->error ='1039';
  1085. return false;
  1086. }
  1087. $payPassword = get_password($payPassword);
  1088. if($payPassword != $userPayPassword){
  1089. $this->error ='1038';
  1090. return false;
  1091. }
  1092. if($money>$balance){
  1093. $this->error = 2648;
  1094. return false;
  1095. }
  1096. $wxpayQrcode = isset($userInfo['wxpay_qrcode'])? $userInfo['wxpay_qrcode'] : '';
  1097. $alipayQrcode = isset($userInfo['alipay_qrcode'])? $userInfo['alipay_qrcode'] : '';
  1098. if($payType == 10){
  1099. $qrcode = $wxpayQrcode;
  1100. if(empty($wxpayQrcode)) {
  1101. $this->error = 2420;
  1102. return false;
  1103. }
  1104. }
  1105. if($payType == 20){
  1106. $qrcode = $alipayQrcode;
  1107. if(empty($alipayQrcode)){
  1108. $this->error = 2421;
  1109. return false;
  1110. }
  1111. }
  1112. if($payType == 50){
  1113. $bankInfo = MemberBankModel::where(['id'=> $bankId,'mark'=>1])
  1114. ->select(['realname','bank_name','bank_card'])
  1115. ->first();
  1116. $bankInfo = $bankInfo? $bankInfo->toArray() : [];
  1117. $bankInfo = array_values($bankInfo);
  1118. if(empty($bankInfo)){
  1119. $this->error = 2422;
  1120. return false;
  1121. }else {
  1122. $account = $bankInfo? implode(' ', $bankInfo) : '';
  1123. }
  1124. }
  1125. }
  1126. // 提现订单
  1127. $orderNo = get_order_num('DW');
  1128. $data = [
  1129. 'order_no'=> $orderNo,
  1130. 'user_id'=> $userId,
  1131. 'type'=> 2,
  1132. 'user_type'=> $type,
  1133. 'money'=> $money,
  1134. 'actual_money'=> 0,
  1135. 'qrcode'=> $qrcode,
  1136. 'account'=> $account,
  1137. 'pay_type'=> $payType,
  1138. 'create_time'=> time(),
  1139. 'update_time'=> time(),
  1140. 'status'=> 1,
  1141. 'mark'=> 1,
  1142. ];
  1143. // 提现记录
  1144. DB::beginTransaction();
  1145. if(!$id = BalanceLogModel::insertGetId($data)){
  1146. DB::rollBack();
  1147. $this->error = 2033;
  1148. return false;
  1149. }
  1150. // 商户扣款
  1151. if($type == 2){
  1152. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1153. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1154. DB::rollBack();
  1155. $this->error = 2423;
  1156. return false;
  1157. }
  1158. /*$log = [
  1159. 'user_id' => 0,
  1160. 'merch_id' => $merchant['id'],
  1161. 'source_uid' => $userId,
  1162. 'source_order_no' => $orderNo,
  1163. 'type' => 12,
  1164. 'coin_type' => 4,
  1165. 'user_type'=> 2,
  1166. 'money' => -$money,
  1167. 'balance' => $merchBalance,
  1168. 'create_time' => time(),
  1169. 'update_time' => time(),
  1170. 'remark' => "商户余额提现",
  1171. 'status' => 1,
  1172. 'mark' => 1,
  1173. ];*/
  1174. }
  1175. // 用户扣款
  1176. else{
  1177. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1178. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1179. DB::rollBack();
  1180. $this->error = 2423;
  1181. return false;
  1182. }
  1183. /*$log = [
  1184. 'user_id' => $userId,
  1185. 'merch_id' => 0,
  1186. 'source_uid' => 0,
  1187. 'source_order_no' => $orderNo,
  1188. 'type' => 11,
  1189. 'coin_type' => 4,
  1190. 'user_type'=> 1,
  1191. 'money' => -$money,
  1192. 'balance' => $balance,
  1193. 'create_time' => time(),
  1194. 'update_time' => time(),
  1195. 'remark' => "账户余额提现",
  1196. 'status' => 1,
  1197. 'mark' => 1,
  1198. ];*/
  1199. }
  1200. // 推送消息
  1201. $pushData = [
  1202. 'title' => '提现申请提交成功',
  1203. 'body' => "您的提现申请已提交请耐心等候审核",
  1204. 'type' => 3, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  1205. 'content' => [
  1206. 'order_no' => ['name' => '订单号', 'text' => $orderNo],
  1207. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  1208. 'pay_type' => ['name' => '付款方式', 'text' => $payType==10?'微信支付':($payType==20?'支付宝':'银行卡')],
  1209. 'money' => ['name' => '提现金额', 'text' => $money],
  1210. 'status' => ['name' => '状态', 'text' => '待审核'],
  1211. ],
  1212. 'click_type' => 'payload',
  1213. 'url' => '',
  1214. ];
  1215. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  1216. if($type == 2 && $merchId){
  1217. PushService::make()->pushMessageByUser($merchId, $pushData, 2);
  1218. }else{
  1219. PushService::make()->pushMessageByUser($userId, $pushData);
  1220. }
  1221. DB::commit();
  1222. $this->error = 2424;
  1223. return [
  1224. 'id'=> $id,
  1225. 'money'=> $money,
  1226. 'pay_type'=> $payType,
  1227. ];
  1228. }
  1229. /**
  1230. * 分销佣金入账
  1231. * @param $info
  1232. * @return false
  1233. */
  1234. public function settleBonus($info, $level=1)
  1235. {
  1236. $money = isset($info['money'])? floatval($info['money']) : 0;
  1237. $merchId = isset($info['merch_id'])? intval($info['merch_id']) : 0;
  1238. $userId = isset($info['user_id'])? intval($info['user_id']) : 0;
  1239. // 分销佣金
  1240. $settleUserId = isset($info["bonus_level{$level}_id"])? $info["bonus_level{$level}_id"] : 0;
  1241. $settleMoney = isset($info["bonus_level{$level}_money"])? $info["bonus_level{$level}_money"] : 0;
  1242. if($settleUserId>0 && $settleMoney>0){
  1243. $userInfo = $this->model->where(['id'=> $settleUserId,'mark'=>1])
  1244. ->select(['id','nickname','balance'])
  1245. ->first();
  1246. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1247. if($settleUserId && $userInfo){
  1248. // 更新账户
  1249. $updateData = [
  1250. 'balance'=>DB::raw("balance + {$settleMoney}"),
  1251. 'bonus_total'=>DB::raw("bonus_total + {$settleMoney}"),
  1252. "bonus_level{$level}_total"=>DB::raw("bonus_level{$level}_total + {$settleMoney}"),
  1253. 'update_time'=> time()
  1254. ];
  1255. if(!MemberModel::where(['id'=> $settleUserId,'mark'=>1])->update($updateData)){
  1256. $this->error = lang(3413, ['level'=> $level]);
  1257. return false;
  1258. }
  1259. $log = [
  1260. 'user_id'=> $settleUserId,
  1261. 'merch_id'=> $merchId,
  1262. 'source_uid'=> $userId,
  1263. 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
  1264. 'type'=> 3,
  1265. 'coin_type'=> 2,
  1266. 'user_type'=> 1,
  1267. 'money'=> $settleMoney,
  1268. 'balance'=>$balance? $balance : 0.00,
  1269. 'create_time'=> time(),
  1270. 'update_time'=> time(),
  1271. 'remark'=> "服务订单[{$money}]元的{$level}级分销佣金",
  1272. 'status'=>1,
  1273. 'mark'=>1
  1274. ];
  1275. if(!AccountLogModel::insertGetId($log)){
  1276. $this->error = lang(3414, ['level'=> $level]);
  1277. return false;
  1278. }
  1279. $this->error = lang(3415, ['level'=> $level]);
  1280. return true;
  1281. }
  1282. }
  1283. $this->error = lang(3412, ['level'=> $level]);
  1284. return true;
  1285. }
  1286. /**
  1287. * 获取点对点上级节点
  1288. * @param $userId 推荐人ID
  1289. * @return int|mixed
  1290. */
  1291. public function getPointParentId($userId)
  1292. {
  1293. if($userId<=0){
  1294. return 0;
  1295. }
  1296. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1297. $userList = $userList? $userList->toArray() : [];
  1298. if(count($userList) < 3){
  1299. return $userId;
  1300. }
  1301. $users = [];
  1302. foreach($userList as $item){
  1303. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1304. $points = array_filter($points);
  1305. $users[$item['id']]['pointLen'] = count($points);
  1306. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1307. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1308. }
  1309. //var_dump($users);
  1310. $pointId = 0;
  1311. $tempPointLen = 0;
  1312. $tempChildren = 0;
  1313. foreach ($userList as $item) {
  1314. $id = isset($item['id'])? $item['id'] : 0;
  1315. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1316. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1317. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1318. if($pointLen<=$tempPointLen || $pointId == 0){
  1319. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1320. if($children <$tempChildren || $pointId == 0){
  1321. $pointId = $id;
  1322. $tempPointLen = $pointLen;
  1323. $tempChildren = $children;
  1324. }
  1325. }
  1326. }
  1327. return $pointId;
  1328. }
  1329. }