User.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\user;
  12. use app\admin\model\wechat\WechatQrcode;
  13. use basic\ModelBasic;
  14. use service\SystemConfigService;
  15. use think\Cookie;
  16. use think\Request;
  17. use think\response\Redirect;
  18. use think\Session;
  19. use think\Url;
  20. use traits\ModelTrait;
  21. use app\wap\model\user\WechatUser;
  22. use service\WechatTemplateService;
  23. use app\wap\model\routine\RoutineTemplate;
  24. use app\wap\model\special\Special;
  25. use app\wap\model\store\StoreProduct;
  26. use app\wap\model\user\MemberShip;
  27. /**用户表
  28. * Class User
  29. * @package app\wap\model\user
  30. */
  31. class User extends ModelBasic
  32. {
  33. use ModelTrait;
  34. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  35. protected function setAddTimeAttr($value)
  36. {
  37. return time();
  38. }
  39. protected function setAddIpAttr($value)
  40. {
  41. return Request::instance()->ip();
  42. }
  43. protected function setLastTimeAttr($value)
  44. {
  45. return time();
  46. }
  47. protected function setLastIpAttr($value)
  48. {
  49. return Request::instance()->ip();
  50. }
  51. public static function ResetSpread($openid)
  52. {
  53. $uid = WechatUser::openidToUid($openid);
  54. if (self::be(['uid' => $uid, 'is_promoter' => 0])) self::where('uid', $uid)->update(['spread_uid' => 0]);
  55. }
  56. /**
  57. * 绑定用户手机号码修改手机号码用户购买的专题和其他数据
  58. * @param $bindingPhone 绑定手机号码
  59. * @param $uid 当前用户id
  60. * @param $newUid 切换用户id
  61. * @param bool $isDel 是否删除
  62. * @param int $qcodeId 扫码id
  63. * @return bool
  64. * @throws \think\exception\PDOException
  65. */
  66. public static function setUserRelationInfos($bindingPhone, $uid, $newUid, $isDel = true, $qcodeId = 0)
  67. {
  68. self::startTrans();
  69. try {
  70. //修改下级推广人关系
  71. self::where('spread_uid', $uid)->update(['spread_uid' => $newUid]);
  72. //修改用户金额变动记录表
  73. self::getDb('user_bill')->where('uid', $uid)->update(['uid' => $newUid]);
  74. //修改签到记录表
  75. self::getDb('user_sign')->where('uid', $uid)->update(['uid' => $newUid]);
  76. //修改虚拟币充值记录表
  77. self::getDb('user_recharge')->where('uid', $uid)->update(['uid' => $newUid]);
  78. //修改收货地址表
  79. self::getDb('user_address')->where('uid', $uid)->update(['uid' => $newUid]);
  80. //修改提现记录用户
  81. self::getDb('user_extract')->where('uid', $uid)->update(['uid' => $newUid]);
  82. //修改专题购买记录表
  83. self::getDb('special_buy')->where('uid', $uid)->update(['uid' => $newUid]);
  84. //修改购物车记录表
  85. self::getDb('store_cart')->where('uid', $uid)->update(['uid' => $newUid]);
  86. //修改用户订单记录
  87. self::getDb('store_order')->where('uid', $uid)->update(['uid' => $newUid]);
  88. //修改拼团用户记录
  89. self::getDb('store_pink')->where('uid', $uid)->update(['uid' => $newUid]);
  90. //修改手机用户表记录
  91. self::getDb('phone_user')->where('uid', $uid)->update(['uid' => $newUid]);
  92. //修改会员记录表记录
  93. self::getDb('member_record')->where('uid', $uid)->update(['uid' => $newUid]);
  94. //修改搜索记录表记录
  95. self::getDb('search_history')->where('uid', $uid)->update(['uid' => $newUid]);
  96. //修改用户报名表记录
  97. self::getDb('event_sign_up')->where('uid', $uid)->update(['uid' => $newUid]);
  98. //修改资料订单表记录
  99. self::getDb('data_download_buy')->where('uid', $uid)->update(['uid' => $newUid]);
  100. //修改考试相关表记录
  101. self::getDb('examination_record')->where('uid', $uid)->update(['uid' => $newUid]);
  102. self::getDb('examination_test_record')->where('uid', $uid)->update(['uid' => $newUid]);
  103. self::getDb('examination_wrong_bank')->where('uid', $uid)->update(['uid' => $newUid]);
  104. self::getDb('test_paper_obtain')->where('uid', $uid)->update(['uid' => $newUid]);
  105. self::getDb('test_paper_order')->where('uid', $uid)->update(['uid' => $newUid]);
  106. //删除用户表H5用户记录
  107. $user = self::where('uid', $uid)->find();
  108. if ($isDel) self::where('uid', $uid)->delete();
  109. //修改上级推广关系和绑定手机号码
  110. self::where('uid', $newUid)->update(['phone' => $bindingPhone,
  111. 'spread_uid' => $user['spread_uid'],
  112. 'valid_time' => $user['valid_time'],
  113. 'now_money' => $user['now_money'],
  114. 'gold_num' => $user['gold_num'],
  115. 'brokerage_price' => $user['brokerage_price'],
  116. 'is_permanent' => $user['is_permanent'],
  117. 'member_time' => $user['member_time'],
  118. 'overdue_time' => $user['overdue_time'],
  119. 'level' => $user['level']
  120. ]);
  121. if ($qcodeId) WechatQrcode::where('id', $qcodeId)->update(['scan_id' => $newUid]);
  122. self::commit();
  123. Session::clear('wap');
  124. Session::set('loginUid', $newUid, 'wap');
  125. return true;
  126. } catch (\Exception $e) {
  127. self::rollback();
  128. return self::setErrorInfo($e->getMessage());
  129. }
  130. }
  131. /**
  132. * 保存微信用户信息
  133. * @param $wechatUser 用户信息
  134. * @param int $spread_uid 上级用户uid
  135. * @return mixed
  136. */
  137. public static function setWechatUser($wechatUser, $spread_uid = 0)
  138. {
  139. if (isset($wechatUser['uid']) && $wechatUser['uid'] == $spread_uid) $spread_uid = 0;
  140. $data = [
  141. 'account' => 'wx' . date('YmdHis'),
  142. 'pwd' => md5(123456),
  143. 'nickname' => $wechatUser['nickname'] ?: '',
  144. 'avatar' => $wechatUser['headimgurl'] ?: '',
  145. 'user_type' => 'wechat'
  146. ];
  147. //处理推广关系
  148. if ($spread_uid) {
  149. $spreadUserInfo = self::getUserData($spread_uid);
  150. if($spreadUserInfo) {
  151. $data = self::manageSpread($spread_uid, $data, $spreadUserInfo['is_promoter']);
  152. }
  153. }
  154. $res = self::set($data);
  155. if ($res) $wechatUser['uid'] = (int)$res['uid'];
  156. return $wechatUser;
  157. }
  158. /**
  159. * 设置上下级推广人关系
  160. * 普通推广人星级关系由字段 is_promoter 区分, is_promoter = 1 为 0 星, is_promoter = 2 为 1 星,依次类推
  161. * @param $spread_uid 上级推广人
  162. * @param array $data 更新字段
  163. * @param bool $isForever
  164. * @return array|bool
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public static function manageSpread($spread_uid, $data = [], $is_promoter)
  170. {
  171. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  172. if ($storeBrokerageStatu == 1) {
  173. if ($is_promoter) {
  174. $data['spread_uid'] = $spread_uid;
  175. } else {
  176. $data['spread_uid'] = 0;
  177. }
  178. } else {
  179. $data['spread_uid'] = $spread_uid;
  180. }
  181. $data['spread_time'] = time();
  182. return $data;
  183. }
  184. /**
  185. * 更新用户数据并绑定上下级关系
  186. * @param $wechatUser
  187. * @param $uid
  188. * @return bool
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. */
  193. public static function updateWechatUser($wechatUser, $uid)
  194. {
  195. $name = '__login_phone_num' . $uid;
  196. $userinfo = self::where('uid', $uid)->find();
  197. //检查是否有此字段
  198. $spread_uid = isset($wechatUser['spread_uid']) ? $wechatUser['spread_uid'] : 0;
  199. //自己不能成为自己的下线
  200. $spread_uid = $spread_uid == $userinfo->uid ? 0 : $spread_uid;
  201. //手机号码存在直接登陆
  202. if ($userinfo['phone']) {
  203. Cookie::set('__login_phone', 1);
  204. Session::set($name, $userinfo['phone'], 'wap');
  205. Session::set('__login_phone_number', $userinfo['phone'], 'wap');
  206. }
  207. //有推广人直接更新
  208. $editData = [];
  209. //不是推广人,并且有上级id绑定关系
  210. if (!$userinfo->is_promoter && $spread_uid && !$userinfo->spread_uid && $spread_uid != $uid) {
  211. $spreadUserInfo = self::getUserData($spread_uid);
  212. if($spreadUserInfo){
  213. $editData = self::manageSpread($spread_uid, $editData, $spreadUserInfo['is_promoter']);
  214. }
  215. }
  216. if ($userinfo['nickname'] == '' || $userinfo['avatar'] == '') {
  217. $editData['nickname'] = $wechatUser['nickname'];
  218. $editData['avatar'] = $wechatUser['headimgurl'];
  219. }
  220. return self::edit($editData, $uid, 'uid');
  221. }
  222. public static function setSpreadUid($uid, $spreadUid)
  223. {
  224. return self::where('uid', $uid)->update(['spread_uid' => $spreadUid]);
  225. }
  226. public static function getUserInfo($uid)
  227. {
  228. $userInfo = self::where('uid', $uid)->find();
  229. if (!Session::has('__login_phone_num' . $uid) && $userInfo['phone']) {
  230. Cookie::set('__login_phone', 1);
  231. Session::set('__login_phone_num' . $uid, $userInfo['phone'], 'wap');
  232. }
  233. unset($userInfo['pwd']);
  234. if (!$userInfo) {
  235. Session::delete(['loginUid', 'loginOpenid']);
  236. throw new \Exception('未查询到此用户');
  237. }
  238. return $userInfo->toArray();
  239. }
  240. public static function getUserData($uid)
  241. {
  242. $userInfo = self::where('uid', $uid)->find();
  243. if (!$userInfo) return false;
  244. return $userInfo->toArray();
  245. }
  246. /**
  247. * 获得当前登陆用户UID
  248. * @return int $uid
  249. */
  250. public static function getActiveUid()
  251. {
  252. $uid = null;
  253. if (!Cookie::get('is_login')) exit(exception('请登陆!'));
  254. if (Session::has('loginUid', 'wap')) $uid = Session::get('loginUid', 'wap');
  255. if (!$uid && Session::has('loginOpenid', 'wap') && ($openid = Session::get('loginOpenid', 'wap')))
  256. $uid = WechatUser::openidToUid($openid);
  257. if (!$uid) exit(exception('请登陆!'));
  258. return $uid;
  259. }
  260. /**
  261. * 获取登陆的手机号码
  262. * @param int $uid 用户id
  263. * @param string $phone 用户号码
  264. * @return string
  265. * */
  266. public static function getLogPhone($uid, $phone = null)
  267. {
  268. $name = '__login_phone_num' . $uid;
  269. if (!Cookie::get('__login_phone') && $uid) {
  270. Cookie::set('__login_phone', 1);
  271. } else if (!Cookie::get('__login_phone') && !$uid) {
  272. return null;
  273. }
  274. if (Session::has($name, 'wap')) $phone = Session::get($name, 'wap');
  275. if (is_null($phone)) {
  276. if (Session::has('__login_phone_number', 'wap')) $phone = Session::get('__login_phone_number', 'wap');
  277. }
  278. return $phone;
  279. }
  280. /**
  281. * 一级推广 专题
  282. * @param $orderInfo
  283. * @return bool
  284. */
  285. public static function backOrderBrokerage($orderInfo)
  286. {
  287. $userInfo = self::getUserData($orderInfo['uid']);
  288. if (!$userInfo || !$userInfo['spread_uid']) return true;
  289. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  290. if ($storeBrokerageStatu == 1) {
  291. if (!self::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
  292. }
  293. $data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
  294. if (isset($data['is_alone']) && $data['is_alone']) {
  295. if (!isset($data['brokerage_ratio']) || !$data['brokerage_ratio']) return true;
  296. $brokerageRatio = bcdiv($data['brokerage_ratio'], 100, 2);
  297. } else {
  298. $course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关
  299. if ($course_distribution_switch == 0) return true;
  300. $brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_ratio'), 100, 2);
  301. }
  302. if ($brokerageRatio <= 0) return true;
  303. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  304. if ($brokeragePrice <= 0) return true;
  305. $mark = '一级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买专题,奖励推广佣金' . floatval($brokeragePrice);
  306. self::beginTrans();
  307. $res1 = UserBill::income('购买专题返佣', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  308. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  309. $User = self::getUserData($userInfo['spread_uid']);
  310. if(!$User) {
  311. self::checkTrans(false);
  312. return true;
  313. }
  314. if ($openid = WechatUser::where('uid', $userInfo['spread_uid'])->value('openid')) {
  315. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  316. if ($wechat_notification_message == 1) {
  317. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  318. 'first' => '叮!您收到一笔专题返佣,真是太优秀了!',
  319. 'keyword1' => '返佣金额',
  320. 'keyword2' => $brokeragePrice,
  321. 'keyword3' => date('Y-m-d H:i:s', time()),
  322. 'keyword4' => $User['brokerage_price'],
  323. 'remark' => '点击查看详情'
  324. ], Url::build('wap/spread/commission', [], true, true));
  325. } else {
  326. $dat['thing8']['value'] = '返佣金额';
  327. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  328. $dat['amount1']['value'] = $brokeragePrice;
  329. $dat['amount2']['value'] = $User['brokerage_price'];
  330. $dat['thing5']['value'] = '您收到一笔专题返佣!';
  331. RoutineTemplate::sendAccountChanges($dat, $userInfo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  332. }
  333. }
  334. $res = $res1 && $res2;
  335. self::checkTrans($res);
  336. if ($res) self::backOrderBrokerageTwo($orderInfo);
  337. return $res;
  338. }
  339. /**
  340. * 二级推广 专题
  341. * @param $orderInfo
  342. * @return bool
  343. */
  344. public static function backOrderBrokerageTwo($orderInfo)
  345. {
  346. $userInfo = self::getUserData($orderInfo['uid']);
  347. $userInfoTwo = self::getUserData($userInfo['spread_uid']);
  348. if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
  349. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  350. if ($storeBrokerageStatu == 1) {
  351. if (!self::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  352. }
  353. $data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
  354. if (isset($data['is_alone']) && $data['is_alone']) {
  355. if (!isset($data['brokerage_two']) || !$data['brokerage_two']) return true;
  356. $brokerageRatio = bcdiv($data['brokerage_two'], 100, 2);
  357. } else {
  358. $course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关
  359. if ($course_distribution_switch == 0) return true;
  360. $brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_two'), 100, 2);
  361. }
  362. if ($brokerageRatio <= 0) return true;
  363. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  364. if ($brokeragePrice <= 0) return true;
  365. $mark = '二级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买专题,奖励推广佣金' . floatval($brokeragePrice);
  366. self::beginTrans();
  367. $res1 = UserBill::income('购买专题返佣', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  368. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  369. $User = self::getUserData($userInfoTwo['spread_uid']);
  370. if(!$User) {
  371. self::checkTrans(false);
  372. return true;
  373. }
  374. if ($openid = WechatUser::where('uid', $userInfoTwo['spread_uid'])->value('openid')) {
  375. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  376. if ($wechat_notification_message == 1) {
  377. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  378. 'first' => '叮!您收到一笔专题返佣,真是太优秀了!',
  379. 'keyword1' => '返佣金额',
  380. 'keyword2' => $brokeragePrice,
  381. 'keyword3' => date('Y-m-d H:i:s', time()),
  382. 'keyword4' => $User['brokerage_price'],
  383. 'remark' => '点击查看详情'
  384. ], Url::build('wap/spread/commission', [], true, true));
  385. } else {
  386. $dat['thing8']['value'] = '返佣金额';
  387. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  388. $dat['amount1']['value'] = $brokeragePrice;
  389. $dat['amount2']['value'] = $User['brokerage_price'];
  390. $dat['thing5']['value'] = '您收到一笔专题返佣!';
  391. RoutineTemplate::sendAccountChanges($dat, $userInfoTwo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  392. }
  393. }
  394. $res = $res1 && $res2;
  395. self::checkTrans($res);
  396. return $res;
  397. }
  398. /**
  399. * 一级推广 商品
  400. * @param $orderInfo
  401. * @return bool
  402. */
  403. public static function backGoodsOrderBrokerage($orderInfo)
  404. {
  405. $userInfo = self::getUserData($orderInfo['uid']);
  406. if (!$userInfo || !$userInfo['spread_uid']) return true;
  407. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  408. if ($storeBrokerageStatu == 1) {
  409. if (!self::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
  410. }
  411. $data = StoreProduct::getIndividualDistributionSettings($orderInfo['id']);
  412. if (isset($data['is_alone']) && $data['is_alone']) {
  413. if (!isset($data['brokerage_ratio']) || !$data['brokerage_ratio']) return true;
  414. $brokerageRatio = bcdiv($data['brokerage_ratio'], 100, 2);
  415. } else {
  416. $course_distribution_switch = SystemConfigService::get('goods_distribution_switch');//商品分销开关
  417. if ($course_distribution_switch == 0) return true;
  418. $brokerageRatio = bcdiv(SystemConfigService::get('goods_brokerage_ratio'), 100, 2);
  419. }
  420. if ($brokerageRatio <= 0) return true;
  421. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  422. if ($brokeragePrice <= 0) return true;
  423. $mark = '一级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买商品,奖励推广佣金' . floatval($brokeragePrice);
  424. self::beginTrans();
  425. $res1 = UserBill::income('购买商品返佣', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  426. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  427. $User = self::getUserData($userInfo['spread_uid']);
  428. if(!$User) {
  429. self::checkTrans(false);
  430. return true;
  431. }
  432. if ($openid = WechatUser::where('uid', $userInfo['spread_uid'])->value('openid')) {
  433. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  434. if ($wechat_notification_message == 1) {
  435. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  436. 'first' => '叮!您收到一笔商品返佣,真是太优秀了!',
  437. 'keyword1' => '返佣金额',
  438. 'keyword2' => $brokeragePrice,
  439. 'keyword3' => date('Y-m-d H:i:s', time()),
  440. 'keyword4' => $User['brokerage_price'],
  441. 'remark' => '点击查看详情'
  442. ], Url::build('wap/spread/commission', [], true, true));
  443. } else {
  444. $dat['thing8']['value'] = '返佣金额';
  445. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  446. $dat['amount1']['value'] = $brokeragePrice;
  447. $dat['amount2']['value'] = $User['brokerage_price'];
  448. $dat['thing5']['value'] = '您收到一笔商品返佣!';
  449. RoutineTemplate::sendAccountChanges($dat, $userInfo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  450. }
  451. }
  452. $res = $res1 && $res2;
  453. self::checkTrans($res);
  454. if ($res) self::backGoodsOrderBrokerageTwo($orderInfo);
  455. return $res;
  456. }
  457. /**
  458. * 二级推广 商品
  459. * @param $orderInfo
  460. * @return bool
  461. */
  462. public static function backGoodsOrderBrokerageTwo($orderInfo)
  463. {
  464. $userInfo = self::getUserData($orderInfo['uid']);
  465. $userInfoTwo = self::getUserData($userInfo['spread_uid']);
  466. if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
  467. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  468. if ($storeBrokerageStatu == 1) {
  469. if (!self::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  470. }
  471. $data = StoreProduct::getIndividualDistributionSettings($orderInfo['id']);
  472. if (isset($data['is_alone']) && $data['is_alone']) {
  473. if (!isset($data['brokerage_two']) || !$data['brokerage_two']) return true;
  474. $brokerageRatio = bcdiv($data['brokerage_two'], 100, 2);
  475. } else {
  476. $course_distribution_switch = SystemConfigService::get('goods_distribution_switch');//商品分销开关
  477. if ($course_distribution_switch == 0) return true;
  478. $brokerageRatio = bcdiv(SystemConfigService::get('goods_brokerage_two'), 100, 2);
  479. }
  480. if ($brokerageRatio <= 0) return true;
  481. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  482. if ($brokeragePrice <= 0) return true;
  483. $mark = '二级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买商品,奖励推广佣金' . floatval($brokeragePrice);
  484. self::beginTrans();
  485. $res1 = UserBill::income('购买商品返佣', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  486. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  487. $User = self::getUserData($userInfoTwo['spread_uid']);
  488. if(!$User) {
  489. self::checkTrans(false);
  490. return true;
  491. }
  492. if ($openid = WechatUser::where('uid', $userInfoTwo['spread_uid'])->value('openid')) {
  493. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  494. if ($wechat_notification_message == 1) {
  495. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  496. 'first' => '叮!您收到一笔专题商品,真是太优秀了!',
  497. 'keyword1' => '返佣金额',
  498. 'keyword2' => $brokeragePrice,
  499. 'keyword3' => date('Y-m-d H:i:s', time()),
  500. 'keyword4' => $User['brokerage_price'],
  501. 'remark' => '点击查看详情'
  502. ], Url::build('wap/spread/commission', [], true, true));
  503. } else {
  504. $dat['thing8']['value'] = '返佣金额';
  505. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  506. $dat['amount1']['value'] = $brokeragePrice;
  507. $dat['amount2']['value'] = $User['brokerage_price'];
  508. $dat['thing5']['value'] = '您收到一笔商品返佣!';
  509. RoutineTemplate::sendAccountChanges($dat, $userInfoTwo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  510. }
  511. }
  512. $res = $res1 && $res2;
  513. self::checkTrans($res);
  514. return $res;
  515. }
  516. /**
  517. * 一级推广 会员
  518. * @param $orderInfo
  519. * @return bool
  520. */
  521. public static function backOrderBrokerageMember($orderInfo)
  522. {
  523. $userInfo = self::getUserData($orderInfo['uid']);
  524. if (!$userInfo || !$userInfo['spread_uid']) return true;
  525. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  526. if ($storeBrokerageStatu == 1) {
  527. if (!self::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
  528. }
  529. $data = MemberShip::getIndividualDistributionSettings($orderInfo['member_id']);
  530. if (isset($data['is_alone']) && $data['is_alone']) {
  531. if (!isset($data['brokerage_ratio']) || !$data['brokerage_ratio']) return true;
  532. $brokerageRatio = bcdiv($data['brokerage_ratio'], 100, 2);
  533. } else {
  534. $member_distribution_switch = SystemConfigService::get('member_distribution_switch');//会员分销开关
  535. if ($member_distribution_switch == 0) return true;
  536. $brokerageRatio = bcdiv(SystemConfigService::get('member_brokerage_ratio'), 100, 2);
  537. }
  538. if ($brokerageRatio <= 0) return true;
  539. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  540. if ($brokeragePrice <= 0) return true;
  541. $mark = '一级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买会员,奖励推广佣金' . floatval($brokeragePrice);
  542. self::beginTrans();
  543. $res1 = UserBill::income('购买会员返佣', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  544. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  545. $User = self::getUserData($userInfo['spread_uid']);
  546. if(!$User) {
  547. self::checkTrans(false);
  548. return true;
  549. }
  550. if ($openid = WechatUser::where('uid', $userInfo['spread_uid'])->value('openid')) {
  551. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  552. if ($wechat_notification_message == 1) {
  553. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  554. 'first' => '叮!您收到一笔会员返佣,真是太优秀了!',
  555. 'keyword1' => '返佣金额',
  556. 'keyword2' => $brokeragePrice,
  557. 'keyword3' => date('Y-m-d H:i:s', time()),
  558. 'keyword4' => $User['brokerage_price'],
  559. 'remark' => '点击查看详情'
  560. ], Url::build('wap/spread/commission', [], true, true));
  561. } else {
  562. $dat['thing8']['value'] = '返佣金额';
  563. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  564. $dat['amount1']['value'] = $brokeragePrice;
  565. $dat['amount2']['value'] = $User['brokerage_price'];
  566. $dat['thing5']['value'] = '您收到一笔会员返佣!';
  567. RoutineTemplate::sendAccountChanges($dat, $userInfo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  568. }
  569. }
  570. $res = $res1 && $res2;
  571. self::checkTrans($res);
  572. if ($res) self::backOrderBrokerageTwoMember($orderInfo);
  573. return $res;
  574. }
  575. /**
  576. * 二级推广 会员
  577. * @param $orderInfo
  578. * @return bool
  579. */
  580. public static function backOrderBrokerageTwoMember($orderInfo)
  581. {
  582. $userInfo = self::getUserData($orderInfo['uid']);
  583. $userInfoTwo = self::getUserData($userInfo['spread_uid']);
  584. if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
  585. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  586. if ($storeBrokerageStatu == 1) {
  587. if (!self::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  588. }
  589. $data = MemberShip::getIndividualDistributionSettings($orderInfo['member_id']);
  590. if (isset($data['is_alone']) && $data['is_alone']) {
  591. if (!isset($data['brokerage_two']) || !$data['brokerage_two']) return true;
  592. $brokerageRatio = bcdiv($data['brokerage_two'], 100, 2);
  593. } else {
  594. $member_distribution_switch = SystemConfigService::get('member_distribution_switch');//会员分销开关
  595. if ($member_distribution_switch == 0) return true;
  596. $brokerageRatio = bcdiv(SystemConfigService::get('member_brokerage_two'), 100, 2);
  597. }
  598. if ($brokerageRatio <= 0) return true;
  599. $brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
  600. if ($brokeragePrice <= 0) return true;
  601. $mark = '二级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买会员,奖励推广佣金' . floatval($brokeragePrice);
  602. self::beginTrans();
  603. $res1 = UserBill::income('购买会员返佣', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
  604. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  605. $User = self::getUserData($userInfoTwo['spread_uid']);
  606. if(!$User) {
  607. self::checkTrans(false);
  608. return true;
  609. }
  610. if ($openid = WechatUser::where('uid', $userInfoTwo['spread_uid'])->value('openid')) {
  611. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  612. if ($wechat_notification_message == 1) {
  613. WechatTemplateService::sendTemplate($openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  614. 'first' => '叮!您收到一笔会员返佣,真是太优秀了!',
  615. 'keyword1' => '返佣金额',
  616. 'keyword2' => $brokeragePrice,
  617. 'keyword3' => date('Y-m-d H:i:s', time()),
  618. 'keyword4' => $User['brokerage_price'],
  619. 'remark' => '点击查看详情'
  620. ], Url::build('wap/spread/commission', [], true, true));
  621. } else {
  622. $dat['thing8']['value'] = '返佣金额';
  623. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  624. $dat['amount1']['value'] = $brokeragePrice;
  625. $dat['amount2']['value'] = $User['brokerage_price'];
  626. $dat['thing5']['value'] = '您收到一笔会员返佣!';
  627. RoutineTemplate::sendAccountChanges($dat, $userInfoTwo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
  628. }
  629. }
  630. $res = $res1 && $res2;
  631. self::checkTrans($res);
  632. return $res;
  633. }
  634. /**
  635. * 获取推广人列表
  636. * @param $where array 查询条件
  637. * @param $uid int 用户uid
  638. * @return array
  639. * */
  640. public static function getSpreadList($where, $uid)
  641. {
  642. $uids = self::getSpeadUids($uid, true);
  643. if (!count($uids)) return ['list' => [], 'page' => 2];
  644. $model = self::where('uid', 'in', $uids)->field(['nickname', 'avatar', 'phone', 'uid']);
  645. if ($where['search']) $model = $model->where('nickname|uid|phone', 'like', "%$where[search]%");
  646. $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
  647. $list = count($list) ? $list->toArray() : [];
  648. $page = $where['page'] + 1;
  649. foreach ($list as $key => &$item) {
  650. $item['sellout_count'] = UserBill::where(['a.paid' => 1, 'a.is_del' => 0, 'u.category' => 'now_money', 'u.type' => 'brokerage', 'u.uid' => $item['uid']])->alias('u')->join('__STORE_ORDER__ a', 'a.id=u.link_id')
  651. ->count();
  652. $item['sellout_money'] = UserBill::where(['a.paid' => 1, 'a.is_del' => 0, 'u.category' => 'now_money', 'u.type' => 'brokerage', 'u.uid' => $item['uid']])->alias('u')->join('__STORE_ORDER__ a', 'a.id=u.link_id')
  653. ->sum('a.total_price');
  654. }
  655. return compact('list', 'page');
  656. }
  657. /**
  658. * 获取当前用户的下两级
  659. * @param int $uid 用户uid
  660. * @return array
  661. * */
  662. public static function getSpeadUids($uid, $isOne = false)
  663. {
  664. $uids = User::where('spread_uid', $uid)->column('uid');
  665. if ($isOne) return $uids;
  666. $two_uids = count($uids) ? User::where('spread_uid', 'in', $uids)->column('uid') : [];
  667. return array_merge($uids, $two_uids);
  668. }
  669. }