Usercompany.php 9.7 KB

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