Weapp.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\weapp\shop\controller;
  11. use addon\weapp\model\Config as ConfigModel;
  12. use app\model\share\WeappShareBase as ShareModel;
  13. use app\model\system\Upgrade;
  14. use app\shop\controller\BaseShop;
  15. use addon\weapp\model\Weapp as WeappModel;
  16. /**
  17. * 微信小程序功能设置
  18. */
  19. class Weapp extends BaseShop
  20. {
  21. protected $replace = []; //视图输出字符串内容替换 相当于配置文件中的'view_replace_str'
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->replace = [
  26. 'WEAPP_CSS' => __ROOT__ . '/addon/weapp/shop/view/public/css',
  27. 'WEAPP_JS' => __ROOT__ . '/addon/weapp/shop/view/public/js',
  28. 'WEAPP_IMG' => __ROOT__ . '/addon/weapp/shop/view/public/img',
  29. 'WEAPP_SVG' => __ROOT__ . '/addon/weapp/shop/view/public/svg',
  30. ];
  31. }
  32. /**
  33. * 功能设置
  34. */
  35. public function setting()
  36. {
  37. $config_model = new ConfigModel();
  38. $config_result = $config_model->getWeappConfig($this->site_id);
  39. $config = $config_result[ 'data' ][ "value" ];
  40. $is_new_version = 0;
  41. // 获取站点小程序版本信息
  42. $version_info = $config_model->getWeappVersion($this->site_id);
  43. $version_info = $version_info[ 'data' ][ 'value' ];
  44. $currrent_version_info = config('info');
  45. if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
  46. $is_new_version = 1;
  47. }
  48. $this->assign('is_new_version', $is_new_version);
  49. $weapp_menu = event('WeappMenu', [ 'site_id' => $this->site_id ]);
  50. $this->assign('weapp_menu', $weapp_menu);
  51. return $this->fetch('weapp/setting', [], $this->replace);
  52. }
  53. /**
  54. * 公众号配置
  55. */
  56. public function config()
  57. {
  58. $weapp_model = new ConfigModel();
  59. if (request()->isAjax()) {
  60. $weapp_name = input('weapp_name', '');
  61. $weapp_original = input('weapp_original', '');
  62. $appid = input('appid', '');
  63. $appsecret = input('appsecret', '');
  64. $token = input('token', 'TOKEN');
  65. $encodingaeskey = input('encodingaeskey', '');
  66. $is_use = input('is_use', 0);
  67. $qrcode = input('qrcode', '');
  68. $data = array (
  69. "appid" => $appid,
  70. "appsecret" => $appsecret,
  71. "token" => $token,
  72. "weapp_name" => $weapp_name,
  73. "weapp_original" => $weapp_original,
  74. "encodingaeskey" => $encodingaeskey,
  75. 'qrcode' => $qrcode
  76. );
  77. $res = $weapp_model->setWeAppConfig($data, $is_use, $this->site_id);
  78. return $res;
  79. } else {
  80. $weapp_config_result = $weapp_model->getWeAppConfig($this->site_id);
  81. $config_info = $weapp_config_result[ 'data' ][ "value" ];
  82. $this->assign("config_info", $config_info);
  83. // 获取当前域名
  84. $url = __ROOT__;
  85. // 去除链接的http://头部
  86. $url_top = str_replace("https://", "", $url);
  87. $url_top = str_replace("http://", "", $url_top);
  88. // 去除链接的尾部/?s=
  89. $url_top = str_replace('/?s=', '', $url_top);
  90. $call_back_url = addon_url("weapp://api/auth/relateweixin");
  91. $this->assign("url", $url_top);
  92. $this->assign("call_back_url", $call_back_url);
  93. return $this->fetch('weapp/config', [], $this->replace);
  94. }
  95. }
  96. /**
  97. * 小程序包管理
  98. *
  99. */
  100. public function package()
  101. {
  102. $config = new ConfigModel();
  103. $weapp_config = $config->getWeappConfig($this->site_id);
  104. $weapp_config = $weapp_config[ 'data' ][ 'value' ];
  105. if (empty($weapp_config) || empty($weapp_config[ 'appid' ])) $this->error('小程序尚未配置,请先配置您的小程序!', addon_url('weapp://shop/weapp/config'));
  106. $this->assign('config', $weapp_config);
  107. $is_new_version = 0;
  108. // 获取站点小程序版本信息
  109. $version_info = $config->getWeappVersion($this->site_id);
  110. $version_info = $version_info[ 'data' ][ 'value' ];
  111. $currrent_version_info = config('info');
  112. if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
  113. $is_new_version = 1;
  114. }
  115. $this->assign('is_new_version', $is_new_version);
  116. // 检测授权
  117. $upgrade_model = new Upgrade();
  118. $auth_info = $upgrade_model->authInfo();
  119. $this->assign('is_auth', ( $auth_info[ 'code' ] == 0 ));
  120. return $this->fetch('weapp/package', [], $this->replace);
  121. }
  122. /**
  123. * 小程序包管理
  124. */
  125. public function deploy()
  126. {
  127. $config = new ConfigModel();
  128. $weapp_config = $config->getWeappConfig($this->site_id);
  129. $weapp_config = $weapp_config[ 'data' ][ 'value' ];
  130. $this->assign('config', $weapp_config);
  131. $is_new_version = 0;
  132. // 获取站点小程序版本信息
  133. $version_info = $config->getWeappVersion($this->site_id);
  134. $version_info = $version_info[ 'data' ][ 'value' ];
  135. $currrent_version_info = config('info');
  136. if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
  137. $is_new_version = 1;
  138. }
  139. $this->assign('is_new_version', $is_new_version);
  140. // 检测授权
  141. $upgrade_model = new Upgrade();
  142. $auth_info = $upgrade_model->authInfo();
  143. $this->assign('is_auth', ( $auth_info[ 'code' ] == 0 ));
  144. return $this->fetch('addon/weapp/shop/view/weapp/deploy.html', [], $this->replace);
  145. }
  146. /**
  147. * 小程序包下载
  148. */
  149. public function download()
  150. {
  151. if (strstr(ROOT_URL, 'niuteam.cn') === false) {
  152. $weapp = new WeappModel();
  153. $weapp->download($this->site_id);
  154. $config = new ConfigModel();
  155. $version_info = config('info');
  156. $config->setWeappVersion([ 'version' => $version_info[ 'version_no' ] ], 1, $this->site_id);
  157. }
  158. }
  159. /**
  160. * 下载uniapp源码
  161. */
  162. public function downloadUniapp()
  163. {
  164. if (strstr(ROOT_URL, 'niuteam.cn') === false) {
  165. $app_info = config('info');
  166. $upgrade_model = new Upgrade();
  167. $res = $upgrade_model->downloadUniapp($app_info[ 'version_no' ]);
  168. if ($res[ 'code' ] == 0) {
  169. $filename = "upload/{$app_info['version_no']}_uniapp.zip";
  170. $res = file_put_contents($filename, base64_decode($res[ 'data' ]));
  171. header("Content-Type: application/zip");
  172. header("Content-BirthdayGift-Encoding: Binary");
  173. header("Content-Length: " . filesize($filename));
  174. header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
  175. readfile($filename);
  176. @unlink($filename);
  177. } else {
  178. $this->error($res[ 'message' ]);
  179. }
  180. }
  181. }
  182. /**
  183. * 分享
  184. */
  185. public function shareBack()
  186. {
  187. $config_model = new ConfigModel();
  188. if (request()->isAjax()) {
  189. $key = input('key', 'index');
  190. $value = [
  191. 'title' => input('title', ''),
  192. 'path' => input('path', '')
  193. ];
  194. $res = $config_model->setShareConfig($this->site_id, $this->app_module, $key, $value);
  195. return $res;
  196. }
  197. $scene = [
  198. [
  199. 'key' => 'index',
  200. 'title' => '首页'
  201. ]
  202. ];
  203. $this->assign('scene', $scene);
  204. $config = $config_model->getShareConfig($this->site_id, $this->app_module);
  205. $this->assign('config', $config[ 'data' ][ 'value' ]);
  206. $this->assign('shop_info', $this->shop_info);
  207. return $this->fetch('weapp/share_back', [], $this->replace);
  208. }
  209. /**
  210. * 分享
  211. */
  212. public function share()
  213. {
  214. if (request()->isAjax()) {
  215. $data_json = input('data_json', '');
  216. $data = json_decode($data_json, true);
  217. $share_model = new ShareModel();
  218. return $share_model->setShareConfig($this->site_id, $data);
  219. } else {
  220. $share_config = event('WeappShareConfig', [ 'site_id' => $this->site_id ]);
  221. $config_list = [];
  222. foreach ($share_config as $data) {
  223. foreach ($data[ 'data' ] as $val) {
  224. $config_list[] = $val;
  225. }
  226. }
  227. $this->assign('config_list', $config_list);
  228. return $this->fetch('weapp/share', [], $this->replace);
  229. }
  230. }
  231. }