dialog.js 7.1 KB

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