Common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Agreement;
  4. use app\common\controller\Api;
  5. use app\common\exception\UploadException;
  6. use app\common\library\Upload;
  7. use app\common\model\Ad;
  8. use app\common\model\Area;
  9. use app\common\model\Feedback;
  10. use app\common\model\FeedbackBusiness;
  11. use app\common\service\SmsSend;
  12. use think\Cache;
  13. use think\Config;
  14. use think\File;
  15. /**
  16. * 公共接口
  17. */
  18. class Common extends Api
  19. {
  20. protected $noNeedLogin = ['agreement','ads','area','area_sort','area_tree','config','get_area','upload','feedback','business_form'];
  21. protected $noNeedRight="*";
  22. /**
  23. * 获取各种协议
  24. * @param int id 1入网许可协议2个人信息收集证明3查询订单说明4商务合作说明5隐私权政策6法律责任风险提示告知书
  25. * @ApiReturnParams(name=title,type=string,description=标题)
  26. * @ApiReturnParams(name=content,type=string,description=内容)
  27. */
  28. public function agreement(){
  29. $data=input();
  30. $this->validate($data,[
  31. 'id'=>['require'],
  32. ]);
  33. $agreement=Agreement::where('id',$data['id'])->value('content','');
  34. $this->success('',$agreement);
  35. }
  36. /**
  37. * 获取轮播图
  38. * @param string pos 位置index首页
  39. * @ApiReturnParams (name=img,description=图片地址)
  40. * @ApiReturnParams (name=link,description=链接)
  41. */
  42. public function ads(){
  43. $data=input();
  44. $this->validate($data,[
  45. 'pos'=>'require',
  46. ]);
  47. $ads=Ad::getAd($data['pos']);
  48. $this->success('',$ads);
  49. }
  50. /**
  51. * 上传文件
  52. * @ApiMethod (POST)
  53. * @param File $file 文件流
  54. */
  55. public function upload()
  56. {
  57. Config::set('default_return_type', 'json');
  58. //必须设定cdnurl为空,否则cdnurl函数计算错误
  59. Config::set('upload.cdnurl', '');
  60. $chunkid = $this->request->post("chunkid");
  61. if ($chunkid) {
  62. if (!Config::get('upload.chunking')) {
  63. $this->error(__('Chunk file disabled'));
  64. }
  65. $action = $this->request->post("action");
  66. $chunkindex = $this->request->post("chunkindex/d");
  67. $chunkcount = $this->request->post("chunkcount/d");
  68. $filename = $this->request->post("filename");
  69. $method = $this->request->method(true);
  70. if ($action == 'merge') {
  71. $attachment = null;
  72. //合并分片文件
  73. try {
  74. $upload = new Upload();
  75. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  76. } catch (UploadException $e) {
  77. $this->error($e->getMessage());
  78. }
  79. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  80. } elseif ($method == 'clean') {
  81. //删除冗余的分片文件
  82. try {
  83. $upload = new Upload();
  84. $upload->clean($chunkid);
  85. } catch (UploadException $e) {
  86. $this->error($e->getMessage());
  87. }
  88. $this->success();
  89. } else {
  90. //上传分片文件
  91. //默认普通上传文件
  92. $file = $this->request->file('file');
  93. try {
  94. $upload = new Upload($file);
  95. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  96. } catch (UploadException $e) {
  97. $this->error($e->getMessage());
  98. }
  99. $this->success();
  100. }
  101. } else {
  102. $attachment = null;
  103. //默认普通上传文件
  104. $file = $this->request->file('file');
  105. try {
  106. $upload = new Upload($file);
  107. $attachment = $upload->upload();
  108. } catch (UploadException $e) {
  109. $this->error($e->getMessage());
  110. }
  111. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  112. }
  113. }
  114. /**
  115. * 意见反馈
  116. * @ApiParams (name=name,description="称呼")
  117. * @ApiParams (name=phone,description=电话)
  118. * @ApiParams (name=sms_code,description=验证码)
  119. * @ApiParams (name=title,description=标题)
  120. * @ApiParams (name=content,description=内容)
  121. * @ApiParams (name=images,description=图片数组)
  122. */
  123. public function feedback(){
  124. $data=input();
  125. $this->validate($data,[
  126. 'name'=>['require','max:10'],
  127. 'phone'=>['require','integer','length:11'],
  128. 'title'=>['require','max:10'],
  129. 'content'=>['require','max:120'],
  130. 'images'=>['require','array','max:3','min:1'],
  131. 'images.0'=>['require','url'],
  132. ]);
  133. SmsSend::setMobile($data['phone'])->setCode($data['sms_code']??'')->check();
  134. $feedback=new Feedback();
  135. $feedback->allowField(true)->save($data);
  136. $this->success('',$feedback);
  137. }
  138. /**
  139. * 商务合作
  140. * @ApiParams (name=type,description="1个人2公司")
  141. * @ApiParams (name=name,description="名称")
  142. * @ApiParams (name=for,description="方向1直播合作2学习直播3卡商合作")
  143. * @ApiParams (name=has_exp,description="1有0无经验")
  144. * @ApiParams (name=phone,description="手机号")
  145. * @ApiParams (name=sms_code,description="验证码")
  146. *
  147. */
  148. public function business_form(){
  149. $data=$this->_validate([
  150. 'type'=>['require','in:1,2'],
  151. 'name'=>['require','max:50'],
  152. 'for'=>['require','in:1,2,3'],
  153. 'has_exp'=>['require','in:1,0'],
  154. 'phone'=>['require','integer','length:11'],
  155. ]);
  156. SmsSend::setMobile($data['phone'])->setCode($data['sms_code']??'')->check();
  157. $feedback=new FeedbackBusiness;
  158. $feedback->allowField(true)->save($data);
  159. $this->success('',$feedback);
  160. }
  161. /**
  162. * 获取地区信息
  163. * @ApiParams (name=level,description=1省2市3区)
  164. * @ApiParams (name=pid,description=上级区域ID)
  165. */
  166. public function area(){
  167. $data=$this->_validate([
  168. 'level'=>['require'],
  169. 'pid'=>['require'],
  170. ]);
  171. $this->success(
  172. '',
  173. Area::where('level',$data['level'])
  174. ->where('pid',$data['pid'])
  175. ->order('pinyin')
  176. ->select()
  177. );
  178. }
  179. /**
  180. * 获取地区信息(按字母排序)
  181. */
  182. public function area_sort(){
  183. $citys=Area::area()->field(['id','name','first'])->select();
  184. $a=[];
  185. for ($i=65;$i<91;$i++) {
  186. $one=[
  187. 'index'=>chr($i),
  188. 'child'=>[]
  189. ];
  190. foreach ($citys as $key=>&$city) {
  191. if($city['first']==$one['index']){
  192. $one['child'][]=[
  193. 'title'=>$city['name'],
  194. 'weight'=>$city['id'],
  195. 'id' =>$city['id'],
  196. ];
  197. unset($citys[$key]);
  198. }
  199. }
  200. $a[]=$one;
  201. }
  202. $this->success('',$a);
  203. }
  204. /**
  205. * 地区递归三级联动
  206. */
  207. public function area_tree(){
  208. $areas=Area::where('level','<=',3)->field('id as value,name as label,pid')->select()->toArray();
  209. $fina=[];
  210. function area(&$data,$areas,$first=false){
  211. foreach ($areas as $k=>$area){
  212. if($first){
  213. if($area['pid']==0) {
  214. area($area, $areas);
  215. unset($area['pid']);
  216. $data[] = $area;
  217. unset($areas[$k]);
  218. }
  219. }else{
  220. if($data['value']==$area['pid']){
  221. area($area, $areas);
  222. unset($area['pid']);
  223. $data['children'][]=$area;
  224. unset($areas[$k]);
  225. }
  226. }
  227. }
  228. }
  229. $fromCache=Cache::get('app_area');
  230. if(!$fromCache){
  231. area($fina,$areas,true);
  232. $fromCache=$fina;
  233. Cache::remember('app_area',$fina,0);
  234. }
  235. $this->success('',$fromCache);
  236. }
  237. /**
  238. * 根据名称获取信息
  239. * @ApiParams (name="name",description="城市名")
  240. * @ApiReturnParams (name="id",description="城市ID")
  241. */
  242. public function get_area(){
  243. $rs=null;
  244. if($name=input('name')){
  245. $rs=Area::where('name|shortname',$name)->find();
  246. }
  247. $this->success('',$rs);
  248. }
  249. /**
  250. * 获取系统配置
  251. * @ApiReturnParams (name="site_name",description="站点名称")
  252. * @ApiReturnParams (name="com_name",description="公司名称")
  253. * @ApiReturnParams (name="wxp_name",description="微信公众号名称")
  254. * @ApiReturnParams (name="wxp_qrcode",description="微信公众号二维码")
  255. * @ApiReturnParams (name="service_mobile",description="客服手机号")
  256. * @ApiReturnParams (name="service_work_time",description="客服工作时间")
  257. * @ApiReturnParams (name="system_sms_open",description="短信验证码开关")
  258. * @ApiReturnParams (name="disable_send_province",description="禁止发货地区")
  259. * @ApiReturnParams (name="system_index_tr",description="首页右上角展示1投诉2商务合作")
  260. * @ApiReturnParams (name="system_index_m1",description="首页特价号1流量卡2号码定制")
  261. */
  262. public function config(){
  263. $data=[
  264. 'site_name'=>\config('site.name'),
  265. 'com_name'=>\config('site.base_company_name'),
  266. 'wxp_name'=>\config('site.system_wxp_name'),
  267. 'wxp_qrcode'=>\config('site.system_wxp_qrcode'),
  268. 'service_mobile'=>\config('site.system_service_mobile'),
  269. 'service_work_time'=>\config('site.service_work_time'),
  270. 'system_sms_open'=>\config('site.system_sms_open')?true:false,
  271. 'disable_send_province'=>Area::whereIn('id',\config('site.disable_send_province')?:0)->field('id,name')->select(),
  272. 'system_index_tr'=>intval(config('site.system_index_tr')?:1),
  273. 'system_index_m1'=>intval(config('site.system_index_m1')?:1),
  274. ];
  275. $this->success('',$data);
  276. }
  277. }