MemberService.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. $this->error = 2008;
  496. return [
  497. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  498. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  499. ];
  500. }
  501. /**
  502. * 邮箱登录
  503. * @param $params
  504. * @return array|false
  505. */
  506. public function loginByEmail($params)
  507. {
  508. // 授权
  509. $email = isset($params['email']) ? $params['email'] : '';
  510. $code = isset($params['code']) ? $params['code'] : '';
  511. $scene = isset($params['scene']) ? $params['scene'] : '';
  512. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  513. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  514. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  515. $this->error = 2014;
  516. return false;
  517. }
  518. // 验证码
  519. if (!EmailService::make()->check($email, $code, $scene)) {
  520. $this->error = SmsService::make()->getError();
  521. return false;
  522. }
  523. // 验证是否注册,没有则注册
  524. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  525. $data = $data ? $data->toArray() : [];
  526. $userId = isset($data['id']) ? $data['id'] : 0;
  527. $status = isset($data['status']) ? $data['status'] : 0;
  528. if ($userId && $status != 1) {
  529. $this->error = 2015;
  530. return false;
  531. }
  532. // 获取登录授权token
  533. $jwt = new Jwt('jwt_dys_app');
  534. $token = $jwt->getToken($userId);
  535. // 结果返回
  536. $result = [
  537. 'access_token' => $token,
  538. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  539. ];
  540. // 用户信息
  541. $ip = get_client_ip();
  542. $updateData = [
  543. 'login_time' => time(),
  544. 'login_ip' => $ip,
  545. ];
  546. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  547. $ipData = ToolService::make()->getIpAddress($ip,'');
  548. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  549. $city = isset($ipData['city'])? $ipData['city'] : '';
  550. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  551. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  552. }
  553. $this->model->where(['id'=> $userId])->update($updateData);
  554. $this->error = 2004;
  555. $data['token'] = $token;
  556. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  557. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  558. return $result;
  559. }
  560. /**
  561. * 手机号登录
  562. * @param $params
  563. * @return array|false
  564. */
  565. public function loginByMobile($params)
  566. {
  567. // 授权
  568. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  569. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  570. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  571. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  572. $openid = isset($params['openid']) ? $params['openid'] : '';
  573. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  574. $regType = isset($params['type']) ? $params['type'] : 'app';
  575. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  576. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  577. if (empty($params) || empty($mobile) || $code <= 0) {
  578. $this->error = 2014;
  579. return false;
  580. }
  581. // 验证码
  582. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  583. $this->error = SmsService::make()->getError();
  584. return false;
  585. }
  586. // 推荐人
  587. $parentId = 0;
  588. $parents = '';
  589. if ($inviteCode) {
  590. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  591. ->select(['id', 'parents', 'parent_id'])
  592. ->first();
  593. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  594. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  595. }
  596. // 验证是否注册,没有则注册
  597. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  598. $data = $data ? $data->toArray() : [];
  599. $userId = isset($data['id']) ? $data['id'] : 0;
  600. $status = isset($data['status']) ? $data['status'] : 0;
  601. if ($userId && $status != 1) {
  602. $this->error = 2015;
  603. return false;
  604. }
  605. // 是否是商家或技师,有订单提醒
  606. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  607. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  608. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  609. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  610. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  611. if($merchantStatus==2){
  612. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  613. if($uid>0){
  614. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  615. }
  616. }else if($mechanicStatus == 2){
  617. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  618. if($uid>0){
  619. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  620. }
  621. }
  622. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  623. if (!$userId) {
  624. $id = $this->model->max('id') + 1;
  625. $data = [
  626. 'parent_id' => $parentId,
  627. 'openid' => $openid,
  628. 'avatar' => $avatar,
  629. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  630. 'username' => get_username($id),
  631. 'password' => get_password('123456'),
  632. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  633. 'mobile' => $mobile,
  634. 'parents' => $parents,
  635. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  636. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  637. 'device' => $appSources == 'android' ? 2 : 1,
  638. 'status' => 1,
  639. 'balance' => 0,
  640. 'score' => 0,
  641. 'mark' => 1,
  642. 'login_time' => time(),
  643. 'login_ip' => get_client_ip(),
  644. 'create_time' => time(),
  645. ];
  646. $userId = $this->model->insertGetId($data);
  647. } elseif ($regType != 'h5') {
  648. // 登录更新
  649. $updateData = [
  650. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  651. 'login_count' => DB::raw("login_count+1"),
  652. 'login_time' => time(),
  653. 'login_ip' => get_client_ip(),
  654. 'update_time' => time(),
  655. ];
  656. if ($pushCid) {
  657. $updateData['push_cid'] = $pushCid;
  658. }
  659. $this->model->where(['id' => $userId])->update($updateData);
  660. }
  661. // H5 邀请注册,无需登录
  662. if ($regType == 'h5') {
  663. $this->error = $status ? 2023 : 2022;
  664. return [
  665. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  666. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  667. ];
  668. }
  669. // 获取登录授权token
  670. $jwt = new Jwt('jwt_dys_app');
  671. $token = $jwt->getToken($userId);
  672. // 结果返回
  673. $result = [
  674. 'access_token' => $token,
  675. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  676. ];
  677. // 用户信息
  678. $this->error = 2004;
  679. $data['token'] = $token;
  680. unset($data['password']);
  681. // 实现初始化账单明细表
  682. AccountLogModel::where('id','<=',1)->value('id');
  683. RedisService::clear( "caches:map:{$userId}");
  684. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  685. return $result;
  686. }
  687. /**
  688. * 修改头像/商家logo/技师头像
  689. * @param $userId
  690. * @param $avatar
  691. * @return mixed
  692. */
  693. public function saveAvatar($userId, $avatar)
  694. {
  695. $type = request()->post('type', 1);
  696. if($type == 1){
  697. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  698. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  699. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  700. @unlink(ATTACHMENT_PATH . $oldAvatar);
  701. }
  702. return true;
  703. }
  704. }else if($type == 2){
  705. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  706. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  707. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  708. @unlink(ATTACHMENT_PATH . $oldAvatar);
  709. }
  710. return true;
  711. }
  712. }else if($type == 3){
  713. $oldAvatar = MechanicModel::where(['user_id' => $userId])->value('avatar');
  714. if (MechanicModel::where(['user_id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  715. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  716. @unlink(ATTACHMENT_PATH . $oldAvatar);
  717. }
  718. return true;
  719. }
  720. }
  721. return false;
  722. }
  723. /**
  724. * 修改收款码
  725. * @param $userId
  726. * @param $params
  727. * @return mixed
  728. */
  729. public function saveQrcode($userId, $params)
  730. {
  731. if (isset($params['wxpay']) && $params['wxpay']) {
  732. $qrcode = $this->model->where(['id' => $userId])->value('wxpay_qrcode');
  733. if ($this->model->where(['id' => $userId])->update(['wxpay_qrcode' => $params['wxpay'], 'update_time' => time()])) {
  734. if ($qrcode && file_exists(ATTACHMENT_PATH . $qrcode)) {
  735. @unlink(ATTACHMENT_PATH . $qrcode);
  736. }
  737. return true;
  738. }
  739. }
  740. if (isset($params['alipay']) && $params['alipay']) {
  741. $qrcode = $this->model->where(['id' => $userId])->value('alipay_qrcode');
  742. if ($this->model->where(['id' => $userId])->update(['alipay_qrcode' => $params['alipay'], 'update_time' => time()])) {
  743. if ($qrcode && file_exists(ATTACHMENT_PATH . $qrcode)) {
  744. @unlink(ATTACHMENT_PATH . $qrcode);
  745. }
  746. return true;
  747. }
  748. }
  749. return false;
  750. }
  751. /**
  752. * 修改账号
  753. * @param $userId
  754. * @param $params
  755. * @return bool
  756. */
  757. public function modify($userId, $params)
  758. {
  759. $username = isset($params['username']) ? $params['username'] : '';
  760. $password = isset($params['password']) ? $params['password'] : '';
  761. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  762. // 用户验证
  763. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id', 'status'])->first();
  764. if (!$info) {
  765. $this->error = 2001;
  766. return false;
  767. }
  768. // 使用状态校验
  769. if ($info['status'] != 1) {
  770. $this->error = 2009;
  771. return false;
  772. }
  773. // 密码校验
  774. $data = ['update_time' => time()];
  775. if (isset($params['password']) && $password) {
  776. $password = get_password($password);
  777. $data['password'] = $password;
  778. if ($password != $info['password']) {
  779. $this->error = 2002;
  780. return false;
  781. }
  782. }
  783. // 用户账号验证
  784. if (isset($params['username']) && $username) {
  785. $data['username'] = $username;
  786. $checkInfo = $this->model->where(['username' => $username, 'mark' => 1])->select(['id', 'status'])->first();
  787. if ($checkInfo && $checkInfo['id'] != $userId) {
  788. $this->error = 2005;
  789. return false;
  790. }
  791. }
  792. // 手机号验证
  793. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  794. if (isset($params['mobile']) && $mobile) {
  795. $data['mobile'] = $mobile;
  796. $checkInfo = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'status'])->first();
  797. if ($checkInfo && $checkInfo['id'] != $userId) {
  798. $this->error = 2009;
  799. return false;
  800. }
  801. // 短信验证码
  802. $code = isset($params['code']) ? $params['code'] : '';
  803. if (empty($code)) {
  804. $this->error = 2013;
  805. return false;
  806. }
  807. if (!SmsService::make()->check($mobile, $code, 'modify')) {
  808. $this->error = SmsService::make()->getError();
  809. return false;
  810. }
  811. }
  812. // 修改数据
  813. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  814. if (isset($params['nickname']) && $nickname) {
  815. $data['nickname'] = $nickname;
  816. }
  817. $sex = isset($params['sex']) ? $params['sex'] : 0;
  818. if (isset($params['sex']) && $sex) {
  819. $data['sex'] = $sex;
  820. }
  821. $intro = isset($params['intro']) ? $params['intro'] : '';
  822. if (isset($params['intro']) && $intro) {
  823. $data['intro'] = $intro;
  824. }
  825. if (isset($params['pay_password']) && $payPassword) {
  826. $payPassword = get_password($payPassword);
  827. $data['pay_password'] = $payPassword;
  828. }
  829. $this->model->where(['id' => $userId])->update($data);
  830. $this->error = 1008;
  831. return true;
  832. }
  833. /**
  834. * 身份认证
  835. * @param $userId
  836. * @param $params
  837. * @return bool
  838. */
  839. public function auth($userId, $params)
  840. {
  841. // 用户验证
  842. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  843. if (!$info) {
  844. $this->error = 2001;
  845. return false;
  846. }
  847. // 使用状态校验
  848. if ($info['status'] != 1) {
  849. $this->error = 2009;
  850. return false;
  851. }
  852. // 密码校验
  853. $data = ['update_time' => time(),'idcard_check'=>1];
  854. if (isset($params['realname']) && $params['realname']) {
  855. $data['realname'] = trim($params['realname']);
  856. }
  857. $idcard = isset($params['idcard']) && $params['idcard'];
  858. if ($idcard) {
  859. $data['idcard'] = trim($params['idcard']);
  860. }
  861. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  862. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  863. }
  864. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  865. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  866. }
  867. $this->model->where(['id' => $userId])->update($data);
  868. // 发送消息
  869. $params = [
  870. 'title'=> "身份认证提交成功",
  871. 'body' => "您的身份认证信息已提交成功,请耐心等候审核",
  872. 'type' => 1, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  873. 'content' => [
  874. 'account' => ['name' => '账号', 'text' => $info['username']],
  875. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  876. 'status' => ['name' => '状态', 'text' => '待审核'],
  877. ],
  878. 'click_type' => 'payload',
  879. 'url' => '/pages/my/profile/auth',
  880. ];
  881. PushService::make()->pushMessageByUser($userId, $params, 0);
  882. $this->error = 2036;
  883. return true;
  884. }
  885. /**
  886. * 更新用户定位信息
  887. * @param $userId
  888. * @param $params
  889. * @return bool
  890. */
  891. public function updateMap($userId, $params)
  892. {
  893. $cacheKey = "caches:map:{$userId}";
  894. if (RedisService::get($cacheKey)) {
  895. $this->error = 1015;
  896. return false;
  897. }
  898. $lng = isset($params['lng']) ? $params['lng'] : '';
  899. $lat = isset($params['lat']) ? $params['lat'] : '';
  900. if (empty($lng) || empty($lat)) {
  901. $this->error = 2014;
  902. return false;
  903. }
  904. $address = [];
  905. $data = ['update_time' => time(), 'lng' => $lng, 'lat' => $lat];
  906. $addressData = isset($params['address']) && $params['address'] ? json_decode($params['address'], true) : [];
  907. $city = isset($addressData['city']) ? $addressData['city'] : '';
  908. $address[] = isset($addressData['province']) ? $addressData['province'] : '';
  909. $address[] = $city;
  910. $address[] = isset($addressData['district']) ? $addressData['district'] : '';
  911. $ids = CityModel::whereIn('name',$address)->where(['mark'=>1])->pluck('id');
  912. $address = array_filter($address);
  913. if ($address) {
  914. $data['province_id'] = isset($ids[0])? $ids[0] : 0;
  915. $data['city_id'] = isset($ids[1])? $ids[1] : 0;
  916. $data['district_id'] = isset($ids[2])? $ids[2] : 0;
  917. $data['address'] = implode(' ', $address);
  918. $data['city'] = $city;
  919. }
  920. RedisService::set("caches:maps:{$userId}",['data'=> $data,'params'=>$params], 300);
  921. if ($this->model->where(['id' => $userId])->update($data)) {
  922. // 如果是个人商家
  923. $updateData = [
  924. 'province' => isset($addressData['province']) ? $addressData['province'] : '',
  925. 'city' => isset($addressData['city']) ? $addressData['city'] : '',
  926. 'district' => isset($addressData['district']) ? $addressData['district'] : '',
  927. 'lng' => $lng,
  928. 'lat' => $lat,
  929. ];
  930. MerchantModel::where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->whereIn('status', [1, 2])->update($updateData);
  931. RedisService::set($cacheKey, $data, rand(30, 60));
  932. $this->error = 1016;
  933. return true;
  934. } else {
  935. RedisService::set($cacheKey.'_error', $data, rand(30, 60));
  936. $this->error = 1016;
  937. return false;
  938. }
  939. }
  940. /**
  941. * 转账
  942. * @param $userId
  943. * @param $params
  944. * @return bool
  945. */
  946. public function transfer($userId, $params)
  947. {
  948. $account = isset($params['account'])? trim($params['account']) : '';
  949. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  950. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  951. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  952. $money = isset($params['money'])? floatval($params['money']) : 0;
  953. if(empty($account) || $money<=0){
  954. $this->error = 2501;
  955. return false;
  956. }
  957. $cachekey = "caches:members:transfer:{$userId}";
  958. if(RedisService::get($cachekey)){
  959. $this->error = 1034;
  960. return false;
  961. }
  962. // 验证账户
  963. $userInfo = $this->model->with(['merchant'])
  964. ->where(['id'=> $userId,'mark'=>1])
  965. ->select(['id','nickname','username','pay_password','balance','status'])
  966. ->first();
  967. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  968. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  969. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  970. $username = isset($userInfo['username'])? $userInfo['username'] : '';
  971. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  972. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  973. $merchName = isset($merchant['name'])? $merchant['name'] : '';
  974. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  975. if(empty($userInfo) || $status != 1){
  976. $this->error = 2024;
  977. return false;
  978. }
  979. // 余额支付支付密码验证
  980. if(empty($userPayPassword)){
  981. $this->error ='1039';
  982. return false;
  983. }
  984. $payPassword = get_password($payPassword);
  985. if($payPassword != $userPayPassword){
  986. $this->error ='1038';
  987. return false;
  988. }
  989. // 验证账户
  990. $accountInfo = $this->model->where(['username'=> $account,'mark'=>1])
  991. ->select(['id','nickname','username','balance','status'])
  992. ->first();
  993. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  994. $accountBalance = isset($accountInfo['balance'])? $accountInfo['balance'] : 0;
  995. if(empty($accountInfo) || $status != 1){
  996. $this->error = 2025;
  997. return false;
  998. }
  999. if($accountInfo['id'] == $userId && $type == 1){
  1000. $this->error = 2645;
  1001. return false;
  1002. }
  1003. // 用户余额转账到用户
  1004. DB::beginTransaction();
  1005. if($type == 1){
  1006. if($money>$balance){
  1007. $this->error = 2026;
  1008. return false;
  1009. }
  1010. // 扣除账户余额
  1011. RedisService::set($cachekey, $params, rand(2,5));
  1012. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  1013. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1014. DB::rollBack();
  1015. $this->error = 2028;
  1016. RedisService::clear($cachekey);
  1017. return false;
  1018. }
  1019. }
  1020. // 商户余额转账到用户
  1021. else if($type == 2){
  1022. if($money>$merchBalance){
  1023. $this->error = 2027;
  1024. return false;
  1025. }
  1026. //
  1027. if($merchId<=0){
  1028. $this->error = 2018;
  1029. return false;
  1030. }
  1031. // 扣除商户余额
  1032. RedisService::set($cachekey, $params, rand(2,5));
  1033. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  1034. if(!MerchantModel::where(['id'=> $merchId,'mark'=>1])->update($updateData)){
  1035. DB::rollBack();
  1036. $this->error = 2028;
  1037. RedisService::clear($cachekey);
  1038. return false;
  1039. }
  1040. }
  1041. // 到账
  1042. $updateData = ['balance'=> DB::raw("balance + {$money}"),'update_time'=> time()];
  1043. if(!$this->model->where(['id'=> $accountInfo['id'],'mark'=>1])->update($updateData)){
  1044. DB::rollBack();
  1045. $this->error = 2028;
  1046. RedisService::clear($cachekey);
  1047. return false;
  1048. }
  1049. // 明细
  1050. $log = [
  1051. 'user_id' => $userId,
  1052. 'merch_id' => $merchId,
  1053. 'source_uid' => $accountInfo['id'],
  1054. 'source_order_no' => '',
  1055. 'type' => 6,
  1056. 'coin_type' => 4,
  1057. 'user_type'=> $type==2? 2 : ($userType!=2? $userType : 1),
  1058. 'money' => -$money,
  1059. 'balance' => $type==2? $merchBalance : $balance,
  1060. 'create_time' => time(),
  1061. 'update_time' => time(),
  1062. 'remark' => $type==2? "商户余额转账给【{$account}】": "账户余额转账给【{$account}】",
  1063. 'status' => 1,
  1064. 'mark' => 1,
  1065. ];
  1066. if(!AccountLogModel::insertGetId($log)){
  1067. DB::rollBack();
  1068. $this->error = 2029;
  1069. RedisService::clear($cachekey);
  1070. return false;
  1071. }
  1072. // 到账明细
  1073. $log = [
  1074. 'user_id' => $accountInfo['id'],
  1075. 'merch_id' => $merchId,
  1076. 'source_uid' => $userId,
  1077. 'source_order_no' => '',
  1078. 'type' => 6,
  1079. 'coin_type' => 4,
  1080. 'user_type'=> 1,
  1081. 'money' => $money,
  1082. 'balance' => $accountBalance,
  1083. 'create_time' => time(),
  1084. 'update_time' => time(),
  1085. 'remark' => $type==2? "来自商家【{$merchName}】的转账": "来自".($userType==3?'技师':($userType==4?'代理':'用户'))."【{$username}】的转账",
  1086. 'status' => 1,
  1087. 'mark' => 1,
  1088. ];
  1089. if(!AccountLogModel::insertGetId($log)){
  1090. DB::rollBack();
  1091. $this->error = 2029;
  1092. RedisService::clear($cachekey);
  1093. return false;
  1094. }
  1095. // 发送消息
  1096. $params = [
  1097. 'title'=> "转账成功通知",
  1098. 'body' => $type == 2? "收到一笔来自商家的转账":"收到一笔来自用户的转账",
  1099. 'type' => 2, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  1100. 'content' => [
  1101. 'account' => ['name' => $type==2?'来自商家':'来自用户', 'text' => $type==2? $merchName : $userInfo['username']],
  1102. 'pay_time' => ['name' => '到账时间', 'text' => date('Y-m-d H:i:s')],
  1103. 'money' => ['name' => '到账金额', 'text' => $money],
  1104. 'status' => ['name' => '状态', 'text' => '已到账'],
  1105. ],
  1106. 'click_type' => 'payload',
  1107. 'url' => '/pages/account/index',
  1108. ];
  1109. if(!PushService::make()->pushMessageByUser($accountInfo['id'], $params, 0)){
  1110. DB::rollBack();
  1111. $this->error = PushService::make()->getError();
  1112. RedisService::clear($cachekey);
  1113. return false;
  1114. }
  1115. DB::commit();
  1116. $this->error = 2030;
  1117. return true;
  1118. }
  1119. /**
  1120. * 余额充值
  1121. * @param $userId
  1122. * @param $params
  1123. * @return array|false
  1124. */
  1125. public function recharge($userId, $params)
  1126. {
  1127. $money = isset($params['money'])? $params['money'] : 0;
  1128. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  1129. if($money<=0){
  1130. $this->error = 2031;
  1131. return false;
  1132. }
  1133. if(!in_array($payType, [10,20])){
  1134. $this->error = 2032;
  1135. return false;
  1136. }
  1137. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1138. ->select(['id','nickname','balance','status'])
  1139. ->first();
  1140. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1141. if(empty($userInfo) || $status != 1){
  1142. $this->error = 2024;
  1143. return false;
  1144. }
  1145. // 充值订单
  1146. $orderNo = get_order_num('DR');
  1147. $data = [
  1148. 'order_no'=> $orderNo,
  1149. 'user_id'=> $userId,
  1150. 'type'=> 1,
  1151. 'user_type'=> 1,
  1152. 'money'=> $money,
  1153. 'gift_money'=> 0,
  1154. 'pay_type'=> $payType,
  1155. 'create_time'=> time(),
  1156. 'update_time'=> time(),
  1157. 'status'=> 1,
  1158. 'mark'=> 1,
  1159. ];
  1160. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1161. $this->error = 2033;
  1162. return false;
  1163. }
  1164. // 支付方式
  1165. $order = [
  1166. 'order_no'=> $orderNo,
  1167. 'type'=> 0,
  1168. 'pay_type'=> $payType,
  1169. 'pay_money'=> $money,
  1170. 'body'=> '充值订单支付',
  1171. ];
  1172. switch($payType){
  1173. case 20: // 支付宝
  1174. $payment = PaymentService::make()->aliPay($userInfo, $order,'recharge');
  1175. if(empty($payment)){
  1176. DB::rollBack();
  1177. $this->error = PaymentService::make()->getError();
  1178. return false;
  1179. }
  1180. break;
  1181. case 10: // 微信支付
  1182. $payment = PaymentService::make()->wechatPay($userInfo, $order,'recharge');
  1183. if(empty($payment)){
  1184. DB::rollBack();
  1185. $this->error = PaymentService::make()->getError();
  1186. return false;
  1187. }
  1188. break;
  1189. default:
  1190. $this->error = 1030;
  1191. return false;
  1192. }
  1193. $this->error = 2034;
  1194. return [
  1195. 'id'=> $orderId,
  1196. 'payment'=> $payment,
  1197. 'total'=> $money,
  1198. 'order_no'=> $orderNo,
  1199. 'pay_type'=> $payType,
  1200. ];
  1201. }
  1202. /**
  1203. * 余额提现
  1204. * @param $userId
  1205. * @param $params
  1206. * @return array|false
  1207. */
  1208. public function withdraw($userId, $params)
  1209. {
  1210. $money = isset($params['money'])? floatval($params['money']) : 0;
  1211. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1212. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  1213. $bankId = isset($params['bank_id']) && $params['bank_id']? intval($params['bank_id']) : 0;
  1214. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  1215. if($money<=0){
  1216. $this->error = 2031;
  1217. return false;
  1218. }
  1219. if(!in_array($payType, [10,20,50])){
  1220. $this->error = 2425;
  1221. return false;
  1222. }
  1223. $userInfo = $this->model->with(['merchant'])
  1224. ->where(['id'=> $userId,'mark'=>1])
  1225. ->select(['id','nickname','balance','pay_password','wxpay_qrcode','alipay_qrcode','status'])
  1226. ->first();
  1227. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1228. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1229. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1230. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1231. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  1232. $merchPayPassword = isset($merchant['pay_password'])? $merchant['pay_password'] : '';
  1233. if(empty($userInfo) || $status != 1){
  1234. $this->error = 2024;
  1235. return false;
  1236. }
  1237. // 验证提现类型金额
  1238. $qrcode = '';
  1239. $account = '';
  1240. if($type == 2){
  1241. // 支付密码验证
  1242. if(empty($merchPayPassword)){
  1243. $this->error ='1042';
  1244. return false;
  1245. }
  1246. $payPassword = get_password($payPassword);
  1247. if($payPassword != $merchPayPassword){
  1248. $this->error ='1043';
  1249. return false;
  1250. }
  1251. if(empty($merchant)){
  1252. $this->error = 2646;
  1253. return false;
  1254. }
  1255. if($money>$merchBalance){
  1256. $this->error = 2647;
  1257. return false;
  1258. }
  1259. $wxpayQrcode = isset($merchant['wxpay_qrcode'])? $merchant['wxpay_qrcode'] : '';
  1260. $alipayQrcode = isset($merchant['alipay_qrcode'])? $merchant['alipay_qrcode'] : '';
  1261. if($payType == 10){
  1262. $qrcode = $wxpayQrcode;
  1263. if(empty($wxpayQrcode)){
  1264. $this->error = 2649;
  1265. return false;
  1266. }
  1267. }
  1268. if($payType == 20){
  1269. $qrcode = $alipayQrcode;
  1270. if(empty($alipayQrcode)) {
  1271. $this->error = 2649;
  1272. return false;
  1273. }
  1274. }
  1275. }else{
  1276. // 支付密码验证
  1277. if(empty($userPayPassword)){
  1278. $this->error ='1039';
  1279. return false;
  1280. }
  1281. $payPassword = get_password($payPassword);
  1282. if($payPassword != $userPayPassword){
  1283. $this->error ='1038';
  1284. return false;
  1285. }
  1286. if($money>$balance){
  1287. $this->error = 2648;
  1288. return false;
  1289. }
  1290. $wxpayQrcode = isset($userInfo['wxpay_qrcode'])? $userInfo['wxpay_qrcode'] : '';
  1291. $alipayQrcode = isset($userInfo['alipay_qrcode'])? $userInfo['alipay_qrcode'] : '';
  1292. if($payType == 10){
  1293. $qrcode = $wxpayQrcode;
  1294. if(empty($wxpayQrcode)) {
  1295. $this->error = 2420;
  1296. return false;
  1297. }
  1298. }
  1299. if($payType == 20){
  1300. $qrcode = $alipayQrcode;
  1301. if(empty($alipayQrcode)){
  1302. $this->error = 2421;
  1303. return false;
  1304. }
  1305. }
  1306. if($payType == 50){
  1307. $bankInfo = MemberBankModel::where(['id'=> $bankId,'mark'=>1])
  1308. ->select(['realname','bank_name','bank_card'])
  1309. ->first();
  1310. $bankInfo = $bankInfo? $bankInfo->toArray() : [];
  1311. $bankInfo = array_values($bankInfo);
  1312. if(empty($bankInfo)){
  1313. $this->error = 2422;
  1314. return false;
  1315. }else {
  1316. $account = $bankInfo? implode(' ', $bankInfo) : '';
  1317. }
  1318. }
  1319. }
  1320. // 提现订单
  1321. $orderNo = get_order_num('DW');
  1322. $data = [
  1323. 'order_no'=> $orderNo,
  1324. 'user_id'=> $userId,
  1325. 'type'=> 2,
  1326. 'user_type'=> $type,
  1327. 'money'=> $money,
  1328. 'actual_money'=> 0,
  1329. 'qrcode'=> $qrcode,
  1330. 'account'=> $account,
  1331. 'pay_type'=> $payType,
  1332. 'create_time'=> time(),
  1333. 'update_time'=> time(),
  1334. 'status'=> 1,
  1335. 'mark'=> 1,
  1336. ];
  1337. // 提现记录
  1338. DB::beginTransaction();
  1339. if(!$id = BalanceLogModel::insertGetId($data)){
  1340. DB::rollBack();
  1341. $this->error = 2033;
  1342. return false;
  1343. }
  1344. // 商户扣款
  1345. if($type == 2){
  1346. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1347. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1348. DB::rollBack();
  1349. $this->error = 2423;
  1350. return false;
  1351. }
  1352. /*$log = [
  1353. 'user_id' => 0,
  1354. 'merch_id' => $merchant['id'],
  1355. 'source_uid' => $userId,
  1356. 'source_order_no' => $orderNo,
  1357. 'type' => 12,
  1358. 'coin_type' => 4,
  1359. 'user_type'=> 2,
  1360. 'money' => -$money,
  1361. 'balance' => $merchBalance,
  1362. 'create_time' => time(),
  1363. 'update_time' => time(),
  1364. 'remark' => "商户余额提现",
  1365. 'status' => 1,
  1366. 'mark' => 1,
  1367. ];*/
  1368. }
  1369. // 用户扣款
  1370. else{
  1371. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1372. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1373. DB::rollBack();
  1374. $this->error = 2423;
  1375. return false;
  1376. }
  1377. /*$log = [
  1378. 'user_id' => $userId,
  1379. 'merch_id' => 0,
  1380. 'source_uid' => 0,
  1381. 'source_order_no' => $orderNo,
  1382. 'type' => 11,
  1383. 'coin_type' => 4,
  1384. 'user_type'=> 1,
  1385. 'money' => -$money,
  1386. 'balance' => $balance,
  1387. 'create_time' => time(),
  1388. 'update_time' => time(),
  1389. 'remark' => "账户余额提现",
  1390. 'status' => 1,
  1391. 'mark' => 1,
  1392. ];*/
  1393. }
  1394. // 推送消息
  1395. $pushData = [
  1396. 'title' => '提现申请提交成功',
  1397. 'body' => "您的提现申请已提交请耐心等候审核",
  1398. 'type' => 3, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  1399. 'content' => [
  1400. 'order_no' => ['name' => '订单号', 'text' => $orderNo],
  1401. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  1402. 'pay_type' => ['name' => '付款方式', 'text' => $payType==10?'微信支付':($payType==20?'支付宝':'银行卡')],
  1403. 'money' => ['name' => '提现金额', 'text' => $money],
  1404. 'status' => ['name' => '状态', 'text' => '待审核'],
  1405. ],
  1406. 'click_type' => 'payload',
  1407. 'url' => '',
  1408. ];
  1409. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  1410. if($type == 2 && $merchId){
  1411. PushService::make()->pushMessageByUser($merchId, $pushData, 2);
  1412. }else{
  1413. PushService::make()->pushMessageByUser($userId, $pushData);
  1414. }
  1415. DB::commit();
  1416. $this->error = 2424;
  1417. return [
  1418. 'id'=> $id,
  1419. 'money'=> $money,
  1420. 'pay_type'=> $payType,
  1421. ];
  1422. }
  1423. /**
  1424. * 分销佣金入账
  1425. * @param $info
  1426. * @return false
  1427. */
  1428. public function settleBonus($info, $level=1)
  1429. {
  1430. $money = isset($info['money'])? floatval($info['money']) : 0;
  1431. $merchId = isset($info['merch_id'])? intval($info['merch_id']) : 0;
  1432. $userId = isset($info['user_id'])? intval($info['user_id']) : 0;
  1433. // 分销佣金
  1434. $settleUserId = isset($info["bonus_level{$level}_id"])? $info["bonus_level{$level}_id"] : 0;
  1435. $settleMoney = isset($info["bonus_level{$level}_money"])? $info["bonus_level{$level}_money"] : 0;
  1436. if($settleUserId>0 && $settleMoney>0){
  1437. $userInfo = $this->model->where(['id'=> $settleUserId,'mark'=>1])
  1438. ->select(['id','nickname','balance'])
  1439. ->first();
  1440. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1441. if($settleUserId && $userInfo){
  1442. // 更新账户
  1443. $updateData = [
  1444. 'balance'=>DB::raw("balance + {$settleMoney}"),
  1445. 'bonus_total'=>DB::raw("bonus_total + {$settleMoney}"),
  1446. "bonus_level{$level}_total"=>DB::raw("bonus_level{$level}_total + {$settleMoney}"),
  1447. 'update_time'=> time()
  1448. ];
  1449. if(!MemberModel::where(['id'=> $settleUserId,'mark'=>1])->update($updateData)){
  1450. $this->error = lang(3413, ['level'=> $level]);
  1451. return false;
  1452. }
  1453. $log = [
  1454. 'user_id'=> $settleUserId,
  1455. 'merch_id'=> $merchId,
  1456. 'source_uid'=> $userId,
  1457. 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
  1458. 'type'=> 3,
  1459. 'coin_type'=> 2,
  1460. 'user_type'=> 1,
  1461. 'money'=> $settleMoney,
  1462. 'balance'=>$balance? $balance : 0.00,
  1463. 'create_time'=> time(),
  1464. 'update_time'=> time(),
  1465. 'remark'=> "服务订单[{$money}]元的{$level}级分销佣金",
  1466. 'status'=>1,
  1467. 'mark'=>1
  1468. ];
  1469. if(!AccountLogModel::insertGetId($log)){
  1470. $this->error = lang(3414, ['level'=> $level]);
  1471. return false;
  1472. }
  1473. $this->error = lang(3415, ['level'=> $level]);
  1474. return true;
  1475. }
  1476. }
  1477. $this->error = lang(3412, ['level'=> $level]);
  1478. return true;
  1479. }
  1480. /**
  1481. * 商家上级分销佣金入账
  1482. * @param $info
  1483. * @return false
  1484. */
  1485. public function settleMerchBonus($info, $level=1)
  1486. {
  1487. $money = isset($info['money'])? floatval($info['money']) : 0;
  1488. $merchId = isset($info['merch_id'])? intval($info['merch_id']) : 0;
  1489. // 分销佣金
  1490. $settleUserId = isset($info["merch_level{$level}_id"])? $info["merch_level{$level}_id"] : 0;
  1491. $settleMoney = isset($info["merch_level{$level}_money"])? $info["merch_level{$level}_money"] : 0;
  1492. if($settleUserId>0 && $settleMoney>0){
  1493. $userInfo = $this->model->where(['id'=> $settleUserId,'mark'=>1])
  1494. ->select(['id','nickname','balance'])
  1495. ->first();
  1496. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1497. if($settleUserId && $userInfo){
  1498. // 更新账户
  1499. $updateData = [
  1500. 'balance'=>DB::raw("balance + {$settleMoney}"),
  1501. 'bonus_total'=>DB::raw("bonus_total + {$settleMoney}"),
  1502. "bonus_level{$level}_total"=>DB::raw("bonus_level{$level}_total + {$settleMoney}"),
  1503. 'update_time'=> time()
  1504. ];
  1505. if(!MemberModel::where(['id'=> $settleUserId,'mark'=>1])->update($updateData)){
  1506. $this->error = lang(3413, ['level'=> $level]);
  1507. return false;
  1508. }
  1509. $log = [
  1510. 'user_id'=> $settleUserId,
  1511. 'merch_id'=> $merchId,
  1512. 'source_uid'=> $merchId,
  1513. 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
  1514. 'type'=> 3,
  1515. 'coin_type'=> 2,
  1516. 'user_type'=> 1,
  1517. 'money'=> $settleMoney,
  1518. 'balance'=>$balance? $balance : 0.00,
  1519. 'create_time'=> time(),
  1520. 'update_time'=> time(),
  1521. 'remark'=> "服务订单[{$money}]元的{$level}级分销佣金",
  1522. 'status'=>1,
  1523. 'mark'=>1
  1524. ];
  1525. if(!AccountLogModel::insertGetId($log)){
  1526. $this->error = lang(3414, ['level'=> $level]);
  1527. return false;
  1528. }
  1529. $this->error = lang(3415, ['level'=> $level]);
  1530. return true;
  1531. }
  1532. }
  1533. $this->error = lang(3412, ['level'=> $level]);
  1534. return true;
  1535. }
  1536. /**
  1537. * 获取点对点上级节点
  1538. * @param $userId 推荐人ID
  1539. * @return int|mixed
  1540. */
  1541. public function getPointParentId($userId)
  1542. {
  1543. if($userId<=0){
  1544. return 0;
  1545. }
  1546. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1547. $userList = $userList? $userList->toArray() : [];
  1548. if(count($userList) < 3){
  1549. return $userId;
  1550. }
  1551. $users = [];
  1552. foreach($userList as $item){
  1553. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1554. $points = array_filter($points);
  1555. $users[$item['id']]['pointLen'] = count($points);
  1556. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1557. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1558. }
  1559. $pointId = 0;
  1560. $tempPointLen = 0;
  1561. $tempChildren = 0;
  1562. foreach ($userList as $item) {
  1563. $id = isset($item['id'])? $item['id'] : 0;
  1564. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1565. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1566. if($pointLen<=$tempPointLen || $pointId = 0){
  1567. if($children < $tempChildren || $pointId = 0){
  1568. $pointId = $id;
  1569. $tempPointLen = $pointLen;
  1570. }
  1571. }
  1572. }
  1573. return $pointId;
  1574. }
  1575. }