説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Qiniuyun.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-04-23
  12. */
  13. namespace weapp\Qiniuyun\controller;
  14. use think\Page;
  15. use think\Db;
  16. use app\common\controller\Weapp;
  17. use weapp\Qiniuyun\model\QiniuyunModel;
  18. /**
  19. * 插件的控制器
  20. */
  21. class Qiniuyun extends Weapp
  22. {
  23. /**
  24. * 实例化模型
  25. */
  26. private $model;
  27. /**
  28. * 实例化对象
  29. */
  30. private $Weapp_db;
  31. // 构造方法
  32. public function __construct(){
  33. parent::__construct();
  34. $this->model = new QiniuyunModel;
  35. $this->Weapp_db = Db::name('weapp');
  36. /*插件基本信息*/
  37. $this->weappInfo = $this->getWeappInfo();
  38. $this->assign('weappInfo', $this->weappInfo);
  39. /*--end*/
  40. }
  41. /**
  42. * 插件安装的前置操作
  43. * @return [type] [description]
  44. */
  45. public function beforeInstall()
  46. {
  47. $result = Db::name('weapp')->where([
  48. 'code' => ['IN', ['AliyunOss', 'Cos']],
  49. 'status' => ['gt', 0],
  50. ])->find();
  51. if (!empty($result)) {
  52. $this->error("已安装【{$result['name']}】插件,两者不能同时使用!");
  53. }
  54. }
  55. //插件安装后置操作
  56. public function afterInstall()
  57. {
  58. $this->change_channel_switch();
  59. }
  60. //插件启用的后置操作
  61. public function afterEnable()
  62. {
  63. $this->change_channel_switch();
  64. }
  65. public function change_channel_switch()
  66. {
  67. $channeltype = Db::name('channeltype')->where('nid','in',['media','download'])->field('nid,data')->select();
  68. if (!empty($channeltype)){
  69. foreach ($channeltype as $val){
  70. if (!empty($val['data'])){
  71. $val['data'] = json_decode($val['data'],true);
  72. $val['data']['qiniuyun_open'] = 1;
  73. if (isset($val['data']['oss_open'])){$val['data']['oss_open'] = 0;}
  74. if (isset($val['data']['cos_open'])){$val['data']['cos_open'] = 0;}
  75. $val['data'] = json_encode($val['data']);
  76. Db::name('channeltype')->where('nid',$val['nid'])->update(['data'=>$val['data']]);
  77. }
  78. }
  79. }
  80. }
  81. //插件卸载后置操作
  82. public function afterUninstall()
  83. {
  84. $channeltype = Db::name('channeltype')->where('nid','in',['media','download'])->field('nid,data')->select();
  85. if (!empty($channeltype)){
  86. foreach ($channeltype as $val){
  87. if (!empty($val['data'])){
  88. $val['data'] = json_decode($val['data'],true);
  89. if (isset($val['data']['qiniuyun_open'])){$val['data']['qiniuyun_open'] = 0;}
  90. $val['data'] = json_encode($val['data']);
  91. Db::name('channeltype')->where('nid',$val['nid'])->update(['data'=>$val['data']]);
  92. }
  93. }
  94. }
  95. }
  96. // 插件使用指南
  97. public function doc(){
  98. return $this->fetch('doc');
  99. }
  100. // 插件后台管理
  101. public function index()
  102. {
  103. if (IS_POST) {
  104. $post = input('post.');
  105. if (!empty($post)) {
  106. // 判断提交的数据是否为空
  107. if (empty($post['access_key'])) {
  108. $this->error("AccessKey不可为空!");
  109. }
  110. if (empty($post['secret_key'])) {
  111. $this->error("SecretKey不可为空!");
  112. }
  113. // 查询七牛云插件配置信息
  114. $data = $this->Weapp_db->where('code','Qiniuyun')->field('data')->find();
  115. if (empty($data['data'])) {
  116. // data为空则表示第一次添加插件配置,自动生成一次存储空间
  117. $post['bucket'] = 'eyou_qiniuyun';
  118. $IsCreate = $this->model->createBucket($post);
  119. if (!empty($IsCreate['code'])) {
  120. // 创建成功后,拉取对应存储空间下的域名列表,降序排序自动选中第一个,用于存入数据库
  121. $Domain = $this->model->listBucketDomain($post);
  122. if (!empty($Domain)) {
  123. rsort($Domain);
  124. $post['domain'] = $Domain['0'];
  125. }
  126. }else{
  127. $this->error($IsCreate['msg']);
  128. }
  129. }else{
  130. // data不为空则表示修改插件配置
  131. // 查询存储空间列表,判断AccessKey或SecretKey配置是否正确
  132. $ResultList = $this->model->listBucket($post);
  133. if ('false' == $ResultList) {
  134. $this->error("错误代码:102,AccessKey或SecretKey配置有误,请检查!");
  135. }
  136. if (empty($ResultList)) {
  137. // 为空表示配置正确但七牛云上的存储空间已被删除,自动生成一次存储空间
  138. $post['bucket'] = 'eyou_qiniuyun';
  139. $IsCreate = $this->model->createBucket($post);
  140. if (!empty($IsCreate['code'])) {
  141. // 创建成功后,拉取对应存储空间下的域名列表,降序排序自动选中第一个,用于存入数据库
  142. $Domain = $this->model->listBucketDomain($post);
  143. if (!empty($Domain)) {
  144. rsort($Domain);
  145. $post['domain'] = $Domain['0'];
  146. }
  147. }else{
  148. $this->error($IsCreate['msg']);
  149. }
  150. }else{
  151. if (isset($post['is_bucket']) && !empty($post['is_bucket']) && empty($post['bucket'])) {
  152. $this->error("存储空间名不可为空!");
  153. } else if (empty($post['bucket'])) {
  154. $post['bucket'] = $ResultList[0];
  155. }
  156. if (isset($post['is_domain']) && !empty($post['is_domain']) && empty($post['domain'])) {
  157. $this->error("访问域名不可为空!");
  158. }
  159. }
  160. }
  161. // 更新七牛云插件配置信息
  162. $data = [
  163. 'data' => json_encode($post),
  164. 'update_time' => getTime(),
  165. ];
  166. $IsResult = $this->Weapp_db->where('code','Qiniuyun')->update($data);
  167. if (!empty($IsResult)) {
  168. $this->success("操作成功!");
  169. }else{
  170. $this->error("操作失败!");
  171. }
  172. }
  173. }
  174. // 查询插件配置信息
  175. $data = $this->Weapp_db->where('code','Qiniuyun')->field('data')->find();
  176. $Qiniuyun = json_decode($data['data'], true);
  177. $this->assign('Qiniuyun', $Qiniuyun);
  178. // 查询七牛云存储空间名称列表
  179. $ListBucket = $this->model->listBucket($Qiniuyun);
  180. $this->assign('ListBucket', $ListBucket);
  181. // 查询七牛云存储空间域名列表
  182. $ListDomain = $this->model->listBucketDomain($Qiniuyun);
  183. $this->assign('ListDomain', $ListDomain);
  184. // 系统v1.4.9+以上才支持
  185. $version = getVersion();
  186. $this->assign('version', $version);
  187. return $this->fetch('index');
  188. }
  189. // 查询选中的存储空间名对应的访问域名
  190. public function select_domain()
  191. {
  192. if (IS_AJAX_POST) {
  193. $post = input('post.');
  194. // 查询插件配置信息
  195. $data = $this->Weapp_db->where('code','Qiniuyun')->field('data')->find();
  196. $Qiniuyun = json_decode($data['data'], true);
  197. // 查询域名
  198. $Domain = $this->model->listBucketDomain($post);
  199. // 降序排序
  200. rsort($Domain);
  201. // 拼装数据
  202. if (!empty($Domain)) {
  203. foreach($Domain as $value){
  204. if ($value == $Qiniuyun['domain']) {
  205. $html .= "<option value='{$value}' selected>{$value}</option>";
  206. }else{
  207. $html .= "<option value='{$value}'>{$value}</option>";
  208. }
  209. }
  210. $this->success("查询正确",'',$html);
  211. }else{
  212. $this->error("错误代码:104,存储空间访问域名可能已过期!请检查!");
  213. }
  214. }
  215. }
  216. }