main.py 7.7 KB

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