Common.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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','about'];
  21. protected $noNeedRight="*";
  22. /**
  23. * 获取各种协议
  24. * @param int id 1用户协议2隐私政策
  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=answer[exp],description="购物体验")
  117. * @ApiParams (name=answer[speed],description="响应速度")
  118. * @ApiParams (name=answer[design],description="设计符合购物习惯吗")
  119. * @ApiParams (name=answer[kf],description="服务态度满意吗")
  120. * @ApiParams (name=feed,description="其他意见")
  121. */
  122. public function feedback(){
  123. $data=input();
  124. $this->validate($data,[
  125. 'answer|意见'=>['require','array'],
  126. 'answer.exp|购物体验'=>['require'],
  127. 'answer.speed|响应速度'=>['require'],
  128. 'answer.design|设计符合购物习惯吗'=>['require'],
  129. 'answer.kf|服务态度满意吗'=>['require'],
  130. 'feed|其他意见'=>['require'],
  131. ]);
  132. $feedback=new Feedback();
  133. $feedback['user_id']=$this->auth->id;
  134. $feedback['content']=[
  135. 'answer'=>$data['answer'],
  136. 'feed'=>$data['feed'],
  137. ];
  138. $feedback->save();
  139. $this->success('',$feedback);
  140. }
  141. /**
  142. * 获取地区信息
  143. * @ApiParams (name=level,description=1省2市3区)
  144. * @ApiParams (name=pid,description=上级区域ID)
  145. */
  146. public function area(){
  147. $data=$this->_validate([
  148. 'level'=>['require'],
  149. 'pid'=>['require'],
  150. ]);
  151. $this->success(
  152. '',
  153. Area::where('level',$data['level'])
  154. ->where('pid',$data['pid'])
  155. ->order('pinyin')
  156. ->select()
  157. );
  158. }
  159. /**
  160. * 获取地区信息(按字母排序)
  161. */
  162. public function area_sort(){
  163. $citys=Area::area()->field(['id','name','first'])->select();
  164. $a=[];
  165. for ($i=65;$i<91;$i++) {
  166. $one=[
  167. 'index'=>chr($i),
  168. 'child'=>[]
  169. ];
  170. foreach ($citys as $key=>&$city) {
  171. if($city['first']==$one['index']){
  172. $one['child'][]=[
  173. 'title'=>$city['name'],
  174. 'weight'=>$city['id'],
  175. 'id' =>$city['id'],
  176. ];
  177. unset($citys[$key]);
  178. }
  179. }
  180. $a[]=$one;
  181. }
  182. $this->success('',$a);
  183. }
  184. /**
  185. * 地区递归三级联动
  186. */
  187. public function area_tree(){
  188. function area(&$data,$areas,$first=false){
  189. foreach ($areas as $k=>$area){
  190. if($first){
  191. if($area['pid']==0) {
  192. area($area, $areas);
  193. unset($area['pid']);
  194. $data[] = $area;
  195. unset($areas[$k]);
  196. }
  197. }else{
  198. if($data['value']==$area['pid']){
  199. area($area, $areas);
  200. unset($area['pid']);
  201. $data['children'][]=$area;
  202. unset($areas[$k]);
  203. }
  204. }
  205. }
  206. }
  207. $fromCache=Cache::get('app_area');
  208. if(!$fromCache){
  209. $fina=[];
  210. $areas=Area::where('level','<=',3)->field('id as value,name as label,pid')->select()->toArray();
  211. area($fina,$areas,true);
  212. $fromCache=$fina;
  213. Cache::remember('app_area',$fina,0);
  214. }
  215. $this->success('',$fromCache);
  216. }
  217. /**
  218. * 根据名称获取信息
  219. * @ApiParams (name="name",description="城市名")
  220. * @ApiReturnParams (name="id",description="城市ID")
  221. */
  222. public function get_area(){
  223. $rs=null;
  224. if($name=input('name')){
  225. $cacheName="area_name_".$name;
  226. $rs=Cache::get($cacheName,null);
  227. if(!$rs){
  228. $rs=Area::where('name|shortname',$name)->find();
  229. Cache::set($cacheName,$rs);
  230. }
  231. }
  232. $this->success('',$rs);
  233. }
  234. /**
  235. * 获取系统配置
  236. * @ApiReturnParams (name="site_name",description="站点名称")
  237. * @ApiReturnParams (name="com_name",description="公司名称")
  238. * @ApiReturnParams (name="beian",description="备案号")
  239. * @ApiReturnParams (name="telephone",description="座机号")
  240. * @ApiReturnParams (name="company_address",description="公司地址")
  241. * @ApiReturnParams (name="service_time",description="服务时间")
  242. * @ApiReturnParams (name="mobile_kp",description="手机屏酷码")
  243. * @ApiReturnParams (name="footer_left_logo",description="左下角logo")
  244. * @ApiReturnParams (name="cloud_link",description="云平台链接")
  245. */
  246. public function config(){
  247. $data=[
  248. 'site_name'=>\config('site.name'),
  249. 'com_name'=>\config('site.base_company_name'),
  250. 'beian'=>\config('site.beian'),
  251. 'telephone'=>\config('site.telephone'),
  252. 'company_address'=>\config('site.company_address'),
  253. 'service_time'=>\config('site.service_time'),
  254. 'mobile_kp'=>\config('site.mobile_kp'),
  255. 'footer_left_logo'=>\config('site.footer_left_logo'),
  256. 'cloud_link'=>\config('site.cloud_link'),
  257. ];
  258. $this->success('',$data);
  259. }
  260. /**
  261. * 关于酷屏
  262. * @ApiReturnParams(name=content,type=string,description=内容)
  263. */
  264. public function about(){
  265. $content=Db('AboutScreen')->where('id',1)->value('content','');
  266. $this->success('',$content);
  267. }
  268. }