User.php 15 KB

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