UserServices.php 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\jobs\UserJob;
  13. use app\services\activity\bargain\StoreBargainServices;
  14. use app\services\activity\combination\StoreCombinationServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\agent\AgentLevelServices;
  17. use app\services\agent\SpreadApplyServices;
  18. use app\services\BaseServices;
  19. use app\dao\user\UserDao;
  20. use app\services\activity\coupon\StoreCouponUserServices;
  21. use app\services\diy\DiyServices;
  22. use app\services\kefu\service\StoreServiceRecordServices;
  23. use app\services\kefu\service\StoreServiceServices;
  24. use app\services\order\OtherOrderServices;
  25. use app\services\order\StoreOrderCreateServices;
  26. use app\services\order\StoreOrderServices;
  27. use app\services\order\StoreOrderTakeServices;
  28. use app\services\other\QrcodeServices;
  29. use app\services\product\product\StoreProductLogServices;
  30. use app\services\product\product\StoreProductRelationServices;
  31. use app\services\message\MessageSystemServices;
  32. use app\services\system\SystemUserLevelServices;
  33. use app\services\user\member\MemberCardServices;
  34. use app\services\wechat\WechatUserServices;
  35. use crmeb\exceptions\AdminException;
  36. use crmeb\exceptions\ApiException;
  37. use crmeb\services\CacheService;
  38. use crmeb\services\FormBuilder as Form;
  39. use crmeb\services\FormBuilder;
  40. use crmeb\services\app\WechatService;
  41. use think\Exception;
  42. use think\facade\Route as Url;
  43. /**
  44. * Class UserServices
  45. * @package app\services\user
  46. * @method array getUserInfoArray(array $where, string $field, string $key) 根据条件查询对应的用户信息以数组形式返回
  47. * @method update($id, array $data, ?string $key = null) 修改数据
  48. * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
  49. * @method count(array $where) 获取指定条件下的数量
  50. * @method value(array $where, string $field) 获取指定的键值
  51. * @method bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
  52. * @method bcDec($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度减法
  53. * @method getTrendData($time, $type, $timeType)
  54. * @method incPayCount(int $uid) 用户支付成功个数增加
  55. */
  56. class UserServices extends BaseServices
  57. {
  58. /**
  59. * UserServices constructor.
  60. * @param UserDao $dao
  61. */
  62. public function __construct(UserDao $dao)
  63. {
  64. $this->dao = $dao;
  65. }
  66. /**
  67. * 获取用户信息
  68. * @param int $uid
  69. * @param string $field
  70. * @return array|\think\Model|null
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @author 吴汐
  75. * @email 442384644@qq.com
  76. * @date 2023/03/01
  77. */
  78. public function getUserInfo(int $uid, $field = '*')
  79. {
  80. if (is_string($field)) $field = explode(',', $field);
  81. return $this->dao->get($uid, $field);
  82. }
  83. /**
  84. * 获取用户列表
  85. * @param array $where
  86. * @param string $field
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function getUserList(array $where, string $field): array
  93. {
  94. [$page, $limit] = $this->getPageValue();
  95. $list = $this->dao->getList($where, $field, $page, $limit);
  96. $count = $this->getCount($where);
  97. return compact('list', 'count');
  98. }
  99. /**
  100. * 列表条数
  101. * @param array $where
  102. * @return int
  103. */
  104. public function getCount(array $where, bool $is_list = false)
  105. {
  106. return $this->dao->getCount($where, $is_list);
  107. }
  108. /**
  109. * 保存用户信息
  110. * @param $user
  111. * @param int $spreadUid
  112. * @param string $userType
  113. * @return User|\think\Model
  114. * @throws Exception
  115. */
  116. public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat')
  117. {
  118. $data = [
  119. 'account' => $user['account'] ?? 'wx' . rand(1, 9999) . time(),
  120. 'pwd' => $user['pwd'] ?? md5('123456'),
  121. 'nickname' => $user['nickname'] ?? '',
  122. 'avatar' => $user['headimgurl'] ?? '',
  123. 'phone' => $user['phone'] ?? '',
  124. 'add_time' => time(),
  125. 'add_ip' => app()->request->ip(),
  126. 'last_time' => time(),
  127. 'last_ip' => app()->request->ip(),
  128. 'user_type' => $userType,
  129. 'staff_id' => $user['staff_id'] ?? 0,
  130. 'agent_id' => $user['agent_id'] ?? 0,
  131. 'division_id' => $user['division_id'] ?? 0,
  132. ];
  133. if ($spreadUid) {
  134. $data['spread_uid'] = $spreadUid;
  135. $data['spread_time'] = time();
  136. }
  137. $res = $this->dao->save($data);
  138. if (!$res)
  139. throw new AdminException(400684);
  140. //新用户注册奖励
  141. $this->rewardNewUser((int)$res->uid);
  142. //用户生成后置事件
  143. event('UserRegisterListener', [$spreadUid, $userType, $user['nickname'], $res->uid, 1]);
  144. //自定义事件-用户注册
  145. event('CustomEventListener', ['user_register', [
  146. 'uid' => $res->uid,
  147. 'nickname' => $user['nickname'],
  148. 'phone' => $data['phone'],
  149. 'add_time' => date('Y-m-d H:i:s'),
  150. 'user_type' => $userType,
  151. ]]);
  152. if ($spreadUid) {
  153. //推送消息
  154. event('NoticeListener', [['spreadUid' => $spreadUid, 'user_type' => $userType, 'nickname' => $user['nickname']], 'bind_spread_uid']);
  155. //自定义事件-绑定关系
  156. event('CustomEventListener', ['user_spread', [
  157. 'uid' => $res->uid,
  158. 'nickname' => $user['nickname'],
  159. 'spread_uid' => $spreadUid,
  160. 'spread_time' => date('Y-m-d H:i:s'),
  161. 'user_type' => $userType,
  162. ]]);
  163. }
  164. return $res;
  165. }
  166. /**
  167. * 某些条件用户佣金总和
  168. * @param array $where
  169. * @return mixed
  170. */
  171. public function getSumBrokerage(array $where)
  172. {
  173. return $this->dao->getWhereSumField($where, 'brokerage_price');
  174. }
  175. /**
  176. * 根据条件获取用户指定字段列表
  177. * @param array $where
  178. * @param string $field
  179. * @param string $key
  180. * @return array
  181. */
  182. public function getColumn(array $where, string $field = '*', string $key = '')
  183. {
  184. return $this->dao->getColumn($where, $field, $key);
  185. }
  186. /**
  187. * 获取某个用户的推广下线
  188. */
  189. public function getSpreadList($uid)
  190. {
  191. $one_uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  192. $two_uids = $this->dao->getColumn([['spread_uid', 'in', $one_uids], ['spread_uid', '<>', 0]], 'uid');
  193. $uids = array_merge($one_uids, $two_uids);
  194. [$page, $limit] = $this->getPageValue();
  195. $list = $this->dao->getList(['uid' => $uids], 'uid,nickname,real_name,avatar,add_time', $page, $limit);
  196. foreach ($list as $k => $user) {
  197. $list[$k]['type'] = in_array($user['uid'], $one_uids) ? '一级' : '二级';
  198. $list[$k]['add_time'] = date('Y-m-d', $user['add_time']);
  199. }
  200. $count = count($uids);
  201. return compact('count', 'list');
  202. }
  203. /**查找多个uid信息
  204. * @param $uids
  205. * @param bool $field
  206. * @return UserDao|bool|\crmeb\basic\BaseModel|mixed|\think\Collection
  207. */
  208. public function getUserListByUids($uids, $field = false)
  209. {
  210. if (!$uids || !is_array($uids)) return false;
  211. return $this->dao->getUserListByUids($uids, $field);
  212. }
  213. /**
  214. * 获取分销用户
  215. * @param array $where
  216. * @param string $field
  217. * @return array
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function getAgentUserList(array $where = [], string $field = '*', $is_page = true)
  223. {
  224. $where_data['status'] = 1;
  225. $where_data['is_promoter'] = 1;
  226. $where_data['spread_open'] = 1;
  227. //人人分销时 去除分销员字段的限制
  228. $store_brokerage_statu = sys_config('store_brokerage_statu');
  229. if ($store_brokerage_statu == 2) unset($where_data['is_promoter']);
  230. if (isset($where['nickname']) && $where['nickname'] !== '') {
  231. $where_data['like'] = $where['nickname'];
  232. }
  233. if (isset($where['data']) && $where['data']) {
  234. $where_data['time'] = $where['data'];
  235. }
  236. [$page, $limit] = $this->getPageValue($is_page);
  237. $list = $this->dao->getAgentUserList($where_data, $field, $page, $limit);
  238. $count = $this->dao->count($where_data);
  239. return compact('count', 'list');
  240. }
  241. /**
  242. * 获取分销员ids
  243. * @param array $where
  244. * @return array
  245. * @throws \ReflectionException
  246. */
  247. public function getAgentUserIds(array $where)
  248. {
  249. $where['status'] = 1;
  250. if (sys_config('store_brokerage_statu') != 2) $where['is_promoter'] = 1;
  251. $where['spread_open'] = 1;
  252. if (isset($where['nickname']) && $where['nickname'] !== '') {
  253. $where['like'] = $where['nickname'];
  254. unset($where['nickname']);
  255. }
  256. if (isset($where['data']) && $where['data']) {
  257. $where['time'] = $where['data'];
  258. }
  259. return $this->dao->getAgentUserIds($where);
  260. }
  261. /**
  262. * 获取推广人列表
  263. * @param array $where
  264. * @param string $field
  265. * @param int $page
  266. * @param int $limit
  267. * @throws \think\db\exception\DataNotFoundException
  268. * @throws \think\db\exception\DbException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. */
  271. public function getSairList(array $where, string $field = '*')
  272. {
  273. $where_data = [];
  274. if (isset($where['uid'])) {
  275. if (isset($where['type'])) {
  276. $type = (int)$where['type'];
  277. $type = in_array($type, [1, 2]) ? $type : 0;
  278. $uids = $this->getUserSpredadUids((int)$where['uid'], $type);
  279. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  280. }
  281. if (isset($where['data']) && $where['data']) {
  282. $where_data['time'] = $where['data'];
  283. }
  284. if (isset($where['nickname']) && $where['nickname']) {
  285. $where_data['like'] = $where['nickname'];
  286. }
  287. $where_data['status'] = 1;
  288. }
  289. [$page, $limit] = $this->getPageValue();
  290. $list = $this->dao->getSairList($where_data, '*', $page, $limit);
  291. $count = $this->dao->count($where_data);
  292. return compact('list', 'count');
  293. }
  294. /**
  295. * 获取推广人统计
  296. * @param array $where
  297. * @param string $field
  298. * @param int $page
  299. * @param int $limit
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\DbException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. */
  304. public function getSairCount(array $where)
  305. {
  306. $where_data = [];
  307. if (isset($where['uid'])) {
  308. if (isset($where['type'])) {
  309. $uids = $this->getColumn(['spread_uid' => $where['uid']], 'uid');
  310. switch ((int)$where['type']) {
  311. case 1:
  312. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  313. break;
  314. case 2:
  315. if (count($uids))
  316. $spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid');
  317. else
  318. $spread_uid_two = [];
  319. $where_data['uid'] = count($spread_uid_two) > 0 ? $spread_uid_two : 0;
  320. break;
  321. default:
  322. if (count($uids)) {
  323. if ($spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid')) {
  324. $uids = array_merge($uids, $spread_uid_two);
  325. $uids = array_unique($uids);
  326. $uids = array_merge($uids);
  327. }
  328. }
  329. $where_data['uid'] = count($uids) > 0 ? $uids : 0;
  330. break;
  331. }
  332. }
  333. if (isset($where['data']) && $where['data']) {
  334. $where_data['time'] = $where['data'];
  335. }
  336. if (isset($where['nickname']) && $where['nickname']) {
  337. $where_data['like'] = $where['nickname'];
  338. }
  339. $where_data['status'] = 1;
  340. }
  341. return $this->dao->count($where_data);
  342. }
  343. /**
  344. * 写入用户信息
  345. * @param array $data
  346. * @return bool
  347. */
  348. public function create(array $data)
  349. {
  350. if (!$this->dao->save($data))
  351. throw new AdminException(100000);
  352. return true;
  353. }
  354. /**
  355. * 重置密码
  356. * @param $id
  357. * @param string $password
  358. * @return mixed
  359. */
  360. public function resetPwd(int $uid, string $password)
  361. {
  362. if (!$this->dao->update($uid, ['pwd' => $password]))
  363. throw new AdminException(400685);
  364. return true;
  365. }
  366. /**
  367. * 增加推广人数
  368. * @param int $uid
  369. * @param int $num
  370. * @return bool
  371. * @throws Exception
  372. */
  373. public function incSpreadCount(int $uid, int $num = 1)
  374. {
  375. if (!$this->dao->incField($uid, 'spread_count', $num))
  376. throw new AdminException(400686);
  377. return true;
  378. }
  379. /**
  380. * 设置用户登录类型
  381. * @param int $uid
  382. * @param string $type
  383. * @return bool
  384. * @throws Exception
  385. */
  386. public function setLoginType(int $uid, string $type = 'h5')
  387. {
  388. if (!$this->dao->update($uid, ['login_type' => $type]))
  389. throw new AdminException(400687);
  390. return true;
  391. }
  392. /**
  393. * 设置推广员
  394. * @param int $uid
  395. * @param int $is_promoter
  396. * @return bool
  397. * @throws Exception
  398. */
  399. public function setIsPromoter(int $uid, $is_promoter = 1)
  400. {
  401. if (!$this->dao->update($uid, ['is_promoter' => $is_promoter]))
  402. throw new AdminException(400688);
  403. return true;
  404. }
  405. /**
  406. * 设置用户分组
  407. * @param $uids
  408. * @param int $group_id
  409. */
  410. public function setUserGroup($uids, int $group_id)
  411. {
  412. return $this->dao->batchUpdate($uids, ['group_id' => $group_id], 'uid');
  413. }
  414. /**
  415. * 增加用户余额
  416. * @param int $uid
  417. * @param float $old_now_money
  418. * @param float $now_money
  419. * @return bool
  420. * @throws Exception
  421. */
  422. public function addNowMoney(int $uid, $old_now_money, $now_money)
  423. {
  424. if (!$this->dao->update($uid, ['now_money' => bcadd($old_now_money, $now_money, 2)]))
  425. throw new AdminException(400689);
  426. return true;
  427. }
  428. /**
  429. * 减少用户余额
  430. * @param int $uid
  431. * @param float $old_now_money
  432. * @param float $now_money
  433. * @return bool
  434. * @throws Exception
  435. */
  436. public function cutNowMoney(int $uid, $old_now_money, $now_money)
  437. {
  438. if ($old_now_money > $now_money) {
  439. $money = ['now_money' => bcsub($old_now_money, $now_money, 2)];
  440. } else {
  441. $money = ['now_money' => 0];
  442. }
  443. if (!$this->dao->update($uid, $money, 'uid'))
  444. throw new AdminException(400690);
  445. return true;
  446. }
  447. /**
  448. * 减少用户佣金
  449. * @param int $uid
  450. * @param float $brokerage_price
  451. * @param float $price
  452. * @return bool
  453. * @throws Exception
  454. */
  455. public function cutBrokeragePrice(int $uid, $brokerage_price, $price)
  456. {
  457. if (!$this->dao->update($uid, ['brokerage_price' => bcsub($brokerage_price, $price, 2)]))
  458. throw new AdminException(400691);
  459. return true;
  460. }
  461. /**
  462. * 增加用户积分
  463. * @param int $uid
  464. * @param float $old_integral
  465. * @param float $integral
  466. * @return bool
  467. * @throws Exception
  468. */
  469. public function addIntegral(int $uid, $old_integral, $integral)
  470. {
  471. if (!$this->dao->update($uid, ['integral' => bcadd($old_integral, $integral, 2)]))
  472. throw new AdminException(400692);
  473. return true;
  474. }
  475. /**
  476. * 减少用户积分
  477. * @param int $uid
  478. * @param float $old_integral
  479. * @param float $integral
  480. * @return bool
  481. * @throws Exception
  482. */
  483. public function cutIntegral(int $uid, $old_integral, $integral)
  484. {
  485. if (!$this->dao->update($uid, ['integral' => bcsub($old_integral, $integral, 2)]))
  486. throw new AdminException(400693);
  487. return true;
  488. }
  489. /**
  490. * 增加用户经验
  491. * @param int $uid
  492. * @param float $old_exp
  493. * @param float $exp
  494. * @return bool
  495. * @throws Exception
  496. */
  497. public function addExp(int $uid, float $old_exp, float $exp)
  498. {
  499. if (!$this->dao->update($uid, ['exp' => bcadd($old_exp, $exp, 2)]))
  500. throw new AdminException(400694);
  501. return true;
  502. }
  503. /**
  504. * 减少用户经验
  505. * @param int $uid
  506. * @param float $old_exp
  507. * @param float $exp
  508. * @return bool
  509. * @throws Exception
  510. */
  511. public function cutExp(int $uid, float $old_exp, float $exp)
  512. {
  513. if (!$this->dao->update($uid, ['exp' => bcsub($old_exp, $exp, 2)]))
  514. throw new AdminException(400695);
  515. return true;
  516. }
  517. /**
  518. * 获取用户标签
  519. * @param $uid
  520. * @return \think\Collection
  521. * @throws \think\db\exception\DataNotFoundException
  522. * @throws \think\db\exception\DbException
  523. * @throws \think\db\exception\ModelNotFoundException
  524. */
  525. public function getUserLablel(array $uids)
  526. {
  527. /** @var UserLabelRelationServices $services */
  528. $services = app()->make(UserLabelRelationServices::class);
  529. $userlabels = $services->getUserLabelList($uids);
  530. $data = [];
  531. foreach ($uids as $uid) {
  532. $labels = array_filter($userlabels, function ($item) use ($uid) {
  533. if ($item['uid'] == $uid) {
  534. return true;
  535. }
  536. });
  537. $data[$uid] = implode(',', array_column($labels, 'label_name'));
  538. }
  539. return $data;
  540. }
  541. /**
  542. * 会员列表
  543. * @param array $where
  544. * @return array
  545. */
  546. public function index(array $where)
  547. {
  548. /** @var UserWechatuserServices $userWechatUser */
  549. $userWechatUser = app()->make(UserWechatuserServices::class);
  550. $fields = 'u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
  551. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  552. if ($list) {
  553. $uids = array_column($list, 'uid');
  554. $userlabel = $this->getUserLablel($uids);
  555. $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
  556. $userExtract = app()->make(UserExtractServices::class)->getUsersSumList($uids);
  557. $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  558. $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  559. $agentLevel = app()->make(AgentLevelServices::class)->getAgentLevelArr();
  560. $spread_names = $this->dao->getColumn([['uid', 'in', array_unique(array_column($list, 'spread_uid'))]], 'nickname', 'uid');
  561. foreach ($list as &$item) {
  562. if (empty($item['addres'])) {
  563. if (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) {
  564. $item['addres'] = $item['country'] . $item['province'] . $item['city'];
  565. }
  566. }
  567. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  568. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  569. $item['extract_count_price'] = $userExtract[$item['uid']] ?? 0;//累计提现
  570. $item['spread_uid_nickname'] = $item['spread_uid'] ? ($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid'] : '无';
  571. //用户类型
  572. if ($item['user_type'] == 'routine') {
  573. $item['user_type'] = '小程序';
  574. } else if ($item['user_type'] == 'wechat') {
  575. $item['user_type'] = '公众号';
  576. } else if ($item['user_type'] == 'h5') {
  577. $item['user_type'] = 'H5';
  578. } else if ($item['user_type'] == 'pc') {
  579. $item['user_type'] = 'PC';
  580. } else if ($item['user_type'] == 'app' || $item['user_type'] == 'apple') {
  581. $item['user_type'] = 'APP';
  582. } else $item['user_type'] = '其他';
  583. if ($item['sex'] == 1) {
  584. $item['sex'] = '男';
  585. } else if ($item['sex'] == 2) {
  586. $item['sex'] = '女';
  587. } else $item['sex'] = '保密';
  588. //等级名称
  589. $item['level'] = $levelName[$item['level']] ?? '无';
  590. //分组名称
  591. $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
  592. //用户等级
  593. $item['vip_name'] = false;
  594. $levelInfo = $userLevel[$item['uid']] ?? null;
  595. if ($levelInfo) {
  596. if ($levelInfo && ($levelInfo['is_forever'] || time() < $levelInfo['valid_time'])) {
  597. $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
  598. }
  599. }
  600. $item['agent_level_name'] = $agentLevel[$item['agent_level']] ?? '无';
  601. $item['labels'] = $userlabel[$item['uid']] ?? '';
  602. $item['isMember'] = $item['is_money_level'] > 0 ? 1 : 0;
  603. if (strpos($item['avatar'], '/statics/system_images/') !== false) {
  604. $item['avatar'] = set_file_url($item['avatar']);
  605. }
  606. }
  607. }
  608. return compact('count', 'list');
  609. }
  610. /**
  611. * 获取修改页面数据
  612. * @param int $id
  613. * @return array
  614. * @throws \FormBuilder\Exception\FormBuilderException
  615. */
  616. public function edit(int $id)
  617. {
  618. $user = $this->getUserInfo($id);
  619. if (!$user)
  620. throw new AdminException(100026);
  621. $f = array();
  622. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(true);
  623. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  624. $f[] = Form::input('phone', '手机号码', $user->getData('phone'));
  625. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '');
  626. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  627. $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
  628. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  629. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('不改密码请留空');
  630. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('不改密码请留空');
  631. //查询高于当前会员的所有会员等级
  632. // $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  633. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  634. $setOptionLevel = function () use ($systemLevelList) {
  635. $menus = [];
  636. foreach ($systemLevelList as $menu) {
  637. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  638. }
  639. return $menus;
  640. };
  641. $f[] = Form::select('level', '用户等级', (int)$user->getData('level'))->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  642. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  643. $setOptionGroup = function () use ($systemGroupList) {
  644. $menus = [];
  645. foreach ($systemGroupList as $menu) {
  646. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  647. }
  648. return $menus;
  649. };
  650. $f[] = Form::select('group_id', '用户分组', $user->getData('group_id'))->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  651. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  652. $labels = app()->make(UserLabelRelationServices::class)->getUserLabels($user['uid']);
  653. $setOptionLabel = function () use ($systemLabelList) {
  654. $menus = [];
  655. foreach ($systemLabelList as $menu) {
  656. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  657. }
  658. return $menus;
  659. };
  660. $f[] = Form::select('label_id', '用户标签', $labels)->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  661. $f[] = Form::radio('spread_open', '推广资格', $user->getData('spread_open'))->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  662. //分销模式 人人分销
  663. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  664. if ($storeBrokerageStatus == 1) {
  665. $f[] = Form::radio('is_promoter', '推广员权限', $user->getData('is_promoter'))->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  666. }
  667. $f[] = Form::radio('status', '用户状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  668. return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  669. }
  670. /**
  671. * 添加用户表单
  672. * @param int $id
  673. * @return array
  674. * @throws \FormBuilder\Exception\FormBuilderException
  675. */
  676. public function saveForm()
  677. {
  678. $f = array();
  679. $f[] = Form::input('real_name', '真实姓名', '')->placeholder('请输入真实姓名');
  680. $f[] = Form::input('phone', '手机号码', '')->placeholder('请输入手机号码')->required();
  681. $f[] = Form::date('birthday', '生日', '')->placeholder('请选择生日');
  682. $f[] = Form::input('card_id', '身份证号', '')->placeholder('请输入身份证号');
  683. $f[] = Form::input('addres', '用户地址', '')->placeholder('请输入用户地址');
  684. $f[] = Form::textarea('mark', '用户备注', '')->placeholder('请输入用户备注');
  685. $f[] = Form::input('pwd', '登录密码')->type('password')->placeholder('请输入登录密码');
  686. $f[] = Form::input('true_pwd', '确认密码')->type('password')->placeholder('请再次确认密码');
  687. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList([], 'id,name');
  688. $setOptionLevel = function () use ($systemLevelList) {
  689. $menus = [];
  690. foreach ($systemLevelList as $menu) {
  691. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  692. }
  693. return $menus;
  694. };
  695. $f[] = Form::select('level', '用户等级', '')->setOptions(FormBuilder::setOptions($setOptionLevel))->filterable(true);
  696. $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
  697. $setOptionGroup = function () use ($systemGroupList) {
  698. $menus = [];
  699. foreach ($systemGroupList as $menu) {
  700. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  701. }
  702. return $menus;
  703. };
  704. $f[] = Form::select('group_id', '用户分组', '')->setOptions(FormBuilder::setOptions($setOptionGroup))->filterable(true);
  705. $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
  706. $setOptionLabel = function () use ($systemLabelList) {
  707. $menus = [];
  708. foreach ($systemLabelList as $menu) {
  709. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  710. }
  711. return $menus;
  712. };
  713. $f[] = Form::select('label_id', '用户标签', '')->setOptions(FormBuilder::setOptions($setOptionLabel))->filterable(true)->multiple(true);
  714. $f[] = Form::radio('spread_open', '推广资格', 1)->info('禁用用户的推广资格后,在任何分销模式下该用户都无分销权限')->options([['value' => 1, 'label' => '启用'], ['value' => 0, 'label' => '禁用']]);
  715. //分销模式 人人分销
  716. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  717. if ($storeBrokerageStatus == 1) {
  718. $f[] = Form::radio('is_promoter', '推广员权限', 0)->info('指定分销模式下,开启或关闭用户的推广权限')->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  719. }
  720. $f[] = Form::radio('status', '用户状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  721. return create_form('添加用户', $f, $this->url('/user/user'), 'POST');
  722. }
  723. /**
  724. * 修改提交处理
  725. * @param int $id
  726. * @param array $data
  727. * @return bool
  728. * @throws Exception
  729. * @throws \think\db\exception\DataNotFoundException
  730. * @throws \think\db\exception\ModelNotFoundException
  731. */
  732. public function updateInfo(int $id, array $data)
  733. {
  734. $user = $this->getUserInfo($id);
  735. if (!$user) {
  736. throw new AdminException(100026);
  737. }
  738. $res1 = false;
  739. $res2 = false;
  740. $edit = array();
  741. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  742. /** @var UserMoneyServices $userMoneyServices */
  743. $userMoneyServices = app()->make(UserMoneyServices::class);
  744. if ($data['money_status'] == 1) {//增加
  745. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  746. $res1 = $userMoneyServices->income('system_add', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  747. //增加充值记录
  748. $recharge_data = [
  749. 'order_id' => app()->make(StoreOrderCreateServices::class)->getNewOrderId('cz'),
  750. 'uid' => $id,
  751. 'price' => $data['money'],
  752. 'recharge_type' => 'system',
  753. 'paid' => 1,
  754. 'add_time' => time(),
  755. 'give_price' => 0,
  756. 'channel_type' => 'system',
  757. 'pay_time' => time(),
  758. ];
  759. /** @var UserRechargeServices $rechargeServices */
  760. $rechargeServices = app()->make(UserRechargeServices::class);
  761. $rechargeServices->save($recharge_data);
  762. } else if ($data['money_status'] == 2) {//减少
  763. if ($user['now_money'] > $data['money']) {
  764. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  765. } else {
  766. $edit['now_money'] = 0;
  767. $data['money'] = $user['now_money'];
  768. }
  769. $res1 = $userMoneyServices->income('system_sub', $user['uid'], $data['money'], $edit['now_money'], $data['adminId'] ?? 0);
  770. }
  771. event('OutPushListener', ['user_update_push', ['uid' => $id, 'type' => 'money', 'value' => $data['money_status'] == 2 ? -floatval($data['money']) : $data['money']]]);
  772. } else {
  773. $res1 = true;
  774. }
  775. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  776. /** @var UserBillServices $userBill */
  777. $userBill = app()->make(UserBillServices::class);
  778. $integral_data = ['link_id' => $data['adminId'] ?? 0, 'number' => $data['integration']];
  779. if ($data['integration_status'] == 1) {//增加
  780. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  781. $integral_data['balance'] = $edit['integral'];
  782. $integral_data['title'] = '系统增加积分';
  783. $integral_data['mark'] = '系统增加了' . floatval($data['integration']) . '积分';
  784. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  785. } else if ($data['integration_status'] == 2) {//减少
  786. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  787. $integral_data['balance'] = $edit['integral'];
  788. $integral_data['title'] = '系统减少积分';
  789. $integral_data['mark'] = '系统扣除了' . floatval($data['integration']) . '积分';
  790. $res2 = $userBill->expendIntegral($user['uid'], 'system_sub', $integral_data);
  791. }
  792. event('OutPushListener', ['user_update_push', ['uid' => $id, 'type' => 'point', 'value' => $data['integration_status'] == 2 ? -intval($data['integration']) : $data['integration']]]);
  793. } else {
  794. $res2 = true;
  795. }
  796. //修改基本信息
  797. if (!isset($data['is_other']) || !$data['is_other']) {
  798. app()->make(UserLabelRelationServices::class)->setUserLable([$id], $data['label_id']);
  799. if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
  800. $edit['pwd'] = $data['pwd'];
  801. }
  802. if (isset($data['spread_open'])) {
  803. $edit['spread_open'] = $data['spread_open'];
  804. }
  805. $edit['status'] = $data['status'];
  806. $edit['real_name'] = $data['real_name'];
  807. $edit['card_id'] = $data['card_id'];
  808. $edit['birthday'] = strtotime($data['birthday']);
  809. $edit['mark'] = $data['mark'];
  810. $edit['is_promoter'] = $data['is_promoter'];
  811. $edit['level'] = $data['level'];
  812. $edit['phone'] = $data['phone'];
  813. $edit['addres'] = $data['addres'];
  814. $edit['group_id'] = $data['group_id'];
  815. if ($user['level'] != $data['level']) {
  816. /** @var UserLevelServices $userLevelService */
  817. $userLevelService = app()->make(UserLevelServices::class);
  818. $userLevelService->setUserLevel((int)$user['uid'], (int)$data['level']);
  819. }
  820. if ($data['is_promoter'] == 0) {
  821. app()->make(SpreadApplyServices::class)->delete(['uid' => $user['uid']]);
  822. }
  823. }
  824. if ($edit) $res3 = $this->dao->update($id, $edit);
  825. else $res3 = true;
  826. if ($res1 && $res2 && $res3)
  827. return true;
  828. else throw new AdminException(100007);
  829. }
  830. /**
  831. * 编辑其他
  832. * @param $id
  833. * @return mixed
  834. * @throws \FormBuilder\Exception\FormBuilderException
  835. */
  836. public function editOther($id, $type)
  837. {
  838. $user = $this->getUserInfo($id);
  839. if (!$user) {
  840. throw new AdminException(100026);
  841. }
  842. $f = array();
  843. if ($type == 'money') {
  844. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  845. $f[] = Form::number('money', '余额', 0)->min(0)->max(999999.99);
  846. } else {
  847. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  848. $f[] = Form::number('integration', '积分', 0)->min(0)->precision(0)->max(999999);
  849. }
  850. return create_form('修改其他', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  851. }
  852. /**
  853. * 设置会员分组
  854. * @param $id
  855. * @return mixed
  856. */
  857. public function setGroup($uids)
  858. {
  859. /** @var UserGroupServices $groupServices */
  860. $groupServices = app()->make(UserGroupServices::class);
  861. $userGroup = $groupServices->getGroupList();
  862. if (count($uids) == 1) {
  863. $user = $this->getUserInfo($uids[0], ['group_id']);
  864. $setOptionUserGroup = function () use ($userGroup) {
  865. $menus = [];
  866. foreach ($userGroup as $menu) {
  867. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  868. }
  869. return $menus;
  870. };
  871. $field[] = Form::select('group_id', '用户分组', $user->getData('group_id') != 0 ? $user->getData('group_id') : '')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  872. } else {
  873. $setOptionUserGroup = function () use ($userGroup) {
  874. $menus = [];
  875. foreach ($userGroup as $menu) {
  876. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  877. }
  878. return $menus;
  879. };
  880. $field[] = Form::select('group_id', '用户分组')->setOptions(FormBuilder::setOptions($setOptionUserGroup))->filterable(true);
  881. }
  882. $field[] = Form::hidden('uids', implode(',', $uids));
  883. return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  884. }
  885. /**
  886. * 保存会员分组
  887. * @param $id
  888. * @return mixed
  889. */
  890. public function saveSetGroup($uids, int $group_id)
  891. {
  892. /** @var UserGroupServices $userGroup */
  893. $userGroup = app()->make(UserGroupServices::class);
  894. if (!$userGroup->getGroup($group_id)) {
  895. throw new AdminException(400696);
  896. }
  897. if (!$this->setUserGroup($uids, $group_id)) {
  898. throw new AdminException(400697);
  899. }
  900. return true;
  901. }
  902. /**
  903. * 设置用户标签
  904. * @param $uids
  905. * @return mixed
  906. */
  907. public function setLabel($uids)
  908. {
  909. /** @var UserLabelServices $labelServices */
  910. $labelServices = app()->make(UserLabelServices::class);
  911. $userLabel = $labelServices->getLabelList();
  912. if (count($uids) == 1) {
  913. $lids = app()->make(UserLabelRelationServices::class)->getUserLabels($uids[0]);
  914. $setOptionUserLabel = function () use ($userLabel) {
  915. $menus = [];
  916. foreach ($userLabel as $menu) {
  917. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  918. }
  919. return $menus;
  920. };
  921. $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  922. } else {
  923. $setOptionUserLabel = function () use ($userLabel) {
  924. $menus = [];
  925. foreach ($userLabel as $menu) {
  926. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  927. }
  928. return $menus;
  929. };
  930. $field[] = Form::select('label_id', '用户标签')->setOptions(FormBuilder::setOptions($setOptionUserLabel))->filterable(true)->multiple(true);
  931. }
  932. $field[] = Form::hidden('uids', implode(',', $uids));
  933. return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  934. }
  935. /**
  936. * 保存用户标签
  937. * @return mixed
  938. */
  939. public function saveSetLabel($uids, $lable_id)
  940. {
  941. foreach ($lable_id as $id) {
  942. if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
  943. throw new AdminException(400698);
  944. }
  945. }
  946. /** @var UserLabelRelationServices $services */
  947. $services = app()->make(UserLabelRelationServices::class);
  948. if (!$services->setUserLable($uids, $lable_id)) {
  949. throw new AdminException(400668);
  950. }
  951. return true;
  952. }
  953. /**
  954. * 赠送会员等级
  955. * @param int $uid
  956. * @return mixed
  957. * */
  958. public function giveLevel($id)
  959. {
  960. if (!$this->getUserInfo($id)) {
  961. throw new AdminException(400214);
  962. }
  963. //查询高于当前会员的所有会员等级
  964. $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
  965. $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');
  966. $setOptionlevel = function () use ($systemLevelList) {
  967. $menus = [];
  968. foreach ($systemLevelList as $menu) {
  969. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  970. }
  971. return $menus;
  972. };
  973. $field[] = Form::select('level_id', '用户等级')->setOptions(FormBuilder::setOptions($setOptionlevel))->filterable(true);
  974. return create_form('赠送等级', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  975. }
  976. /**
  977. * 执行赠送会员等级
  978. * @param int $id
  979. * @param int $level_id
  980. * @return mixed
  981. * @throws \think\db\exception\DataNotFoundException
  982. * @throws \think\db\exception\DbException
  983. * @throws \think\db\exception\ModelNotFoundException
  984. */
  985. public function saveGiveLevel(int $id, int $level_id)
  986. {
  987. if (!$this->getUserInfo($id)) {
  988. throw new AdminException(400214);
  989. }
  990. /** @var SystemUserLevelServices $systemLevelServices */
  991. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  992. /** @var UserLevelServices $userLevelServices */
  993. $userLevelServices = app()->make(UserLevelServices::class);
  994. //查询当前选择的会员等级
  995. $systemLevel = $systemLevelServices->getLevel($level_id);
  996. if (!$systemLevel) throw new AdminException(400699);
  997. //检查是否拥有此会员等级
  998. $level = $userLevelServices->getWhereLevel(['uid' => $id, 'level_id' => $level_id], 'valid_time,is_forever');
  999. if ($level && $level['status'] == 1 && $level['is_del'] == 0) {
  1000. throw new AdminException(400700);
  1001. }
  1002. //保存会员信息
  1003. if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
  1004. throw new AdminException(400219);
  1005. }
  1006. return true;
  1007. }
  1008. /**
  1009. * 赠送付费会员时长
  1010. * @param $id
  1011. * @return array
  1012. * @throws \FormBuilder\Exception\FormBuilderException
  1013. * @author 吴汐
  1014. * @email 442384644@qq.com
  1015. * @date 2023/03/01
  1016. */
  1017. public function giveLevelTime($id)
  1018. {
  1019. $userInfo = $this->getUserInfo($id);
  1020. if (!$userInfo) {
  1021. throw new AdminException(400214);
  1022. }
  1023. if ($userInfo['is_ever_level'] == 1) {
  1024. $timeDiff = '永久';
  1025. } else {
  1026. $timeDiff = $userInfo['overdue_time'] < time() ? '无' : date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1027. }
  1028. $dayDiff = $userInfo['overdue_time'] > time() ? intval(($userInfo['overdue_time'] - time()) / 86400) : 0;
  1029. $field[] = Form::input('time_diff', '到期时间', $timeDiff)->readonly(true);
  1030. if ($userInfo['is_ever_level'] == 0) {
  1031. $field[] = Form::input('day_diff', '剩余天数', $dayDiff)->readonly(true);
  1032. $field[] = Form::number('days', '增加时长(天)')->precision(0)->required();
  1033. }
  1034. return create_form('赠送付费会员时长', $field, Url::buildUrl('/user/save_give_level_time/' . $id), 'PUT');
  1035. }
  1036. /**
  1037. * 执行赠送付费会员时长
  1038. * @param int $id
  1039. * @param int $days
  1040. * @return mixed
  1041. * @throws \think\db\exception\DataNotFoundException
  1042. * @throws \think\db\exception\DbException
  1043. * @throws \think\db\exception\ModelNotFoundException
  1044. */
  1045. public function saveGiveLevelTime(int $id, int $days)
  1046. {
  1047. $userInfo = $this->getUserInfo($id);
  1048. if ($userInfo->is_ever_level == 1) {
  1049. return true;
  1050. }
  1051. if (!$userInfo) {
  1052. throw new AdminException(400214);
  1053. }
  1054. if ($days == 0) throw new AdminException(400701);
  1055. if ($days < -1) throw new AdminException(400702);
  1056. if ($userInfo->is_money_level == 0) {
  1057. $userInfo->is_money_level = 3;
  1058. if ($days == -1) {
  1059. $userInfo->is_ever_level = 1;
  1060. $time = 0;
  1061. } else {
  1062. $userInfo->overdue_time = $time = time() + ($days * 86400);
  1063. }
  1064. } else {
  1065. if ($days == -1) {
  1066. $userInfo->is_ever_level = 1;
  1067. $time = 0;
  1068. } else {
  1069. $userInfo->overdue_time = $time = $userInfo->overdue_time + ($days * 86400);
  1070. }
  1071. }
  1072. $userInfo->save();
  1073. /** @var StoreOrderCreateServices $storeOrderCreateService */
  1074. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  1075. $orderInfo = [
  1076. 'uid' => $id,
  1077. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  1078. 'type' => 3,
  1079. 'member_type' => 0,
  1080. 'pay_type' => 'admin',
  1081. 'paid' => 1,
  1082. 'pay_time' => time(),
  1083. 'is_free' => 1,
  1084. 'overdue_time' => $time,
  1085. 'vip_day' => $days,
  1086. 'add_time' => time()
  1087. ];
  1088. /** @var OtherOrderServices $otherOrder */
  1089. $otherOrder = app()->make(OtherOrderServices::class);
  1090. $otherOrder->save($orderInfo);
  1091. return true;
  1092. }
  1093. /**
  1094. * 清除会员等级
  1095. * @paran int $uid
  1096. * @paran boolean
  1097. * */
  1098. public function cleanUpLevel($uid)
  1099. {
  1100. if (!$this->getUserInfo($uid))
  1101. throw new AdminException(400214);
  1102. /** @var UserLevelServices $services */
  1103. $services = app()->make(UserLevelServices::class);
  1104. return $this->transaction(function () use ($uid, $services) {
  1105. $res = $services->delUserLevel($uid);
  1106. $res1 = $this->dao->update($uid, ['clean_time' => time(), 'level' => 0, 'exp' => 0], 'uid');
  1107. if (!$res && !$res1)
  1108. throw new AdminException(400186);
  1109. return true;
  1110. });
  1111. }
  1112. /**
  1113. * 用户详细信息
  1114. * @param int $uid
  1115. * @param array $userIfno
  1116. * @return array
  1117. * @throws \think\db\exception\DataNotFoundException
  1118. * @throws \think\db\exception\DbException
  1119. * @throws \think\db\exception\ModelNotFoundException
  1120. */
  1121. public function getUserDetailed(int $uid, $userIfno = [])
  1122. {
  1123. /** @var UserAddressServices $userAddress */
  1124. $userAddress = app()->make(UserAddressServices::class);
  1125. $field = 'real_name,phone,province,city,district,detail,post_code';
  1126. $address = $userAddress->getUserDefaultAddress($uid, $field);
  1127. if (!$address) {
  1128. $address = $userAddress->getUserAddressList($uid, $field);
  1129. $address = $address[0] ?? [];
  1130. }
  1131. $userInfo = $this->getUserInfo($uid);
  1132. return [
  1133. ['name' => '默认收货地址', 'value' => $address ? '收货人:' . $address['real_name'] . '邮编:' . $address['post_code'] . ' 收货人电话:' . $address['phone'] . ' 地址:' . $address['province'] . ' ' . $address['city'] . ' ' . $address['district'] . ' ' . $address['detail'] : ''],
  1134. ['name' => '手机号码', 'value' => $userInfo['phone']],
  1135. ['name' => '姓名', 'value' => ''],
  1136. ['name' => '微信昵称', 'value' => $userInfo['nickname']],
  1137. ['name' => '头像', 'value' => $userInfo['avatar']],
  1138. ['name' => '邮箱', 'value' => ''],
  1139. ['name' => '生日', 'value' => ''],
  1140. ['name' => '积分', 'value' => $userInfo['integral']],
  1141. ['name' => '上级推广人', 'value' => $userInfo['spread_uid'] ? $this->getUserInfo($userInfo['spread_uid'], ['nickname'])['nickname'] ?? '' : ''],
  1142. ['name' => '账户余额', 'value' => $userInfo['now_money']],
  1143. ['name' => '佣金总收入', 'value' => app()->make(UserBillServices::class)->getBrokerageSum($uid)],
  1144. ['name' => '提现总金额', 'value' => app()->make(UserExtractServices::class)->getUserExtract($uid)],
  1145. ];
  1146. }
  1147. /**
  1148. * 获取用户详情里面的用户消费能力和用户余额积分等
  1149. * @param $uid
  1150. * @return array[]
  1151. */
  1152. public function getHeaderList(int $uid, $userInfo = [])
  1153. {
  1154. if (!$userInfo) {
  1155. $userInfo = $this->getUserInfo($uid);
  1156. }
  1157. /** @var StoreOrderServices $orderServices */
  1158. $orderServices = app()->make(StoreOrderServices::class);
  1159. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  1160. return [
  1161. [
  1162. 'title' => '余额',
  1163. 'value' => $userInfo['now_money'] ?? 0,
  1164. 'key' => '元',
  1165. ],
  1166. [
  1167. 'title' => '总计订单',
  1168. 'value' => $orderServices->count($where),
  1169. 'key' => '笔',
  1170. ],
  1171. [
  1172. 'title' => '总消费金额',
  1173. 'value' => $orderServices->together($where, 'pay_price'),
  1174. 'key' => '元',
  1175. ],
  1176. [
  1177. 'title' => '积分',
  1178. 'value' => $userInfo['integral'] ?? 0,
  1179. 'key' => '',
  1180. ],
  1181. [
  1182. 'title' => '本月订单',
  1183. 'value' => $orderServices->count($where + ['time' => 'month']),
  1184. 'key' => '笔',
  1185. ],
  1186. [
  1187. 'title' => '本月消费金额',
  1188. 'value' => $orderServices->together($where + ['time' => 'month'], 'pay_price'),
  1189. 'key' => '元',
  1190. ]
  1191. ];
  1192. }
  1193. /**
  1194. * 获取用户记录里的积分总数和签到总数和余额变动总数
  1195. * @param $uid
  1196. * @return array
  1197. */
  1198. public function getUserBillCountData($uid)
  1199. {
  1200. /** @var UserBillServices $userBill */
  1201. $userBill = app()->make(UserBillServices::class);
  1202. $integral_count = $userBill->getIntegralCount($uid);
  1203. $sign_count = $userBill->getSignCount($uid);
  1204. $balanceChang_count = $userBill->getBrokerageCount($uid);
  1205. return [$integral_count, $sign_count, $balanceChang_count];
  1206. }
  1207. /**
  1208. * 用户详情
  1209. * @param int $uid
  1210. * @return array
  1211. * @throws \think\db\exception\DataNotFoundException
  1212. * @throws \think\db\exception\DbException
  1213. * @throws \think\db\exception\ModelNotFoundException
  1214. */
  1215. public function read(int $uid)
  1216. {
  1217. $userInfo = $this->getUserInfo($uid);
  1218. if (!$userInfo) {
  1219. throw new AdminException(100026);
  1220. }
  1221. $userInfo['avatar'] = strpos($userInfo['avatar'], 'http') === false ? (sys_config('site_url') . $userInfo['avatar']) : $userInfo['avatar'];
  1222. $userInfo['overdue_time'] = date('Y-m-d H:i:s', $userInfo['overdue_time']);
  1223. $userInfo['birthday'] = $userInfo['birthday'] < 0 ? 0 : $userInfo['birthday'];
  1224. if ($userInfo['addres'] == '') {
  1225. $defaultAddressInfo = app()->make(UserAddressServices::class)->getUserDefaultAddress($uid);
  1226. if ($defaultAddressInfo) {
  1227. $userInfo['addres'] = $defaultAddressInfo['province'] . $defaultAddressInfo['city'] . $defaultAddressInfo['district'] . $defaultAddressInfo['detail'];
  1228. } else {
  1229. $userInfo['addres'] = '';
  1230. }
  1231. }
  1232. $userInfo['vip_name'] = app()->make(SystemUserLevelServices::class)->value(['id' => $userInfo['level']], 'name');
  1233. $userInfo['group_name'] = app()->make(UserGroupServices::class)->value(['id' => $userInfo['group_id']], 'group_name');
  1234. $userInfo['spread_uid_nickname'] = $this->dao->value(['uid' => $userInfo['spread_uid']], 'nickname') . '/' . $userInfo['spread_uid'];
  1235. $userInfo['label_list'] = implode(',', array_column(app()->make(UserLabelRelationServices::class)->getUserLabelList([$uid]), 'label_name'));
  1236. return [
  1237. 'uid' => $uid,
  1238. 'userinfo' => $this->getUserDetailed($uid, $userInfo),
  1239. 'headerList' => $this->getHeaderList($uid, $userInfo),
  1240. 'count' => $this->getUserBillCountData($uid),
  1241. 'ps_info' => $userInfo
  1242. ];
  1243. }
  1244. /**
  1245. * 获取好友
  1246. * @param int $id
  1247. * @param string $field
  1248. * @return array
  1249. * @throws \think\db\exception\DataNotFoundException
  1250. * @throws \think\db\exception\DbException
  1251. * @throws \think\db\exception\ModelNotFoundException
  1252. */
  1253. public function getFriendList(int $id, string $field = 'uid,nickname,level,add_time,spread_time')
  1254. {
  1255. [$page, $limit] = $this->getPageValue();
  1256. $list = $this->dao->getList(['spread_uid' => $id], $field, $page, $limit);
  1257. /** @var SystemUserLevelServices $systemLevelServices */
  1258. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  1259. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  1260. if ($systemLevelServices) $systemLevelServices = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  1261. foreach ($list as &$item) {
  1262. $item['type'] = $systemLevelServices[$item['level']]['name'] ?? '暂无';
  1263. $item['add_time'] = $item['spread_time'] && is_numeric($item['spread_time']) ? date('Y-m-d H:i:s', $item['spread_time']) : '';
  1264. }
  1265. $count = $this->dao->count(['spread_uid' => $id]);
  1266. return compact('list', 'count');
  1267. }
  1268. /**
  1269. * 获取单个用户信息
  1270. * @param $id 用户id
  1271. * @return mixed
  1272. */
  1273. public function oneUserInfo(int $id, string $type)
  1274. {
  1275. switch ($type) {
  1276. case 'spread':
  1277. // /** @var UserFriendsServices $services */
  1278. // $services = app()->make(UserFriendsServices::class);
  1279. // return $services->getFriendList(['uid' => $id], ['level', 'nickname']);
  1280. return $this->getFriendList($id);
  1281. case 'order':
  1282. /** @var StoreOrderServices $services */
  1283. $services = app()->make(StoreOrderServices::class);
  1284. return $services->getUserOrderList($id);
  1285. case 'integral':
  1286. /** @var UserBillServices $services */
  1287. $services = app()->make(UserBillServices::class);
  1288. return $services->getIntegralList($id, [], 'title,number,balance,mark,add_time,frozen_time,pm');
  1289. case 'sign':
  1290. /** @var UserBillServices $services */
  1291. $services = app()->make(UserBillServices::class);
  1292. return $services->getSignList($id, [], 'title,number,mark,add_time');
  1293. case 'coupon':
  1294. /** @var StoreCouponUserServices $services */
  1295. $services = app()->make(StoreCouponUserServices::class);
  1296. return $services->getUserCouponList($id);
  1297. case 'balance_change':
  1298. /** @var UserMoneyServices $services */
  1299. $services = app()->make(UserMoneyServices::class);
  1300. return $services->balanceList(['uid' => $id]);
  1301. default:
  1302. throw new AdminException(100100);
  1303. }
  1304. }
  1305. /**获取特定时间用户访问量
  1306. * @param $time
  1307. * @param $week
  1308. * @return int
  1309. */
  1310. public function todayLastVisits($time, $week)
  1311. {
  1312. return $this->dao->todayLastVisit($time, $week);
  1313. }
  1314. /**获取特定时间新增用户
  1315. * @param $time
  1316. * @param $week
  1317. * @return int
  1318. */
  1319. public function todayAddVisits($time, $week)
  1320. {
  1321. return $this->dao->todayAddVisit($time, $week);
  1322. }
  1323. /**
  1324. * 用户图表
  1325. */
  1326. public function userChart()
  1327. {
  1328. $starday = date('Y-m-d', strtotime('-30 day'));
  1329. $yesterday = date('Y-m-d', strtotime('+1 day'));
  1330. $user_list = $this->dao->userList($starday, $yesterday);
  1331. $chartdata = [];
  1332. $data = [];
  1333. $chartdata['legend'] = ['用户数'];//分类
  1334. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1335. $chartdata['xAxis'] = [date('m-d')];//X轴值
  1336. $chartdata['series'] = [0];//分类1值
  1337. if (!empty($user_list)) {
  1338. foreach ($user_list as $k => $v) {
  1339. $data['day'][] = $v['day'];
  1340. $data['count'][] = $v['count'];
  1341. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  1342. $chartdata['yAxis']['maxnum'] = $v['count'];
  1343. }
  1344. $chartdata['xAxis'] = $data['day'];//X轴值
  1345. $chartdata['series'] = $data['count'];//分类1值
  1346. }
  1347. $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
  1348. $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  1349. $pay[0] = $this->dao->count(['pay_count' => 0]);
  1350. $pay[1] = $this->dao->count(['pay_count' => 1]);
  1351. $pay[2] = $this->dao->userCount(1);
  1352. $pay[3] = $this->dao->userCount(2);
  1353. foreach ($pay as $key => $item) {
  1354. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  1355. }
  1356. $chartdata['bing_data'] = $bing_data;
  1357. return $chartdata;
  1358. }
  1359. /***********************************************/
  1360. /************ 前端api services *****************/
  1361. /***********************************************/
  1362. /**
  1363. * 用户信息
  1364. * @param $info
  1365. * @return mixed
  1366. */
  1367. public function userInfo($info)
  1368. {
  1369. /** @var UserBillServices $userBill */
  1370. $userBill = app()->make(UserBillServices::class);
  1371. $uid = (int)$info['uid'];
  1372. $broken_time = intval(sys_config('extract_time'));
  1373. $search_time = time() - 86400 * $broken_time;
  1374. //改造时间
  1375. $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
  1376. //可提现佣金
  1377. //返佣 +
  1378. $brokerage_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
  1379. //退款退的佣金 -
  1380. $refund_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
  1381. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  1382. if ($info['broken_commission'] < 0)
  1383. $info['broken_commission'] = 0;
  1384. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  1385. if ($info['commissionCount'] < 0)
  1386. $info['commissionCount'] = 0;
  1387. return $info;
  1388. }
  1389. /**
  1390. * 个人中心
  1391. * @param array $user
  1392. */
  1393. public function personalHome(array $user, $tokenData)
  1394. {
  1395. $userInfo = $user;
  1396. $uid = (int)$user['uid'];
  1397. /** @var StoreCouponUserServices $storeCoupon */
  1398. $storeCoupon = app()->make(StoreCouponUserServices::class);
  1399. /** @var UserBillServices $userBill */
  1400. $userBill = app()->make(UserBillServices::class);
  1401. /** @var UserExtractServices $userExtract */
  1402. $userExtract = app()->make(UserExtractServices::class);
  1403. /** @var StoreOrderServices $storeOrder */
  1404. $storeOrder = app()->make(StoreOrderServices::class);
  1405. /** @var UserLevelServices $userLevel */
  1406. $userLevel = app()->make(UserLevelServices::class);
  1407. /** @var StoreServiceServices $storeService */
  1408. $storeService = app()->make(StoreServiceServices::class);
  1409. /** @var WechatUserServices $wechatUser */
  1410. $wechatUser = app()->make(WechatUserServices::class);
  1411. /** @var UserInvoiceServices $userInvoice */
  1412. $userInvoice = app()->make(UserInvoiceServices::class);
  1413. /** @var MemberCardServices $memberCardService */
  1414. $memberCardService = app()->make(MemberCardServices::class);
  1415. /** @var StoreProductRelationServices $collect */
  1416. $collect = app()->make(StoreProductRelationServices::class);
  1417. /** @var MessageSystemServices $messageSystemServices */
  1418. $messageSystemServices = app()->make(MessageSystemServices::class);
  1419. /** @var DiyServices $diyServices */
  1420. $diyServices = app()->make(DiyServices::class);
  1421. /** @var AgentLevelServices $agentLevelServices */
  1422. $agentLevelServices = app()->make(AgentLevelServices::class);
  1423. //看付费会员是否开启
  1424. $isOpenMember = $memberCardService->isOpenMemberCard();
  1425. $user['is_open_member'] = $isOpenMember;
  1426. $user['agent_level_name'] = '';
  1427. if ($user['agent_level']) {
  1428. $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name,status,grade');
  1429. if (!$levelInfo['status']) {
  1430. $levelInfo = $agentLevelServices->get([
  1431. ['grade', '<', $levelInfo['grade']],
  1432. ['is_del', '=', 0],
  1433. ['status', '=', 1]
  1434. ], ['id', 'name', 'status', 'grade']);
  1435. }
  1436. $user['agent_level_name'] = $levelInfo && $levelInfo['name'] && $levelInfo['status'] ? $levelInfo['name'] : '';
  1437. }
  1438. //会员领取优惠券
  1439. // $couponService->sendMemberCoupon($uid);
  1440. //看是否会员过期
  1441. $this->offMemberLevel($uid, $userInfo);
  1442. $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
  1443. $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
  1444. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  1445. $user['like'] = app()->make(StoreProductRelationServices::class)->getUserCollectCount($user['uid']);
  1446. $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
  1447. $user['notice'] = 0;
  1448. /** @var UserMoneyServices $userMoney */
  1449. $userMoney = app()->make(UserMoneyServices::class);
  1450. $user['recharge'] = $userMoney->sum([
  1451. ['uid', '=', $uid], ['pm', '=', 1], ['type', 'in', ['recharge', 'system_add', 'extract', 'register_system_add', 'lottery_add']]
  1452. ], 'number');
  1453. $user['orderStatusSum'] = bcsub((string)$user['recharge'], (string)$user['now_money'], 2);
  1454. $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
  1455. $user['extractPrice'] = $user['brokerage_price'];//可提现
  1456. $user['statu'] = (int)sys_config('store_brokerage_statu');
  1457. if (!$user['is_promoter']) {
  1458. $price = $storeOrder->sum(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']], 'pay_price');
  1459. if (is_brokerage_statu($price)) {
  1460. $this->dao->update($uid, ['is_promoter' => 1], 'uid');
  1461. $user['is_promoter'] = 1;
  1462. }
  1463. }
  1464. /** @var UserBrokerageServices $frozenPrices */
  1465. $frozenPrices = app()->make(UserBrokerageServices::class);
  1466. $user['broken_commission'] = $frozenPrices->getUserFrozenPrice($uid);
  1467. if ($user['broken_commission'] < 0)
  1468. $user['broken_commission'] = 0;
  1469. $user['commissionCount'] = bcsub((string)$user['brokerage_price'], (string)$user['broken_commission'], 2);
  1470. if ($user['commissionCount'] < 0)
  1471. $user['commissionCount'] = 0;
  1472. if (!sys_config('member_func_status'))
  1473. $user['vip'] = false;
  1474. else {
  1475. $userLevel = $userLevel->getUerLevelInfoByUid($user['uid']);
  1476. $user['vip'] = (bool)$userLevel;
  1477. if ($user['vip']) {
  1478. $user['vip_id'] = $userLevel['id'] ?? 0;
  1479. $user['vip_icon'] = set_file_url($userLevel['icon']) ?? '';
  1480. $user['vip_name'] = $userLevel['name'] ?? '';
  1481. }
  1482. }
  1483. $user['yesterDay'] = $frozenPrices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
  1484. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  1485. $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
  1486. if ($user['phone'] && $user['user_type'] != 'h5') {
  1487. $user['switchUserInfo'][] = $userInfo;
  1488. $h5UserInfo = $this->dao->getOne(['account' => $user['phone'], 'user_type' => 'h5']);
  1489. if ($h5UserInfo) {
  1490. $user['switchUserInfo'][] = $h5UserInfo;
  1491. }
  1492. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  1493. $wechatUserInfo = $this->getOne([['phone', '=', $user['phone']], ['user_type', '<>', 'h5']]);
  1494. if ($wechatUserInfo) {
  1495. $user['switchUserInfo'][] = $wechatUserInfo;
  1496. }
  1497. $user['switchUserInfo'][] = $userInfo;
  1498. } else if (!$user['phone']) {
  1499. $user['switchUserInfo'][] = $userInfo;
  1500. }
  1501. $user['broken_day'] = (int)sys_config('extract_time');//佣金冻结时间
  1502. $user['balance_func_status'] = (int)sys_config('balance_func_status', 0);
  1503. $invoice_func = $userInvoice->invoiceFuncStatus();
  1504. $user['invioce_func'] = $invoice_func['invoice_func'];
  1505. $user['special_invoice'] = $invoice_func['special_invoice'];
  1506. $user['collectCount'] = $collect->count(['uid' => $uid]);
  1507. $user['spread_status'] = $this->checkUserPromoter($user['uid']);
  1508. $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
  1509. $user['member_style'] = (int)$diyServices->getColorChange('member');
  1510. if ($user['is_ever_level']) {
  1511. $user['vip_status'] = 1;//永久会员
  1512. } else {
  1513. if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
  1514. $user['vip_status'] = -1;//开通过已过期
  1515. } else if (!$user['overdue_time'] && !$user['is_money_level']) {
  1516. $user['vip_status'] = 2;//没有开通过
  1517. } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
  1518. $user['vip_status'] = 3;//开通了,没有到期
  1519. }
  1520. }
  1521. $user['svip_open'] = (bool)sys_config('member_card_status');
  1522. /** @var StoreServiceRecordServices $servicesRecord */
  1523. $servicesRecord = app()->make(StoreServiceRecordServices::class);
  1524. $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
  1525. $message = $messageSystemServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  1526. $user['service_num'] = $service_num + $message;
  1527. /** @var AgentLevelServices $userSpread */
  1528. $agentLevel = app()->make(AgentLevelServices::class);
  1529. $user['spread_level_count'] = $agentLevel->count(['status' => 1, 'is_del' => 0]);
  1530. $user['extract_type'] = sys_config('extract_type');
  1531. $user['integral'] = intval($user['integral']);
  1532. $user['is_agent_level'] = $agentLevelServices->count(['status' => 1, 'is_del' => 0]) > 0 ? 1 : 0;
  1533. $user['division_open'] = (int)sys_config('division_status', 0);
  1534. $user['agent_apply_open'] = (int)sys_config('agent_apply_open', 0);
  1535. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  1536. $user['avatar'] = strpos($user['avatar'], '/statics/system_images/') !== false ? set_file_url($user['avatar']) : $user['avatar'];
  1537. $user['member_func_status'] = (int)sys_config('member_func_status');
  1538. $user['visit_num'] = app()->make(StoreProductLogServices::class)->getCountByUser($uid);
  1539. $user['level_info'] = app()->make(SystemUserLevelServices::class)->getlevel($user['level']);
  1540. return $user;
  1541. }
  1542. /**
  1543. * 用户资金统计
  1544. * @param int $uid
  1545. */
  1546. public function balance(int $uid)
  1547. {
  1548. $userInfo = $this->getUserInfo($uid);
  1549. if (!$userInfo) {
  1550. throw new AdminException(400214);
  1551. }
  1552. /** @var UserBillServices $userBill */
  1553. $userBill = app()->make(UserBillServices::class);
  1554. /** @var StoreOrderServices $storeOrder */
  1555. $storeOrder = app()->make(StoreOrderServices::class);
  1556. $user['now_money'] = $userInfo['now_money'];//当前总资金
  1557. $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
  1558. $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
  1559. return $user;
  1560. }
  1561. /**
  1562. * 用户修改信息
  1563. * @param Request $request
  1564. * @return mixed
  1565. */
  1566. public function eidtNickname(int $uid, array $data)
  1567. {
  1568. $info = $this->dao->get(['uid' => $uid]);
  1569. if (!$info) {
  1570. throw new ApiException(400214);
  1571. }
  1572. if (!$this->dao->update($uid, $data, 'uid')) {
  1573. throw new ApiException(100007);
  1574. }
  1575. //自定义事件-用户修改信息
  1576. event('CustomEventListener', ['user_change_info', [
  1577. 'uid' => $uid,
  1578. 'nickname' => $info['nickname'],
  1579. 'phone' => $info['phone'],
  1580. 'avatar' => $info['avatar'],
  1581. 'add_time' => date('Y-m-d H:i:s', $info['add_time']),
  1582. 'user_type' => $info['user_type'],
  1583. ]]);
  1584. return true;
  1585. }
  1586. /**
  1587. * 获取推广人排行
  1588. * @param $data 查询条件
  1589. * @return array
  1590. * @throws \think\db\exception\DataNotFoundException
  1591. * @throws \think\db\exception\ModelNotFoundException
  1592. * @throws \think\exception\DbException
  1593. */
  1594. public function getRankList(array $data)
  1595. {
  1596. $startTime = strtotime('this week Monday');
  1597. $endTime = time();
  1598. switch ($data['type']) {
  1599. case 'week':
  1600. $startTime = strtotime('this week Monday');
  1601. break;
  1602. case 'month':
  1603. $startTime = strtotime('last month');
  1604. break;
  1605. }
  1606. [$page, $limit] = $this->getPageValue();
  1607. $field = 't0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar';
  1608. return $this->dao->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
  1609. }
  1610. /**
  1611. * 静默绑定推广人
  1612. * @param int $uid
  1613. * @param int $spreadUid
  1614. * @param $code
  1615. * @return bool
  1616. * @throws \think\db\exception\DataNotFoundException
  1617. * @throws \think\db\exception\DbException
  1618. * @throws \think\db\exception\ModelNotFoundException
  1619. */
  1620. public function spread(int $uid, int $spreadUid, $code, $agent_id)
  1621. {
  1622. $userInfo = $this->dao->getOne(['uid' => $uid]);
  1623. if (!$userInfo) {
  1624. throw new ApiException(100026);
  1625. }
  1626. if ($code && !$spreadUid) {
  1627. /** @var QrcodeServices $qrCode */
  1628. $qrCode = app()->make(QrcodeServices::class);
  1629. if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
  1630. if ($info['third_type'] == 'agent') {
  1631. $agent_id = $info['third_id'];
  1632. } else {
  1633. $spreadUid = $info['third_id'];
  1634. }
  1635. }
  1636. }
  1637. if ($agent_id) {
  1638. $spreadInfo = $this->dao->getOne(['uid' => $agent_id]);
  1639. if ($agent_id == $uid) {
  1640. return '自己不能推荐自己';
  1641. } else if (!$userInfo) {
  1642. return '用户不存在';
  1643. } else if (!$spreadInfo) {
  1644. return '上级用户不存在';
  1645. } else if ($userInfo->is_division) {
  1646. return '您是事业部,不能绑定成为别人的员工';
  1647. } else if ($userInfo->is_agent) {
  1648. return '您是代理商,不能绑定成为别人的员工';
  1649. } else if (app()->make(LoginServices::class)->updateUserInfo(['code' => $agent_id, 'is_staff' => 1], $userInfo, false)) {
  1650. return '绑定店员成功!';
  1651. }
  1652. }
  1653. if ($spreadUid == 0) return '不绑定';
  1654. $userSpreadUid = $this->dao->value(['uid' => $spreadUid], 'spread_uid');
  1655. //记录好友关系
  1656. if ($spreadUid && $uid && $spreadUid != $uid) {
  1657. /** @var UserFriendsServices $serviceFriend */
  1658. $serviceFriend = app()->make(UserFriendsServices::class);
  1659. $serviceFriend->saveFriend([
  1660. 'uid' => $uid,
  1661. 'friends_uid' => $spreadUid,
  1662. ]);
  1663. }
  1664. $check = false;
  1665. if (sys_config('brokerage_bindind') == 1) {
  1666. if (sys_config('store_brokerage_binding_status') == 1) {
  1667. if (!$userInfo['spread_uid']) {
  1668. $check = true;
  1669. }
  1670. } elseif (sys_config('store_brokerage_binding_status') == 2 && (($userInfo['spread_time'] + (sys_config('store_brokerage_binding_time') * 86400)) < time())) {
  1671. $check = true;
  1672. } elseif (sys_config('store_brokerage_binding_status') == 3) {
  1673. $check = true;
  1674. }
  1675. } elseif (sys_config('brokerage_bindind') == 2) {
  1676. if ($userInfo['add_time'] == $userInfo['last_time'] && $userInfo['spread_uid'] == 0) {
  1677. $check = true;
  1678. }
  1679. }
  1680. if ($userInfo['uid'] == $spreadUid || $userInfo['uid'] == $userSpreadUid) $check = false;
  1681. if ($check) {
  1682. $spreadInfo = $this->dao->get($spreadUid, ['division_id', 'agent_id', 'staff_id']);
  1683. $data = [];
  1684. $data['spread_uid'] = $spreadUid;
  1685. $data['spread_time'] = time();
  1686. $data['division_id'] = $spreadInfo['division_id'];
  1687. $data['agent_id'] = $spreadInfo['agent_id'];
  1688. $data['staff_id'] = $spreadInfo['staff_id'];
  1689. if (!$this->dao->update($uid, $data, 'uid')) {
  1690. throw new ApiException(410288);
  1691. }
  1692. return '绑定上级成功,上级uid为' . $spreadUid;
  1693. } else {
  1694. return '不绑定';
  1695. }
  1696. }
  1697. /**
  1698. * 添加访问记录
  1699. * @param Request $request
  1700. * @return mixed
  1701. */
  1702. public function setVisit(array $data)
  1703. {
  1704. $userInfo = $this->getUserInfo($data['uid'], 'uid,user_type');
  1705. if (!$userInfo) {
  1706. throw new ApiException(100026);
  1707. }
  1708. $data['channel_type'] = $userInfo['user_type'];
  1709. $data['add_time'] = time();
  1710. /** @var WechatUserServices $wechatUserServices */
  1711. $wechatUserServices = app()->make(WechatUserServices::class);
  1712. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid'], 'user_type' => $userInfo['user_type']]);
  1713. if (!$wechatUser) {
  1714. $wechatUser = $wechatUserServices->get(['uid' => $userInfo['uid']]);
  1715. }
  1716. if ($wechatUser) {
  1717. $data['province'] = $wechatUser['province'];
  1718. }
  1719. /** @var UserVisitServices $userVisit */
  1720. $userVisit = app()->make(UserVisitServices::class);
  1721. if ($userVisit->save($data)) {
  1722. return true;
  1723. } else {
  1724. throw new ApiException(100015);
  1725. }
  1726. }
  1727. /**
  1728. * 获取活动状态
  1729. * @return mixed
  1730. */
  1731. public function activity()
  1732. {
  1733. /** @var StoreBargainServices $storeBragain */
  1734. $storeBragain = app()->make(StoreBargainServices::class);
  1735. /** @var StoreCombinationServices $storeCombinaion */
  1736. $storeCombinaion = app()->make(StoreCombinationServices::class);
  1737. /** @var StoreSeckillServices $storeSeckill */
  1738. $storeSeckill = app()->make(StoreSeckillServices::class);
  1739. $data['is_bargin'] = (bool)$storeBragain->validBargain();
  1740. $data['is_pink'] = (bool)$storeCombinaion->validCombination();
  1741. $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
  1742. return $data;
  1743. }
  1744. /**
  1745. * 获取用户下级推广人
  1746. * @param int $uid 当前用户
  1747. * @param int $grade 等级 0 一级 1 二级
  1748. * @param string $orderBy 排序
  1749. * @param string $keyword
  1750. * @return array|bool
  1751. */
  1752. public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '')
  1753. {
  1754. $user = $this->getUserInfo($uid);
  1755. if (!$user) {
  1756. throw new AdminException(400214);
  1757. }
  1758. $spread_one_ids = $this->getUserSpredadUids($uid, 1);
  1759. $spread_two_ids = $this->getUserSpredadUids($uid, 2);
  1760. $data = [
  1761. 'total' => count($spread_one_ids),
  1762. 'totalLevel' => count($spread_two_ids),
  1763. 'list' => []
  1764. ];
  1765. if (sys_config('brokerage_level', 2) == 1) {
  1766. $data['count'] = $data['total'];
  1767. } else {
  1768. $data['count'] = $data['total'] + $data['totalLevel'];
  1769. }
  1770. /** @var UserStoreOrderServices $userStoreOrder */
  1771. $userStoreOrder = app()->make(UserStoreOrderServices::class);
  1772. $list = [];
  1773. if ($grade == 0) {
  1774. if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword);
  1775. } else {
  1776. if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword);
  1777. }
  1778. foreach ($list as &$item) {
  1779. if (isset($item['spread_time']) && $item['spread_time']) {
  1780. $item['time'] = date('Y/m/d', $item['spread_time']);
  1781. }
  1782. }
  1783. $data['list'] = $list;
  1784. $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
  1785. return $data;
  1786. }
  1787. /**
  1788. * 获取推广人uids
  1789. * @param int $uid
  1790. * @param bool $one
  1791. * @return array
  1792. */
  1793. public function getUserSpredadUids(int $uid, int $type = 0)
  1794. {
  1795. $uids = $this->dao->getColumn(['spread_uid' => $uid, 'is_del' => 0], 'uid');
  1796. if ($type === 1) {
  1797. return $uids;
  1798. }
  1799. if ($uids) {
  1800. $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids], ['is_del', '=', 0]], 'uid');
  1801. if ($type === 2) {
  1802. return $uidsTwo;
  1803. }
  1804. if ($uidsTwo) {
  1805. $uids = array_merge($uids, $uidsTwo);
  1806. }
  1807. }
  1808. return $uids;
  1809. }
  1810. /**
  1811. * 检测用户是否是推广员
  1812. * @param int $uid
  1813. * @param $user
  1814. * @return bool
  1815. */
  1816. public function checkUserPromoter(int $uid, $user = [])
  1817. {
  1818. if (!$user) {
  1819. $user = $this->getUserInfo($uid, 'spread_open,is_promoter');
  1820. }
  1821. if (!$user) {
  1822. return false;
  1823. }
  1824. //分销是否开启
  1825. if (!sys_config('brokerage_func_status')) {
  1826. return false;
  1827. }
  1828. if (isset($user['spread_open']) && !$user['spread_open']) {
  1829. return false;
  1830. }
  1831. /** @var StoreOrderServices $storeOrder */
  1832. $storeOrder = app()->make(StoreOrderServices::class);
  1833. $sumPrice = $storeOrder->sum(['uid' => $uid, 'paid' => 1], 'pay_price');//累计消费
  1834. $store_brokerage_statu = sys_config('store_brokerage_statu');
  1835. $store_brokerage_price = sys_config('store_brokerage_price');
  1836. if ($user['is_promoter'] || $store_brokerage_statu == 2 || ($store_brokerage_statu == 3 && $sumPrice > $store_brokerage_price)) {
  1837. if (!$user['is_promoter']) {
  1838. $this->dao->update($uid, ['is_promoter' => 1]);
  1839. }
  1840. return true;
  1841. }
  1842. return false;
  1843. }
  1844. /**
  1845. * 同步微信粉丝用户(后台接口)
  1846. * @return bool
  1847. */
  1848. public function syncWechatUsers()
  1849. {
  1850. $appid = sys_config('wechat_appid');
  1851. $appSecret = sys_config('wechat_appsecret');
  1852. if (!$appid || !$appSecret) {
  1853. throw new AdminException(400236);
  1854. }
  1855. $key = md5('sync_wechat_users');
  1856. //一天点击一次
  1857. if (CacheService::get($key)) {
  1858. return true;
  1859. }
  1860. $next_openid = null;
  1861. do {
  1862. $result = WechatService::getUsersList($next_openid);
  1863. $userOpenids = $result['data'];
  1864. //拆分大数组
  1865. $opemidArr = array_chunk($userOpenids, 100);
  1866. foreach ($opemidArr as $openids) {
  1867. //加入同步|更新用户队列
  1868. UserJob::dispatch([$openids]);
  1869. }
  1870. $next_openid = $result['next_openid'];
  1871. } while ($next_openid != null);
  1872. CacheService::set($key, 1, 3600 * 24);
  1873. return true;
  1874. }
  1875. /**
  1876. * 导入微信粉丝用户
  1877. * @param array $openids
  1878. * @return bool
  1879. */
  1880. public function importUser(array $noBeOpenids)
  1881. {
  1882. if (!$noBeOpenids) {
  1883. return true;
  1884. }
  1885. $dataAll = $data = [];
  1886. $time = time();
  1887. foreach ($noBeOpenids as $openid) {
  1888. try {
  1889. $info = WechatService::getUserInfo($openid);
  1890. $info = is_object($info) ? $info->toArray() : $info;
  1891. } catch (\Throwable $e) {
  1892. $info = [];
  1893. }
  1894. if (!$info) continue;
  1895. $data['nickname'] = $info['nickname'] ?? '';
  1896. $data['headimgurl'] = $info['headimgurl'] ?? '';
  1897. $userInfoData = $this->setUserInfo($data);
  1898. if (!$userInfoData) {
  1899. throw new AdminException(400703);
  1900. }
  1901. $data['uid'] = $userInfoData['uid'];
  1902. $data['subscribe'] = $info['subscribe'] ?? 1;
  1903. $data['unionid'] = $info['unionid'] ?? '';
  1904. $data['openid'] = $info['openid'] ?? '';
  1905. $data['sex'] = $info['sex'] ?? 0;
  1906. $data['language'] = $info['language'] ?? '';
  1907. $data['city'] = $info['city'] ?? '';
  1908. $data['province'] = $info['province'] ?? '';
  1909. $data['country'] = $info['country'] ?? '';
  1910. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  1911. $data['groupid'] = $info['groupid'] ?? 0;
  1912. $data['remark'] = $info['remark'] ?? '';
  1913. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  1914. $data['add_time'] = $time;
  1915. $data['is_complete'] = 1;
  1916. $dataAll[] = $data;
  1917. }
  1918. if ($dataAll) {
  1919. /** @var WechatUserServices $wechatUser */
  1920. $wechatUser = app()->make(WechatUserServices::class);
  1921. if (!$wechatUser->saveAll($dataAll)) {
  1922. throw new AdminException(400703);
  1923. }
  1924. }
  1925. return true;
  1926. }
  1927. /** 修改会员的时间及是否会员状态
  1928. * @param int $vip_day 会员天数
  1929. * @param array $user_id 用户id
  1930. * @param int $is_money_level 会员来源途径
  1931. * @param bool $member_type 会员卡类型
  1932. * @return mixed
  1933. * @throws \think\db\exception\DataNotFoundException
  1934. * @throws \think\db\exception\DbException
  1935. * @throws \think\db\exception\ModelNotFoundException
  1936. */
  1937. public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
  1938. {
  1939. if ($vip_day == 0) throw new ApiException(410289);
  1940. $user_info = $this->getUserInfo($user_id, 'is_money_level,overdue_time');
  1941. if (!$user_info) throw new ApiException(410032);
  1942. if (!$member_type) $member_type = "month";
  1943. if ($member_type == 'ever') {
  1944. $overdue_time = 0;
  1945. $is_ever_level = 1;
  1946. } else {
  1947. if ($user_info['is_money_level'] == 0) {
  1948. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  1949. } else {
  1950. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
  1951. }
  1952. $is_ever_level = 0;
  1953. }
  1954. $setData['overdue_time'] = $overdue_time;
  1955. $setData['is_ever_level'] = $is_ever_level;
  1956. $setData['is_money_level'] = $is_money_level ?: 0;
  1957. // if ($user_info['level'] == 0) $setData['level'] = 1;
  1958. return $this->dao->update(['uid' => $user_id], $setData);
  1959. }
  1960. /**
  1961. * 会员过期改变状态,变为普通会员
  1962. * @param $uid
  1963. * @param $userInfo
  1964. * @return bool
  1965. * @throws \think\db\exception\DataNotFoundException
  1966. * @throws \think\db\exception\DbException
  1967. * @throws \think\db\exception\ModelNotFoundException
  1968. */
  1969. public function offMemberLevel($uid, $userInfo = [])
  1970. {
  1971. if (!$uid) return false;
  1972. if (!$userInfo) {
  1973. $userInfo = $this->dao->get($uid, ['is_ever_level', 'is_money_level', 'overdue_time']);
  1974. }
  1975. if (!$userInfo) return false;
  1976. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  1977. $this->dao->update(['uid' => $uid], ['is_money_level' => 0/*, 'overdue_time' => 0*/]);
  1978. return false;
  1979. }
  1980. return $userInfo;
  1981. }
  1982. /**
  1983. * @param array $where
  1984. * @return array
  1985. * @throws \think\db\exception\DataNotFoundException
  1986. * @throws \think\db\exception\DbException
  1987. * @throws \think\db\exception\ModelNotFoundException
  1988. */
  1989. public function getUserInfoList(array $where, $field = "*")
  1990. {
  1991. return $this->dao->getUserInfoList($where, $field);
  1992. }
  1993. /**
  1994. * 增加推广用户佣金
  1995. * @param int $uid
  1996. * @param int $spread_uid
  1997. * @param array $userInfo
  1998. * @param array $spread_user
  1999. * @return bool|mixed
  2000. * @throws \think\db\exception\DataNotFoundException
  2001. * @throws \think\db\exception\DbException
  2002. * @throws \think\db\exception\ModelNotFoundException
  2003. */
  2004. public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
  2005. {
  2006. if (!$uid || !$spread_uid) {
  2007. return false;
  2008. }
  2009. //商城分销功能是否开启 0关闭1开启
  2010. if (!sys_config('brokerage_func_status')) return true;
  2011. if (!sys_config('brokerage_user_status')) return true;
  2012. //获取设置推广佣金单价
  2013. $brokerage_price = sys_config('uni_brokerage_price', 0);
  2014. //获取推广佣金当日限额
  2015. $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
  2016. if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
  2017. return true;
  2018. }
  2019. if (!$userInfo) {
  2020. $userInfo = $this->getUserInfo($uid);
  2021. }
  2022. if (!$userInfo) {
  2023. return false;
  2024. }
  2025. //根据手机号码查询此用户注销过,不反推广佣金
  2026. if ($userInfo['phone'] != '' && $this->dao->getCount(['phone' => $userInfo['phone'], 'is_del' => 1])) {
  2027. return false;
  2028. }
  2029. //根据openid查询此用户注销过,不反推广佣金
  2030. $wechatUserServices = app()->make(WechatUserServices::class);
  2031. $openidArray = $wechatUserServices->getColumn(['uid' => $uid], 'openid', 'id');
  2032. if ($wechatUserServices->getCount([['openid', 'in', $openidArray], ['is_del', '=', 1]])) {
  2033. return false;
  2034. }
  2035. if (!$spread_user) {
  2036. $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
  2037. }
  2038. if (!$spread_user) {
  2039. return false;
  2040. }
  2041. if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
  2042. return false;
  2043. }
  2044. /** @var UserBrokerageServices $userBrokerageServices */
  2045. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  2046. // -1不限制
  2047. if ($day_brokerage_price_upper != -1) {
  2048. if ($day_brokerage_price_upper <= 0) {
  2049. return true;
  2050. } else {
  2051. //获取上级用户今日获取推广用户佣金
  2052. $spread_day_brokerage = $userBrokerageServices->getUserBrokerageSum($spread_uid, ['brokerage_user'], 'today');
  2053. //超过上限
  2054. if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
  2055. return true;
  2056. }
  2057. }
  2058. }
  2059. $spreadPrice = $spread_user['brokerage_price'];
  2060. // 上级推广员返佣之后的金额
  2061. $balance = bcadd($spreadPrice, $brokerage_price, 2);
  2062. return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
  2063. // 添加返佣记录
  2064. $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
  2065. 'nickname' => $userInfo['nickname'],
  2066. 'number' => floatval($brokerage_price)
  2067. ], $balance, $uid);
  2068. // 添加用户余额
  2069. $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
  2070. //给上级发送获得佣金的模板消息
  2071. /** @var StoreOrderTakeServices $storeOrderTakeServices */
  2072. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  2073. $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
  2074. return $res1 && $res2;
  2075. });
  2076. }
  2077. /**
  2078. * 获取上级uid
  2079. * @param int $uid
  2080. * @param array $userInfo
  2081. * @param bool $is_spread
  2082. * @return int|mixed
  2083. */
  2084. public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
  2085. {
  2086. if (!$uid) {
  2087. return 0;
  2088. }
  2089. //商城分销功能是否开启 0关闭1开启
  2090. if (!sys_config('brokerage_func_status')) return -1;
  2091. if (!$userInfo) {
  2092. $userInfo = $this->getUserInfo($uid);
  2093. }
  2094. if (!$userInfo) {
  2095. return 0;
  2096. }
  2097. //上级的上级不需要检测自购
  2098. if ($is_spread) {
  2099. //开启自购
  2100. $is_self_brokerage = sys_config('is_self_brokerage', 0);
  2101. if ($is_self_brokerage && $is_spread) {
  2102. return $uid;
  2103. }
  2104. }
  2105. //绑定类型
  2106. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  2107. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  2108. return $userInfo['spread_uid'];
  2109. }
  2110. //分销绑定类型为时间段且没过期
  2111. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  2112. if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
  2113. return $userInfo['spread_uid'];
  2114. }
  2115. return -1;
  2116. }
  2117. /**
  2118. * 获取事业部/代理/员工列表
  2119. * @param array $where
  2120. * @param string $field
  2121. * @return array
  2122. * @throws \think\db\exception\DataNotFoundException
  2123. * @throws \think\db\exception\DbException
  2124. * @throws \think\db\exception\ModelNotFoundException
  2125. */
  2126. public function getDivisionList(array $where = [], string $field = '*')
  2127. {
  2128. [$page, $limit] = $this->getPageValue();
  2129. $list = $this->dao->getList($where, $field, $page, $limit);
  2130. $count = $this->dao->count($where);
  2131. return compact('list', 'count');
  2132. }
  2133. /**
  2134. * 添加编辑用户信息时候的信息
  2135. * @param $uid
  2136. * @return array
  2137. * @throws \think\db\exception\DataNotFoundException
  2138. * @throws \think\db\exception\DbException
  2139. * @throws \think\db\exception\ModelNotFoundException
  2140. */
  2141. public function getUserSaveInfo($uid)
  2142. {
  2143. /** @var UserLabelServices $userLabelServices */
  2144. $userLabelServices = app()->make(UserLabelServices::class);
  2145. /** @var UserLabelRelationServices $userLabelRelationServices */
  2146. $userLabelRelationServices = app()->make(UserLabelRelationServices::class);
  2147. /** @var UserLabelCateServices $userLabelCateServices */
  2148. $userLabelCateServices = app()->make(UserLabelCateServices::class);
  2149. /** @var UserGroupServices $userGroupServices */
  2150. $userGroupServices = app()->make(UserGroupServices::class);
  2151. /** @var SystemUserLevelServices $systemUserLevelServices */
  2152. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  2153. $userInfo = $this->dao->get($uid);
  2154. if ($userInfo) {
  2155. $label_ids = $userLabelRelationServices->getUserLabels($uid);
  2156. $userInfo['label_id'] = !empty($label_ids) ? $userLabelServices->getLabelList(['ids' => $label_ids], ['id', 'label_name']) : [];
  2157. $userInfo['birthday'] = (int)$userInfo['birthday'] ? date('Y-m-d', (int)$userInfo['birthday']) : '';
  2158. $userInfo['level'] = $userInfo['level'] != 0 ? $userInfo['level'] : '';
  2159. $userInfo['group_id'] = $userInfo['group_id'] != 0 ? $userInfo['group_id'] : '';
  2160. }
  2161. $levelInfo = $systemUserLevelServices->getWhereLevelList([], 'id,name');
  2162. $groupInfo = $userGroupServices->getGroupList();
  2163. $labelInfo = $userLabelCateServices->getUserLabel($uid);
  2164. return compact('userInfo', 'levelInfo', 'groupInfo', 'labelInfo');
  2165. }
  2166. /**
  2167. * 新用户注册奖励
  2168. * @param int $id
  2169. * @return bool
  2170. * @throws Exception
  2171. *
  2172. * @date 2022/09/28
  2173. * @author yyw
  2174. */
  2175. public function rewardNewUser(int $id)
  2176. {
  2177. $user = $this->getUserInfo($id);
  2178. if (!$user) {
  2179. throw new AdminException(100026);
  2180. }
  2181. $res1 = false;
  2182. $res2 = false;
  2183. $reward_money = sys_config('reward_money');
  2184. $reward_integral = sys_config('reward_integral');
  2185. $edit = array();
  2186. if ($reward_money > 0) {//余额增加
  2187. /** @var UserMoneyServices $userMoneyServices */
  2188. $userMoneyServices = app()->make(UserMoneyServices::class);
  2189. $edit['now_money'] = bcadd($user['now_money'], $reward_money, 2);
  2190. $res1 = $userMoneyServices->income('register_system_add', $user['uid'], $reward_money, $edit['now_money'], 1);
  2191. } else {
  2192. $res1 = true;
  2193. }
  2194. if ($reward_integral > 0) {//积分增加
  2195. /** @var UserBillServices $userBill */
  2196. $userBill = app()->make(UserBillServices::class);
  2197. $integral_data = ['link_id' => 1, 'number' => $reward_integral];
  2198. $edit['integral'] = bcadd($user['integral'], $reward_integral, 2);
  2199. $integral_data['balance'] = $edit['integral'];
  2200. $integral_data['title'] = '新用户注册增加积分';
  2201. $integral_data['mark'] = '新用户注册增加了' . floatval($reward_integral) . '积分';
  2202. $res2 = $userBill->incomeIntegral($user['uid'], 'system_add', $integral_data);
  2203. } else {
  2204. $res2 = true;
  2205. }
  2206. if ($edit) {
  2207. $res3 = $this->dao->update($id, $edit);
  2208. } else {
  2209. $res3 = true;
  2210. }
  2211. if ($res1 && $res2 && $res3) {
  2212. return true;
  2213. } else {
  2214. throw new AdminException(100007);
  2215. }
  2216. }
  2217. /**
  2218. * 推送用户信息
  2219. * @param $data
  2220. * @param $pushUrl
  2221. * @return bool
  2222. */
  2223. public function userUpdate($data, $pushUrl)
  2224. {
  2225. return out_push($pushUrl, $data, '更新用户信息');
  2226. }
  2227. /**
  2228. * 获取礼包商品列表
  2229. * @param array $where
  2230. * @return array
  2231. * @author wuhaotian
  2232. * @email 442384644@qq.com
  2233. * @date 2024/12/17
  2234. */
  2235. public function getGiftProductList($where = [])
  2236. {
  2237. /** @var \app\services\product\product\StoreProductGiftGroupService $giftGroupService */
  2238. $giftGroupService = app()->make(\app\services\product\product\StoreProductGiftGroupService::class);
  2239. return $giftGroupService->getGiftProductList($where);
  2240. }
  2241. /**
  2242. * 获取推荐关系列表
  2243. * @param array $where
  2244. * @return array
  2245. * @author wuhaotian
  2246. * @email 442384644@qq.com
  2247. * @date 2024/12/17
  2248. */
  2249. public function getRecommendationRelationList($where = [])
  2250. {
  2251. /** @var \app\services\product\product\StoreProductGiftGroupService $giftGroupService */
  2252. $giftGroupService = app()->make(\app\services\product\product\StoreProductGiftGroupService::class);
  2253. return $giftGroupService->getRecommendationRelationList($where);
  2254. }
  2255. }