MemberService.php 52 KB

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