Usercompany.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\company\controller;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use app\common\model\User as Usermodel;
  6. use think\Exception;
  7. /**
  8. * 客户管理
  9. */
  10. class Usercompany extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = ['find_user'];
  14. protected $table = 'user_company';
  15. public function index(){
  16. $search = [];
  17. $user_nickname = input('user_nickname','');
  18. $projectname = input('projectname','');
  19. $weituo = input('weituo','');
  20. $header = input('header','');
  21. if(!empty($user_nickname)){
  22. $search['user.nickname'] = ['LIKE','%'.$user_nickname.'%'];
  23. }
  24. if(!empty($projectname)){
  25. $search['a.projectname'] = ['LIKE','%'.$projectname.'%'];
  26. }
  27. if(!empty($weituo)){
  28. $search['a.weituo'] = ['LIKE','%'.$weituo.'%'];
  29. }
  30. if(!empty($header)){
  31. $search['a.header'] = ['LIKE','%'.$header.'%'];
  32. }
  33. $list = Db::name($this->table)->alias('a')
  34. ->field('a.id,a.projectname,a.starttime,a.endtime,a.weituo,a.fuwujigou,a.header,a.header_mobile,user.nickname as user_nickname,user.mobile as user_mobile')
  35. ->join('user','a.user_id = user.id','LEFT')
  36. ->where('a.company_id',$this->auth->company_id)
  37. ->where('a.deletetime',NULL)
  38. ->where($search)
  39. ->paginate();
  40. $total = $list->total();
  41. $list = $list->items();
  42. $rs = [
  43. 'list' => $list,
  44. 'total'=> $total,
  45. ];
  46. $this->success(1,$rs);
  47. }
  48. //添加客户
  49. public function add(){
  50. $mobile = input('user_mobile','');
  51. if(empty($mobile)){
  52. $this->error('手机号必填');
  53. }
  54. //检查用户
  55. $find = Db::name('user')->where('mobile',$mobile)->find();
  56. if($find){
  57. $this->error('该手机号已被注册为客户');
  58. }
  59. //注册到用户表
  60. $nickname = input('user_nickname','');
  61. if(empty($nickname)){
  62. $this->error('客户名称必填');
  63. }
  64. $extend = [
  65. 'nickname' => $nickname,
  66. 'company_id' => $this->auth->company_id,
  67. ];
  68. $register_rs = $this->register($mobile,$extend);
  69. if($register_rs === false){
  70. $this->error('添加客户失败');
  71. }
  72. $user_id = $register_rs;
  73. $zuobiao = input('zuobiao','','trim');
  74. $zuobiao = explode(',',$zuobiao);
  75. Db::startTrans();
  76. //添加客户
  77. $data = [
  78. 'user_id' => $user_id,
  79. 'company_id' => $this->auth->company_id,
  80. 'projectname' => input('projectname',''),
  81. 'projectaddress' => input('projectaddress',''),
  82. 'image' => input('image',''),
  83. 'starttime' => input('starttime','','strtotime'),
  84. 'endtime' => input('endtime','','strtotime'),
  85. 'header' => input('header',''),
  86. 'header_avatar' => input('header_avatar',''),
  87. 'header_mobile' => input('header_mobile',''),
  88. 'xiaofang' => input('xiaofang',''),
  89. 'xiaofang_mobile' => input('xiaofang_mobile',''),
  90. 'weituo' => input('weituo',''),
  91. 'fuwujigou' => input('fuwujigou',''),
  92. 'weibaofanwei' => input('weibaofanwei',''),
  93. 'longitude' => isset($zuobiao[0]) ? $zuobiao[0] : '',
  94. 'latitude' => isset($zuobiao[1]) ? $zuobiao[1] : '',
  95. ];
  96. $uc_id = Db::name($this->table)->insertGetId($data);
  97. if(!$uc_id){
  98. Db::rollback();
  99. $this->error('添加客户失败,请重新再试');
  100. }
  101. Db::commit();
  102. $this->success();
  103. }
  104. public function info(){
  105. $id = input('id',0);
  106. $info = Db::name($this->table)->alias('a')
  107. ->field('a.*,user.nickname as user_nickname,user.mobile as user_mobile')
  108. ->join('user','a.user_id = user.id','LEFT')
  109. ->where('a.id',$id)
  110. ->where('a.company_id',$this->auth->company_id)
  111. ->find();
  112. $info = info_domain_image($info,['image','header_avatar']);
  113. $info['zuobiao'] = '';
  114. if(!empty($info['longitude']) && !empty($info['longitude'])){
  115. $info['zuobiao'] = $info['longitude'].','.$info['latitude'];
  116. }
  117. $this->success(1,$info);
  118. }
  119. public function edit(){
  120. $id = input('id',0);
  121. $info = Db::name($this->table)->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  122. if(empty($info)){
  123. $this->error('没找到该信息,请刷新重试');
  124. }
  125. //修改用户
  126. $mobile = input('user_mobile','');
  127. if(empty($mobile)){
  128. $this->error('手机号必填');
  129. }
  130. //检查用户
  131. $find = Db::name('user')->where('mobile',$mobile)->where('id','!=',$info['user_id'])->find();
  132. if($find){
  133. $this->error('该手机号已被注册为客户');
  134. }
  135. //修改昵称
  136. $nickname = input('user_nickname','');
  137. if(empty($nickname)){
  138. $this->error('客户名称必填');
  139. }
  140. $extend = [
  141. 'nickname' => $nickname,
  142. 'mobile' => $mobile,
  143. ];
  144. $update_rs = Db::name('user')->where('id',$info['user_id'])->update($extend);
  145. if($update_rs === false){
  146. $this->error('操作失败');
  147. }
  148. //
  149. $zuobiao = input('zuobiao','','trim');
  150. $zuobiao = explode(',',$zuobiao);
  151. $data = [
  152. 'projectname' => input('projectname',''),
  153. 'projectaddress' => input('projectaddress',''),
  154. 'image' => input('image',''),
  155. 'starttime' => input('starttime','','strtotime'),
  156. 'endtime' => input('endtime','','strtotime'),
  157. 'header' => input('header',''),
  158. 'header_avatar' => input('header_avatar',''),
  159. 'header_mobile' => input('header_mobile',''),
  160. 'xiaofang' => input('xiaofang',''),
  161. 'xiaofang_mobile' => input('xiaofang_mobile',''),
  162. 'weituo' => input('weituo',''),
  163. 'fuwujigou' => input('fuwujigou',''),
  164. 'weibaofanwei' => input('weibaofanwei',''),
  165. 'longitude' => isset($zuobiao[0]) ? $zuobiao[0] : '',
  166. 'latitude' => isset($zuobiao[1]) ? $zuobiao[1] : '',
  167. ];
  168. Db::name($this->table)->where('id',$id)->update($data);
  169. $this->success();
  170. }
  171. public function del(){
  172. $ids = input('ids','');
  173. $ids = explode(',',$ids);
  174. if (empty($ids)) {
  175. $this->error();
  176. }
  177. Db::startTrans();
  178. $user_company = Db::name($this->table)->where('id','IN',$ids)->where('company_id',$this->auth->company_id)->where('deletetime',NULL)->select();
  179. if(!empty($user_company)){
  180. foreach($user_company as $key => $uc){
  181. $rs1 = Db::name($this->table)->where('id',$uc['id'])->update(['deletetime'=>time()]); //软删除
  182. $rs2 = Db::name('user')->where('id',$uc['user_id'])->update(['company_id'=>0]); //用户解除绑定
  183. if($rs1 === false || $rs2 === false){
  184. Db::rollback();
  185. $this->error('删除失败');
  186. }
  187. }
  188. }
  189. Db::commit();
  190. $this->success();
  191. }
  192. //来自app\common\library\register
  193. public function register($mobile = '', $extend = [])
  194. {
  195. $ip = request()->ip();
  196. $time = time();
  197. $data = [
  198. 'mobile' => $mobile,
  199. 'avatar' => config('user_default_avatar'),
  200. ];
  201. $params = array_merge($data, [
  202. 'jointime' => $time,
  203. 'joinip' => $ip,
  204. 'logintime' => $time,
  205. 'loginip' => $ip,
  206. 'prevtime' => $time,
  207. 'status' => 1
  208. ]);
  209. $params = array_merge($params, $extend);
  210. //账号注册时需要开启事务,避免出现垃圾数据
  211. Db::startTrans();
  212. try {
  213. $user = Usermodel::create($params, true);
  214. $this->_user = Usermodel::get($user->id);
  215. $this->_user->username = 'u' . (10000 + $user->id);
  216. $this->_user->save();
  217. Db::commit();
  218. return $user->id;
  219. } catch (Exception $e) {
  220. Db::rollback();
  221. // return $e->getMessage();
  222. return false;
  223. }
  224. return false;
  225. }
  226. }