UploadminiprogramController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use think\facade\Cache;
  5. use app\model\Config;
  6. use app\model\Uploadminiprogram;
  7. class UploadminiprogramController extends Base
  8. {
  9. function index()
  10. {
  11. $weid = weid();
  12. $path = input('post.path', '', 'serach_in');
  13. if ($path == "/config/uploadminiprogram") {
  14. $ptype = 1;
  15. } elseif (($path == "/config/techuploadminiprogram")) {
  16. $ptype = 2;
  17. }
  18. $query = Uploadminiprogram::where(['weid' => $weid, 'is_up' => 1, 'ptype' => $ptype]);
  19. $res = $query->order('id desc')
  20. ->paginate(getpage())
  21. ->toArray();
  22. $data['data'] = $res;
  23. return $this->json($data);
  24. }
  25. function listUpdate()
  26. {
  27. $data = only('id,status,sort');
  28. if (!$data['id']) throw new ValidateException('参数错误');
  29. Uploadminiprogram::update($data);
  30. return $this->json(['msg' => '操作成功']);
  31. }
  32. public function update()
  33. {
  34. $postdata = $this->request->post();
  35. //$updata = $this->uploadWinpro($postdata);
  36. $updata = $this->uploadmini($postdata);
  37. if ($updata['data']['code'] == 2000) {
  38. $postdata['is_up'] = Uploadminiprogram::where('id', $postdata['id'])->update(['is_up' => 1]);
  39. }
  40. return $this->json(['msg' => $updata['data']['msg'], 'data' => $postdata]);
  41. }
  42. function getInfo()
  43. {
  44. $path = input('post.path', '', 'serach_in');
  45. if ($path == "/config/uploadminiprogram") {
  46. $ptype = 1;
  47. } elseif (($path == "/config/techuploadminiprogram")) {
  48. $ptype = 2;
  49. }
  50. $res = Uploadminiprogram::getnewversion($ptype);
  51. return $this->json(['data' => $res]);
  52. }
  53. /**
  54. * @title 上传微信小程序
  55. */
  56. public function uploadmini($postdata)
  57. {
  58. $weid = weid();
  59. $wechatConfig = Config::getconfig('miniprogram');
  60. if ($postdata['ptype'] == 2) {
  61. $multiid = '1';
  62. $wechatConfig['app_id'] = $wechatConfig['techapp_id'];
  63. $wechatConfig['secret'] = $wechatConfig['techsecret'];
  64. $wechatConfig['private_key'] = $wechatConfig['techprivate_key'];
  65. } else {
  66. $multiid = '0';
  67. }
  68. if ($wechatConfig) {
  69. if (!$wechatConfig['app_id']) throw new ValidateException('请先配置小程序appId');
  70. if (!$wechatConfig['secret']) throw new ValidateException('请先配置小程序appSecret');
  71. if (!$wechatConfig['private_key']) throw new ValidateException('请先配置小程序上传密钥');
  72. } else {
  73. throw new ValidateException('请先配置小程序参数');
  74. }
  75. $domain = Author()::getdomainname();
  76. $configFiles = [
  77. [
  78. 'name' => 'siteinfo.js',
  79. 'path' => '/',
  80. 'content' => 'var siteinfo = {"uniacid": "' . $weid . '","acid": "' . $weid . '","multiid": "' . $multiid . '","version": "' . $postdata['id'] . '","siteroot": "https://' . $domain . '/app/index.php","design_method": "3"};module.exports = siteinfo;'
  81. ],
  82. ];
  83. $url = 'http://console.samcms.com/public/index.php/index/authorization/uploadminiprogram';
  84. $post = [
  85. 'appid' => trim($wechatConfig['app_id']),
  86. 'wxapp_private_key' => trim($wechatConfig['private_key']),
  87. 'version' => $postdata['version'],
  88. 'desc' => $postdata['desctext'],
  89. 'config_files' => json_encode($configFiles)
  90. ];
  91. $authorization = Author()::authorizationInfo();
  92. $post['secret'] = trim($authorization['secret']);
  93. $post['seed'] = trim($authorization['seed']);
  94. $post['biaoshi'] = config('database.app_name');
  95. $result = vit_http_request($url, $post);
  96. $result = json_decode($result, true);
  97. //var_dump($result);
  98. if (!$result || $result['code'] != 2000) {
  99. throw new ValidateException($result['msg'] ?? '请求错误.');
  100. }
  101. $result['msg'] = $result['message'] ?? '上传成功';
  102. return ['data' => $result];
  103. }
  104. //荫析小程序上传开始
  105. /**
  106. * @title 上传微信小程序
  107. */
  108. public function uploadwinpro($postdata)
  109. {
  110. $weid = weid();
  111. $code = $this->getSiteCode();
  112. $wechatConfig = Config::getconfig('miniprogram');
  113. if ($postdata['ptype'] == 2) {
  114. $multiid = '1';
  115. $wechatConfig['app_id'] = $wechatConfig['techapp_id'];
  116. $wechatConfig['secret'] = $wechatConfig['techsecret'];
  117. $wechatConfig['private_key'] = $wechatConfig['techprivate_key'];
  118. } else {
  119. $multiid = '0';
  120. }
  121. if (!$code) throw new ValidateException('请先配置在线更新');
  122. $pid = input('pid');
  123. if ($wechatConfig) {
  124. if (!$wechatConfig['app_id']) throw new ValidateException('请先配置小程序appId');
  125. if (!$wechatConfig['secret']) throw new ValidateException('请先配置小程序appSecret');
  126. if (!$wechatConfig['private_key']) throw new ValidateException('请先配置小程序上传密钥');
  127. } else {
  128. throw new ValidateException('请先配置小程序参数');
  129. }
  130. $domain = Author()::getdomainname();
  131. $configFiles = [
  132. [
  133. 'name' => 'siteinfo.js',
  134. 'path' => '/',
  135. 'content' => 'var siteinfo = {"uniacid": "' . $weid . '","acid": "' . $weid . '","multiid": "' . $multiid . '","version": "' . $postdata['id'] . '","siteroot": "https://' . $domain . '/app/index.php","design_method": "3"};module.exports = siteinfo;'
  136. ],
  137. ];
  138. $url = 'https://www.mzapp.cn/api/wxapp';
  139. $post = [
  140. 'code' => $code,
  141. 'goods' => getmzgoodsid(),
  142. 'pid' => $pid,
  143. 'appid' => trim($wechatConfig['app_id']),
  144. 'wxapp_private_key' => trim($wechatConfig['private_key']),
  145. 'version' => $postdata['version'],
  146. 'desc' => $postdata['desctext'],
  147. 'config_files' => json_encode($configFiles)
  148. ];
  149. $result = vit_http_request($url, $post);
  150. $result = is_null(json_decode($result)) ? [] : json_decode($result, true);
  151. if (!$result || $result['code'] != 1) {
  152. return $result['msg'] ?? '请求错误.';
  153. }
  154. return $this->queryUpload($result['data']['order_no']);
  155. }
  156. public function queryUpload($order)
  157. {
  158. if (empty($order)) {
  159. return;
  160. }
  161. $url = 'https://www.mzapp.cn/api/wxapp/upload';
  162. $post = [
  163. 'code' => $this->getSiteCode(),
  164. 'order' => $order
  165. ];
  166. $result = vit_http_request($url, $post);
  167. $result = is_null(json_decode($result)) ? [] : json_decode($result, true);
  168. if (!$result || $result['code'] != 1) {
  169. return $result['code'];
  170. }
  171. return $result;
  172. }
  173. /**
  174. * 获取站点code
  175. */
  176. public function getSiteCode($refresh = false)
  177. {
  178. if ($refresh) {
  179. return $this->getRefreshSiteCode();
  180. }
  181. $code = Cache::get('vit_site_code');
  182. if (!$code) {
  183. return $this->getRefreshSiteCode();
  184. }
  185. return $code;
  186. }
  187. /**
  188. * 重新获取新的站点code值
  189. */
  190. private function getRefreshSiteCode()
  191. {
  192. $url = 'http://console.samcms.com/public/index.php/index/authorization/getrefreshsitecode';
  193. $postparam = Author()::authorizationInfo();
  194. $postparam['biaoshi'] = config('database.app_name');
  195. $result = vit_http_request($url, $postparam);
  196. $result = is_null(json_decode($result)) ? [] : json_decode($result, true);
  197. if (($result['data']['appid'])) {
  198. $Configdata = $result['data'];
  199. } else {
  200. throw new ValidateException($result['msg']);
  201. }
  202. if ($Configdata['appid']) {
  203. $appid = $Configdata['appid'];
  204. $appkey = $Configdata['appkey'];
  205. $time = time();
  206. $sign = sha1($appid . $appkey . $Configdata['domain'] . $time);
  207. $url = 'https://www.mzapp.cn/api/code/get';
  208. $post = [
  209. 'domain' => $Configdata['domain'],
  210. 'appid' => $appid,
  211. 'sign' => $sign,
  212. 'time' => $time
  213. ];
  214. $result = vit_http_request($url, $post);
  215. $result = is_null(json_decode($result)) ? [] : json_decode($result, true);
  216. if (!$result || $result['code'] != 1) {
  217. return '';
  218. }
  219. Cache::set('vit_site_code', $result['data']['code'], '600');
  220. }
  221. return $result['data']['code'];
  222. }
  223. //荫析小程序上传结束
  224. }