Usercompany.php 9.1 KB

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