Custom.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Custom as Request;
  3. use App\Models\City;
  4. use App\Models\Custom as Model;
  5. use App\Models\CustomScore;
  6. use Illuminate\Support\Carbon;
  7. use App\Models\WeiBan\Follow as WeiBanFollow;
  8. /**
  9. * 客户管理
  10. *
  11. * @author 刘相欣
  12. *
  13. */
  14. class Custom extends Auth{
  15. protected function _initialize(){
  16. parent::_initialize();
  17. $this->assign('breadcrumb1','用户管理');
  18. $this->assign('breadcrumb2','客户管理');
  19. }
  20. /**
  21. * 列表页
  22. *
  23. * */
  24. public function index(Model $Model,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow){
  25. // 接受参数
  26. $code = request('custom_code','');
  27. $phone = request('phone','');
  28. $username = request('username','');
  29. $weibanId = request('weiban_extid','');
  30. $status = request('status');
  31. $startTime = request('start_time','');
  32. // 编码转ID
  33. $uid = $Model->codeToId($code);
  34. // 查询条件
  35. $map = [];
  36. // 编码ID
  37. if( $uid ) $map[] = ['uid','=',$uid];
  38. if( $phone ) $map[] = ['phone','=',$phone];
  39. if( $username ) $map[] = ['username','=',$username];
  40. if( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
  41. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  42. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  43. if( !is_null($status) ) $map[] = ['status','=',$status];
  44. // 查询数据
  45. $list = $Model->query()->where($map)->orderByDesc('uid')->paginate(config('page_num',10))->appends(request()->all());
  46. // 循环处理数据
  47. foreach ($list as $key => $value) {
  48. // id转编号
  49. $value['custom_code'] = $Model->idToCode($value['uid']);
  50. // id转编号
  51. $value['custom_score'] = $CustomScore->getCustomScore($value['uid']);
  52. // 如果不存在微伴ID
  53. if( !$value['weiban_extid'] ) {
  54. // 通过手机号查询注册的账号
  55. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  56. // 如果存在的话,修正
  57. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  58. }
  59. // 重组
  60. $list[$key] = $value;
  61. }
  62. // 分配数据
  63. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  64. $this->assign('list',$list);
  65. // 加载模板
  66. return $this->fetch();
  67. }
  68. /**
  69. * 添加
  70. *
  71. * */
  72. public function add(Request $request,Model $Model){
  73. if( request()->isMethod('post') ){
  74. // 验证参数
  75. $request->scene('add')->validate();
  76. // 接收数据
  77. // 接收数据
  78. $data['username'] = request('username','');
  79. $data['phone'] = request('phone','');
  80. // 写入数据表
  81. $uid = $Model->add($data);
  82. // 如果操作失败
  83. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  84. // 记录行为
  85. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  86. // 告知结果
  87. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  88. }
  89. // 分配数据
  90. $this->assign('crumbs','新增');
  91. // 加载模板
  92. return $this->fetch();
  93. }
  94. /**
  95. * 修改
  96. *
  97. * */
  98. public function edit(Request $request,Model $Model,City $City){
  99. // 接收参数
  100. $uid = request('uid',0);
  101. // 查询用户
  102. $oldData = $Model->where(['uid'=>$uid])->first();
  103. // 修改
  104. if(request()->isMethod('post')){
  105. // 验证参数
  106. $request->scene('edit')->validate();
  107. // 如果用户不存在
  108. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  109. // 转数组
  110. $oldData = $oldData->toArray();
  111. // 接收数据
  112. $data['username'] = request('username','');
  113. $data['phone'] = request('phone','');
  114. $data['city_id'] = request('city_id',0);
  115. $data['weiban_extid'] = request('weiban_extid','');
  116. // 写入数据表
  117. $result = $Model->edit($uid,$data);
  118. // 如果操作失败
  119. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  120. // 记录行为
  121. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  122. // 告知结果
  123. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  124. }
  125. // 错误告知
  126. if( !$oldData ) return $this->error('查无数据');
  127. // 获取列表
  128. $cityList = $City->getCityList();
  129. // 分配数据
  130. $this->assign('cityList',$cityList);
  131. $this->assign('oldData',$oldData);
  132. $this->assign('crumbs','修改');
  133. // 加载模板
  134. return $this->fetch();
  135. }
  136. /**
  137. * 修改状态
  138. *
  139. * */
  140. public function set_status(Request $request,Model $Model){
  141. // 验证参数
  142. $request->scene('set_status')->validate();
  143. // 设置状态
  144. $uid = request('uid',0);
  145. $status = request('status',0);
  146. // 查询用户
  147. $oldData = $Model->where(['uid'=>$uid])->first();
  148. // 如果用户不存在
  149. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  150. // 执行修改
  151. $result = $Model->edit($uid,['status'=>$status]);
  152. // 提示新增失败
  153. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  154. // 记录行为
  155. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  156. // 告知结果
  157. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  158. }
  159. }