User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. //表里不存ids这个字段,要把ids改回表对应的字段名:id
  109. //user这个模型没有updateBy这个方法,所以回报错,既然循环,也不可能直接单个id对应
  110. // $user->allowField(true)->updateBy($id, $params);
  111. //懒得去补模型的方法了,我直接用原生写法对表操作了。
  112. //2022-3-3 丘 改动
  113. //判断 原状态 和 修改致状态 未交费之前 冻结,启用动作 都忽略;
  114. $olduser=Db::table('ins_users_motor_agent')->where('id',$id)->find();
  115. if(!$olduser){
  116. return IResponse::failure('用户不存在!');
  117. }
  118. if( $params['status']==40){
  119. if($olduser['status']<40){
  120. //没有通过交费 啥也不做
  121. }else{
  122. //做解冻,或冬季操作
  123. $res=Db::table('ins_users_motor_agent')->update(['status' => $params['status'],'id'=>$id]);
  124. }
  125. }
  126. if( $params['status']==50){
  127. if($olduser['status']<40){
  128. //没有通过交费 啥也不做
  129. }else{
  130. //做解冻,或冬季操作
  131. $res=Db::table('ins_users_motor_agent')->update(['status' => $params['status'],'id'=>$id]);
  132. }
  133. }
  134. }
  135. return IResponse::success([],'操作成功');
  136. }
  137. /**
  138. * 恢复删除用户
  139. *
  140. * @author 许祖兴 < zuxing.xu@lettered.cn>
  141. * @date 2020/6/11 14:46
  142. *
  143. * @param $id
  144. * @return mixed
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. */
  149. public function restore($id)
  150. {
  151. // 查询数据
  152. $user = model('common/Users')->onlyTrashed()->find($id);
  153. if (!$user){
  154. return IResponse::failure('用户不存在!');
  155. }
  156. // 恢复
  157. return $user->restore() ? IResponse::success('恢复用户成功!')
  158. : IResponse::failure('恢复用户失败!');
  159. }
  160. // 取消用户代理身份
  161. public function userCancelAgent($id){
  162. // 读取用户
  163. $user = model('common/Users')->findBy($id);
  164. if(!$user){
  165. return IResponse::failure('用户不存在!');
  166. }
  167. // 读取代理
  168. $agent = model('common/UsersAgent')->getBy(['user_id' => $id]);
  169. if(!$agent && !$user['is_agent']){
  170. return IResponse::failure('当前用户不是代理!');
  171. }
  172. // 删除当前代理
  173. model('common/UsersAgent')->deleteBy(['user_id' => $id]);
  174. // 更改数据
  175. model('common/Users')->updateBy($id,[
  176. 'is_agent' => "0"
  177. ]);
  178. return IResponse::success('已取消用户代理身份!');
  179. }
  180. /**
  181. * 获取用户资金记录
  182. *
  183. * @author 许祖兴 < zuxing.xu@lettered.cn>
  184. * @date 2020/7/9 11:01
  185. *
  186. * @param $id
  187. * @return mixed
  188. * @throws \think\exception\DbException
  189. */
  190. public function userVerify($id)
  191. {
  192. $verify = model('common/UsersVerify')
  193. ->getBy(['user_id' => $id]);
  194. if (!$verify){
  195. return IResponse::failure("查找不到");
  196. }
  197. // 更新信息
  198. if ($this->request->isPost()){
  199. // 接收参数
  200. $params = $this->request->param();
  201. // 参数校验
  202. $valid = $this->validate($params, [
  203. 'name|真实姓名' => 'require',
  204. 'id_card|身份证号' => 'require',
  205. 'id_card_img|身份证信息' => 'require',
  206. 'status|状态' => 'require'
  207. ]);
  208. // 错误返回
  209. if(true !== $valid){
  210. return IResponse::failure($valid);
  211. }
  212. // 用户修改
  213. model('common/Users')->updateBy($verify['user_id'],[
  214. 'is_verify' => $params['status']
  215. ]);
  216. // 用户修改
  217. model('common/UsersVerify')->updateBy($verify['id'],[
  218. 'status' => $params['status']
  219. ]);
  220. return IResponse::success('提交成功');
  221. }
  222. return IResponse::success($verify);
  223. }
  224. /**
  225. * 代理列表
  226. *
  227. * @author 许祖兴 < zuxing.xu@lettered.cn>
  228. * @date 2020/7/15 11:49
  229. *
  230. * @return mixed
  231. * @throws \think\exception\DbException
  232. */
  233. public function getUserAgent()
  234. {
  235. return IResponse::paginate(model('common/UsersAgent')->with(['user','area'])
  236. ->paginate(input('limit'),false));
  237. }
  238. /**
  239. * 用户代理信息
  240. *
  241. * @author 许祖兴 < zuxing.xu@lettered.cn>
  242. * @date 2020/7/14 18:28
  243. *
  244. * @param $id
  245. * @return mixed
  246. */
  247. public function userAgent($id)
  248. {
  249. $agent = model('common/UsersAgent')
  250. ->getBy(['user_id' => $id]);
  251. if (!$agent){
  252. return IResponse::failure("查找不到");
  253. }
  254. // 更新信息
  255. if ($this->request->isPost()) {
  256. // 接收参数
  257. $params = $this->request->param();
  258. // 参数校验
  259. $valid = $this->validate($params, [
  260. 'status|状态' => 'require'
  261. ]);
  262. // 错误返回
  263. if (true !== $valid) {
  264. return IResponse::failure($valid);
  265. }
  266. // 是否已经存在
  267. $isV = model('common/UsersAgent')->getBy(['area_id' => $agent['area_id'],'status' => 2]);
  268. if($isV){
  269. // 修改申请
  270. model('common/UsersAgent')::update([
  271. 'status' => '0',
  272. 'deleted_at' => time()
  273. ],['user_id' => $agent['user_id']]);
  274. return IResponse::failure("当前区域已经存在代理!");
  275. }
  276. // 更新数据
  277. $ret = $agent->updateBy($agent['id'], $params);
  278. // 用户修改
  279. model('common/Users')->updateBy($agent['user_id'], [
  280. 'is_agent' => $params['status']
  281. ]);
  282. // 修改申请
  283. model('common/UsersAgent')::update([
  284. 'status' => $params['status']
  285. ],['user_id' => $agent['user_id']]);
  286. // 消息
  287. if ($ret) {
  288. return IResponse::success('提交成功');
  289. }
  290. return IResponse::failure('数据异常,请稍后再试');
  291. }
  292. $agent['area'] = db('china')->where(['id' => $agent['area_id']])->value('name');
  293. return IResponse::success($agent);
  294. }
  295. /**
  296. *
  297. * @author 许祖兴 < zuxing.xu@lettered.cn>
  298. * @date 2020/8/10 13:34
  299. *
  300. * @return mixed
  301. */
  302. public function balanceRecharge()
  303. {
  304. // 接收数据
  305. $params = $this->request->param();
  306. // 数据校验
  307. $valid = $this->validate($params, [
  308. 'user|会员编号/手机号' => 'require',
  309. 'amount|充值金额' => 'require'
  310. ]);
  311. // 错误返回
  312. (true !== $valid) && IResponse::failure($valid);
  313. // 查询用户
  314. $user = model('common/Users')->where(['card_id|mobile' => $params['user']])->find();
  315. if (!$user || $user['status'] !== 1){
  316. return IResponse::failure("用户不存在或者已被禁用");
  317. }
  318. $result = false;
  319. Db::startTrans();
  320. try {
  321. // 资金
  322. $amount = round($params['amount'], 2);
  323. // // 1. 写入交易单
  324. // model('common/UsersBalanceRecharge')::create([
  325. // 'user_id' => $user['id'],
  326. // 'charge_no' => get_order_no(),
  327. // 'pay_type' => "system",
  328. // 'amount' => $amount,
  329. // 'status' => 2
  330. // ],true);
  331. // 2. 资金记录
  332. $result = model('common/Users')->changeBalance(
  333. $user['id'],
  334. $amount,
  335. "系统充值,充值金额【" . $amount . '】',
  336. true
  337. );
  338. $result = true;
  339. Db::commit();
  340. }catch (\Exception $e){
  341. Db::rollback();
  342. }
  343. return $result ? IResponse::success("充值成功") : IResponse::failure("充值失败");
  344. }
  345. /**
  346. * 获取用户资金记录 20201211 添加资产的查询
  347. *
  348. * @author 许祖兴 < zuxing.xu@lettered.cn>
  349. * @date 2020/7/9 11:01
  350. *
  351. * @param $id
  352. * @return mixed
  353. * @throws \think\exception\DbException
  354. */
  355. public function balanceRecord($id, $type)
  356. {
  357. $model = "UsersBalanceRecord";
  358. if($type == 'property'){
  359. $model = 'UsersPropertyRecord';
  360. }
  361. $withdraw = model('common/' . $model)
  362. ->where(['user_id' => $id])
  363. ->order(['id' => 'desc','updated_at'=>'desc']);
  364. return IResponse::paginate($withdraw->paginate(input('limit'),false));
  365. }
  366. }