Custom.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\CustomAddr;
  6. use App\Models\CustomScore;
  7. use Illuminate\Support\Carbon;
  8. use App\Models\WeiBan\Follow as WeiBanFollow;
  9. use App\Models\FilesManager;
  10. use App\Models\WeiBan\External as WeiBanExternal;
  11. /**
  12. * 客户管理
  13. *
  14. * @author 刘相欣
  15. *
  16. */
  17. class Custom extends Auth{
  18. protected function _initialize(){
  19. parent::_initialize();
  20. $this->assign('breadcrumb1','用户管理');
  21. $this->assign('breadcrumb2','客户管理');
  22. }
  23. /**
  24. * 列表页
  25. *
  26. * */
  27. public function index(Model $Model,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow){
  28. // 接受参数
  29. $code = request('custom_code','');
  30. $phone = request('phone','');
  31. $username = request('username','');
  32. $weibanId = request('weiban_extid','');
  33. $status = request('status');
  34. $startTime = request('start_time','');
  35. // 编码转ID
  36. $uid = $Model->codeToId($code);
  37. // 查询条件
  38. $map = [];
  39. // 编码ID
  40. if( $uid ) $map[] = ['uid','=',$uid];
  41. if( $phone ) $map[] = ['phone','=',$phone];
  42. if( $username ) $map[] = ['username','=',$username];
  43. if( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
  44. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  45. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  46. if( !is_null($status) ) $map[] = ['status','=',$status];
  47. // 查询数据
  48. $list = $Model->query()->where($map)->orderByDesc('uid')->paginate(config('page_num',10))->appends(request()->all());
  49. // 循环处理数据
  50. foreach ($list as $key => $value) {
  51. // id转编号
  52. $value['custom_code'] = $Model->idToCode($value['uid']);
  53. // id转编号
  54. $value['custom_score'] = $CustomScore->getCustomScore($value['uid']);
  55. // 如果不存在微伴ID
  56. if( !$value['weiban_extid'] ) {
  57. // 通过手机号查询注册的账号
  58. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  59. // 如果存在的话,修正
  60. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  61. }
  62. // 重组
  63. $list[$key] = $value;
  64. }
  65. // 分配数据
  66. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  67. $this->assign('list',$list);
  68. // 加载模板
  69. return $this->fetch();
  70. }
  71. /**
  72. * 添加
  73. *
  74. * */
  75. public function add(Request $request,Model $Model){
  76. if( request()->isMethod('post') ){
  77. // 验证参数
  78. $request->scene('add')->validate();
  79. // 接收数据
  80. // 接收数据
  81. $data['username'] = request('username','');
  82. $data['phone'] = request('phone','');
  83. // 写入数据表
  84. $uid = $Model->add($data);
  85. // 如果操作失败
  86. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  87. // 记录行为
  88. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  89. // 告知结果
  90. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  91. }
  92. // 分配数据
  93. $this->assign('crumbs','新增');
  94. // 加载模板
  95. return $this->fetch();
  96. }
  97. /**
  98. * 修改
  99. *
  100. * */
  101. public function edit(Request $request,Model $Model,City $City){
  102. // 接收参数
  103. $uid = request('uid',0);
  104. // 查询用户
  105. $oldData = $Model->where(['uid'=>$uid])->first();
  106. // 修改
  107. if(request()->isMethod('post')){
  108. // 验证参数
  109. $request->scene('edit')->validate();
  110. // 如果用户不存在
  111. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  112. // 转数组
  113. $oldData = $oldData->toArray();
  114. // 接收数据
  115. $data['username'] = request('username','');
  116. $data['phone'] = request('phone','');
  117. $data['city_id'] = request('city_id',0);
  118. $data['weiban_extid'] = request('weiban_extid','');
  119. // 写入数据表
  120. $result = $Model->edit($uid,$data);
  121. // 如果操作失败
  122. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  123. // 记录行为
  124. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  125. // 告知结果
  126. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  127. }
  128. // 错误告知
  129. if( !$oldData ) return $this->error('查无数据');
  130. // 获取列表
  131. $cityList = $City->getCityList();
  132. // 分配数据
  133. $this->assign('cityList',$cityList);
  134. $this->assign('oldData',$oldData);
  135. $this->assign('crumbs','修改');
  136. // 加载模板
  137. return $this->fetch();
  138. }
  139. /**
  140. * 修改状态
  141. *
  142. * */
  143. public function set_status(Request $request,Model $Model){
  144. // 验证参数
  145. $request->scene('set_status')->validate();
  146. // 设置状态
  147. $uid = request('uid',0);
  148. $status = request('status',0);
  149. // 查询用户
  150. $oldData = $Model->where(['uid'=>$uid])->first();
  151. // 如果用户不存在
  152. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  153. // 执行修改
  154. $result = $Model->edit($uid,['status'=>$status]);
  155. // 提示新增失败
  156. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  157. // 记录行为
  158. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  159. // 告知结果
  160. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  161. }
  162. /**
  163. * 表格导入
  164. *
  165. * */
  166. public function import_execl( Request $request,Model $Model,FilesManager $FilesManager,City $City,CustomAddr $CustomAddr,WeiBanExternal $WeiBanExternal){
  167. // 验证参数
  168. $request->scene('import_execl')->validate();
  169. // 获取表格信息
  170. $file = request()->file('custom_file');
  171. // 返回结果
  172. $sheetList = $FilesManager->excelToCustom($file);
  173. // 如果不存在结果
  174. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  175. // 循环表格数据
  176. foreach ($sheetList as $value) {
  177. // 获取城市ID
  178. $value['city_id'] = 0;
  179. // 存在城市名称,查询城市ID
  180. if( $value['contact_city'] ) {
  181. // 获取城市ID
  182. $value['city_id'] = (int) $City->getIdByName($value['contact_city']);
  183. // 如果城市不存在的话
  184. if ( !$value['city_id'] ) return json_send(['code'=>'error','msg'=>$value['contact_city'].' => 未找到匹配的城市,请核对城市全称']);
  185. }
  186. // 获取手机号,查询是否用客户
  187. $custom = $Model->getOneByPhone($value['phone']);
  188. // 组装数据
  189. $data = ['weiban_extid'=>$value['weiban_extid'],'username'=>$value['username']];
  190. // 如果客户不存在
  191. if( !$custom ) $data['phone'] = $value['phone'];
  192. // 如果存在城市ID,才修改城市
  193. if( $value['city_id'] ) $data['city_id'] = $value['city_id'];
  194. // 如果存在手机号
  195. $uid = $custom ? $Model->edit($custom['uid'],$data) : $Model->add($data);
  196. // 如果客户存在
  197. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['username'].'【'.$value['phone'].'】'.'用户创建或者更新失败']);
  198. // 存在详细地址,才创建地址库
  199. if( $value['contact_name'] && $value['contact_phone'] && $value['contact_province'] && $value['contact_city'] && $value['contact_area'] && $value['contact_addr'] ) {
  200. // 收件地址是否存在
  201. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  202. // 如果不存在地址
  203. if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$value['contact_name'],'contact_shop'=>$value['contact_shop'],'contact_phone'=>$value['contact_phone'],'contact_province'=>$value['contact_province'],'contact_city'=>$value['contact_city'],'contact_area'=>$value['contact_area'],'contact_addr'=>$value['contact_addr']]);
  204. }
  205. }
  206. // 提示成功
  207. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  208. }
  209. }