User.php 15 KB

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