main.py 8.6 KB

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