Archives.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. //小程序不支持内容页分页
  136. $content = str_replace("##pagebreak##", "<br>", $content);
  137. $archives->content = $content;
  138. $commentList = Comment::getCommentList(['aid' => $archives['id']]);
  139. $commentList = $commentList->getCollection();
  140. foreach ($commentList as $index => &$item) {
  141. if ($item->user) {
  142. $item->user->avatar = cdnurl($item->user->avatar, true);
  143. $item->user->visible(explode(',', 'id,nickname,avatar,bio'));
  144. }
  145. $item->hidden(['ip', 'useragent', 'deletetime', 'aid', 'subscribe', 'status', 'type', 'updatetime']);
  146. }
  147. $this->request->token();
  148. $channel = $channel->toArray();
  149. $channel['url'] = $channel['fullurl'];
  150. unset($channel['channeltpl'], $channel['listtpl'], $channel['showtpl'], $channel['status'], $channel['weigh'], $channel['parent_id']);
  151. $this->success('', ['archivesInfo' => $archives, 'channelInfo' => $channel, 'commentList' => $commentList]);
  152. }
  153. /**
  154. * 赞与踩
  155. */
  156. public function vote()
  157. {
  158. $id = (int)$this->request->post("id");
  159. $type = trim($this->request->post("type", ""));
  160. if (!$id || !$type) {
  161. $this->error(__('Operation failed'));
  162. }
  163. $archives = ArchivesModel::get($id);
  164. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  165. $this->error(__('No specified article found'));
  166. }
  167. $archives->where('id', $id)->setInc($type === 'like' ? 'likes' : 'dislikes', 1);
  168. $archives = ArchivesModel::get($id);
  169. $this->success(__('Operation completed'), ['likes' => $archives->likes, 'dislikes' => $archives->dislikes, 'likeratio' => $archives->likeratio]);
  170. }
  171. /**
  172. * 提交订单
  173. */
  174. public function order()
  175. {
  176. $id = $this->request->post('id/d');
  177. $third = Third::where('platform', 'wechat')->where('apptype', 'miniapp')->where('user_id', $this->auth->id)->find();
  178. if (!$third) {
  179. $this->error("未找到登录用户信息");
  180. }
  181. $openid = $third['openid'];
  182. $archives = \addons\cms\model\Archives::get($id);
  183. if (!$archives) {
  184. $this->error("文档未找到");
  185. }
  186. //优先使用余额的方式发起支付
  187. try {
  188. $response = Order::submit($id, 'balance');
  189. } catch (OrderException $e) {
  190. if ($e->getCode() == 1) {
  191. $this->success($e->getMessage(), null);
  192. } else {
  193. //
  194. }
  195. } catch (Exception $e) {
  196. $this->error($e->getMessage());
  197. }
  198. //以微信小程序应用内支付的方式发起支付
  199. try {
  200. $response = Order::submit($id, 'wechat', 'miniapp', $openid);
  201. } catch (Exception $e) {
  202. $this->error($e->getMessage());
  203. }
  204. //如果是Collection则需要转换为数组
  205. $data = $response instanceof Collection ? $response->toArray() : (string)$response;
  206. $this->success("请求成功", $data);
  207. return;
  208. }
  209. }