Usercompany.php 8.8 KB

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