Album.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\upload;
  13. use think\facade\Cache;
  14. use app\model\BaseModel;
  15. /**
  16. * 相册组件模型
  17. */
  18. class Album extends BaseModel
  19. {
  20. /*******************************************************************相册编辑查询 start*****************************************************/
  21. /**
  22. * 创建相册
  23. * @param $data
  24. */
  25. public function addAlbum($data){
  26. $site_id = isset($data['site_id']) ? $data['site_id'] : '';
  27. if ($site_id === '') {
  28. return $this->error('', 'REQUEST_SITE_ID');
  29. }
  30. $data["update_time"] = time();
  31. Cache::tag("album_" . $site_id)->clear();
  32. $res = model("album")->add($data);
  33. if ($res === false) {
  34. return $this->error('', 'UNKNOW_ERROR');
  35. }
  36. return $this->success($res);
  37. }
  38. /**
  39. * 编辑相册
  40. * @param $data
  41. * @param $condition
  42. */
  43. public function editAlbum($data, $condition){
  44. $check_condition = array_column($condition, 2, 0);
  45. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  46. if ($site_id === '') {
  47. return $this->error('', 'REQUEST_SITE_ID');
  48. }
  49. $data["update_time"] = time();
  50. Cache::tag("album_" . $site_id)->clear();
  51. $res = model("album")->update($data, $condition);
  52. if ($res === false) {
  53. return $this->error('', 'UNKNOW_ERROR');
  54. }
  55. return $this->success($res);
  56. }
  57. /**
  58. * 删除相册
  59. * @param array $condition
  60. * @return multitype:string mixed
  61. */
  62. public function deleteAlbum($condition)
  63. {
  64. $check_condition = array_column($condition, 2, 0);
  65. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  66. if ($site_id === '')
  67. return $this->error('', 'REQUEST_SITE_ID');
  68. //判断当前相册是否存在默认, 默认相册不可删除
  69. $temp_count = model("album_pic")->getCount($condition, "*");
  70. if($temp_count > 0)
  71. return $this->error("", "当前删除相册中存在图片,不可删除!");
  72. $temp_condition = $condition;
  73. $temp_condition[] = ["is_default", "=", 1];
  74. $temp_count = model('album')->getCount($temp_condition, "album_id");
  75. if($temp_count > 0)
  76. return $this->error('', '当前删除相册中存在默认相册,默认相册不可删除!');
  77. Cache::tag("album_" . $site_id)->clear();
  78. $res = model('album')->delete($condition);
  79. if ($res === false) {
  80. return $this->error('', 'UNKNOW_ERROR');
  81. }
  82. return $this->success($res);
  83. }
  84. /**
  85. * 设置默认相册
  86. * @param $status
  87. * @param $condition
  88. */
  89. public function modifyAlbumDefault($condition){
  90. $check_condition = array_column($condition, 2, 0);
  91. $album_id = isset($check_condition['album_id']) ? $check_condition['album_id'] : '';
  92. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  93. if ($site_id === '') {
  94. return $this->error('', 'REQUEST_SITE_ID');
  95. }
  96. if ($album_id === '') {
  97. return $this->error('', 'REQUEST_SITE_ID');
  98. }
  99. //先将所有本站点的相册都设为非默认(一个站点只能有一个默认相册)
  100. $temp_condition = array(
  101. ["site_id", "=", $site_id],
  102. );
  103. Cache::tag("album_" . $site_id)->clear();
  104. $res = model('user')->update(["is_default" => 0, "update_time" => time()], $temp_condition);
  105. if ($res === false) {
  106. return $this->error('', 'UNKNOW_ERROR');
  107. }
  108. //将本相册设置为默认相册
  109. $data = array(
  110. "is_default" => 1,
  111. "update_time" => time()
  112. );
  113. $res = model('album')->update($data, $condition);
  114. if ($res === false) {
  115. return $this->error('', 'UNKNOW_ERROR');
  116. }
  117. return $this->success($res);
  118. }
  119. /**
  120. * 获取相册信息
  121. * @param $condition
  122. * @param string $field
  123. * @return \multitype
  124. */
  125. public function getAlbumInfo($condition, $field = "album_id, site_id, album_name, sort, cover, desc, is_default, update_time, num"){
  126. $check_condition = array_column($condition, 2, 0);
  127. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  128. if ($site_id === '') {
  129. return $this->error('', 'REQUEST_SITE_ID');
  130. }
  131. $data = json_encode([ $condition, $field ]);
  132. $cache = Cache::get("ablum_getAlbumInfo_" . $site_id .'_' . $data);
  133. if (!empty($cache)) {
  134. return $this->success($cache);
  135. }
  136. $info = model('album')->getInfo($condition, $field);
  137. Cache::tag("album_" . $site_id)->set("ablum_getAlbumInfo_" . $site_id . '_'. $data, $info);
  138. return $this->success($info);
  139. }
  140. /**
  141. * 获取相册列表
  142. * @param array $condition
  143. * @param string $field
  144. * @param string $order
  145. * @param string $limit
  146. * @return multitype:string mixed
  147. */
  148. public function getAlbumList($condition = [], $field = "album_id, site_id, album_name, sort, cover, desc, is_default, update_time, num", $order = '', $limit = null)
  149. {
  150. $check_condition = array_column($condition, 2, 0);
  151. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  152. if ($site_id === '') {
  153. return $this->error('', 'REQUEST_SITE_ID');
  154. }
  155. $data = json_encode([ $condition, $field, $order, $limit ]);
  156. $cache = Cache::get("ablum_getAlbumList_" . $site_id . '_' . $data);
  157. if (!empty($cache)) {
  158. return $this->success($cache);
  159. }
  160. $list = model('album')->getList($condition, $field, $order, '', '', '', $limit);
  161. Cache::tag("album_" . $site_id)->set("ablum_getAlbumList_" . $site_id . '_' . '_' . $data, $list);
  162. return $this->success($list);
  163. }
  164. /**
  165. * 获取会员分页列表
  166. *
  167. * @param array $condition
  168. * @param number $page
  169. * @param string $page_size
  170. * @param string $order
  171. * @param string $field
  172. * @return multitype:string mixed
  173. */
  174. public function getAlbumPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'album_id, site_id, album_name, sort, cover, desc, is_default, update_time, num')
  175. {
  176. $check_condition = array_column($condition, 2, 0);
  177. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  178. if ($site_id === '') {
  179. return $this->error('', 'REQUEST_SITE_ID');
  180. }
  181. $data = json_encode([ $condition, $page, $page_size, $order, $field ]);
  182. $cache = Cache::get("album_getAlbumPageList_" . $site_id . '_' . $data);
  183. if (!empty($cache)) {
  184. return $this->success($cache);
  185. }
  186. $list = model('album')->pageList($condition, $field, $order, $page, $page_size);
  187. Cache::tag("album_" . $site_id )->set("album_getAlbumPageList_" . $site_id . '_' . '_' . $data, $list);
  188. return $this->success($list);
  189. }
  190. /**
  191. * 同步修改相册下的图片数量
  192. * @param unknown $condition
  193. */
  194. public function syncAlbumNum($album_id)
  195. {
  196. $count = model("album_pic")->getCount([["album_id", "=", $album_id]], "*");//获取本商品分组下的图片数量
  197. $data = array(
  198. "num" => $count
  199. );
  200. $res = model("album")->update($data, [["album_id", "=", $album_id]]);
  201. return $this->success($res);
  202. }
  203. /*******************************************************************相册编辑查询 end*****************************************************/
  204. /*******************************************************************相册图片编辑查询 start*****************************************************/
  205. /**
  206. * 添加相册图片
  207. * @param $data
  208. */
  209. public function addAlbumPic($data){
  210. $site_id = isset($data['site_id']) ? $data['site_id'] : '';
  211. if ($site_id === '') {
  212. return $this->error('', 'REQUEST_SITE_ID');
  213. }
  214. $data["update_time"] = time();
  215. Cache::tag("album_pic_" . $site_id)->clear();
  216. $res = model("album_pic")->add($data);
  217. $this->syncAlbumNum($data["album_id"]);//同步当前相册下的图片数量
  218. if ($res === false) {
  219. return $this->error('', 'UNKNOW_ERROR');
  220. }
  221. return $this->success($res);
  222. }
  223. /**
  224. * 编辑相册图片
  225. * @param $data
  226. * @param $condition
  227. */
  228. public function editAlbumPic($data, $condition){
  229. $check_condition = array_column($condition, 2, 0);
  230. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  231. if ($site_id === '') {
  232. return $this->error('', 'REQUEST_SITE_ID');
  233. }
  234. $data["update_time"] = time();
  235. Cache::tag("album_pic_" . $site_id)->clear();
  236. $res = model("album_pic")->update($data, $condition);
  237. $this->syncAlbumNum($check_condition["album_id"]);//同步当前相册下的图片数量
  238. if ($res === false) {
  239. return $this->error('', 'UNKNOW_ERROR');
  240. }
  241. return $this->success($res);
  242. }
  243. /**
  244. * 删除相册图片
  245. * @param array $condition
  246. * @return multitype:string mixed
  247. */
  248. public function deleteAlbumPic($condition)
  249. {
  250. $check_condition = array_column($condition, 2, 0);
  251. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  252. if ($site_id === '')
  253. return $this->error('', 'REQUEST_SITE_ID');
  254. Cache::tag("album_pic_" . $site_id)->clear();
  255. $res = model('album_pic')->delete($condition);
  256. $this->syncAlbumNum($check_condition["album_id"]);//同步当前相册下的图片数量
  257. if ($res === false) {
  258. return $this->error('', 'UNKNOW_ERROR');
  259. }
  260. return $this->success($res);
  261. }
  262. /**
  263. * 编辑图片所在分组
  264. * @param $album_id
  265. * @param $condition
  266. */
  267. public function modifyAlbumPicAlbum($album_id, $condition){
  268. $check_condition = array_column($condition, 2, 0);
  269. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  270. if ($site_id === '')
  271. return $this->error('', 'REQUEST_SITE_ID');
  272. $info = model("album_pic")->getInfo($condition);
  273. $original_album_id = $info["album_id"];
  274. if($original_album_id == $album_id){
  275. return $this->success();
  276. }
  277. Cache::tag("album_pic_" . $site_id)->clear();
  278. Cache::tag("album_" . $site_id)->clear();
  279. $res = model("album_pic")->update(["album_id" => $album_id], $condition);//切换图片所在分组
  280. $this->syncAlbumNum($album_id);//同步当前相册下的图片数量
  281. $this->syncAlbumNum($original_album_id);//同步当前相册下的图片数量
  282. if ($res === false)
  283. return $this->error('', 'UNKNOW_ERROR');
  284. return $this->success($res);
  285. }
  286. /**
  287. * 获取相册图片信息
  288. * @param $condition
  289. * @param string $field
  290. * @return \multitype
  291. */
  292. public function getAlbumPicInfo($condition, $field = "pic_id, pic_name, pic_path, pic_spec, site_id, update_time"){
  293. $check_condition = array_column($condition, 2, 0);
  294. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  295. if ($site_id === '') {
  296. return $this->error('', 'REQUEST_SITE_ID');
  297. }
  298. $data = json_encode([ $condition, $field ]);
  299. $cache = Cache::get("album_pic_getAlbumPicInfo_" . $site_id .'_' . $data);
  300. if (!empty($cache)) {
  301. return $this->success($cache);
  302. }
  303. $info = model('album_pic')->getInfo($condition, $field);
  304. Cache::tag("album_pic_" . $site_id)->set("album_pic_getAlbumPicInfo_" . $site_id . '_'. $data, $info);
  305. return $this->success($info);
  306. }
  307. /**
  308. * 获取相册图片列表
  309. * @param array $condition
  310. * @param string $field
  311. * @param string $order
  312. * @param string $limit
  313. * @return multitype:string mixed
  314. */
  315. public function getAlbumPicList($condition = [], $field = "pic_id, pic_name, pic_path, pic_spec, site_id, update_time", $order = '', $limit = null)
  316. {
  317. $check_condition = array_column($condition, 2, 0);
  318. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  319. if ($site_id === '') {
  320. return $this->error('', 'REQUEST_SITE_ID');
  321. }
  322. $data = json_encode([ $condition, $field, $order, $limit ]);
  323. $cache = Cache::get("album_pic_getAlbumPicList_" . $site_id . '_' . $data);
  324. if (!empty($cache)) {
  325. return $this->success($cache);
  326. }
  327. $list = model('album_pic')->getList($condition, $field, $order, '', '', '', $limit);
  328. Cache::tag("album_pic_" . $site_id)->set("album_pic_getAlbumPicList_" . $site_id . '_' . '_' . $data, $list);
  329. return $this->success($list);
  330. }
  331. /**
  332. * 获取相册图片分页列表
  333. *
  334. * @param array $condition
  335. * @param number $page
  336. * @param string $page_size
  337. * @param string $order
  338. * @param string $field
  339. * @return multitype:string mixed
  340. */
  341. public function getAlbumPicPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'pic_id, pic_name, pic_path, pic_spec, site_id, update_time')
  342. {
  343. $check_condition = array_column($condition, 2, 0);
  344. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  345. if ($site_id === '') {
  346. return $this->error('', 'REQUEST_SITE_ID');
  347. }
  348. $data = json_encode([ $condition, $page, $page_size, $order, $field ]);
  349. $cache = Cache::get("album_pic_getAlbumPicPageList_" . $site_id . '_' . $data);
  350. if (!empty($cache)) {
  351. return $this->success($cache);
  352. }
  353. $list = model('album_pic')->pageList($condition, $field, $order, $page, $page_size);
  354. Cache::tag("album_pic_" . $site_id )->set("album_pic_getAlbumPicPageList_" . $site_id . '_' . '_' . $data, $list);
  355. return $this->success($list);
  356. }
  357. /*******************************************************************相册图片 end*****************************************************/
  358. }