User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Message;
  4. use app\admin\model\UserPower;
  5. use app\common\controller\Backend;
  6. use app\common\library\Auth;
  7. use fast\Random;
  8. use think\Db;
  9. use think\Exception;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. /**
  13. * 会员管理
  14. *
  15. * @icon fa fa-user
  16. */
  17. class User extends Backend
  18. {
  19. protected $relationSearch = true;
  20. protected $searchFields = 'u_id,username,nickname';
  21. /**
  22. * @var \app\admin\model\User
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = model('User');
  29. $typeList = [
  30. 'genderList' => $this->model->getGenderList(),
  31. 'isCoolList' => $this->model->getIsCoolList(),
  32. 'isManagerList' => $this->model->getIsManagerList(),
  33. ];
  34. $this->view->assign($typeList);
  35. $this->assignconfig($typeList);
  36. }
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. $this->relationSearch = true;
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model
  52. ->with(['noble','preuser','auth','age'])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $k => $v) {
  57. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  58. $v->hidden(['password', 'salt']);
  59. $v->getRelation('age')->visible(['name']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 添加
  68. */
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. $params = $this->preExcludeFields($params);
  77. if (!$params) {
  78. $this->error(__('Parameter %s can not be empty', ''));
  79. }
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  87. $this->model->validateFailException(true)->validate($validate);
  88. }
  89. if (empty($params['avatar'])) {
  90. $params['avatar'] = '/assets/img/default_avatar.png';
  91. }
  92. $ids = $this->model->column("u_id");
  93. $invite_no = $this->model->column("invite_no");
  94. $params['u_id'] = $this->model->getUinqueId(8, [$ids]);
  95. $params['invite_no'] = $this->model->getUinqueNo(8, $invite_no);
  96. if (empty($params['nickname'])) {
  97. $params['nickname'] = 'gg_'.$params['u_id'];
  98. }
  99. $params['image'] = '/assets/img/default_avatar.png';
  100. $params['username'] = $params['mobile'];
  101. $params['status'] = 'normal';
  102. $params['salt'] = Random::alnum();
  103. $params['has_info'] = 1;
  104. $result = $this->model->allowField(true)->save($params);
  105. $userId = $this->model->id;
  106. $userPower = new UserPower();
  107. $userPowerData['user_id'] = $userId;
  108. $userPowerRes = $userPower->insertGetId($userPowerData);
  109. if (!$userPowerRes) {
  110. throw new Exception('创建用户权限失败');
  111. }
  112. } catch (ValidateException|PDOException|Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if ($result == false) {
  117. $this->error(__('No rows were inserted'));
  118. }
  119. Db::commit();
  120. $this->success();
  121. }
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit($ids = null)
  128. {
  129. $row = $this->model->get($ids);
  130. if (!$row) {
  131. $this->error(__('No Results were found'));
  132. }
  133. $adminIds = $this->getDataLimitAdminIds();
  134. if (is_array($adminIds)) {
  135. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  136. $this->error(__('You have no permission'));
  137. }
  138. }
  139. if ($this->request->isPost()) {
  140. $params = $this->request->post("row/a");
  141. if (!$params) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $result = false;
  146. try {
  147. //是否采用模型验证
  148. if ($this->modelValidate) {
  149. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  150. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  151. $row->validateFailException(true)->validate($validate);
  152. }
  153. if (!empty($params['u_id'])) {
  154. $userWhere['u_id'] = $params['u_id'];
  155. $userWhere['id'] = ['neq',$ids];
  156. $user = $this->model->where($userWhere)->find();
  157. if (!empty($user)) {
  158. throw new Exception('前端用户ID已存在');
  159. }
  160. }
  161. if (!empty($params['mobile'])) {
  162. $userWhere['mobile'] = $params['mobile'];
  163. $user = $this->model->where($userWhere)->find();
  164. if (!empty($user)) {
  165. throw new Exception('手机号已存在');
  166. }
  167. }
  168. $result = $row->allowField(true)->save($params);
  169. } catch (ValidateException|PDOException|Exception $e) {
  170. $this->error($e->getMessage());
  171. }
  172. if ($result == false) {
  173. $this->error(__('No rows were updated'));
  174. }
  175. $this->success();
  176. }
  177. $this->view->assign("row", $row);
  178. return $this->view->fetch();
  179. }
  180. /**
  181. * 删除
  182. */
  183. public function del($ids = "")
  184. {
  185. if (!$this->request->isPost()) {
  186. $this->error(__("Invalid parameters"));
  187. }
  188. $ids = $ids ? $ids : $this->request->post("ids");
  189. $row = $this->model->get($ids);
  190. $this->modelValidate = true;
  191. if (!$row) {
  192. $this->error(__('No Results were found'));
  193. }
  194. Auth::instance()->delete($row['id']);
  195. $this->success();
  196. }
  197. /**
  198. * 详情
  199. * @param null $ids
  200. * @return string
  201. * @throws \think\Exception
  202. * @throws \think\exception\DbException
  203. */
  204. public function detail($ids = null)
  205. {
  206. /* 判断数据是否存在*/
  207. $row = $this->model->get($ids);
  208. if (!$row) {
  209. $this->error(__('No Results were found'));
  210. }
  211. /* 判断是否有权限访问*/
  212. $adminIds = $this->getDataLimitAdminIds();
  213. if (is_array($adminIds)) {
  214. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  215. $this->error(__('You have no permission'));
  216. }
  217. }
  218. $this->view->assign("row", $row);
  219. return $this->view->fetch();
  220. }
  221. /**
  222. * 编辑
  223. */
  224. public function infocheck($ids = null)
  225. {
  226. $row = $this->model->get($ids);
  227. if (!$row) {
  228. $this->error(__('No Results were found'));
  229. }
  230. $adminIds = $this->getDataLimitAdminIds();
  231. if (is_array($adminIds)) {
  232. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  233. $this->error(__('You have no permission'));
  234. }
  235. }
  236. if ($this->request->isPost()) {
  237. $params = $this->request->post("row/a");
  238. if (!$params) {
  239. $this->error(__('Parameter %s can not be empty', ''));
  240. }
  241. $params = $this->preExcludeFields($params);
  242. $result = false;
  243. try {
  244. //是否采用模型验证
  245. if ($this->modelValidate) {
  246. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  247. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  248. $row->validateFailException(true)->validate($validate);
  249. }
  250. $nickStatusStr = $statusStr = '';
  251. if (isset($params['check_nick_status'])) {
  252. if ($params['check_nick_status'] == 1) {//审核通过 更新昵称
  253. !empty($params['pre_nickname']) && $params['nickname'] = $params['pre_nickname'];
  254. $params['pre_nickname'] = '';
  255. $nickStatusStr = '昵称通过';
  256. } else {//审核拒绝 清空新昵称
  257. $params['pre_nickname'] = '';
  258. $nickStatusStr = '昵称拒绝';
  259. }
  260. }
  261. if (isset($params['check_status'])) {
  262. if ($params['check_status'] == 1) {//审核通过 更新头像
  263. !empty($params['pre_avatar']) && $params['avatar'] = $params['pre_avatar'];
  264. $params['pre_avatar'] = '';
  265. $statusStr = '头像通过';
  266. } else {//审核拒绝 清空新头像
  267. $params['pre_avatar'] = '';
  268. $statusStr = '头像拒绝';
  269. }
  270. }
  271. $result = $row->allowField(true)->save($params);
  272. } catch (ValidateException|PDOException|Exception $e) {
  273. $this->error($e->getMessage());
  274. }
  275. if ($result == false) {
  276. $this->error(__('No rows were updated'));
  277. }
  278. if (!empty($nickStatusStr) || !empty($statusStr)) {
  279. //通过发消息
  280. $title = '用户信息变更审核通知';
  281. $content = '您申请的'.$nickStatusStr.$statusStr;
  282. Message::addMessage($row['id'],$title,$content);
  283. }
  284. $this->success();
  285. }
  286. $checkStatusList = [1=>'通过', 0=>'拒绝'];
  287. $showNickname = $showAvatar = 0;
  288. if (!empty($row['pre_nickname']) && $row['pre_nickname'] != $row['nickname']) {
  289. $showNickname = 1;
  290. }
  291. if (!empty($row['pre_avatar']) && $row['pre_avatar'] != $row['avatar']) {
  292. $showAvatar = 1;
  293. }
  294. $this->view->assign([
  295. 'row' => $row,
  296. 'checkStatusList' => $checkStatusList,
  297. 'showNickname' => $showNickname,
  298. 'showAvatar' => $showAvatar,
  299. ]);
  300. return $this->view->fetch();
  301. }
  302. /**
  303. * 钻石充值
  304. * @param null $ids
  305. * @return string
  306. */
  307. public function addJewel($ids=null)
  308. {
  309. /* 判断数据是否存在*/
  310. $row = $this->model->get($ids);
  311. if (!$row) {
  312. $this->error(__('No Results were found'));
  313. }
  314. /* 判断是否有权限访问*/
  315. $adminIds = $this->getDataLimitAdminIds();
  316. if (is_array($adminIds)) {
  317. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  318. $this->error(__('You have no permission'));
  319. }
  320. }
  321. if ($this->request->isPost()) {
  322. $params = $this->request->post("row/a");
  323. if (!$params) {
  324. $this->error(__('Parameter %s can not be empty', ''));
  325. }
  326. $params = $this->preExcludeFields($params);
  327. Db::startTrans();
  328. try {
  329. //是否采用模型验证
  330. if ($this->modelValidate) {
  331. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  332. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  333. $row->validateFailException(true)->validate($validate);
  334. }
  335. if (!empty($params['jewel_add'])) {//钻石充值
  336. $userWhere['id'] = $row['id'];
  337. $user = Db::name('user')->where($userWhere)->lock(true)->find();
  338. $before = isset($user['jewel']) ? $user['jewel'] : 0;
  339. $jewelRes = model('Wallet')->lockChangeAccountRemain($row['id'],$params['jewel_add'],'+',$before,$remark='钻石充值()',17,'jewel');
  340. if (!$jewelRes['status']) {
  341. throw new Exception($jewelRes['msg']);
  342. }
  343. $params['jewel'] = bcadd($user['jewel'],$params['jewel_add']);
  344. //充值日志记录
  345. //判断是否首充
  346. $jewellogWhere['user_id'] = $row['id'];
  347. $jewellogWhere['type'] = 1;
  348. $userJewelLog = model('UserJewelLog')->where($jewellogWhere)->find();
  349. $isFirst = 1;
  350. if (!empty($userJewelLog)) {
  351. $isFirst = 0;
  352. }
  353. $preUserId = $user['pre_userid'];
  354. $userRechargeLogRes = model('UserRechargeLog')->addRecord($row['id'], $params['jewel_add'], $user['money'], $params['jewel'], $user['money'], 4, 4,$isFirst,$preUserId);
  355. if (!$userRechargeLogRes) {
  356. throw new Exception('充值记录生成失败');
  357. }
  358. }
  359. $result = $row->allowField(true)->save($params);
  360. if ($result == false) {
  361. throw new Exception(__('No rows were updated'));
  362. }
  363. Db::commit();
  364. $this->success();
  365. } catch (ValidateException|PDOException|Exception $e) {
  366. Db::rollback();
  367. $this->error($e->getMessage());
  368. }
  369. }
  370. $this->view->assign("row", $row);
  371. return $this->view->fetch();
  372. }
  373. }