Archives.php 9.3 KB

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