Material.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\shop\controller;
  11. use addon\wechat\model\Material as MaterialModel;
  12. use addon\wechat\model\Wechat as WechatModel;
  13. /**
  14. * 微信素材控制器
  15. */
  16. class Material extends BaseWechat
  17. {
  18. /**
  19. * 素材列表--图文消息
  20. */
  21. public function lists()
  22. {
  23. if (request()->isAjax()) {
  24. $type = input('type', '');
  25. $name = input('name', '');
  26. $page_index = input('page', 1);
  27. $page_size = input('limit', 9);
  28. if (!empty($type)) {
  29. $condition[] = ['type', "=", $type];
  30. }
  31. if (!empty($name)) {
  32. $condition[] = array(
  33. 'value', 'like', '%"name":"%' . $name . '%","url"%'
  34. );
  35. }
  36. $condition[] = ['site_id', '=', $this->site_id];
  37. $material_model = new MaterialModel();
  38. $material_list = $material_model->getMaterialPageList($condition, $page_index, $page_size,'type asc,create_time desc');
  39. if (!empty($material_list['data']['list']) && is_array($material_list['data']['list'])) {
  40. foreach ($material_list['data']['list'] as $k => $v) {
  41. if (!empty($v['value']) && json_decode($v['value'])) {
  42. $material_list['data']['list'][$k]['value'] = json_decode($v['value'], true);
  43. }
  44. }
  45. }
  46. return $material_list;
  47. } else {
  48. return $this->fetch('material/lists', [], $this->replace);
  49. }
  50. }
  51. /**
  52. * 添加图文消息
  53. */
  54. public function add()
  55. {
  56. if (request()->isAjax()) {
  57. $type = input('type', 1);
  58. $param['value'] = input('value', '');
  59. if ($type != 1) {
  60. // 图片、音频、视频、缩略图素材
  61. $file_path = input('path', '');
  62. $res = $this->uploadApi($type, ['path' => $file_path]);
  63. if ($res['code'] != 0) {
  64. return $res;
  65. }
  66. $value['file_path'] = $file_path;
  67. $value['url'] = $res['data']['url'];
  68. $param['value'] = json_encode($value);
  69. $param['media_id'] = $res['data']['media_id'];
  70. } else {
  71. $param['media_id'] = time() . 'GRAPHIC' . 'MESSAGE' . rand(1, 1000);
  72. }
  73. $param['type'] = $type;
  74. $param['create_time'] = time();
  75. $param['update_time'] = time();
  76. $param['site_id'] = $this->site_id;
  77. $material_model = new MaterialModel();
  78. $res = $material_model->addMaterial($param);
  79. return $res;
  80. } else {
  81. $this->assign('material_id', '0');
  82. $this->assign('flag', false);
  83. return $this->fetch('material/edit', [], $this->replace);
  84. }
  85. }
  86. /**
  87. * 修改图文消息
  88. */
  89. public function edit()
  90. {
  91. if (request()->isAjax()) {
  92. $condition = [];
  93. $condition['id'] = input('id', '');
  94. $data['value'] = input('value', '');
  95. $data['update_time'] = time();
  96. $material_model = new MaterialModel();
  97. $res = $material_model->editMaterial($data, $condition);
  98. return $res;
  99. } else {
  100. $material_id = input('id', '');
  101. $this->assign('material_id', $material_id);
  102. $this->assign('flag', true);
  103. return $this->fetch('material/edit', [], $this->replace);
  104. }
  105. }
  106. /**
  107. * 添加文本素材
  108. */
  109. public function addTextMaterial()
  110. {
  111. if (request()->isAjax()) {
  112. $type = input('type', 1);
  113. $param['value'] = input('value', '');
  114. $param['type'] = $type;
  115. $param['create_time'] = time();
  116. $param['update_time'] = time();
  117. $param['site_id'] = $this->site_id;
  118. $material_model = new MaterialModel();
  119. $res = $material_model->addMaterial($param);
  120. return $res;
  121. }else{
  122. return $this->fetch('material/add_text');
  123. }
  124. }
  125. /**
  126. * 修改文本素材
  127. */
  128. public function editTextMaterial(){
  129. $material_model = new MaterialModel();
  130. if (request()->isAjax()) {
  131. $media_id = input("media_id", "");
  132. $media_id = str_replace("MATERIAL_TEXT_MESSAGE_", "", $media_id);
  133. $content = input("content", "");
  134. $content = ["content" => $content];
  135. $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  136. $condition['id'] = $media_id;
  137. $data['value'] = $content;
  138. $data['update_time'] = time();
  139. $res = $material_model->editMaterial($data, $condition);
  140. return $res;
  141. }else{
  142. $material_id = input('id', '');
  143. $data = $material_model->getMaterialInfo([['id','=',$material_id],['site_id','=',$this->site_id]]);
  144. if(!empty($data['data'])){
  145. $data['data']['value'] = json_decode($data['data']['value'],true);
  146. }
  147. $this->assign('material_data', $data['data']);
  148. return $this->fetch('material/edit_text');
  149. }
  150. }
  151. /**
  152. * 预览图文
  153. */
  154. public function previewGraphicMessage()
  155. {
  156. $id = input('id', '');
  157. $index = input('i', '');
  158. $material_model = new MaterialModel();
  159. $info = $material_model->getMaterialInfo(['id' => $id]);
  160. if (!empty($info['data']['value']) && json_decode($info['data']['value'], true)) {
  161. $info['data']['value'] = json_decode($info['data']['value'], true);
  162. }
  163. $this->assign('info', $info['data']);
  164. $this->assign('index', $index);
  165. return $this->fetch('material/preview_material', [], $this->replace);
  166. }
  167. /**
  168. * 预览文本
  169. */
  170. public function previewTextMessage()
  171. {
  172. $id = input('id', '');
  173. $material_model = new MaterialModel();
  174. $info = $material_model->getMaterialInfo(['id' => $id]);
  175. if (!empty($info['data']['value']) && json_decode($info['data']['value'], true)) {
  176. $info['data']['value'] = json_decode($info['data']['value'], true);
  177. }
  178. $this->assign('info', $info['data']);
  179. return $this->fetch('material/preview_text', [], $this->replace);
  180. }
  181. /**
  182. * 上传永久素材
  183. * @param number $type
  184. * @param object $data
  185. * @return multitype:string mixed
  186. */
  187. public function uploadApi($type, $data)
  188. {
  189. $wechat_model = new WechatModel();
  190. if ($type == 1) {
  191. $res = $wechat_model->uploadArticle($data);
  192. } else {
  193. if ($type == 2) {
  194. $type = 'image';
  195. $res = $wechat_model->uploadImage($data['path']);
  196. } else if ($type == 3) {
  197. $type = 'voice';
  198. $res = $wechat_model->uploadVoice($data['path']);
  199. } else if ($type == 4) {
  200. $type = 'video';
  201. $res = $wechat_model->uploadVideo($data['path']);
  202. } else if ($type == 6) {
  203. $res = $wechat_model->uploadVideo($data['path']);
  204. $type = 'thumb';
  205. }
  206. }
  207. return $res;
  208. }
  209. /**
  210. * 删除微信素材
  211. */
  212. public function delete()
  213. {
  214. if (request()->isAjax()) {
  215. $id = input('id', 0);
  216. $material_model = new MaterialModel();
  217. $condition = array(
  218. ["id", "=", $id]
  219. );
  220. $res = $material_model->deleteMaterial($condition);
  221. return $res;
  222. }
  223. }
  224. /**
  225. * 获取素材详情
  226. * @return mixed
  227. */
  228. public function getMaterialInfo()
  229. {
  230. if (request()->isAjax()) {
  231. $material_model = new MaterialModel();
  232. $condition = array(
  233. ['id', '=', input('id', '')]
  234. );
  235. $material_info = $material_model->getMaterialInfo($condition);
  236. if (json_decode($material_info['data']['value'])) {
  237. $material_info['data']['value'] = json_decode($material_info['data']['value']);
  238. }
  239. return $material_info;
  240. }
  241. }
  242. /**
  243. * 图文素材
  244. */
  245. public function articleList()
  246. {
  247. $material_model = new MaterialModel();
  248. $condition = array(
  249. ['type', '=', 1]
  250. );
  251. $material_list = $material_model->getMaterialList($condition, '*', 'update_time desc');
  252. if (!empty($material_list['data']) && is_array($material_list['data'])) {
  253. foreach ($material_list['data'] as $k => $v) {
  254. if (!empty($v['value']) && json_decode($v['value'])) {
  255. $material_list['data'][$k]['value'] = json_decode($v['value'], true);
  256. }
  257. }
  258. }
  259. $this->assign('material_list', $material_list);
  260. return $this->fetch('material/index', [], $this->replace);
  261. }
  262. /**
  263. * 素材管理
  264. */
  265. public function material()
  266. {
  267. //这这里的常量要与base中的区分,如果一致界面将无法渲染
  268. $type = input("type", 1);
  269. $this->assign("type", $type);
  270. // return array( 'shop/material/material', [], $this->replace );
  271. return $this->fetch('material/material', [], $this->replace);
  272. }
  273. }