main.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import os
  2. import requests
  3. import json
  4. import subprocess
  5. import time
  6. from flask import Flask,request,redirect
  7. from flask_cors import CORS
  8. app = Flask(__name__,static_folder="static")
  9. CORS(app, resources=r'/*')
  10. # 操作文件,读取本地
  11. def set_local_file_json(obj):
  12. with open("local.json","w",encoding="utf-8") as f:
  13. f.write(json.dumps(obj,ensure_ascii=False))
  14. f.close()
  15. def get_local_file_json():
  16. try:
  17. with open("local.json","r",encoding="utf-8") as f:
  18. return json.loads(f.read())
  19. except:
  20. return {"status":0,"project":[]}
  21. # current_path
  22. current_path = os.getcwd()
  23. res_json = {"code":0,"msg":"成功"}
  24. global_params ={
  25. "token":"",
  26. }
  27. # 1.小程序打包
  28. def build_mini_program(branch):
  29. command_str = (
  30. "cd "+current_path+ ";"
  31. "cd longVideoFactory;"
  32. "rm -rf dist;"
  33. "git checkout "+branch+";"
  34. "git pull;"
  35. "cnpm install && npm install ;"
  36. "npm run build;"
  37. )
  38. ret = subprocess.run(command_str,shell=True)
  39. if ret.returncode == 0:
  40. res_json["code"] = 0
  41. res_json["msg"] = "打包成功"
  42. else:
  43. res_json["code"] = 1
  44. res_json["msg"] = "打包失败"
  45. # # 3.微信编译图片
  46. def create_preview(project,page):
  47. # 小程序 cli path for mac
  48. wx_cli ="/Applications/wechatwebdevtools.app/Contents/MacOS/cli "
  49. dist_path = os.path.join(current_path,'longVideoFactory/dist/'+project)
  50. code_img_path = os.path.join(current_path,'1.png')
  51. path = """ '{"pathName":"%s"}' """ % page
  52. os.system(wx_cli+ " open --project " + dist_path)
  53. os.system(wx_cli+ " reset-fileutils --project " + dist_path)
  54. f_read = os.popen(wx_cli+" preview --project "+dist_path+" --compile-condition "+path+" --qr-output "+code_img_path+" --qr-format image").read()
  55. os.system(wx_cli+ " quit")
  56. if "[error]" in f_read:
  57. res_json["code"] = 3
  58. res_json["msg"] = f_read
  59. else:
  60. res_json["code"] = 0
  61. res_json["msg"] = "微信小程序编译成功"
  62. # 修改环境文件
  63. def edit_mode_by_file(project,mode):
  64. path = os.path.join(current_path,"longVideoFactory/dist/"+project+"/network/volatileConfig.js")
  65. file_lines = []
  66. with open(path,"r") as f:
  67. for item in f.readlines():
  68. if "export const currentMode" in item:
  69. item = "export const currentMode = MODE."+mode+" \n"
  70. file_lines.append(item)
  71. f.close()
  72. with open(path,"w") as f2:
  73. f2.writelines(file_lines)
  74. f2.close()
  75. res_json["code"] = 0
  76. res_json["msg"] = "文件修改成功"
  77. def upload_img_by_feishu():
  78. token = global_params["token"]
  79. key = ""
  80. url = "https://open.feishu.cn/open-apis/im/v1/images"
  81. files = {
  82. "image_type":(None,"message"),
  83. "image":('1.png',open(os.path.join(current_path,"1.png"),"rb"),"image/png")
  84. }
  85. headers = {
  86. "Authorization":"Bearer "+token,
  87. }
  88. res = requests.post(url,headers=headers,files=files).json()
  89. if res["code"] == 0:
  90. global_params["img_key"] = res["data"]["image_key"]
  91. res_json["code"] = 0
  92. else:
  93. res_json["code"] = 4
  94. res_json["msg"] = "飞书上传图片失败"
  95. def tenant_access_token_by_feishu():
  96. url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
  97. params = {
  98. "app_id":"cli_a1ec07458838d00c",
  99. "app_secret":"Ngm0kfaLkZnpM2T2DH7L8cM5hdAqY0hI"
  100. }
  101. res = requests.post(url,data=params).json()
  102. if res["code"] == 0:
  103. global_params["token"] = res["tenant_access_token"]
  104. res_json["msg"] = "获取飞书签名成功"
  105. res_json["code"] = 0
  106. else:
  107. res_json["code"] = 4
  108. res_json["msg"] = "获取飞书签名失败"
  109. def send_msg_by_feishu(chat_id,project_name,mode_name):
  110. token = global_params["token"]
  111. img_key = global_params["img_key"]
  112. url ="https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id"
  113. headers = {
  114. "Authorization":"Bearer "+token,
  115. "Content-Type": "application/json;charset=UTF-8"
  116. }
  117. params = {
  118. "receive_id":chat_id,
  119. "msg_type": "post",
  120. "content": json.dumps({
  121. "zh_cn": {
  122. "title": mode_name+':'+project_name+"小程序预览码",
  123. "content": [
  124. [
  125. {
  126. "tag": "img",
  127. "image_key": img_key,
  128. "width": 500,
  129. "height": 500
  130. }
  131. ]]
  132. }
  133. })
  134. }
  135. res = requests.post(url,headers=headers,data=json.dumps(params)).json()
  136. if res["code"] == 0:
  137. res_json["code"] = 0
  138. res_json["msg"] = "飞书消息发送成功"
  139. else:
  140. res_json["code"] = 5
  141. res_json["msg"] = "飞书消息发送失败"
  142. # 获取群
  143. def get_group_by_feishu():
  144. token = global_params["token"]
  145. headers = {
  146. "Authorization":"Bearer "+token,
  147. "Content-Type": "application/json;charset=UTF-8"
  148. }
  149. url = "https://open.feishu.cn/open-apis/im/v1/chats"
  150. res = requests.get(url,headers=headers).json()
  151. data = []
  152. for item in res["data"]["items"]:
  153. if item["name"] != '':
  154. data.append(item)
  155. return data
  156. @app.route('/')
  157. def func():
  158. # return 'Welcome to auto-mini-program-build-preview server. only can run on MacOS.'
  159. return redirect("static/dist/index.html")
  160. @app.route('/getProject')
  161. def getProject():
  162. local_data = get_local_file_json()
  163. res_json["code"] = 0
  164. res_json["data"] = local_data["project"]
  165. return res_json
  166. @app.route('/getGroup')
  167. def getGroup():
  168. tenant_access_token_by_feishu()
  169. res_json["code"] = 0
  170. res_json["data"] = get_group_by_feishu()
  171. return res_json
  172. @app.route('/send',methods=['POST'])
  173. def send():
  174. local_data = get_local_file_json()
  175. try:
  176. if local_data["status"] == "1":
  177. res_json["code"] = 9
  178. res_json["msg"] = "当前有人打包,请稍后尝试"
  179. return res_json
  180. local_data["status"] = '1'
  181. set_local_file_json(local_data)
  182. res_json["code"] = 0
  183. res_json["msg"] = "成功"
  184. # 接受参数
  185. chat_id = request.json["chat_id"]
  186. branch = request.json["branch"]
  187. page = request.json["page"]
  188. project = request.json["project"]
  189. mode = request.json["mode"]
  190. project_name = request.json["project_name"]
  191. mode_name = request.json["mode_name"]
  192. func_arr = [
  193. # ------- 微信打包 ------
  194. # 1.小程序打包
  195. build_mini_program(branch),
  196. # 2.修改环境
  197. edit_mode_by_file(project,mode),
  198. time.sleep(1),
  199. create_preview(project,page),
  200. # -------- 飞书 --------
  201. # 1.获取签名
  202. tenant_access_token_by_feishu(),
  203. upload_img_by_feishu(),
  204. send_msg_by_feishu(chat_id,project_name,mode_name)
  205. ]
  206. # 循环方法判断
  207. for item in func_arr:
  208. item
  209. if res_json["code"] != 0:
  210. local_data["status"] = '0'
  211. set_local_file_json(local_data)
  212. return res_json
  213. # 正常执行完
  214. local_data["status"] = '0'
  215. set_local_file_json(local_data)
  216. except Exception as ex:
  217. local_data["status"] = '0'
  218. set_local_file_json(local_data)
  219. res_json["code"] = 9
  220. print(ex)
  221. res_json["msg"] = ex.args
  222. return res_json
  223. @app.route("/getBranch")
  224. def get_branch():
  225. # 查看所有分支
  226. command_str= (
  227. "cd "+current_path+ ";"
  228. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  229. "cd longVideoFactory;"
  230. "git pull;"
  231. )
  232. os.system(command_str)
  233. command_str= (
  234. "cd "+current_path+ ";"
  235. "cd longVideoFactory;"
  236. "git branch -r;"
  237. )
  238. f_read = os.popen(command_str).read()
  239. branch_list = []
  240. for item in f_read.split():
  241. if "->" not in item and "HEAD" not in item and "*" not in item:
  242. branch_list.append(item.replace("origin/",""))
  243. res = {"code":0,"data":branch_list}
  244. return res
  245. if __name__ == '__main__':
  246. app.run(host='0.0.0.0', port=7777, debug=True)