dialog.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @Descripttion: app升级弹框
  3. * @Version: 1.0.0
  4. * @Author: lgen
  5. */
  6. import config from '@/lgen-config.js'
  7. import upgrade from './Upgrade'
  8. const {
  9. title='发现新版本',
  10. confirmText='立即更新',
  11. cancelTtext='稍后再说',
  12. confirmBgColor='#409eff',
  13. showCancel=true,
  14. titleAlign='left',
  15. descriAlign='left',
  16. icon,
  17. description
  18. }=config.upgrade;
  19. class AppDialog{
  20. constructor(){
  21. this.maskEl={}
  22. this.popupEl={}
  23. this.screenHeight=600;
  24. this.popupHeight=230;
  25. this.popupWidth=300;
  26. this.viewWidth=260;
  27. this.descrTop=130;
  28. this.viewPadding=20;
  29. this.iconSize=80;
  30. this.titleHeight=30;
  31. this.textHeight=18;
  32. this.textSpace=10;
  33. this.popupContent=[]
  34. this.apkUrl='';
  35. }
  36. // 显示
  37. show(apkUrl){
  38. this.drawView()
  39. this.maskEl.show()
  40. this.popupEl.show()
  41. this.apkUrl=apkUrl;
  42. }
  43. // 隐藏
  44. hide(){
  45. this.maskEl.hide()
  46. this.popupEl.hide()
  47. }
  48. // 绘制
  49. drawView(){
  50. this.screenHeight=plus.screen.resolutionHeight;
  51. this.popupWidth=plus.screen.resolutionWidth*0.8;
  52. this.popupHeight=this.viewPadding*3+this.iconSize+100;
  53. this.viewWidth=this.popupWidth-this.viewPadding*2;
  54. this.descrTop=this.viewPadding+this.iconSize+this.titleHeight;
  55. this.popupContent=[]
  56. // 图标
  57. if(icon){
  58. this.popupContent.push({
  59. id:'logo',
  60. tag:'img',
  61. src:icon,
  62. position:{
  63. top:'0px',
  64. left:(this.popupWidth-this.iconSize)/2+'px',
  65. width:this.iconSize+'px',
  66. height:this.iconSize+'px'
  67. }
  68. })
  69. }else{
  70. this.popupHeight-=this.iconSize/2;
  71. this.descrTop-=this.iconSize/2-this.textSpace;
  72. }
  73. // 标题
  74. if(title){
  75. this.popupContent.push({
  76. id:'title',
  77. tag:'font',
  78. text:title,
  79. textStyles:{
  80. size:'18px',
  81. color:'#333',
  82. weight:'bold',
  83. align:titleAlign
  84. },
  85. position:{
  86. top:this.descrTop-this.titleHeight-this.textSpace+'px',
  87. left:this.viewPadding+'px',
  88. width:this.viewWidth+'px',
  89. height:this.titleHeight+'px'
  90. }
  91. })
  92. }else{
  93. this.descrTop-=this.titleHeight;
  94. }
  95. this.drawText()
  96. // 取消
  97. if(showCancel){
  98. const width=(this.viewWidth-this.viewPadding)/2;
  99. const confirmLeft=width+this.viewPadding*2;
  100. this.drawBtn('cancel',width,cancelTtext)
  101. this.drawBtn('confirm',width,confirmText,confirmLeft)
  102. }else{
  103. this.drawBtn('confirmBox',this.viewWidth,confirmText)
  104. }
  105. this.drawBox(showCancel)
  106. }
  107. // 描述内容
  108. drawText(){
  109. if(!description) return [];
  110. const textArr=description.split('')
  111. const len=textArr.length;
  112. let prevNode=0;
  113. let nodeWidth=0;
  114. let letterWidth=0;
  115. const chineseWidth=14;
  116. const otherWidth=7;
  117. let rowText=[];
  118. for(let i=0;i<len;i++){
  119. // 包含中文
  120. if(/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])){
  121. // 包含字母
  122. let textWidth=''
  123. if(letterWidth>0){
  124. textWidth=nodeWidth+chineseWidth+letterWidth*otherWidth;
  125. letterWidth=0;
  126. }else{
  127. // 不含字母
  128. textWidth=nodeWidth+chineseWidth;
  129. }
  130. if(textWidth>this.viewWidth){
  131. rowArrText(i,chineseWidth)
  132. }else{
  133. nodeWidth=textWidth;
  134. }
  135. }else{
  136. // 不含中文
  137. // 包含换行符
  138. if(/\n/g.test(textArr[i])){
  139. rowArrText(i,0,1)
  140. letterWidth=0;
  141. }else if(textArr[i]=='\\' && textArr[i+1]=='n'){
  142. rowArrText(i,0,2)
  143. letterWidth=0;
  144. }else if(/[a-zA-Z0-9]/g.test(textArr[i])){
  145. // 包含字母数字
  146. letterWidth+=1;
  147. const textWidth=nodeWidth+letterWidth*otherWidth;
  148. if(textWidth>this.viewWidth){
  149. const preNode=i+1-letterWidth;
  150. rowArrText(preNode,letterWidth*otherWidth)
  151. letterWidth=0;
  152. }
  153. }else{
  154. if(nodeWidth+otherWidth>this.viewWidth){
  155. rowArrText(i,otherWidth)
  156. }else{
  157. nodeWidth+=otherWidth;
  158. }
  159. }
  160. }
  161. }
  162. if(prevNode<len){
  163. rowArrText(len,-1)
  164. }
  165. this.drawDesc(rowText)
  166. function rowArrText(i,nWidth=0,type=0){
  167. const typeVal=type>0?'break':'text';
  168. rowText.push({
  169. type:typeVal,
  170. content:description.substring(prevNode,i)
  171. })
  172. if(nWidth>=0){
  173. prevNode=i+type;
  174. nodeWidth=nWidth;
  175. }
  176. }
  177. }
  178. // 描述
  179. drawDesc(rowText){
  180. rowText.forEach((item,index)=>{
  181. if(index>0){
  182. this.descrTop+=this.textHeight;
  183. this.popupHeight+=this.textHeight;
  184. }
  185. this.popupContent.push({
  186. id:'content'+index+1,
  187. tag:'font',
  188. text:item.content,
  189. textStyles:{
  190. size:'14px',
  191. color:'#666',
  192. align:descriAlign
  193. },
  194. position:{
  195. top:this.descrTop+'px',
  196. left:this.viewPadding+'px',
  197. width:this.viewWidth+'px',
  198. height:this.textHeight+'px'
  199. }
  200. })
  201. if(item.type=='break'){
  202. this.descrTop+=this.textSpace;
  203. this.popupHeight+=this.textSpace;
  204. }
  205. })
  206. }
  207. // 按钮
  208. drawBtn(id,width,text,left=this.viewPadding){
  209. let boxColor=confirmBgColor,
  210. textColor='#ffffff';
  211. if(id=='cancel'){
  212. boxColor='#f0f0f0';
  213. textColor='#666666';
  214. }
  215. this.popupContent.push({
  216. id:id+'Box',
  217. tag:'rect',
  218. rectStyles:{
  219. radius:'6px',
  220. color:boxColor
  221. },
  222. position:{
  223. bottom:this.viewPadding+'px',
  224. left:left+'px',
  225. width:width+'px',
  226. height:'40px'
  227. }
  228. })
  229. this.popupContent.push({
  230. id:id+'Text',
  231. tag:'font',
  232. text:text,
  233. textStyles:{
  234. size:'14px',
  235. color:textColor
  236. },
  237. position:{
  238. bottom:this.viewPadding+'px',
  239. left:left+'px',
  240. width:width+'px',
  241. height:'40px'
  242. }
  243. })
  244. }
  245. // 内容框
  246. drawBox(showCancel){
  247. this.maskEl=new plus.nativeObj.View('maskEl',{
  248. top:'0px',
  249. left:'0px',
  250. width:'100%',
  251. height:'100%',
  252. backgroundColor:'rgba(0,0,0,0.5)'
  253. })
  254. this.popupEl=new plus.nativeObj.View('popupEl',{
  255. tag:'rect',
  256. top:(this.screenHeight-this.popupHeight)/2+'px',
  257. left:'10%',
  258. height:this.popupHeight+'px',
  259. width:'80%'
  260. })
  261. // 白色背景
  262. this.popupEl.drawRect({
  263. color:'#ffffff',
  264. radius:'8px'
  265. },{
  266. top:this.iconSize/2+'px',
  267. height:this.popupHeight-this.iconSize/2+'px'
  268. })
  269. this.popupEl.draw(this.popupContent)
  270. this.popupEl.addEventListener('click',e=>{
  271. const maxTop=this.popupHeight-this.viewPadding;
  272. const maxLeft=this.popupWidth-this.viewPadding;
  273. const buttonWidth=(this.viewWidth-this.viewPadding)/2;
  274. if(e.clientY>maxTop-40 && e.clientY<maxTop){
  275. if(showCancel){
  276. // 取消
  277. // if(e.clientX>this.viewPadding && e.clientX<maxLeft-buttonWidth-this.viewPadding){}
  278. // 确定
  279. if(e.clientX>maxLeft-buttonWidth && e.clientX<maxLeft){
  280. upgrade.checkOs(this.apkUrl)
  281. }
  282. }else{
  283. if(e.clientX>this.viewPadding && e.clientX<maxLeft){
  284. upgrade.checkOs(this.apkUrl)
  285. }
  286. }
  287. this.hide()
  288. }
  289. })
  290. }
  291. }
  292. export default new AppDialog()