Common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\AdminConfig;
  4. use addons\qingdong\model\FormField;
  5. use app\common\library\Upload;
  6. use app\common\exception\UploadException;
  7. use app\common\model\Area;
  8. use addons\qingdong\model\Field;
  9. use addons\qingdong\model\Form;
  10. use app\common\model\Version;
  11. use think\Config;
  12. use think\Hook;
  13. /**
  14. * * 操作文档:https://doc.fastadmin.net/qingdong
  15. * 软件介绍:https://www.fastadmin.net/store/qingdong.html
  16. * 售后微信:qingdong_crm
  17. * 公共接口
  18. */
  19. class Common extends StaffApi {
  20. protected $noNeedLogin = ['init','getConfig'];
  21. protected $noNeedRight = '*';
  22. /**
  23. * 加载初始化
  24. * @param string $version 版本号
  25. * @param string $lng 经度
  26. * @param string $lat 纬度
  27. */
  28. public function init() {
  29. if ($version = $this->request->request('version')) {
  30. $lng = $this->request->request('lng');
  31. $lat = $this->request->request('lat');
  32. //配置信息
  33. $upload = Config::get('upload');
  34. //如果非服务端中转模式需要修改为中转
  35. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  36. //临时修改上传模式为服务端中转
  37. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  38. $upload = \app\common\model\Config::upload();
  39. // 上传信息配置后
  40. Hook::listen("upload_config_init", $upload);
  41. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  42. }
  43. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  44. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  45. $content = [
  46. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  47. 'versiondata' => Version::check($version),
  48. 'uploaddata' => $upload,
  49. 'coverdata' => Config::get("cover"),
  50. ];
  51. $this->success('', $content);
  52. } else {
  53. $this->error(__('Invalid parameters'));
  54. }
  55. }
  56. /**
  57. * 上传文件
  58. * @ApiMethod (POST)
  59. * @param File $file 文件流
  60. */
  61. public function upload() {
  62. $attachment = null;
  63. //默认普通上传文件
  64. $file = $this->request->file('file');
  65. $name = input('name','');
  66. try {
  67. $upload = new Upload($file);
  68. $attachment = $upload->upload();
  69. $info = $attachment->toArray();
  70. $file = new \addons\qingdong\model\File();
  71. $params = [
  72. 'name' => $name ? $name :$info['filename'],
  73. 'save_name' => $info['url'],
  74. 'size' => isset($info['filesize']) ? $info['filesize'] : 0,
  75. 'types' => $info['mimetype'],
  76. 'file_path' => $info['url'],
  77. 'create_staff_id' => empty($staff)?0:$staff->id,
  78. ];
  79. $file->data(array_filter($params));
  80. $file->save();
  81. $fileId = $file->id;
  82. } catch (UploadException $e) {
  83. return json_encode(['code' => 0, 'msg' => $e->getMessage()]);
  84. }
  85. $this->success(__('Uploaded successful'), [
  86. 'id' => $fileId,
  87. 'flie_url' => $params['file_path'],
  88. 'url' => cdnurl($params['file_path'], true)
  89. ]);
  90. }
  91. //select 字段表
  92. public function selectOption() {
  93. $fields = Field::where([])->select();
  94. $data = [];
  95. foreach ($fields as $v) {
  96. $data[$v['name']] = json_decode($v['data'],true);
  97. }
  98. $this->success('请求成功', $data);
  99. }
  100. //获取form表单
  101. public function getForm() {
  102. $type = input('type', 'leads');//leads 线索 customer 客户 contacts 联系人 contract 合同 examine回款 product产品
  103. $form = Form::where(['type' => $type])->find();
  104. $this->success('请求成功', json_decode($form['data'], true));
  105. }
  106. //获取员工
  107. public function getSelectStaff() {
  108. $name = input('name');
  109. $where = ['status' => 1];
  110. if ($name) {
  111. $where['name'] = ['like', "%{$name}%"];
  112. }
  113. $staffs = \addons\qingdong\model\Staff::where($where)->with(['department'])->field('id,name,department_id,nickname,post,img')->select();
  114. $this->success('请求成功', $staffs);
  115. }
  116. /**
  117. * 获取后台配置
  118. */
  119. public function getConfig(){
  120. $config=get_addon_config('qingdong');
  121. $data=[
  122. 'map_key'=>AdminConfig::getConfigValue('map_key','wechat'),
  123. 'appid'=>AdminConfig::getConfigValue('appid','wechat'),
  124. 'mobile_name'=>$config['mobile_name']??'CRM'
  125. ];
  126. $this->success('请求成功',$data);
  127. }
  128. /**
  129. * 获取时间
  130. */
  131. public function getTimes()
  132. {
  133. $times = input('times');
  134. $times = setTimes($times, 'date');
  135. $this->success('请求成功', ['times' => $times]);
  136. }
  137. /**
  138. * 获取搜索字段
  139. */
  140. public function getSearchFields(){
  141. $type=input('type');//leads
  142. $fields=FormField::where(['types'=>$type,'field'=>['like',"main_%"],'list_show'=>1,'info_type'=>'main'])->select();
  143. $this->success('请求成功',$fields);
  144. }
  145. }