MemberService.php 66 KB

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