Common.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\AdminConfig;
  4. use addons\qingdongams\model\FormField;
  5. use addons\qingdongams\model\Staff;
  6. use addons\qingdongams\model\StaffRole;
  7. use app\common\exception\UploadException;
  8. use app\common\library\Upload;
  9. use app\common\model\Area;
  10. use app\common\model\Version;
  11. use addons\qingdongams\model\Field;
  12. use addons\qingdongams\model\Form;
  13. use addons\qingdongams\model\StaffRule;
  14. use fast\Http;
  15. use think\Cache;
  16. use think\Config;
  17. use think\Hook;
  18. /**
  19. * 公共接口
  20. */
  21. class Common extends StaffApi {
  22. protected $noNeedLogin = ['init','getConfig','getGeocoder'];
  23. protected $noNeedRight = '*';
  24. /**
  25. * 加载初始化
  26. * @param string $version 版本号
  27. * @param string $lng 经度
  28. * @param string $lat 纬度
  29. */
  30. public function init() {
  31. if ($version = $this->request->request('version')) {
  32. $lng = $this->request->request('lng');
  33. $lat = $this->request->request('lat');
  34. //配置信息
  35. $upload = Config::get('upload');
  36. //如果非服务端中转模式需要修改为中转
  37. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  38. //临时修改上传模式为服务端中转
  39. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  40. $upload = \app\common\model\Config::upload();
  41. // 上传信息配置后
  42. Hook::listen("upload_config_init", $upload);
  43. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  44. }
  45. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  46. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  47. $content = [
  48. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  49. 'versiondata' => Version::check($version),
  50. 'uploaddata' => $upload,
  51. 'coverdata' => Config::get("cover"),
  52. ];
  53. $this->success('', $content);
  54. } else {
  55. $this->error(__('Invalid parameters'));
  56. }
  57. }
  58. /**
  59. * 上传文件
  60. * @ApiMethod (POST)
  61. * @param File $file 文件流
  62. */
  63. public function upload()
  64. {
  65. $attachment = null;
  66. //默认普通上传文件
  67. $file = $this->request->file('file');
  68. $name = input('name');
  69. try {
  70. $upload = new Upload($file);
  71. $attachment = $upload->upload(null);
  72. if (stristr($attachment->mimetype, 'image')) {
  73. $types = 'image';
  74. } else {
  75. $types = 'file';
  76. }
  77. $staff = Staff::info();
  78. $params = array(
  79. 'name' => $name ?: $attachment->filename,
  80. 'save_name' => $attachment->url,
  81. 'size' => $attachment->filesize,
  82. 'types' => $types,
  83. 'file_path' => $attachment->url,
  84. 'create_staff_id' => empty($staff) ? 0 : $staff->id
  85. );
  86. $attachment = new \addons\qingdongams\model\File();
  87. $attachment->data(array_filter($params));
  88. $attachment->save();
  89. $fileId = $attachment->id;
  90. } catch (UploadException $e) {
  91. return json_encode(['code' => 0, 'msg' => $e->getMessage()]);
  92. }
  93. $this->success(__('Uploaded successful'), [
  94. 'id' => $fileId,
  95. 'name' => $params['name'],
  96. 'url' => cdnurl($attachment->file_path, true)
  97. ]);
  98. }
  99. //select 字段表
  100. public function selectOption() {
  101. $fields = Field::where([])->select();
  102. $data = [];
  103. foreach ($fields as $v) {
  104. $data[$v['type']][$v['name']] = json_decode($v['data']);
  105. }
  106. $this->success('请求成功', $data);
  107. }
  108. //获取form表单
  109. public function getForm() {
  110. $type = input('type', 'leads');//leads 线索 customer 客户 contacts 联系人 contract 合同 examine回款 product产品
  111. $form = Form::where(['type' => $type])->find();
  112. $this->success('请求成功', json_decode($form['data'], true));
  113. }
  114. //获取员工
  115. public function getSelectStaff() {
  116. $name = input('name');
  117. $department_id = input('department_id');
  118. $where = ['status' => 1, 'id' => ['<>', 1]];
  119. if ($name) {
  120. $where['name'] = ['like', "%{$name}%"];
  121. }
  122. if ($department_id) {
  123. $where['department_id'] = ['in',explode(',',$department_id)];
  124. }
  125. $staffs = \addons\qingdongams\model\Staff::where($where)->field('id,name,department_id,nickname,post,img,mobile')->select();
  126. $this->success('请求成功', $staffs);
  127. }
  128. //获取员工权限
  129. public function getStaffRule(){
  130. $staff_id= $this->auth->id;
  131. $staff=\addons\qingdongams\model\Staff::get($staff_id);
  132. $role=StaffRole::where(['id'=>$staff['role']])->find();
  133. $rules=isset($role['rules'])?explode(',',$role['rules']):[];
  134. $row=StaffRule::where(['pid'=>0])->column('name','id');
  135. $staffRules=StaffRule::where(['pid'=>['neq',0],'type'=>['neq','data']])->select();
  136. $roleList=[];
  137. foreach ($staffRules as $v) {
  138. $pname = $row[$v['pid']] ?? '';
  139. if ($pname == 'menu') {
  140. $roleList[$pname][$v['name']] = [
  141. 'name' => $v['title'],
  142. 'is_show' => in_array($v['id'], $rules) ? 1 : 0
  143. ];
  144. } else {
  145. $roleList[$pname][$v['type']][$v['name']] = [
  146. 'name' => $v['title'],
  147. 'is_show' => in_array($v['id'], $rules) ? 1 : 0
  148. ];
  149. }
  150. }
  151. $this->success('请求成功',$roleList);
  152. }
  153. /**
  154. * 设置数据缓存
  155. */
  156. public function setCacheData()
  157. {
  158. $type = input('type');
  159. $id = $this->auth->id;
  160. $params = $this->request->post();
  161. unset($params['type']);
  162. Cache::set($type . $id, json_encode($params), 60*60 * 3);
  163. $this->success('设置成功');
  164. }
  165. /**
  166. * 读取数据缓存
  167. */
  168. public function getCacheData()
  169. {
  170. $type = input('type');
  171. $id = $this->auth->id;
  172. $data=json_decode(Cache::get($type . $id),true);
  173. if(empty($data)){
  174. $data=null;
  175. }
  176. $this->success('获取成功', $data);
  177. }
  178. /**
  179. * 获取后台配置
  180. */
  181. public function getConfig(){
  182. $map_key = AdminConfig::getConfigValue('map_key','wechat');
  183. $appid =AdminConfig::getConfigValue('appid','wechat');
  184. $mobile_name =AdminConfig::getConfigValue('user_name','user');
  185. $data=[
  186. 'map_key'=>$map_key ? $map_key : '',
  187. 'appid'=>$appid ? $appid : '',
  188. 'mobile_name'=>$mobile_name? $mobile_name :'E售后'
  189. ];
  190. $this->success('请求成功',$data);
  191. }
  192. /**
  193. * 逆解析
  194. */
  195. public function getGeocoder(){
  196. $map_key=AdminConfig::getConfigValue('map_key', AdminConfig::TYPE_WECHAT);
  197. if(empty($map_key)){
  198. $this->error('腾讯地图key未设置,请联系管理员设置!');
  199. }
  200. $lat=input('lat');
  201. $lng=input('lng');
  202. if(empty($lat) || empty($lng)){
  203. $this->error('经度或纬度不能为空');
  204. }
  205. $url='https://apis.map.qq.com/ws/geocoder/v1/';
  206. $params = ['location' => $lat.','.$lng, 'get_poi' => 1, 'key' => $map_key];
  207. $result=Http::get($url,$params);
  208. $result=json_decode($result);
  209. $this->success('请求成功',$result);
  210. }
  211. /**
  212. *省市区转地址
  213. */
  214. public function addressGeocoder(){
  215. $map_key=AdminConfig::getConfigValue('map_key', AdminConfig::TYPE_WECHAT);
  216. if(empty($map_key)){
  217. $this->error('腾讯地图key未设置,请联系管理员设置!');
  218. }
  219. $address=input('address');
  220. $url="https://apis.map.qq.com/ws/geocoder/v1/?address={$address}&key={$map_key}";
  221. $result=file_get_contents($url);
  222. $result=json_decode($result,true);
  223. $this->success('请求成功',$result);
  224. }
  225. /**
  226. * 获取时间
  227. */
  228. public function getTimes()
  229. {
  230. $times = input('times');
  231. $times = setTimes($times, 'date');
  232. $this->success('请求成功', ['times' => $times]);
  233. }
  234. /**
  235. * 获取搜索字段
  236. */
  237. public function getSearchFields(){
  238. $type=input('type');//leads
  239. // 'field'=>['like',"main_%"],
  240. $fields=FormField::where(['types'=>$type,'list_show'=>1,'info_type'=>'main'])->select();
  241. $this->success('请求成功',$fields);
  242. }
  243. }