main.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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(val):
  12. send_status_value = {"status":val}
  13. with open("1.json","w",encoding="utf-8") as f:
  14. f.write(json.dumps(send_status_value))
  15. f.close()
  16. def get_local_file_json():
  17. try:
  18. with open("1.json","r",encoding="utf-8") as f:
  19. return json.loads(f.read())
  20. except:
  21. set_local_file_json("0")
  22. return {"status":0}
  23. # current_path
  24. current_path = os.getcwd()
  25. res_json = {"code":0,"msg":"成功"}
  26. # 1.小程序打包
  27. def build_mini_program(branch):
  28. command_str = (
  29. "cd "+current_path+ ";"
  30. "cd longVideoFactory;"
  31. "rm -rf dist;"
  32. "git checkout "+branch+";"
  33. "git pull;"
  34. "cnpm install && npm install ;"
  35. "npm run build;"
  36. )
  37. ret = subprocess.run(command_str,shell=True)
  38. if ret.returncode == 0:
  39. res_json["code"] = 0
  40. res_json["msg"] = "打包成功"
  41. else:
  42. res_json["code"] = 1
  43. res_json["msg"] = "打包失败"
  44. # # 3.微信编译图片
  45. def create_preview(project,page):
  46. # 小程序 cli path for mac
  47. wx_cli ="/Applications/wechatwebdevtools.app/Contents/MacOS/cli "
  48. dist_path = os.path.join(current_path,'longVideoFactory/dist/'+project)
  49. code_img_path = os.path.join(current_path,'1.png')
  50. path = """ '{"pathName":"%s"}' """ % page
  51. os.system(wx_cli+ " open --project " + dist_path)
  52. os.system(wx_cli+ " reset-fileutils --project " + dist_path)
  53. f_read = os.popen(wx_cli+" preview --project "+dist_path+" --compile-condition "+path+" --qr-output "+code_img_path+" --qr-format image").read()
  54. os.system(wx_cli+ " quit")
  55. if "[error]" in f_read:
  56. res_json["code"] = 3
  57. res_json["msg"] = f_read
  58. else:
  59. res_json["code"] = 0
  60. res_json["msg"] = "微信小程序编译成功"
  61. # 修改环境文件
  62. def edit_mode_by_file(project,mode):
  63. path = os.path.join(current_path,"longVideoFactory/dist/"+project+"/network/volatileConfig.js")
  64. file_lines = []
  65. with open(path,"r") as f:
  66. for item in f.readlines():
  67. if "export const currentMode" in item:
  68. item = "export const currentMode = MODE."+mode+" \n"
  69. file_lines.append(item)
  70. f.close()
  71. with open(path,"w") as f2:
  72. f2.writelines(file_lines)
  73. f2.close()
  74. res_json["code"] = 0
  75. res_json["msg"] = "文件修改成功"
  76. def upload_img_by_feishu(token):
  77. key = ""
  78. url = "https://open.feishu.cn/open-apis/im/v1/images"
  79. files = {
  80. "image_type":(None,"message"),
  81. "image":('1.png',open(os.path.join(current_path,"1.png"),"rb"),"image/png")
  82. }
  83. headers = {
  84. "Authorization":"Bearer "+token,
  85. }
  86. res = requests.post(url,headers=headers,files=files).json()
  87. if res["code"] == 0:
  88. key = res["data"]["image_key"]
  89. res_json["code"] = 0
  90. else:
  91. res_json["code"] = 4
  92. res_json["msg"] = "飞书上传图片失败"
  93. return key
  94. def tenant_access_token_by_feishu():
  95. token = ""
  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. 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. return token
  110. def send_msg_by_feishu(token,img_key,chat_id,project_name,mode_name):
  111. token = tenant_access_token_by_feishu()
  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(token):
  144. headers = {
  145. "Authorization":"Bearer "+token,
  146. "Content-Type": "application/json;charset=UTF-8"
  147. }
  148. url = "https://open.feishu.cn/open-apis/im/v1/chats"
  149. res = requests.get(url,headers=headers).json()
  150. data = []
  151. for item in res["data"]["items"]:
  152. if item["name"] != '':
  153. data.append(item)
  154. return data
  155. @app.route('/')
  156. def func():
  157. # return 'Welcome to auto-mini-program-build-preview server. only can run on MacOS.'
  158. return redirect("static/dist/index.html")
  159. @app.route('/getGroup')
  160. def getGroup():
  161. token = tenant_access_token_by_feishu()
  162. res_json["code"] = 0
  163. res_json["data"] = get_group_by_feishu(token)
  164. return res_json
  165. @app.route('/send',methods=['POST'])
  166. def send():
  167. local_data = get_local_file_json()
  168. try:
  169. if local_data["status"] == "1":
  170. res_json["code"] = 9
  171. res_json["msg"] = "当前有人打包,请稍后尝试"
  172. return res_json
  173. set_local_file_json("1")
  174. res_json["code"] = 0
  175. res_json["msg"] = "成功"
  176. chat_id = request.json["chat_id"]
  177. branch = request.json["branch"]
  178. page = request.json["page"]
  179. project = request.json["project"]
  180. mode = request.json["mode"]
  181. project_name = request.json["project_name"]
  182. mode_name = request.json["mode_name"]
  183. # ------- 微信打包 ------
  184. # 1.小程序打包
  185. build_mini_program(branch)
  186. if res_json["code"] != 0:
  187. set_local_file_json("0")
  188. return res_json
  189. # 2.修改环境
  190. edit_mode_by_file(project,mode)
  191. if res_json["code"] != 0:
  192. set_local_file_json("0")
  193. return res_json
  194. # 3.微信编译图片
  195. time.sleep(1)
  196. create_preview(project,page)
  197. # -------- 飞书 --------
  198. # 1.获取签名
  199. if res_json["code"] != 0:
  200. set_local_file_json("0")
  201. return res_json
  202. token = tenant_access_token_by_feishu()
  203. # # 2.获取imgKey
  204. if res_json["code"] != 0:
  205. set_local_file_json("0")
  206. return res_json
  207. img_key = upload_img_by_feishu(token)
  208. # # 3.飞书发送消息
  209. if res_json["code"] != 0:
  210. set_local_file_json("0")
  211. return res_json
  212. send_msg_by_feishu(token,img_key,chat_id,project_name,mode_name)
  213. set_local_file_json("0")
  214. except Exception as ex:
  215. set_local_file_json("0")
  216. res_json["code"] = 9
  217. print(ex)
  218. res_json["msg"] = ex.args
  219. return res_json
  220. @app.route("/getBranch")
  221. def get_branch():
  222. # 查看所有分支
  223. command_str= (
  224. "cd "+current_path+ ";"
  225. "git clone https://git.yishihui.com/weapp/longVideoFactory.git;"
  226. "cd longVideoFactory;"
  227. "git pull;"
  228. )
  229. os.system(command_str)
  230. command_str= (
  231. "cd "+current_path+ ";"
  232. "cd longVideoFactory;"
  233. "git branch -r;"
  234. )
  235. f_read = os.popen(command_str).read()
  236. branch_list = []
  237. for item in f_read.split():
  238. if "->" not in item and "HEAD" not in item and "*" not in item:
  239. branch_list.append(item.replace("origin/",""))
  240. res = {"code":0,"data":branch_list}
  241. return res
  242. if __name__ == '__main__':
  243. app.run(host='0.0.0.0', port=7777, debug=True)