User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. namespace app\admin\controller\users;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use think\Db;
  6. class User extends AdminController
  7. {
  8. /**
  9. * 获取系统会员列表
  10. *
  11. * @author 许祖兴 < zuxing.xu@lettered.cn>
  12. * @date 2020/6/11 14:10
  13. * @return mixed
  14. * @throws \think\exception\DbException
  15. */
  16. public function index()
  17. {
  18. $where = [];
  19. //组合搜索
  20. !empty(input('mobile')) && $where[]
  21. = ['mobile', 'like', '%' . input('mobile') . '%'];
  22. !empty(input('card_id')) && $where[]
  23. = ['card_id', 'like', '%' . input('card_id') . '%'];
  24. !empty(input('nickname')) && $where[]
  25. = ['nickname', 'like', '%' . input('nickname') . '%'];
  26. (!empty(input('is_agent')) || input('is_agent') == '0') &&
  27. $where[] = ['is_agent', 'eq', input('is_agent')];
  28. (!empty(input('is_seller')) || input('is_seller') == '0') &&
  29. $where[] = ['is_seller', 'eq', input('is_seller')];
  30. (!empty(input('is_verify')) || input('is_verify') == '0') &&
  31. $where[] = ['is_verify', 'eq', input('is_verify')];
  32. (!empty(input('gender')) || input('gender') == '0') &&
  33. $where[] = ['gender', 'eq', input('gender')];
  34. (!empty(input('status')) || input('status') == '0') &&
  35. $where[] = ['status', 'eq', input('status')];
  36. // 时间处理
  37. if (!empty(input('created_at'))){
  38. list($start, $end) = str2arr(input('created_at'),'-');
  39. $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]];
  40. }
  41. return IResponse::paginate(model('common/Users')->where($where)->with(['recommend'])
  42. ->order(['created_at' => 'desc'])
  43. ->paginate(input('limit'),false));
  44. }
  45. /**
  46. * 更新数据
  47. *
  48. * @author 许祖兴 < zuxing.xu@lettered.cn>
  49. * @date 2020/6/11 14:20
  50. *
  51. * @param $id
  52. * @return \think\response\Json
  53. */
  54. public function update($id)
  55. {
  56. // 接收数据
  57. $params = $this->request->param();
  58. // 查询用户
  59. $user = model('common/Users')->findBy($id);
  60. // 是否更改状态操作
  61. if (isset($params['status']) && $params['status'] != '') {
  62. $valid = $this->validate($params, [
  63. 'status|配置状态' => 'require|integer'
  64. ]);
  65. }else {
  66. // 数据校验
  67. $valid = $this->validate($params, [
  68. 'email|账号' => 'require|email',
  69. 'username|用户名' => 'require|alpha'
  70. ]);
  71. }
  72. // 错误返回
  73. (true !== $valid) && IResponse::failure($valid);
  74. // 更新用户信息
  75. $user->updateBy($id, $params);
  76. return IResponse::success('更新用户信息成功');
  77. }
  78. /**
  79. * 删除
  80. *
  81. * @author 许祖兴 < zuxing.xu@lettered.cn>
  82. * @date 2020/6/11 14:26
  83. *
  84. * @param $id
  85. * @return \think\response\Json
  86. */
  87. public function delete($id)
  88. {
  89. model('common/Users')->deleteBy($id);
  90. return IResponse::success([],'删除用户成功');
  91. }
  92. /**
  93. * 用户批量操作
  94. *
  95. * @author 许祖兴 < zuxing.xu@lettered.cn>
  96. * @date 2020/6/11 14:34
  97. *
  98. * @return mixed
  99. */
  100. public function plectron(){
  101. // 收参数
  102. $params = $this->request->param();
  103. foreach (str2arr($params['ids']) as $id){
  104. $user = model('common/Users')->getBy($id);
  105. if ($this->request->isDelete()){
  106. $user->deleteBy($id);
  107. }
  108. $user->allowField(true)->updateBy($id, $params);
  109. }
  110. return IResponse::success([],'操作成功');
  111. }
  112. /**
  113. * 恢复删除用户
  114. *
  115. * @author 许祖兴 < zuxing.xu@lettered.cn>
  116. * @date 2020/6/11 14:46
  117. *
  118. * @param $id
  119. * @return mixed
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @throws \think\exception\DbException
  123. */
  124. public function restore($id)
  125. {
  126. // 查询数据
  127. $user = model('common/Users')->onlyTrashed()->find($id);
  128. if (!$user){
  129. return IResponse::failure('用户不存在!');
  130. }
  131. // 恢复
  132. return $user->restore() ? IResponse::success('恢复用户成功!')
  133. : IResponse::failure('恢复用户失败!');
  134. }
  135. // 取消用户代理身份
  136. public function userCancelAgent($id){
  137. // 读取用户
  138. $user = model('common/Users')->findBy($id);
  139. if(!$user){
  140. return IResponse::failure('用户不存在!');
  141. }
  142. // 读取代理
  143. $agent = model('common/UsersAgent')->getBy(['user_id' => $id]);
  144. if(!$agent && !$user['is_agent']){
  145. return IResponse::failure('当前用户不是代理!');
  146. }
  147. // 删除当前代理
  148. model('common/UsersAgent')->deleteBy(['user_id' => $id]);
  149. // 更改数据
  150. model('common/Users')->updateBy($id,[
  151. 'is_agent' => "0"
  152. ]);
  153. return IResponse::success('已取消用户代理身份!');
  154. }
  155. /**
  156. * 获取用户资金记录
  157. *
  158. * @author 许祖兴 < zuxing.xu@lettered.cn>
  159. * @date 2020/7/9 11:01
  160. *
  161. * @param $id
  162. * @return mixed
  163. * @throws \think\exception\DbException
  164. */
  165. public function userVerify($id)
  166. {
  167. $verify = model('common/UsersVerify')
  168. ->getBy(['user_id' => $id]);
  169. if (!$verify){
  170. return IResponse::failure("查找不到");
  171. }
  172. // 更新信息
  173. if ($this->request->isPost()){
  174. // 接收参数
  175. $params = $this->request->param();
  176. // 参数校验
  177. $valid = $this->validate($params, [
  178. 'name|真实姓名' => 'require',
  179. 'id_card|身份证号' => 'require',
  180. 'id_card_img|身份证信息' => 'require',
  181. 'status|状态' => 'require'
  182. ]);
  183. // 错误返回
  184. if(true !== $valid){
  185. return IResponse::failure($valid);
  186. }
  187. // 用户修改
  188. model('common/Users')->updateBy($verify['user_id'],[
  189. 'is_verify' => $params['status']
  190. ]);
  191. // 用户修改
  192. model('common/UsersVerify')->updateBy($verify['id'],[
  193. 'status' => $params['status']
  194. ]);
  195. return IResponse::success('提交成功');
  196. }
  197. return IResponse::success($verify);
  198. }
  199. /**
  200. * 代理列表
  201. *
  202. * @author 许祖兴 < zuxing.xu@lettered.cn>
  203. * @date 2020/7/15 11:49
  204. *
  205. * @return mixed
  206. * @throws \think\exception\DbException
  207. */
  208. public function getUserAgent()
  209. {
  210. return IResponse::paginate(model('common/UsersAgent')->with(['user','area'])
  211. ->paginate(input('limit'),false));
  212. }
  213. /**
  214. * 用户代理信息
  215. *
  216. * @author 许祖兴 < zuxing.xu@lettered.cn>
  217. * @date 2020/7/14 18:28
  218. *
  219. * @param $id
  220. * @return mixed
  221. */
  222. public function userAgent($id)
  223. {
  224. $agent = model('common/UsersAgent')
  225. ->getBy(['user_id' => $id]);
  226. if (!$agent){
  227. return IResponse::failure("查找不到");
  228. }
  229. // 更新信息
  230. if ($this->request->isPost()) {
  231. // 接收参数
  232. $params = $this->request->param();
  233. // 参数校验
  234. $valid = $this->validate($params, [
  235. 'status|状态' => 'require'
  236. ]);
  237. // 错误返回
  238. if (true !== $valid) {
  239. return IResponse::failure($valid);
  240. }
  241. // 是否已经存在
  242. $isV = model('common/UsersAgent')->getBy(['area_id' => $agent['area_id'],'status' => 2]);
  243. if($isV){
  244. // 修改申请
  245. model('common/UsersAgent')::update([
  246. 'status' => '0',
  247. 'deleted_at' => time()
  248. ],['user_id' => $agent['user_id']]);
  249. return IResponse::failure("当前区域已经存在代理!");
  250. }
  251. // 更新数据
  252. $ret = $agent->updateBy($agent['id'], $params);
  253. // 用户修改
  254. model('common/Users')->updateBy($agent['user_id'], [
  255. 'is_agent' => $params['status']
  256. ]);
  257. // 修改申请
  258. model('common/UsersAgent')::update([
  259. 'status' => $params['status']
  260. ],['user_id' => $agent['user_id']]);
  261. // 消息
  262. if ($ret) {
  263. return IResponse::success('提交成功');
  264. }
  265. return IResponse::failure('数据异常,请稍后再试');
  266. }
  267. $agent['area'] = db('china')->where(['id' => $agent['area_id']])->value('name');
  268. return IResponse::success($agent);
  269. }
  270. /**
  271. *
  272. * @author 许祖兴 < zuxing.xu@lettered.cn>
  273. * @date 2020/8/10 13:34
  274. *
  275. * @return mixed
  276. */
  277. public function balanceRecharge()
  278. {
  279. // 接收数据
  280. $params = $this->request->param();
  281. // 数据校验
  282. $valid = $this->validate($params, [
  283. 'user|会员编号/手机号' => 'require',
  284. 'amount|充值金额' => 'require'
  285. ]);
  286. // 错误返回
  287. (true !== $valid) && IResponse::failure($valid);
  288. // 查询用户
  289. $user = model('common/Users')->where(['card_id|mobile' => $params['user']])->find();
  290. if (!$user || $user['status'] !== 1){
  291. return IResponse::failure("用户不存在或者已被禁用");
  292. }
  293. $result = false;
  294. Db::startTrans();
  295. try {
  296. // 资金
  297. $amount = round($params['amount'], 2);
  298. // // 1. 写入交易单
  299. // model('common/UsersBalanceRecharge')::create([
  300. // 'user_id' => $user['id'],
  301. // 'charge_no' => get_order_no(),
  302. // 'pay_type' => "system",
  303. // 'amount' => $amount,
  304. // 'status' => 2
  305. // ],true);
  306. // 2. 资金记录
  307. $result = model('common/Users')->changeBalance(
  308. $user['id'],
  309. $amount,
  310. "系统充值,充值金额【" . $amount . '】',
  311. true
  312. );
  313. $result = true;
  314. Db::commit();
  315. }catch (\Exception $e){
  316. Db::rollback();
  317. }
  318. return $result ? IResponse::success("充值成功") : IResponse::failure("充值失败");
  319. }
  320. /**
  321. * 获取用户资金记录 20201211 添加资产的查询
  322. *
  323. * @author 许祖兴 < zuxing.xu@lettered.cn>
  324. * @date 2020/7/9 11:01
  325. *
  326. * @param $id
  327. * @return mixed
  328. * @throws \think\exception\DbException
  329. */
  330. public function balanceRecord($id, $type)
  331. {
  332. $model = "UsersBalanceRecord";
  333. if($type == 'property'){
  334. $model = 'UsersPropertyRecord';
  335. }
  336. $withdraw = model('common/' . $model)
  337. ->where(['user_id' => $id])
  338. ->order(['id' => 'desc','updated_at'=>'desc']);
  339. return IResponse::paginate($withdraw->paginate(input('limit'),false));
  340. }
  341. }