Notes.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\notes\model;
  11. use app\model\BaseModel;
  12. use extend\WxCrawler;
  13. /**
  14. * 笔记
  15. */
  16. class Notes extends BaseModel
  17. {
  18. //笔记类型
  19. private $note_type = [
  20. [ 'type' => 'shop_said', 'name' => '掌柜说' ],
  21. [ 'type' => 'goods_item', 'name' => '单品介绍' ],
  22. //['type' => 'article', 'name' => '种草文章'],
  23. //['type' => 'wechat_article', 'name' => '公众号文章'],
  24. //['type' => 'goods_video', 'name' => '短视频']
  25. ];
  26. /**
  27. * 获取笔记类型
  28. * @return array
  29. */
  30. public function getNoteType()
  31. {
  32. return $this->note_type;
  33. }
  34. /**
  35. * 添加笔记
  36. * @param $data
  37. * @return array
  38. */
  39. public function addNotes($data)
  40. {
  41. $data[ 'create_time' ] = time();
  42. model('notes')->startTrans();
  43. try {
  44. //添加笔记
  45. if ($data[ 'status' ] == 1) {
  46. $data[ 'release_time' ] = time();
  47. }
  48. model('notes')->add($data);
  49. //更新分组笔记数等信息
  50. model('notes_group')->setInc([ [ 'group_id', '=', $data[ 'group_id' ] ] ], 'notes_num');
  51. if ($data[ 'status' ] == 1) {
  52. model('notes_group')->setInc([ [ 'group_id', '=', $data[ 'group_id' ] ] ], 'release_num');
  53. }
  54. model('notes')->commit();
  55. return $this->success();
  56. } catch (\Exception $e) {
  57. model('notes')->rollback();
  58. return $this->error('', $e->getMessage());
  59. }
  60. }
  61. /**
  62. * 文章发布
  63. * @param $params
  64. */
  65. public function releaseEvent($params)
  66. {
  67. $site_id = $params[ 'site_id' ] ?? 0;
  68. $note_id = $params[ 'note_id' ];
  69. $status = $params[ 'status' ];
  70. $condition = array (
  71. [ 'note_id', '=', $note_id ],
  72. [ 'site_id', '=', $site_id ]
  73. );
  74. $info = model('notes')->getInfo($condition);
  75. if (!empty($info)) {
  76. if ($info[ 'status' ] != $status) {
  77. if ($status == 1) {
  78. $release_time = time();
  79. model('notes_group')->setInc([ [ 'group_id', '=', $info[ 'group_id' ] ] ], 'release_num');
  80. } else {
  81. $release_time = 0;
  82. model('notes_group')->setDec([ [ 'group_id', '=', $info[ 'group_id' ] ] ], 'release_num');
  83. }
  84. $data = array (
  85. 'release_time' => $release_time,
  86. 'status' => $status
  87. );
  88. model('notes')->update($data, $condition);
  89. }
  90. return $this->success();
  91. } else {
  92. return $this->error();
  93. }
  94. }
  95. /**
  96. * 编辑笔记
  97. * @param $data
  98. * @return array
  99. */
  100. public function editNotes($data)
  101. {
  102. $data[ 'update_time' ] = time();
  103. model('notes')->startTrans();
  104. try {
  105. $info = model('notes')->getInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'note_id', '=', $data[ 'note_id' ] ] ]);
  106. //添加笔记
  107. model('notes')->update($data, [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'note_id', '=', $data[ 'note_id' ] ] ]);
  108. $release_data = array (
  109. 'note_id' => $data[ 'note_id' ],
  110. 'site_id' => $data[ 'site_id' ]
  111. );
  112. if ($info[ 'group_id' ] != $data[ 'group_id' ]) {
  113. model('notes_group')->setDec([ [ 'group_id', '=', $info[ 'group_id' ] ] ], 'notes_num');
  114. model('notes_group')->setInc([ [ 'group_id', '=', $data[ 'group_id' ] ] ], 'notes_num');
  115. //减去原分组的发布数
  116. if ($info[ 'status' ] == 1) {
  117. model('notes_group')->setDec([ [ 'group_id', '=', $info[ 'group_id' ] ] ], 'release_num');
  118. }
  119. //增加新分组的发布数
  120. if ($data[ 'status' ] == 1) {
  121. model('notes_group')->setInc([ [ 'group_id', '=', $data[ 'group_id' ] ] ], 'release_num');
  122. }
  123. } else {
  124. $release_data[ 'status' ] = $data[ 'status' ];
  125. $this->releaseEvent($release_data);
  126. }
  127. //更新分组笔记数等信息
  128. if ($data[ 'status' ] == 1) {
  129. model('notes_group')->setInc([ [ 'group_id', '=', $data[ 'group_id' ] ] ], 'release_num');
  130. }
  131. model('notes')->commit();
  132. return $this->success();
  133. } catch (\Exception $e) {
  134. model('notes')->rollback();
  135. return $this->error('', $e->getMessage());
  136. }
  137. }
  138. /**
  139. * 删除笔记
  140. * @param $condition
  141. * @return array|\multitype
  142. */
  143. public function deleteNotes($condition)
  144. {
  145. //笔记数
  146. $notes_info = model('notes')->getInfo($condition, 'group_id,status');
  147. if (empty($notes_info)) {
  148. return $this->success('', '数据不合法');
  149. } else {
  150. model('notes')->startTrans();
  151. try {
  152. //删除笔记
  153. model('notes')->delete($condition);
  154. //更新分组笔记数等信息
  155. if ($notes_info[ 'status' ] == 1) {
  156. model('notes_group')->setDec([ [ 'group_id', '=', $notes_info[ 'group_id' ] ] ], 'notes_num');
  157. model('notes_group')->setDec([ [ 'group_id', '=', $notes_info[ 'group_id' ] ] ], 'release_num');
  158. } else {
  159. model('notes_group')->setDec([ [ 'group_id', '=', $notes_info[ 'group_id' ] ] ], 'notes_num');
  160. }
  161. model('notes')->commit();
  162. return $this->success();
  163. } catch (\Exception $e) {
  164. model('notes')->rollback();
  165. return $this->error('', $e->getMessage());
  166. }
  167. }
  168. }
  169. /**
  170. * 修改排序
  171. * @param int $sort
  172. * @param int $class_id
  173. */
  174. public function modifyNotesSort($sort, $note_id, $site_id)
  175. {
  176. $res = model('notes')->update([ 'sort' => $sort ], [ [ 'note_id', '=', $note_id ], [ 'site_id', '=', $site_id ] ]);
  177. return $this->success($res);
  178. }
  179. /**
  180. * 获取笔记信息
  181. * @param array $condition
  182. * @param string $field
  183. * @return array
  184. */
  185. public function getNotesInfo($condition = [], $field = '*')
  186. {
  187. $info = model("notes")->getInfo($condition, $field);
  188. return $this->success($info);
  189. }
  190. /**
  191. * 获取笔记信息
  192. * @param array $condition
  193. * @param string $field
  194. * @param int $type
  195. * @return array
  196. */
  197. public function getNotesDetailInfo($condition = [], $field = '*', $type = 1)
  198. {
  199. $info = model('notes')->getInfo($condition, $field);
  200. if (!empty($info)) {
  201. $goods_field = 'sku_id,goods_name,goods_stock,price,goods_image,goods_id';
  202. $goods_list = model('goods')->getList([ [ 'site_id', '=', $info[ 'site_id' ] ], [ 'goods_id', 'in', $info[ 'goods_ids' ] ], [ 'goods_state', '=', 1 ], [ 'is_delete', '=', 0 ] ], $goods_field);
  203. if (!empty($goods_list)) {
  204. foreach ($goods_list as $k => $v) {
  205. $goods_list[ $k ][ 'goods_stock' ] = numberFormat($goods_list[ $k ][ 'goods_stock' ]);
  206. }
  207. }
  208. $info[ 'goods_list' ] = $goods_list;
  209. }
  210. //添加浏览记录
  211. if ($type == 2) {
  212. model('notes')->setInc($condition, 'read_num', 1);
  213. }
  214. return $this->success($info);
  215. }
  216. /**
  217. * 获取笔记列表
  218. * @param array $condition
  219. * @param string $field
  220. * @param string $order
  221. * @param string $limit
  222. */
  223. public function getNotesList($condition = [], $field = '*', $order = '', $limit = null, $alias = '', $join = [])
  224. {
  225. $list = model('notes')->getList($condition, $field, $order, $alias, $join, '', $limit);
  226. return $this->success($list);
  227. }
  228. /**
  229. * 获取笔记分页列表
  230. * @param array $condition
  231. * @param number $page
  232. * @param string $page_size
  233. * @param string $order
  234. * @param string $field
  235. */
  236. public function getNotesPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'pn.sort asc')
  237. {
  238. $field = 'pn.*,png.group_name';
  239. $alias = 'pn';
  240. $join = [
  241. [
  242. 'notes_group png',
  243. 'png.group_id = pn.group_id',
  244. 'left'
  245. ]
  246. ];
  247. $note_type = $this->getNoteType();
  248. $note_type = array_column($note_type, 'name', 'type');
  249. $list = model('notes')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  250. foreach ($list[ 'list' ] as $k => $v) {
  251. $list[ 'list' ][ $k ][ 'note_type_name' ] = $note_type[ $v[ 'note_type' ] ];
  252. }
  253. return $this->success($list);
  254. }
  255. /**
  256. * 采集微信公众号的文章信息
  257. * @param $params
  258. */
  259. public function pullWechatArticle($params)
  260. {
  261. $url = $params[ 'url' ];
  262. $crawler = new WxCrawler();
  263. $data = $crawler->crawByUrl($url);
  264. // echo $data['data']['content_html'];exit();
  265. return $data;
  266. }
  267. }