MemberService.php 65 KB

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