index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import { defaults, defaultOptions } from './utils'
  4. baseComponent({
  5. useFunc: true,
  6. data: defaults,
  7. computed: {
  8. classes: ['prefixCls, buttons, verticalButtons', function(prefixCls, btns, verticalButtons) {
  9. const prompt = `${prefixCls}__prompt`
  10. const input = `${prefixCls}__input`
  11. const buttons = classNames(`${prefixCls}__buttons`, {
  12. [`${prefixCls}__buttons--${verticalButtons ? 'vertical' : 'horizontal'}`]: true,
  13. })
  14. const button = btns.map((button) => {
  15. const wrap = classNames(`${prefixCls}__button`, {
  16. [`${prefixCls}__button--${button.type || 'default'}`]: button.type || 'default',
  17. [`${prefixCls}__button--bold`]: button.bold,
  18. [`${prefixCls}__button--disabled`]: button.disabled,
  19. [`${button.className}`]: button.className,
  20. })
  21. const hover = button.hoverClass && button.hoverClass !== 'default' ? button.hoverClass : `${prefixCls}__button--hover`
  22. return {
  23. wrap,
  24. hover,
  25. }
  26. })
  27. return {
  28. prompt,
  29. input,
  30. buttons,
  31. button,
  32. }
  33. }],
  34. },
  35. methods: {
  36. /**
  37. * 组件关闭时重置其内部数据
  38. */
  39. onClosed() {
  40. if (this.data.resetOnClose) {
  41. const params = {
  42. ...this.$$mergeOptionsToData(defaults),
  43. prompt: null,
  44. }
  45. this.$$setData(params)
  46. }
  47. },
  48. /**
  49. * 点击 x 或 mask 回调
  50. */
  51. onClose() {
  52. this.hide()
  53. },
  54. /**
  55. * 隐藏
  56. */
  57. hide(cb) {
  58. this.$$setData({ in: false })
  59. if (typeof cb === 'function') {
  60. cb.call(this)
  61. }
  62. },
  63. /**
  64. * 显示
  65. */
  66. show(opts = {}) {
  67. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  68. this.$$setData({ in: true, ...options })
  69. this.originalButtons = options.buttons
  70. return this.hide.bind(this)
  71. },
  72. /**
  73. * 触发事件
  74. */
  75. runCallbacks(e, method) {
  76. const { index } = e.currentTarget.dataset
  77. const button = this.originalButtons[index]
  78. if (!button.disabled) {
  79. this.hide(() => typeof button[method] === 'function' && button[method](e))
  80. }
  81. },
  82. /**
  83. * 按钮点击事件
  84. */
  85. buttonTapped(e) {
  86. this.runCallbacks(e, 'onTap')
  87. },
  88. /**
  89. * 用户点击该按钮时,会返回获取到的用户信息,回调的detail数据与wx.getUserInfo返回的一致,open-type="getUserInfo"时有效
  90. */
  91. bindgetuserinfo(e) {
  92. this.runCallbacks(e, 'onGetUserInfo')
  93. },
  94. /**
  95. * 客服消息回调,open-type="contact"时有效
  96. */
  97. bindcontact(e) {
  98. this.runCallbacks(e, 'onContact')
  99. },
  100. /**
  101. * 获取用户手机号回调,open-type=getPhoneNumber时有效
  102. */
  103. bindgetphonenumber(e) {
  104. this.runCallbacks(e, 'onGotPhoneNumber')
  105. },
  106. /**
  107. * 在打开授权设置页后回调,open-type=openSetting时有效
  108. */
  109. bindopensetting(e) {
  110. this.runCallbacks(e, 'onOpenSetting')
  111. },
  112. /**
  113. * 打开 APP 成功的回调,open-type=launchApp时有效
  114. */
  115. bindlaunchapp(e) {
  116. this.runCallbacks(e, 'onLaunchApp')
  117. },
  118. /**
  119. * 获取用户头像回调,open-type=chooseAvatar时有效
  120. */
  121. bindchooseavatar(e) {
  122. this.runCallbacks(e, 'onChooseAvatar')
  123. },
  124. /**
  125. * 当使用开放能力时,发生错误的回调,open-type=launchApp时有效
  126. */
  127. onError(e) {
  128. this.runCallbacks(e, 'onError')
  129. },
  130. /**
  131. * 当键盘输入时,触发 input 事件
  132. */
  133. bindinput(e) {
  134. this.$$setData({
  135. 'prompt.response': e.detail.value,
  136. })
  137. },
  138. /**
  139. * 显示dialog组件
  140. * @param {Object} opts 配置项
  141. * @param {String} opts.title 提示标题
  142. * @param {String} opts.content 提示文本
  143. * @param {Boolean} opts.verticalButtons 是否显示垂直按钮布局
  144. * @param {Array} opts.buttons 按钮
  145. * @param {String} opts.buttons.text 按钮的文字
  146. * @param {String} opts.buttons.type 按钮的类型
  147. * @param {Boolean} opts.buttons.bold 是否加粗按钮的文字
  148. * @param {Function} opts.buttons.onTap 按钮的点击事件
  149. */
  150. open(opts = {}) {
  151. return this.show(opts)
  152. },
  153. /**
  154. * 显示dialog组件
  155. * @param {Object} opts 配置项
  156. * @param {String} opts.title 提示标题
  157. * @param {String} opts.content 提示文本
  158. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  159. * @param {String} opts.confirmType 确定按钮的类型
  160. * @param {Function} opts.onConfirm 确定按钮的点击事件
  161. */
  162. alert(opts = {}) {
  163. return this.open(Object.assign({
  164. ...opts,
  165. buttons: [{
  166. text: opts.confirmText || defaultOptions.confirmText,
  167. type: opts.confirmType || defaultOptions.confirmType,
  168. onTap: (e) => {
  169. typeof opts.onConfirm === 'function' && opts.onConfirm(e)
  170. },
  171. } ],
  172. }))
  173. },
  174. /**
  175. * 显示dialog组件
  176. * @param {Object} opts 配置项
  177. * @param {String} opts.title 提示标题
  178. * @param {String} opts.content 提示文本
  179. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  180. * @param {String} opts.confirmType 确定按钮的类型
  181. * @param {Function} opts.onConfirm 确定按钮的点击事件
  182. * @param {String} opts.cancelText 取消按钮的文字,默认为"取消"
  183. * @param {String} opts.cancelType 取消按钮的类型
  184. * @param {Function} opts.onCancel 取消按钮的点击事件
  185. */
  186. confirm(opts = {}) {
  187. return this.open(Object.assign({
  188. ...opts,
  189. buttons: [{
  190. text: opts.cancelText || defaultOptions.cancelText,
  191. type: opts.cancelType || defaultOptions.cancelType,
  192. onTap: (e) => {
  193. typeof opts.onCancel === 'function' && opts.onCancel(e)
  194. },
  195. },
  196. {
  197. text: opts.confirmText || defaultOptions.confirmText,
  198. type: opts.confirmType || defaultOptions.confirmType,
  199. onTap: (e) => {
  200. typeof opts.onConfirm === 'function' && opts.onConfirm(e)
  201. },
  202. }],
  203. }))
  204. },
  205. /**
  206. * 显示dialog组件
  207. * @param {Object} opts 配置项
  208. * @param {String} opts.title 提示标题
  209. * @param {String} opts.content 提示文本
  210. * @param {String} opts.fieldtype input 的类型,有效值:text, number, idcard, digit
  211. * @param {Boolean} opts.password 是否是密码类型
  212. * @param {String} opts.defaultText 默认值
  213. * @param {String} opts.placeholder 输入框为空时占位符
  214. * @param {Number} opts.maxlength 最大输入长度,设置为 -1 的时候不限制最大长度
  215. * @param {String} opts.confirmText 确定按钮的文字,默认为"确定"
  216. * @param {String} opts.confirmType 确定按钮的类型
  217. * @param {Function} opts.onConfirm 确定按钮的点击事件
  218. * @param {String} opts.cancelText 取消按钮的文字,默认为"取消"
  219. * @param {String} opts.cancelType 取消按钮的类型
  220. * @param {Function} opts.onCancel 取消按钮的点击事件
  221. */
  222. prompt(opts = {}) {
  223. const prompt = {
  224. fieldtype: opts.fieldtype ? opts.fieldtype : 'text',
  225. password: !!opts.password,
  226. response: opts.defaultText ? opts.defaultText : '',
  227. placeholder: opts.placeholder ? opts.placeholder : '',
  228. maxlength: opts.maxlength ? parseInt(opts.maxlength) : '',
  229. }
  230. return this.open(Object.assign({
  231. ...opts,
  232. prompt: prompt,
  233. buttons: [{
  234. text: opts.cancelText || defaultOptions.cancelText,
  235. type: opts.cancelType || defaultOptions.cancelType,
  236. onTap: (e) => {
  237. typeof opts.onCancel === 'function' && opts.onCancel(e)
  238. },
  239. },
  240. {
  241. text: opts.confirmText || defaultOptions.confirmText,
  242. type: opts.confirmType || defaultOptions.confirmType,
  243. onTap: (e) => {
  244. typeof opts.onConfirm === 'function' && opts.onConfirm(e, this.data.prompt.response)
  245. },
  246. }],
  247. }))
  248. },
  249. },
  250. })