UserServices.php 92 KB

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