ConfigServer::get('mnp', 'name', ''), 'original_id' => ConfigServer::get('mnp', 'original_id', ''), 'qr_code' => $qr_code, 'abs_qr_code' => UrlServer::getFileUrl($qr_code), 'app_id' => ConfigServer::get('mnp', 'app_id', ''), 'app_secret' => ConfigServer::get('mnp', 'secret', ''), 'request_domain' => 'https://'.$domain_name, 'socket_domain' => 'wss://'.$domain_name, 'uploadfile_domain' => 'https://'.$domain_name, 'downloadfile_domain' => 'https://'.$domain_name, 'udp_domain' => 'udp://'.$domain_name, 'tcp_domain' => 'tcp://'.$domain_name, 'business_domain' => $domain_name, 'url' => url('api/wechat/index',[],'',true), 'token' => ConfigServer::get('mnp', 'token', 'LikeMall'), 'encoding_ses_key' => ConfigServer::get('mnp', 'encoding_ses_key', ''), 'encryption_type' => ConfigServer::get('mnp', 'encryption_type', ''), 'data_type' => ConfigServer::get('mnp', 'data_type', ''), // 小程序同步发货开关 1开启 0关闭 'express_send_sync' => ConfigServer::get('mnp', 'express_send_sync', 1), //代码上传密钥 'private_key' => ConfigServer::get('mnp', 'private_key', ''), ]; return $config; } public static function setMnp($post) { if($post){ $encryption_type = $post['encryption_type'] ?? ''; $data_type = $post['data_type'] ?? ''; ConfigServer::set('mnp','name',$post['name']); ConfigServer::set('mnp','original_id',$post['original_id']); ConfigServer::set('mnp','qr_code', $post['qr_code']); ConfigServer::set('mnp','app_id',$post['app_id']); ConfigServer::set('mnp','secret',$post['app_secret']); // ConfigServer::set('mnp','token',$post['token']); // ConfigServer::set('mnp','encoding_ses_key',$post['encoding_ses_key']); // ConfigServer::set('mnp','encryption_type', $encryption_type); // ConfigServer::set('mnp','data_type', $data_type); ConfigServer::set('mnp','express_send_sync', $post['express_send_sync'] ?? 1); if (!empty($post['private_key'])) { $saveDir = '../extend/miniprogram-ci/'; if (!file_exists($saveDir)) { mkdir($saveDir, 0775, true); } //保存文件 $savePath = $saveDir.'private.'.$post['app_id'].'.key'; $f = fopen($savePath, 'w'); fwrite($f, $post['private_key']); fclose($f); ConfigServer::set('mnp','private_key',$post['private_key']); } } return true; } //一键上传 public static function uploadMnp($post) { Db::startTrans(); try { //校验是否已安装miniprogram-ci工具 if (!file_exists('../extend/miniprogram-ci/node_modules/miniprogram-ci')) { throw new \think\Exception('请先安装miniprogram-ci工具'); } $baseUrl = 'mp-weixin/common/vendor.js'; //判断是否已存在 vendor_example.js 备份文件,如果不存在则备份 $exampleUrl = 'mp-weixin/common/vendor_example.js'; if(!file_exists($exampleUrl)){ copy($baseUrl, $exampleUrl); } //更换小程序域名 $baseUrlData = file_get_contents($exampleUrl); $domain = request()->domain(true); $baseUrlData = str_replace("[baseUrl]",$domain,$baseUrlData); $f = fopen($baseUrl,"w"); fwrite($f,$baseUrlData); fclose($f); //插入上传日志 $uploadMnpLogId = Db::name('upload_mnp_log')->insertGetId([ 'admin_id' => $post['admin_id'], 'version' => $post['version'], 'create_time' => time(), ]); //上传小程序代码 $data = [ 'version' => $post['version'], 'desc' => $post['upload_desc'] ?? '', 'appid' => ConfigServer::get('mnp', 'app_id', ''), ]; $json_data = json_encode($data); $command = 'node ../extend/miniprogram-ci/upload.js '.escapeshellarg($json_data).' 2>&1'; $output=null; $retval = null; exec($command, $output, $retval); //获取错误信息 $errors = preg_grep('/\[error\]/', is_array($output) ? $output : [$output]); if ($retval || !empty($errors)) { //错误 Db::name('upload_mnp_log')->where('id', $uploadMnpLogId)->update([ 'status' => 2, 'fail_reason' => is_array($output) ? json_encode($output) : $output, ]); } else { //成功 Db::name('upload_mnp_log')->where('id', $uploadMnpLogId)->update([ 'status' => 1 ]); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); return $e->getMessage(); } } //小程序上传日志 public static function uploadMnpLog($get) { $lists = Db::name('upload_mnp_log') ->field('admin_id,version,status,fail_reason,create_time') ->withAttr('admin_desc', function ($value,$data){ return Db::name('admin')->where(['id'=>$data['admin_id']])->value('name'); }) ->withAttr('status_desc', function ($value,$data){ $statusDesc = ['上传中','上传成功','上传失败']; return $statusDesc[$data['status']]; }) ->withAttr('create_time', function ($value,$data){ return date('Y-m-d H:i:s', $data['create_time']); }) ->page($get['page'], $get['limit']) ->order('id', 'desc') ->select(); $count = Db::name('upload_mnp_log')->count(); return ['count' => $count, 'lists' => $lists]; } }