Archives.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace addons\cms\controller\wxapp;
  3. use addons\cms\library\Order;
  4. use addons\cms\library\OrderException;
  5. use addons\cms\library\Service;
  6. use addons\cms\model\Archives as ArchivesModel;
  7. use addons\cms\model\Channel;
  8. use addons\cms\model\Comment;
  9. use addons\cms\model\Fields;
  10. use addons\cms\model\Modelx;
  11. use addons\epay\library\Collection;
  12. use addons\third\model\Third;
  13. use think\Exception;
  14. /**
  15. * 文档
  16. */
  17. class Archives extends Base
  18. {
  19. protected $noNeedLogin = ['index', 'detail'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. //检测ID是否加密
  24. $this->hashids();
  25. }
  26. /**
  27. * 读取文档列表
  28. */
  29. public function index()
  30. {
  31. $config = get_addon_config('cms');
  32. $params = [];
  33. $model = (int)$this->request->request('model');
  34. $channel = (int)$this->request->request('channel');
  35. $page = (int)$this->request->request('page');
  36. if ($model) {
  37. $params['model'] = $model;
  38. }
  39. if ($channel) {
  40. $params['channel'] = $channel;
  41. }
  42. $page = max(1, $page);
  43. $params['limit'] = ($page - 1) * 10 . ',10';
  44. $params['cache'] = 0;
  45. if ($channel) {
  46. $channelInfo = Channel::get($channel);
  47. if ($channelInfo) {
  48. $channelIds = Channel::where(function ($query) use ($channelInfo) {
  49. if ($channelInfo['listtype'] <= 2) {
  50. $query->whereOr("id", $channelInfo['id']);
  51. }
  52. if ($channelInfo['listtype'] == 1 || $channelInfo['listtype'] == 3) {
  53. $query->whereOr('id', 'in', function ($query) use ($channelInfo) {
  54. $query->name("cms_channel")->where('parent_id', $channelInfo['id'])->field("id");
  55. });
  56. }
  57. if ($channelInfo['listtype'] == 0 || $channelInfo['listtype'] == 4) {
  58. $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($channelInfo['id'], false);
  59. if ($childrenIds) {
  60. $query->whereOr('id', 'in', $childrenIds);
  61. }
  62. }
  63. })->column('id');
  64. $params['channel'] = $channelIds;
  65. }
  66. }
  67. $list = ArchivesModel::getArchivesList($params);
  68. foreach ($list as $index => $item) {
  69. if ($item->channel) {
  70. $item->channel->visible(explode(',', 'id,parent_id,name,image,diyname,items'));
  71. }
  72. $item->id = $config['archiveshashids'] ? $item->eid : $item->id;
  73. }
  74. $list = collection($list)->toArray();
  75. foreach ($list as $index => &$item) {
  76. $item['url'] = $item['fullurl'];
  77. //小程序只显示3张图
  78. $item['images_list'] = array_slice(array_filter(explode(',', $item['images'])), 0, 3);
  79. unset($item['imglink'], $item['textlink'], $item['channellink'], $item['taglist'], $item['weigh'], $item['status'], $item['deletetime'], $item['memo'], $item['img'], $item['admin_id']);
  80. }
  81. $this->success('', ['archivesList' => $list]);
  82. }
  83. /**
  84. * 文档详情
  85. */
  86. public function detail()
  87. {
  88. $action = $this->request->post("action");
  89. if ($action && $this->request->isPost()) {
  90. return $this->$action();
  91. }
  92. $diyname = $this->request->param('diyname');
  93. if ($diyname && !is_numeric($diyname)) {
  94. $archives = ArchivesModel::getByDiyname($diyname);
  95. } else {
  96. $id = $this->request->param('id', '');
  97. $archives = ArchivesModel::get($id);
  98. }
  99. if (!$archives || ($archives['status'] != 'normal' && (!$archives['user_id'] || $archives['user_id'] != $this->auth->id)) || $archives['deletetime']) {
  100. $this->error(__('No specified article found'));
  101. }
  102. if (!$this->auth->id && !$archives['isguest']) {
  103. $this->error(__('Please login first'));
  104. }
  105. $channel = Channel::get($archives['channel_id']);
  106. if (!$channel) {
  107. $this->error(__('No specified channel found'));
  108. }
  109. $model = Modelx::get($channel['model_id']);
  110. if (!$model) {
  111. $this->error(__('No specified model found'));
  112. }
  113. $archives->setInc("views", 1);
  114. $addon = db($model['table'])->where('id', $archives['id'])->find();
  115. if ($addon) {
  116. if ($model->fields) {
  117. Service::appendTextAndList('model', $model->id, $addon);
  118. }
  119. $archives->setData($addon);
  120. } else {
  121. $this->error(__('No specified article addon found'));
  122. }
  123. //小程序付费阅读将不可见
  124. $content = $archives->content;
  125. if ($archives->is_paid_part_of_content || $archives->ispaid) {
  126. $value = $archives->getData('content');
  127. $pattern = '/<paid>(.*?)<\/paid>/is';
  128. if (preg_match($pattern, $value) && !$archives->ispaid) {
  129. $value = preg_replace($pattern, "<div class='alert alert-warning' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>付费内容已经隐藏,请付费后查看</div>", $value);
  130. }
  131. $content = $value;
  132. } else {
  133. if (!$archives->ispaid) {
  134. if (isset($channel['vip']) && $channel['vip'] > $this->auth->vip) {
  135. $paytips = "此文章为付费文章,需要VIP {$channel['vip']}" . ($archives->price > 0 ? "或支付¥{$archives->price}元" : "") . "才能查看";
  136. } else {
  137. $paytips = "此文章为付费文章,需要支付¥{$archives->price}元才能查看";
  138. }
  139. $content = "<div class='alert alert-warning alert-paid' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>{$paytips}</div>";
  140. }
  141. }
  142. if (isset($archives['downloadurl'])) {
  143. //$archives['downloadurl'] = is_array($archives['downloadurl']) ? $archives['downloadurl'] : (array)json_decode($archives['downloadurl'], true);
  144. $archives['downloadurl'] = $archives['downloadurl_list'];
  145. unset($archives['downloadurl_text']);
  146. }
  147. if (!$archives->ispaid && isset($archives['downloadurl'])) {
  148. $archives['downloadurl'] = [];
  149. }
  150. //小程序不支持内容页分页
  151. $content = str_replace("##pagebreak##", "<br>", $content);
  152. $archives->content = $content;
  153. $archives->id = $archives->eid;
  154. $archives->hidden(['admin_id', 'deletetime', 'status', 'memo', 'weigh', 'special_ids', 'channel']);
  155. $commentList = Comment::getCommentList(['aid' => $archives['id']]);
  156. $commentList = $commentList->getCollection();
  157. foreach ($commentList as $index => &$item) {
  158. if ($item->user) {
  159. $item->user->avatar = cdnurl($item->user->avatar, true);
  160. $item->user->visible(explode(',', 'id,nickname,avatar,bio'));
  161. }
  162. $item->hidden(['ip', 'useragent', 'deletetime', 'aid', 'subscribe', 'status', 'type', 'updatetime']);
  163. }
  164. $this->request->token();
  165. $channel = $channel->toArray();
  166. $channel['url'] = $channel['fullurl'];
  167. unset($channel['channeltpl'], $channel['listtpl'], $channel['showtpl'], $channel['status'], $channel['weigh'], $channel['parent_id']);
  168. $this->success('', ['archivesInfo' => $archives, 'channelInfo' => $channel, 'commentList' => $commentList]);
  169. }
  170. /**
  171. * 赞与踩
  172. */
  173. public function vote()
  174. {
  175. $id = (int)$this->request->post("id");
  176. $type = trim($this->request->post("type", ""));
  177. if (!$id || !$type) {
  178. $this->error(__('Operation failed'));
  179. }
  180. $archives = ArchivesModel::get($id);
  181. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  182. $this->error(__('No specified article found'));
  183. }
  184. $archives->where('id', $id)->setInc($type === 'like' ? 'likes' : 'dislikes', 1);
  185. $archives = ArchivesModel::get($id);
  186. $this->success(__('Operation completed'), ['likes' => $archives->likes, 'dislikes' => $archives->dislikes, 'likeratio' => $archives->likeratio]);
  187. }
  188. /**
  189. * 提交订单
  190. */
  191. public function order()
  192. {
  193. $id = $this->request->post('id/d');
  194. $third = Third::where('platform', 'wechat')->where('apptype', 'miniapp')->where('user_id', $this->auth->id)->find();
  195. if (!$third) {
  196. $this->error("未找到登录用户信息");
  197. }
  198. $openid = $third['openid'];
  199. $archives = \addons\cms\model\Archives::get($id);
  200. if (!$archives) {
  201. $this->error("文档未找到");
  202. }
  203. //优先使用余额的方式发起支付
  204. $paid = false;
  205. try {
  206. $response = Order::submit($id, 'balance');
  207. } catch (OrderException $e) {
  208. if ($e->getCode() == 1) {
  209. $paid = true;
  210. }
  211. } catch (Exception $e) {
  212. $this->error($e->getMessage());
  213. }
  214. if ($paid) {
  215. $this->success("余额支付成功", null);
  216. }
  217. //以微信小程序应用内支付的方式发起支付
  218. try {
  219. $response = Order::submit($id, 'wechat', 'miniapp', $openid);
  220. } catch (Exception $e) {
  221. $this->error($e->getMessage());
  222. }
  223. //如果是Collection则需要转换为数组
  224. $data = $response instanceof Collection ? $response->toArray() : (string)$response;
  225. $this->success("请求成功", $data);
  226. return;
  227. }
  228. }