main.py 7.1 KB

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