main.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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,branch):
  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": "小程序预览码",
  123. "content": [
  124. [{
  125. "tag":"text",
  126. "text":"项目:"+project_name
  127. }],
  128. [{
  129. "tag":"text",
  130. "text":"环境:"+mode_name
  131. }],
  132. [{
  133. "tag":"text",
  134. "text":"分支:"+branch
  135. }],
  136. [{
  137. "tag": "img",
  138. "image_key": img_key,
  139. "width": 500,
  140. "height": 500
  141. }
  142. ]]
  143. }
  144. })
  145. }
  146. res = requests.post(url,headers=headers,data=json.dumps(params)).json()
  147. if res["code"] == 0:
  148. res_json["code"] = 0
  149. res_json["msg"] = "飞书消息发送成功"
  150. else:
  151. res_json["code"] = 5
  152. res_json["msg"] = "飞书消息发送失败"
  153. # 获取群
  154. def get_group_by_feishu():
  155. token = global_params["token"]
  156. headers = {
  157. "Authorization":"Bearer "+token,
  158. "Content-Type": "application/json;charset=UTF-8"
  159. }
  160. url = "https://open.feishu.cn/open-apis/im/v1/chats"
  161. res = requests.get(url,headers=headers).json()
  162. data = []
  163. for item in res["data"]["items"]:
  164. if item["name"] != '':
  165. data.append(item)
  166. return data
  167. @app.route('/')
  168. def func():
  169. # return 'Welcome to auto-mini-program-build-preview server. only can run on MacOS.'
  170. return redirect("static/dist/index.html")
  171. @app.route('/getProject')
  172. def getProject():
  173. local_data = get_local_file_json()
  174. res_json["code"] = 0
  175. res_json["data"] = local_data["project"]
  176. return res_json
  177. @app.route('/getGroup')
  178. def getGroup():
  179. tenant_access_token_by_feishu()
  180. res_json["code"] = 0
  181. res_json["data"] = get_group_by_feishu()
  182. return res_json
  183. @app.route('/send',methods=['POST'])
  184. def send():
  185. local_data = get_local_file_json()
  186. try:
  187. if local_data["status"] == "1":
  188. res_json["code"] = 9
  189. res_json["msg"] = "当前有人打包,请稍后尝试"
  190. return res_json
  191. local_data["status"] = '1'
  192. set_local_file_json(local_data)
  193. res_json["code"] = 0
  194. res_json["msg"] = "成功"
  195. # 接受参数
  196. chat_id = request.json["chat_id"]
  197. branch = request.json["branch"]
  198. page = request.json["page"]
  199. project = request.json["project"]
  200. mode = request.json["mode"]
  201. project_name = request.json["project_name"]
  202. mode_name = request.json["mode_name"]
  203. func_arr = [
  204. # ------- 微信打包 ------
  205. # 1.小程序打包
  206. build_mini_program(branch),
  207. # 2.修改环境
  208. edit_mode_by_file(project,mode),
  209. time.sleep(1),
  210. create_preview(project,page),
  211. # -------- 飞书 --------
  212. # 1.获取签名
  213. tenant_access_token_by_feishu(),
  214. upload_img_by_feishu(),
  215. send_msg_by_feishu(chat_id,project_name,mode_name,branch)
  216. ]
  217. # 循环方法判断
  218. for item in func_arr:
  219. item
  220. if res_json["code"] != 0:
  221. local_data["status"] = '0'
  222. set_local_file_json(local_data)
  223. return res_json
  224. # 正常执行完
  225. local_data["status"] = '0'
  226. set_local_file_json(local_data)
  227. except Exception as ex:
  228. local_data["status"] = '0'
  229. set_local_file_json(local_data)
  230. res_json["code"] = 9
  231. print(ex)
  232. res_json["msg"] = ex.args
  233. return res_json
  234. @app.route("/getBranch")
  235. def get_branch():
  236. # 查看所有分支
  237. command_str= (
  238. "cd "+current_path+ ";"
  239. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  240. "cd longVideoFactory;"
  241. "git pull;"
  242. )
  243. os.system(command_str)
  244. command_str= (
  245. "cd "+current_path+ ";"
  246. "cd longVideoFactory;"
  247. "git branch -r;"
  248. )
  249. f_read = os.popen(command_str).read()
  250. branch_list = []
  251. for item in f_read.split():
  252. if "->" not in item and "HEAD" not in item and "*" not in item:
  253. branch_list.append(item.replace("origin/",""))
  254. res = {"code":0,"data":branch_list}
  255. return res
  256. if __name__ == '__main__':
  257. app.run(host='0.0.0.0', port=7777, debug=True)