Wechat.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\Config as ConfigModel;
  12. use addon\wechat\model\Qrcode;
  13. use app\model\share\WchatShareBase as ShareModel;
  14. /**
  15. * 微信公众号基础功能
  16. */
  17. class Wechat extends BaseWechat
  18. {
  19. public function __construct()
  20. {
  21. //执行父类构造函数
  22. parent::__construct();
  23. }
  24. /**
  25. * 功能设置
  26. */
  27. public function setting()
  28. {
  29. $setting_arr = [
  30. [
  31. 'url' => addon_url('wechat://shop/wechat/config'),
  32. 'img' => 'config_wechat_icon.png',
  33. 'title' => '公众号管理',
  34. 'content' => '公众号管理',
  35. ],
  36. [
  37. 'url' => addon_url('wechat://shop/material/lists'),
  38. 'img' => 'config_material_icon.png',
  39. 'title' => '消息素材',
  40. 'content' => '消息素材',
  41. ],
  42. /* [
  43. 'url' => addon_url('wechat://shop/fans/lists'),
  44. 'img' => 'config_material_icon.png',
  45. 'title' => '粉丝列表',
  46. 'content' => '粉丝列表',
  47. ],*/
  48. [
  49. 'url' => addon_url('wechat://shop/menu/menu'),
  50. 'img' => 'config_menu_icon.png',
  51. 'title' => '菜单管理',
  52. 'content' => '菜单管理',
  53. ],
  54. // [
  55. // 'url' => addon_url('wechat://shop/wechat/qrcode'),
  56. // 'img' => 'config_qrcode_icon.png',
  57. // 'title' => '推广二维码',
  58. // 'content' => '推广二维码管理',
  59. // ],
  60. [
  61. 'url' => addon_url('wechat://shop/wechat/share'),
  62. 'img' => 'config_share_icon.png',
  63. 'title' => '分享内容设置',
  64. 'content' => '分享内容设置',
  65. ],
  66. [
  67. 'url' => addon_url('wechat://shop/replay/replay'),
  68. 'img' => 'config_replay_icon.png',
  69. 'title' => '回复设置',
  70. 'content' => '回复设置',
  71. ],
  72. // [
  73. // 'url' => addon_url('wechat://shop/wechat/mass'),
  74. // 'img' => 'config_mass_icon.png',
  75. // 'title' => '群发设置',
  76. // 'content' => '群发设置',
  77. // ],
  78. [
  79. 'url' => addon_url('wechat://shop/message/config'),
  80. 'img' => 'config_message_icon.png',
  81. 'title' => '模板消息',
  82. 'content' => '模板消息设置',
  83. ],
  84. ];
  85. $this->assign('setting_arr', $setting_arr);
  86. return $this->fetch('wechat/setting', [], $this->replace);
  87. }
  88. /**
  89. * 公众号配置
  90. */
  91. public function config()
  92. {
  93. $config_model = new ConfigModel();
  94. if (request()->isAjax()) {
  95. $wechat_name = input('wechat_name', '');//公众号名称 (备注: 公众号名称,尽量与公众号平台保持一致)
  96. $wechat_original = input('wechat_original', '');//公众号原始id (备注: 公众号原始id,如:gh_111111111111)
  97. $appid = input('appid', '');//gh_845581623321AppID (备注: AppID 请注意需要与微信公众号平台保持一致,且注意信息安全,不要随意透露给第三方)
  98. $appsecret = input('appsecret', '');//AppSecret (备注: AppSecret密钥需要与微信公众号平台保持一致,没有特别原因不要随意修改,也不要随意透露给第三方)
  99. $token = input('token', 'TOKEN');//Token (备注: 自定义的Token值、确保后台与公众号平台填写的一致)
  100. $encodingaeskey = input('encodingaeskey', '');//EncodingAESKey (备注: 由开发者手动填写或随机生成,将用作消息体加解密密钥)
  101. $is_use = input('is_use', 1);//是否启用
  102. $qrcode = input('qrcode', '');//二维码
  103. $headimg = input('headimg', '');//头像
  104. $data = array(
  105. "wechat_name" => $wechat_name,
  106. "wechat_original" => $wechat_original,
  107. "appid" => $appid,
  108. "appsecret" => $appsecret,
  109. "token" => $token,
  110. "encodingaeskey" => $encodingaeskey,
  111. 'qrcode' => $qrcode,
  112. 'headimg' => $headimg,
  113. );
  114. $res = $config_model->setWechatConfig($data, $is_use, $this->site_id);
  115. return $res;
  116. } else {
  117. $config_result = $config_model->getWechatConfig($this->site_id);
  118. $config = $config_result["data"];
  119. $is_authopen = $config_result['data']['value']['is_authopen'] ?? 0;
  120. if ($is_authopen == 1) {
  121. $config['value'] = [];
  122. }
  123. $this->assign("config", $config);
  124. // 获取当前域名
  125. $url = __ROOT__;
  126. // 去除链接的http://头部
  127. $url_top = str_replace("https://", "", $url);
  128. $url_top = str_replace("http://", "", $url_top);
  129. // 去除链接的尾部/?s=
  130. $url_top = str_replace('/?s=', '', $url_top);
  131. $call_back_url = addon_url("wechat://api/auth/relateweixin");
  132. $this->assign("url", $url_top);
  133. $this->assign("call_back_url", $call_back_url);
  134. return $this->fetch('wechat/config');
  135. }
  136. }
  137. /**
  138. * 推广二维码模板
  139. */
  140. public function qrcode()
  141. {
  142. $qrcode_model = new Qrcode();
  143. $template_list = $qrcode_model->getQrcodePageList(['is_remove' => 0]);
  144. $this->assign("template_list", $template_list['data']);
  145. return $this->fetch('wechat/qrcode', [], $this->replace);
  146. }
  147. /**
  148. * 推广二维码
  149. */
  150. public function addQrcode()
  151. {
  152. if (request()->isAjax()) {
  153. $data = array(
  154. 'background' => input("background", ''),
  155. 'nick_font_color' => input("nick_font_color", '#000'),
  156. 'nick_font_size' => input("nick_font_size", '12'),
  157. 'is_logo_show' => input("is_logo_show", '1'),
  158. 'header_left' => input("header_left", '59') . 'px',
  159. 'header_top' => input("header_top", '15') . 'px',
  160. 'name_left' => input("name_left", '128') . 'px',
  161. 'name_top' => input("name_top", '23') . 'px',
  162. 'logo_left' => input("logo_left", '60') . 'px',
  163. 'logo_top' => input("logo_top", '200') . 'px',
  164. 'code_left' => input("code_left", '70') . 'px',
  165. 'code_top' => input("code_top", '300') . 'px',
  166. );
  167. $qrcode_model = new Qrcode();
  168. $result = $qrcode_model->addQrcode($data);
  169. return $result;
  170. } else {
  171. return $this->fetch('wechat/add_qrcode', [], $this->replace);
  172. }
  173. }
  174. /**
  175. * 编辑二维码模板
  176. * @return mixed
  177. */
  178. public function editQrcode()
  179. {
  180. $id = input("id", 0);
  181. $qrcode_model = new Qrcode();
  182. $condition = array(
  183. "id" => $id
  184. );
  185. if (request()->isAjax()) {
  186. $data = array(
  187. 'background' => input("background", ''),
  188. 'nick_font_color' => input("nick_font_color", '#000'),
  189. 'nick_font_size' => input("nick_font_size", '12'),
  190. 'is_logo_show' => input("is_logo_show", '1'),
  191. 'header_left' => input("header_left", '59') . 'px',
  192. 'header_top' => input("header_top", '15') . 'px',
  193. 'name_left' => input("name_left", '128') . 'px',
  194. 'name_top' => input("name_top", '23') . 'px',
  195. 'logo_left' => input("logo_left", '60') . 'px',
  196. 'logo_top' => input("logo_top", '200') . 'px',
  197. 'code_left' => input("code_left", '70') . 'px',
  198. 'code_top' => input("code_top", '300') . 'px',
  199. );
  200. $result = $qrcode_model->editQrcode($data, $condition);
  201. return $result;
  202. } else {
  203. $info_result = $qrcode_model->getQrcodeInfo($condition);
  204. $info = $info_result["data"];
  205. $this->assign("info", $info);
  206. return $this->fetch('wechat/edit_qrcode', [], $this->replace);
  207. }
  208. }
  209. /**
  210. * 设置默认推广二维码
  211. */
  212. public function qrcodeDefault()
  213. {
  214. if (request()->isAjax()) {
  215. $id = input('id', 0);
  216. $qrcode_model = new Qrcode();
  217. $result = $qrcode_model->modifyQrcodeDefault(['id' => $id]);
  218. return $result;
  219. }
  220. }
  221. /**
  222. * 删除默认推广二维码
  223. */
  224. public function deleteQrcode()
  225. {
  226. if (request()->isAjax()) {
  227. $id = input('id', 0);
  228. $qrcode_model = new Qrcode();
  229. $result = $qrcode_model->deleteQrcode(['id' => $id]);
  230. return $result;
  231. }
  232. }
  233. /**
  234. * 群发消息
  235. */
  236. public function mass()
  237. {
  238. return $this->fetch('wechat/mass', [], $this->replace);
  239. }
  240. /**
  241. * 分享内容设置
  242. */
  243. public function shareBack()
  244. {
  245. $config_model = new ConfigModel();
  246. if (request()->isAjax()) {
  247. $qrcode_param_1 = input('qrcode_param_1', '');
  248. $qrcode_param_2 = input('qrcode_param_2', '');
  249. $goods_param_1 = input('goods_param_1', '');
  250. $goods_param_2 = input('goods_param_2', '');
  251. $shop_param_1 = input('shop_param_1', '');
  252. $shop_param_2 = input('shop_param_2', '');
  253. $shop_param_3 = input('shop_param_3', '');
  254. $data = array(
  255. 'qrcode_param_1' => $qrcode_param_1,
  256. 'qrcode_param_2' => $qrcode_param_2,
  257. 'goods_param_1' => $goods_param_1,
  258. 'goods_param_2' => $goods_param_2,
  259. 'shop_param_1' => $shop_param_1,
  260. 'shop_param_2' => $shop_param_2,
  261. 'shop_param_3' => $shop_param_3,
  262. );
  263. $res = $config_model->setShareConfig($data, 1, $this->site_id);
  264. return $res;
  265. } else {
  266. $config_result = $config_model->getShareConfig($this->site_id);
  267. $this->assign("info", $config_result["data"]['value']);
  268. return $this->fetch('wechat/share_back', [], $this->replace);
  269. }
  270. }
  271. /**
  272. * 分享内容设置
  273. */
  274. public function share()
  275. {
  276. if (request()->isAjax()) {
  277. $data_json = input('data_json', '');
  278. $data = json_decode($data_json, true);
  279. $share_model = new ShareModel();
  280. return $share_model->setShareConfig($this->site_id, $data);
  281. } else {
  282. $share_config = event('WchatShareConfig', ['site_id' => $this->site_id]);
  283. $config_list = [];
  284. foreach($share_config as $data){
  285. foreach($data['data'] as $val){
  286. $config_list[] = $val;
  287. }
  288. }
  289. $this->assign('config_list', $config_list);
  290. return $this->fetch('wechat/share', [], $this->replace);
  291. }
  292. }
  293. /**
  294. * 访问统计
  295. */
  296. public function stat()
  297. {
  298. return $this->fetch('wechat/stat', [], $this->replace);
  299. }
  300. /**
  301. * 公众号设置
  302. */
  303. public function configSetting()
  304. {
  305. $config_model = new ConfigModel();
  306. $config_result = $config_model->getWechatConfig($this->site_id);
  307. $config = $config_result["data"];
  308. if (!empty($config["value"])) {
  309. //是否是开放平台授权
  310. $is_authopen = $config_result['data']['value']['is_authopen'] ?? 0;
  311. if ($is_authopen > 0) {
  312. $this->redirect(addon_url("wxoplatform://shop/oplatform/wechat"));
  313. } else {
  314. $this->redirect(addon_url("wechat://shop/wechat/config"));
  315. }
  316. } else {
  317. $this->redirect(addon_url("wxoplatform://shop/oplatform/wechatsettled"));
  318. }
  319. }
  320. }