User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. namespace app\admin\controller\shopro\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\admin\model\shopro\user\Oauth;
  6. use Exception;
  7. use addons\shopro\exception\Exception as ShoproException;
  8. use think\exception\PDOException;
  9. use app\admin\model\shopro\user\WalletLog;
  10. /**
  11. * 会员管理
  12. *
  13. * @icon fa fa-user
  14. */
  15. class User extends Backend
  16. {
  17. protected $relationSearch = true;
  18. /**
  19. * @var \app\admin\model\user\User
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\shopro\user\User;
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax()) {
  35. //如果发送的来源是Selectpage,则转发到Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. $searchWhere = $this->request->request('searchWhere');
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  41. $total = $this->model
  42. ->with('group')
  43. ->where($where)
  44. ->whereOr('user.id', '=', $searchWhere)
  45. ->whereOr('nickname', 'like', "%$searchWhere%")
  46. ->whereOr('mobile', 'like', "%$searchWhere%")
  47. ->order($sort, $order)
  48. ->count();
  49. $list = $this->model
  50. ->with('group')
  51. ->where($where)
  52. ->whereOr('user.id', '=', $searchWhere)
  53. ->whereOr('nickname', 'like', "%$searchWhere%")
  54. ->whereOr('mobile', 'like', "%$searchWhere%")
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. foreach ($list as $k => $v) {
  59. $v->hidden(['password', 'salt']);
  60. $v->third_platform = Oauth::all(['user_id' => $v->id]);
  61. }
  62. $result = array("total" => $total, "rows" => $list);
  63. $this->success('查看用户', null, $result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 用户详情
  69. */
  70. public function profile($id)
  71. {
  72. $row = $this->model->get($id);
  73. if (!$row) {
  74. $this->error('未找到用户');
  75. }
  76. $row->hidden(['password', 'salt']);
  77. $row->third_platform = Oauth::all(['user_id' => $row->id]);
  78. if (checkEnv('commission', false)) {
  79. $row->parent_user = $this->model->get($row->parent_user_id);
  80. }
  81. if ($this->request->isAjax()) {
  82. $this->success('用户详情', null, $row);
  83. }
  84. $this->assignconfig('row', $row);
  85. $this->assignconfig('groupList', \app\admin\model\UserGroup::field('id,name,status')->select());
  86. return $this->view->fetch();
  87. }
  88. /**
  89. * 更新信息
  90. */
  91. public function update()
  92. {
  93. $params = $this->request->post('data');
  94. $params = json_decode($params, true);
  95. $user = $this->model->get($params['id']);
  96. if (!$user) {
  97. $this->error('未找到用户');
  98. }
  99. $result = Db::transaction(function () use ($user, $params) {
  100. try {
  101. if (!empty($params['password'])) {
  102. $salt = \fast\Random::alnum();
  103. $user->password = \app\common\library\Auth::instance()->getEncryptPassword($params['password'], $salt);
  104. $user->salt = $salt;
  105. $user->save();
  106. }
  107. $verification = $user->verification;
  108. if (!empty($params['mobile'])) {
  109. $verification->mobile = 1;
  110. } else {
  111. $verification->mobile = 0;
  112. }
  113. $user->verification = $verification;
  114. $user->save();
  115. return $user->validate('\app\admin\validate\shopro\user\User.update')->allowField('nickname,avatar,username,group_id,birthday,bio,mobile,email,level,gender,status')->save($params);
  116. } catch (\Exception $e) {
  117. $this->error($e->getMessage());
  118. }
  119. });
  120. if ($result) {
  121. return $this->success('更新成功', null, $user);
  122. } else {
  123. return $this->error($user->getError());
  124. }
  125. }
  126. /**
  127. * 选择
  128. */
  129. public function select()
  130. {
  131. //设置过滤方法
  132. $this->request->filter(['strip_tags']);
  133. if ($this->request->isAjax()) {
  134. //如果发送的来源是Selectpage,则转发到Selectpage
  135. if ($this->request->request('keyField')) {
  136. return $this->selectpage();
  137. }
  138. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  139. $searchWhere = $this->request->request('search');
  140. $total = $this->model
  141. ->where($where)
  142. ->whereOr('id', '=', $searchWhere)
  143. ->whereOr('nickname', 'like', "%$searchWhere%")
  144. ->whereOr('mobile', 'like', "%$searchWhere%")
  145. ->order($sort, $order)
  146. ->field('id, nickname, mobile, avatar')
  147. ->count();
  148. $list = $this->model
  149. ->where($where)
  150. ->whereOr('id', '=', $searchWhere)
  151. ->whereOr('nickname', 'like', "%$searchWhere%")
  152. ->whereOr('mobile', 'like', "%$searchWhere%")
  153. ->order($sort, $order)
  154. ->field('id, nickname, mobile, avatar')
  155. ->limit($offset, $limit)
  156. ->select();
  157. $result = array("total" => $total, "rows" => $list);
  158. $this->success('选择用户', null, $result);
  159. }
  160. return $this->view->fetch();
  161. }
  162. /**
  163. * 用户余额充值
  164. */
  165. public function money_recharge()
  166. {
  167. if ($this->request->isAjax()) {
  168. $params = $this->request->post();
  169. $user = $this->model->get($params['user_id']);
  170. $params['money'] = $params['money'];
  171. if ($params['money'] > 0) {
  172. $type = 'admin_recharge';
  173. } elseif ($params['money'] < 0) {
  174. $type = 'admin_deduct';
  175. } else {
  176. $this->error('请输入正确的金额');
  177. }
  178. $result = Db::transaction(function () use ($params, $user, $type) {
  179. return \addons\shopro\model\User::money($params['money'], $user->id, $type, 0, $params['remarks']);
  180. });
  181. if ($result) {
  182. $this->success('操作成功');
  183. } else {
  184. $this->error('操作失败');
  185. }
  186. }
  187. return $this->view->fetch();
  188. }
  189. /**
  190. * 用户积分充值
  191. */
  192. public function score_recharge()
  193. {
  194. if ($this->request->isAjax()) {
  195. $params = $this->request->post();
  196. $user = $this->model->get($params['user_id']);
  197. $params['score'] = intval($params['score']);
  198. if ($params['score'] > 0) {
  199. $type = 'admin_recharge';
  200. } elseif ($params['score'] < 0) {
  201. $type = 'admin_deduct';
  202. } else {
  203. $this->error('请输入正确的数量');
  204. }
  205. $result = Db::transaction(function () use ($params, $user, $type) {
  206. try {
  207. return \addons\shopro\model\User::score($params['score'], $user->id, $type, 0, $params['remarks']);
  208. } catch (\Exception $e) {
  209. $this->error($e->getMessage());
  210. }
  211. });
  212. if ($result) {
  213. $this->success('操作成功');
  214. } else {
  215. $this->error('操作失败');
  216. }
  217. }
  218. return $this->view->fetch();
  219. }
  220. /**
  221. * 余额明细
  222. */
  223. public function money_log($user_id, $limit = 10)
  224. {
  225. if ($this->request->isAjax()) {
  226. $model = new WalletLog();
  227. $data = $model->where(['user_id' => $user_id, 'wallet_type' => 'money'])->order('id desc')->paginate($limit);
  228. $this->success('余额明细', null, $data);
  229. }
  230. }
  231. /**
  232. * 积分明细
  233. */
  234. public function score_log($user_id, $limit = 10)
  235. {
  236. if ($this->request->isAjax()) {
  237. $model = new WalletLog();
  238. $data = $model->where(['user_id' => $user_id, 'wallet_type' => 'score'])->order('id desc')->paginate($limit);
  239. $this->success('积分明细', null, $data);
  240. }
  241. }
  242. /**
  243. * 订单记录
  244. */
  245. public function order_log($user_id, $limit = 10)
  246. {
  247. if ($this->request->isAjax()) {
  248. $this->loadlang('shopro/order/order');
  249. $model = new \app\admin\model\shopro\order\Order;
  250. $data = $model->where('user_id', $user_id)->order('id desc')->paginate($limit);
  251. $this->success('订单记录', null, $data);
  252. }
  253. }
  254. /**
  255. * 登录记录
  256. */
  257. public function login_log($user_id, $limit = 10)
  258. {
  259. if ($this->request->isAjax()) {
  260. }
  261. }
  262. /**
  263. * 分享记录
  264. */
  265. public function share_log($user_id, $limit = 10)
  266. {
  267. if ($this->request->isAjax()) {
  268. $this->loadlang('shopro/share');
  269. $model = new \app\admin\model\shopro\Share;
  270. $data = $model->where('share_id', $user_id)->order('id desc')->with([
  271. 'user' => function ($query) {
  272. return $query->withField('id,nickname,avatar');
  273. }
  274. ])->paginate($limit);
  275. foreach ($data as &$v) {
  276. if ($v['type'] === 'goods') {
  277. $v['goods'] = \app\admin\model\shopro\goods\Goods::where('id', $v['type_id'])->field('id, image, title')->find();
  278. }
  279. if ($v['type'] === 'groupon') {
  280. $v['groupon'] = \app\admin\model\shopro\activity\Groupon::get($v['type_id']);
  281. }
  282. }
  283. $this->success('分享记录', null, $data);
  284. }
  285. }
  286. /**
  287. * 收藏商品
  288. */
  289. public function goods_favorite($user_id, $limit = 10)
  290. {
  291. if ($this->request->isAjax()) {
  292. $model = new \app\admin\model\shopro\user\Favorite;
  293. $data = $model->where('user_id', $user_id)->order('id desc')->with([
  294. 'goods' => function ($query) {
  295. return $query->withField('id,title,image');
  296. }
  297. ])->paginate($limit);
  298. $this->success('商品收藏', null, $data);
  299. }
  300. }
  301. /**
  302. * 浏览足迹
  303. */
  304. public function goods_view($user_id, $limit = 10)
  305. {
  306. if ($this->request->isAjax()) {
  307. $model = new \app\admin\model\shopro\user\View;
  308. $data = $model->where('user_id', $user_id)->order('id desc')->with([
  309. 'goods' => function ($query) {
  310. return $query->withField('id,title,image');
  311. }
  312. ])->paginate($limit);
  313. $this->success('商品收藏', null, $data);
  314. }
  315. }
  316. /**
  317. * 优惠券
  318. */
  319. public function coupon_log($user_id, $limit = 10)
  320. {
  321. if ($this->request->isAjax()) {
  322. $model = new \app\admin\model\shopro\user\Coupon;
  323. $data = $model->where('user_id', $user_id)->order('id desc')->with(['coupons' => function ($query) {
  324. return $query->withField('id,name,amount');
  325. }])->paginate($limit);
  326. $this->success('优惠券', null, $data);
  327. }
  328. }
  329. /**
  330. * 删除
  331. */
  332. public function del($ids = "")
  333. {
  334. if (!$this->request->isPost()) {
  335. $this->error(__("Invalid parameters"));
  336. }
  337. $ids = $ids ? $ids : $this->request->post("ids");
  338. if ($ids) {
  339. $pk = $this->model->getPk();
  340. $list = $this->model->where($pk, 'in', $ids)->select();
  341. $count = 0;
  342. Db::startTrans();
  343. try {
  344. foreach ($list as $k => $v) {
  345. // 删除这个用户关联的 shopro_user_oauth 记录
  346. Oauth::where('user_id', $v->id)->delete();
  347. // 删除用户
  348. $count += $v->delete();
  349. }
  350. Db::commit();
  351. } catch (PDOException $e) {
  352. Db::rollback();
  353. $this->error($e->getMessage());
  354. } catch (Exception $e) {
  355. Db::rollback();
  356. $this->error($e->getMessage());
  357. }
  358. if ($count) {
  359. $this->success();
  360. } else {
  361. $this->error(__('No rows were deleted'));
  362. }
  363. }
  364. $this->error(__('Parameter %s can not be empty', 'ids'));
  365. }
  366. /**
  367. * 更换上级推荐人
  368. */
  369. public function changeParentUser($id)
  370. {
  371. $user = $this->model->get($id);
  372. $value = $this->request->post('value');
  373. if (!$user) {
  374. $this->error('未找到用户');
  375. }
  376. $agent = new \addons\shopro\library\commission\Agent($id);
  377. if ($agent->agent) {
  378. $this->error('请前往分销中心操作该用户');
  379. }
  380. try {
  381. if ($user->parent_user_id == $value) {
  382. throw \Exception('请勿重复选择');
  383. }
  384. if ($user->id == $value) {
  385. throw \Exception('不能绑定本人');
  386. }
  387. if ($value != 0) {
  388. $parentAgent = new \addons\shopro\library\commission\Agent($value);
  389. if (!$parentAgent || !$parentAgent->agent) {
  390. throw \Exception('未找到该分销商');
  391. }
  392. }
  393. if (!$this->checkChangeParentAgent($user->id, $value)) {
  394. throw \Exception('不能绑定该分销商');
  395. }
  396. $runUpgradeLastAgentId = $user->parent_user_id;
  397. $user->parent_user_id = $value;
  398. $user->save();
  399. if (!empty($runUpgradeLastAgentId)) {
  400. $agent->asyncAgentUpgrade($runUpgradeLastAgentId);
  401. }
  402. if (!empty($user->parent_user_id)) {
  403. $agent->asyncAgentUpgrade($user->parent_user_id);
  404. }
  405. } catch (\Exception $e) {
  406. $this->error($e->getMessage());
  407. }
  408. $this->success('绑定成功');
  409. }
  410. // 递归往上找推荐人,防止出现推荐闭环
  411. private function checkChangeParentAgent($userId, $parentAgentId)
  412. {
  413. if ($userId === $parentAgentId) {
  414. return false;
  415. }
  416. if ($parentAgentId == 0) {
  417. return true;
  418. }
  419. $parentAgent = \app\admin\model\shopro\commission\Agent::get($parentAgentId);
  420. if ($parentAgent) {
  421. if ($parentAgent->parent_agent_id === 0) {
  422. return true;
  423. } else {
  424. return $this->checkChangeParentAgent($userId, $parentAgent->parent_agent_id);
  425. }
  426. }
  427. return false;
  428. }
  429. }